📜 ⬆️ ⬇️

Looking to the future: CSS4


The CSS4 concept, which is gaining popularity, actually came out of nowhere, just like we used to get used to the reality of CSS3, which will still be with us for a while (actually for a long time). Browser manufacturers are making great efforts to implement the advanced features of the standard, and the "front-end" (front-end?) Create new and new tools to work more effectively with style sheets. But now, when you hear about CSS4, you think: “Hey, what about CSS3? Is he still alive? "

We all worked long and hard, and in the end we got to the use of CSS3 in real projects, and now it is simply outdated? “Of course not!” I will answer. All this is the result of evolution, which only helps the entire CSS as a whole, because both “Level 3” and “Level 4” are simply definitions that hide the set of specification documents.

Why do we need level 4? What is heard about CSS3?


“Level 4” is simply the W3C document number. Have you heard of the new “Filter with Effects, Level 1” specifications? Where did they go? In CSS3? Maybe in CSS4? Or maybe in CSS1, because “Level 1” appears there? All this is not important at all, because CSS is just CSS . Selectors are the first document to reach the fourth specification level, which is still in development. Each document is there in itself when it comes to the specification number. They are developed independently of each other.
')
This is a big advantage, because Finished parts of the standard can be set as a recommendation for use as Level 3 selectors. In addition, the finished documents are mobile, and moving problem areas to the next level helps to “move the Web” forward, because the specifications are implemented in small pieces: one by one.

CSS3 will comprehend to consider HTML5: and I mean not the specific version, but the language as a whole. HTML5, in fact, is simply the next version of the markup language, which added support for new elements. But when people talk about it, they can mean anything: from the audio API and Web Sockets, to geolocation (which, by the way, is not included in the HTML5 specification at all).

The same goes for CSS3: for some, it looks like magic, which we use to create modern demo pages. You do not have to know what the border-radius or box-shadow specification belongs to if you know how to use them correctly. A similar situation with new selectors is just another (next) version of the “Selector” specification.

What are Selectors?



In the specification, selectors are described as templates that correspond to the elements of the DOM tree. Most of the 4-level selectors are pseudo-classes. Selectors appeared from the very beginning of the existence of CSS, and continue to successfully exist at the "4 Level", while receiving many new interesting additions. Let's move on to practice and see what is interesting there. I will not describe the entire document - only the innovations of "4 Levels".

Logic combinators:: matches,: not


Let's start with logical pseudo-classes. The first one::: matches, most likely, previously known to you as : -moz-any () from Mozilla , was introduced back in Firefox 4. Thanks to this selector, we can group and find elements in the CSS document. Why do you need it? Well, for example, if we need to collect several links in one fell swoop.
Instead of this:
 ul.menu li a:link, ul.menu li a:hover, ul.menu li a:visited, ul.menu li a:focus { color: red; } 

You can write this:
 ul.menu li a:matches(:link, :hover, :visited, :focus) { color: red; } 

Simple, isn't it? Despite the fact that this example looks rather simple, it shows the potential of the pseudo-class: matches and can be used in more complex situations:
 article:matches(.active, .visible, #important) { background: red; } 

The second selector, which we will consider, in fact, was introduced back in CSS3, but it really became powerful at “Level 4”. I mean the pseudo-class: not, which can now take a list of selectors as a parameter:
 p:not(.active, .visible) { color: red; } 

This code will make red all paragraphs that do not have classes: .active or. visible .

Pseudo-classes for handling links:: any-link and: local-link


Thanks to these pseudo-classes, we will have more control over link styles. The first:: any-link (temporary name, can be changed) - combines the definitions of a:link and a:visited into one so that you don’t have to write:
 a:link, a:visited { color: red; } 

Now it does not matter whether the link is visited or not. She will have the style of a regular link:
 a:any-link { color: red; } 

The second pseudo-class :local-link is more interesting. They can, for example, give your style a link pointing to your personal page, leaving all other links intact:
 nav :local-link { text-decoration: none; } 

Thanks to this pseudo-class, the links pointing to the current page will not have an underscore, thus, differing from the steel ones in the menu or the path.
Here is another usage example:
 :not(:local-link(0)) { color: red; } 

This rule will apply to all external links. (You can add to them, say, an icon or a background image of your choice)

As you can see, in this last example :local-link used with the parameter. The number in parentheses defines the part in the URL of the path by which each matching link will be matched.
It sounds a bit incomprehensible, but everything is simpler than it seems:
 nav :local-link(0) { color: red; } nav :local-link(1) { color: green; } nav :local-link(2) { color: blue; } nav :local-link(3) { color: yellow; } nav :local-link(4) { color: gray; } 

Suppose that the current address of the page: end3r.com/2012/10/20/some-title end3r.com/2012/10/20/some-title , and you have the following links in the menu:

The first link will be red, the second green, the third blue, the fourth yellow, and the fifth gray.

Time-defining pseudo-classes:: past,: current,: future


These pseudo-classes are very convenient for screen reader users. Only one CSS line can be used to form separate words that are currently being voiced (for some reason, karaoke is immediately remembered):
 p:current { background: yellow; } 

This code highlights the voiced words with a yellow background.
The second application is the captioning for the WebVTT video format. You can, for example, change the color or any other properties. Pseudo-classes: past and: future, as you might guess, refer to elements that were voiced earlier and that will be voiced by the following.

Indeterminate pseudo-class:: indeterminate


While many useful pseudo-classes can be applied to the design of form elements, such as :enabled :disabled or :checked , there is one new :indeterminate . As everyone knows, flags and switches can have two states: set and not set. Each state can be defined by pseudo-classes: checked - for set and: not (: checked) - for not set. But what if you want to issue an item that has not yet been used. In fact, neither the set state nor the set state can be determined. In some cases, this is useful, for example, to remind the user that he missed something. Thus, we can stylize elements that have not yet been used and do not have a default state:
 input.checkbox:indeterminate { background: #ccc; } 

Similarly, you can determine the status of a progress bar if its current status is unknown:
 progress:indeterminate { background: #ccc; } 

You can specify the default state and describe the style for it, to indicate to the user that it is not known how much time is left until the end of an abstract task, or this task has not yet begun.

Pseudo-classes for tree structures:: nth-match,: nth-last-match


These pseudo-classes first appeared in the "4 Level", and they are very interesting. Using :nth-match , you can achieve much greater opportunities than ever before. Curious how it works? Okay, imagine that I take the pseudo-class nth-child, which selects the elements and combines it with the power of the pseudo-class :matches , and you will get an answer to your question.

Suppose you have a list of links, some of which have the class .active, and you want to select only the even references to this class. We can use nth-child(even) to select even-numbered elements, but this is not enough, because we need links with the class .active. Does not fit also :matches(.active) , since in this case, we generally select all links with the class .active. In this case, it will help us :nth-match :
 li a:nth-match(even of .active) { color: red; } 

Yes, now we can select only even references with the class .active from all elements.
This is a very simple example. Let's make it a bit more complicated by using the expression An + B:
 p:nth-match(2n+1 of .active, .visible, #important) { color: red; } 

This combination of selectors searches for elements using a more complex expression. Pseudo-class
 :nth-last-match 
works the same way as: nth-match, but starts searching from the end of the DOM tree.

Pseudo-classes:: column,: nth-column,: nth-last-column


Let's try applying some pseudo-classes to tabular data. We all know that using tables for markup is not good, but they are great for data.
HTML tables are row-oriented, i.e. columns are described inside rows. Creating a column-oriented table is possible, but then you will not have single rows, moreover, row-oriented tables are more popular. It would be useful to be able to use CSS styles to design, for example, individual columns in such a table. Let's say you want to give your even and odd columns their own background color. Of course, we can use additional classes, or complicate markup, but with the help of new selectors from “4 Levels” we can make it a pair of lines with pseudo-classes:
 :column(.total) { background: red; } :nth-column(even) { background: blue; } 

This small code sets a red background for each cell with a .total class in one column and a blue each cell in each even column of the table.
Now, we can select columns in the same way as rows, even using insane rules like: nth-column (3n + 2). Just remember that columns are designed based on following them in the DOM tree, and not how they appear on the page. Of course, not only the tables will ultimately benefit from these pseudo-classes, column layouts are already on the way.

Parent (child) selector:


This selector is almost like a Swiss Army Knife, which was very much awaited, and here it is. The most talked about novelty in the "Selector" specification "4 Levels" will give you unprecedented power in working with CSS. Thanks to this selector, it is very easy to design elements without adding classes or identifiers to them. Such a feature can be super-useful when designing, for example, a menu in which there are no classes in the markup for identifying elements.

Let's look at it in action, using some basic examples. Suppose we have a menu consisting of a list of simple links. We would like to issue it, but it is completely generated on the server side of php, and we do not have access to the code. The problem will arise when we want to arrange the li element of the active menu item, despite the fact that the class .active will not be the li element, but the link itself. Known problem, right? In such a situation, the easiest way is to transfer the class responsible for the active item with a link to the li element. But it does not suit us, because we do not have access to the server code.

In the normal structure of the description of CSS rules, certain properties are applied to the last element. In the ul li a.active rule, this will be a link with the class .active, which is in the li element, which is in the ul element. In the p span rule, this will be the span element that is inside the p element, etc. Thanks to the parent selector, this order can be broken:
 ul li! a.active { color: red; } 

Now, we can arrange the li element even if its child has an active class. When adding a parent sector to an element, we say that we want to arrange it, and not the last element.
Another example: you can change the page background by simply adding a link with a specific class to any place:
 body! header a.styleSwitcher:hover { background: red; } 

This code will make the background ( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !

( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !

( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !
( - ) .styleSwitcher, . , , JS . , CSS – .

: ( 29 2011) , ($li). ( 22 2012) , , (li!). , . , , . , , , .

, jQuery cssParentSelector .

:
, ? , . . , , . , , CSS , JS .


, «4- » . , , . , . : . , , .


, , . , , , .
, , , Sel – , . , , .

«4 »
:
CSS4 Media Queries CSS4 Background CSS4 Images CSS4 Text
, , , , – W3C. , , , .

:
, , : W3C « 4 ». , . David Storey - W3C.

?
, , – , . ? , CSS, - ? : , , , , , . , !

Source: https://habr.com/ru/post/167333/


All Articles