class attribute

Available

6.1 The class attribute and the style tag

But that's not all. After hundreds of "styles" were invented, the question arose: how to use them? And then they came up with the idea of ​​grouping them into “classes”. These are, of course, not the same classes as in Java. Just special style groups.

And if before using “classes” you had a picture:

<img src="logo.png" style="width=100px;height=100px;opacity=0.5">

Now it could be written as:

<img src="logo.png" class="picture">

<style>
    img.picture {
        width=100px;
        height=100px;
        opacity=0.5
    }
</style>

We created a special one «style» pictureand transferred the style values ​​from the style. And then we tied the <img> and the "style" picture with the tag class.

6.2 Selector, types of selectors

These styles, rendered separately, became known as classes or selectors. They have several important features.

One html-elementcan have multiple selectors. Their names are indicated with a space. Example:

<img src="logo.png" class="picture main">

One reason we've touched on selectors here is because they can be applied automatically. And this useful property will be used very often in the future. Including Java developers.

Here is a short list of them:

# Selector Example Description
1 *
* {
  margin: 0;
  padding: 0;
}
Applies to all elements of an HTML document.
2 #id
#img123 {
   width:100px;
   height 100px;
}
The # is followed by the id of the element to which the given style is to be applied.
3 tag
table {
   color: black;
}
Applies to all tables in the document.
4 tag.selector
table.important {
   color: red;
}
Applies to all document tables that have the class attribute specified.
5 .selector
.picture {
  opacity: 0.5
}
Applies to all elements that have the class attribute specified. Any tag.
6 parent child
main article {
  color: blue;
}
Applies to all elements where the parent tag contains the main class and the child tag contains the article class.
7 tag: link
a:link {
color: blue;
}
The :link pseudo-class is used to style links that the user hasn't clicked on yet.
8 tag:visited
a:visited {
color: red;
}
The :link pseudo-class is used to style links that the user has already clicked on.
9 tag:checked
input[type=radio]:checked {
 border: 1px solid black;
}
This pseudo-class will select only checked UI elements: radio buttons or checkboxes.
10 tag: hover
div:hover {
  background: red;
}
This pseudo-class allows you to change the style of an element when hovering over it with the mouse.
eleven tag:first child
ul li:first-child {
 border-top: none;
}
This pseudo-class will only allow the first child element to be selected.

You don't have to memorize it all. But it would be good if you studied this table a couple of times and all this was deposited in your head. Without the web in modern life, nowhere, and on the web - nowhere without selectors.

Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet