the Internet Windows Android

CSS adjacent elements. Special selectors in CSS

Vlad Merzehevich

Neighboring the elements of the web page when they follow themselves after each other in the document code. Consider several examples of the relationship of elements.

Lorem Ipsum. dolor Sit Amet.

In this example tag is a subsidiary in relation to the tag

Since it is inside this container. Respectively

Acts as a parent .

Lorem Ipsum. dolor sIT Amet.

Here tags and Do not overlap and are neighboring elements. What they are located inside the container

It does not affect their attitude.

Lorem. iPSUM. Dolor Sit Amet, consectetuer. Adipisting. eLIT..

Neighbor here are tags and , as well as and . Wherein and Neighboring elements do not belong due to the fact that there is a container between them .

To control the style of adjacent elements, a plus symbol (+) is used, which is installed between two selectors. The general syntax is next.

Selector 1 + selector 2 (Description of the style rules)

The spaces around the advantage are not required, the style with such an entry is applied to the selector 2, but only if it is adjacent for the selector 1 and should be immediately after it.

In Example 11.1, the structure of the interaction of tags between them is shown.

Example 11.1. The use of neighboring selectors

HTML5 CSS 2.1 IE CR OP SA FX

Neighboring selectors

Lorem. iPSUM. Dolor Sit Amet, consectetuer. Adipisting ELIT.

Lorem Ipsum Dolor Sit Amet, consectetuer. Adipisting ELIT.

The result of the example is shown in Fig. 11.1.

Fig. 11.1. Selecting text with the help of neighboring selectors

In this example, a text color change for the contents of the container occurs. when it is located immediately after the container . In the first paragraph, this situation is implemented, so the word "consectetuer" in the browser is displayed in red. In the second paragraph, although there is a tag but next door no tag No, so the style to this container does not apply.

We will analyze a more practical example. Often there is a need for the text of the article to include various footnotes and notes. Usually, for this purpose, a new style class is created and used it to paragraph, in such a way you can easily change the type of text. But we will go to another way and take advantage of neighboring selectors. To highlight the comments, create a new class, call it SiC, and we will apply it to the tag

. The first paragraph after such a header is highlighted by the color of the background and indent (Example 11.2). The view of the remaining paragraphs will remain unchanged.

Example 11.2. Changing paragraph style

HTML5 CSS 2.1 IE CR OP SA FX

Changing paragraph style

Lion fishing methods in the desert

The method of consistent integrity

Let the lion having the dimensions of L x W x H, where L is the length of the lion from the tip of the nose to the tails tassel, W is the width of the lion, and H is its height. After that, the desert is divided into a number of elementary rectangles, the size of which coincides with the width and length of the lion. Considering that the lion may be not strictly on a given area, but at the same time on two of them, a cell for fishing should be made of an increased area, namely 2L x 2w. Thanks to this, we avoid mistakes when only half of the lion is caught in the cage or, worse, only his tail.

Important note

To simplify the calculations, the tail as a measurement error can be discarded and not taken into account.

The result of this example is shown in Fig. 11.2.

Fig. 11.2. Changing the type of paragraph due to the use of neighboring selectors

In this example, the text is formatted using paragraphs (tag

) but the record h2.sic + p sets the style only for the first paragraph of the tag

which added class named SiC.

Neighboring selectors are convenient to use for those tags to which indents are automatically added to adjust the bulging value. For example, if there are tags in a row

and

, The distance between them is easy to adjust just with the help of neighboring selectors. Similarly, the case is also for coming tags

and

As well as in other similar cases. In Example 11.3, this manner changes the magnitude of the indents between the specified tags.

Example 11.3. Indents between headlines and text

HTML5 CSS 2.1 IE CR OP SA FX

Neighboring selectors

Title 1.

Title 2.

Paragraph!

Since when using neighboring selectors, the style applies only to the second element, the size of the indents decreases due to the inclusion of the negative value at the MARGIN-TOP property. In this case, the text climbs up, closer to the previous element.

Questions for checking

1. What tags in this code are adjacent?

Sulfuric acid formula:H. 2SO. 4

  1. AND

  2. and
  3. and
  4. and
  5. and

2. There is a following HTML code.:

Great Theorem Fermat.


X. n. + Y. n.
\u003d Z. n.


where n is an integer\u003e 2

Which text is highlighted in red using the SUP + SUP style (Color: Red;)?

  1. Second "n"
  2. The second and third "N".

Hello, dear readers! Continue the topic CSS selectors and today I will try to explain what is neighboring CSS Selectors, as well as what role plays universal selector. Knowledge of the mechanism of using all types of CSS funds will achieve the optimal and compact content of the document, which describes the styles of the elements, which is one of the components of the successful promotion of its resource, so that in no case neglect the possibility of obtaining useful information about the concept of the selector and its all kinds of species.

If you remember and carefully follow the publications, many types of selectors have already been considered; ; . Once again I urge not to neglect the study of the foundations of CSS, as it will give you a lot of preferences in the future.

Universal selector

Now go directly to the essence of today's publication. As for the universal selector, it is quite simple, so I will not stop on it for a long time. Syntax of writing CSS rules, which uses the universal selector, the following:


Here is the operator "*" and means that we have a universal selector. It is used when it is necessary to set a single style for all items on the web page. Sometimes the universal selector specifies not necessarily. For example, recording. * Class i.CLASS in these cases is completely identical. Now necessarily an example. Typically, the most common is the definition of a single font, size and color, as well as the location of the usual text on the blog page or site.

* (Font-Family: Tahoma, Arial, Helvetica, Sans-Serif; / * Font for text * / Color: # 646464; / * Text color * / font-size: 75%; / * Text size * / text-align : left; / * Text location * /)

Thus, you can define the design of the text for all items that are located on the page. I note, for a given case, the result will be similar if instead of a universal selector use the name of the Body tag, which includes the tags of all elements.

Neighboring CSS selectors

Well, now a little more time will pay for neighboring selectors. Neighboring the elements of the web page when they are directly next to each other in the document code. At the same time, CSS styles syntax has this kind:

Now we will take the text paragraph defined by the P tag in which the B, I and BIG tags will be included as child elements, which define text formatting, respectively, the release of bold, italics and isolation by increasing the font:

This paragraph uses tag B., tag I., big tag.

And using the CSS and HTML editing tool, which is embedded in the latest versions of all popular browsers (,) and which is an analogue of the famous plugin for the Firefox () browser (), insert this paragraph to any place on an absolutely any web page (I did right on page of the previous article by the first paragraph). This means, for example, is called by simply pressing the F12 key. It can be used to practically fix the material headings "Basics of HTML" and "CSS textbook". So, I insert a piece of text in the form of the first paragraph:


This paragraph will be experimental and consider using its example. neighboring selectors. As I already said, tags B, I and BIG are subsidiaries for the tag of paragraph P, since they are all directly inside the P container. Here, the neighborings are tags B and I, as well as I and Big. Now apply CSS Rule for neighboring selectors:

B + I (Color: Red;) i + Big (Color: Green;)

After these styles are applied, the paragraph will look like this:


This is true for all webpage tags, which include elements B, I and BIG. Moreover, there must be B and I, I and BIG nearby, for other combinations, this CSS will not work. I think now it is clear to you how neighboring selectors apply when writing or editing a CSS document. Another very important note: if you have time to notice, then in the case of adjacent items, the specified style applies only to the second element.

An example with a paragraph, which was considered very visual and allows you to quickly understand the essence of neighboring selectors in CSS. However, in practice, other areas of applying selectors are commonly used. Say, very often in the body of the article it is necessary to include some piece of text, especially decorated, like that: footnotes, notes, etc.

For these purposes, a separate class usually create and apply it to the necessary paragraph. But a much more optimal way is the use of neighboring selectors. For example, on my blog there are styles created for registration of a conventional header H3.

H3 (font-size: 1.7em; / * font size * / text-align: center; / * text placement * / font-weight: normal; / * normal text design * / font-face: Tahoma, Arial, Helvetica, SANS-SERIF; / * Font style * / color: # 646464; / * Text color * /)

To highlight the title notes or footnotes, set a special class, say, PUT:

H3.PUT (Color: Red; / * Text color * / margin-left: 5px; / * retreat on the left * / margin-top: 0.5em; / * indent from above * / padding: 10px; / * fields around text * / Text-Align: Left; / * Location of text * /)

Now we use adjacent selectors to create a special style of a sentence paragraph, this paragraph will be placed directly under the title with the "H3.PUT" style:

H3.PUT + P (Background: # Ffefd5; / * Background color * / margin-left: 15px; / * retreat on the left * / margin-right: 120px; / * retreat on the right * / margin-top: 0.5em; / * Indent from above * / padding: 5px; / * fields around text * /)

Again, using the Google Chrome editing tool, with you all your ears (but it is worth it), enter the header for footnotes, not forgetting to put it for him by the PUT class:

Attention!

Then write the text of the footnote itself:

The materials presented in this publication are very important in terms of studying the foundations of CSS (cascading style tables)

.

After all these televitations, we get such a paragraph of footnotes on the web page (I remind you that this paragraph I inserted at the end of my article about my subsidiaries and contextual selectors):


Now on a blog or site when binding a "PUT" to any tag H3 will be charged such a footnote on the Web page. Moreover, it is decorated in a special way only the first paragraph after the H3 tag with "Class \u003d" Put "". But this is exactly what we achieved, right?

On this, let me finish today's manual, which contained an algorithm for the use of neighboring, as well as universal selectors. I hope that this article will fade you, dear readers subscribe to

1 Jun 2015.

Unlike adjacent, used less often. The main difference is that they should follow the other. To make it easier for you to learn this topic, I propose to disassemble this structure:

<тег1> <тег2> <тег3> <тег4> <тег5><тег6>...

As you probably guessed from the previous lesson, that tags 2, 3, 4, 5, 6 are contextual selectors for tag1. In turn, the neighboring selectors will be: tag2 and tag3, tag4 and tag5, But at the same time tag2. and tag4 are not adjacent. Style properties will be applied to the last element that you specify the next. You only need to follow the selection selection. The general syntax of writing is:

Selector1 + selector2 + ... + selectorN (Property1: value; property2: value; ... property: value)

Let's consider with you this example:

Neighboring selectors

Title

Tag p. is a neighboring selector tagged h1.

This text should be green

The adjacent element selector selects the element located in the document code directly after another specified element. Let's take a piece of HTML code for example.

Paragraph and in it fatty element and here inclined element.

There are three tags:

, and . Tags and Nested in container

They are a subsidiary towards him. But in relation to each other they are neighboring.

The syntax of the adjacent selector: the selector of the previous element, the "+" sign and the selector of the selected item. Let's work in a nearby selector:

Neighboring selector in CSS.

Paragraph and in it fatty element and here inclined element.

Here fatty and effected Elements, back inclined.

In the example, it can be selected that the adjacent element selector worked in the first paragraph. Here tags and Go for each other. And in the second paragraph between them the tag was used , now there are two other neighboring pairs of tags: + and + .

An error in this case will consider the tag adjacent to the tag

Here tag is a subsidiary in relation to the tag

And he, in turn, is a parent .

The example below will not work:

Neighboring selector in CSS.

Paragraph and in it fatty element and here inclined element.

Here fatty and effected Elements, back inclined.

A more real example

Let's look at the work of the neighboring selector on a more real example. In large articles containing several partitions designated tags

It is desirable to increase the upper indent (the MARGIN-TOP property). An indent to 30px will give text readability. But in case the tag

Going immediately after

, and this may be at the beginning of the article, Veri indent over the tag

It will be superfluous. You can solve such a task using the adjacent element selector.

Here is an HTML code with an example of the selector of the neighboring element.

Neighboring Selector in CSS

Hey!

Header H2.

Header H2.

The text of the paragraph in about incredible adventures.

Header H2.

The text of the paragraph in about incredible adventures.

Header H2.

The text of the paragraph in about incredible adventures.

Also using the adjacent selector tool conveniently adjust the indentation between the title and the first paragraph of the section, in our example these are tags

and

It is also worth noting that to reduce indents it is convenient to use negative values.

Neighboring Selector in CSS

Hey!

Header H2.

The text of the paragraph in about incredible adventures.

Header H2.

The text of the paragraph in about incredible adventures.

Header H2.

The text of the paragraph in about incredible adventures.

Header H2.

The text of the paragraph in about incredible adventures.

Now an example of how with the help of a neighboring selector, select all the list items except the first.

Neighboring Selector in CSS

  • List point number 1.
  • List point number 2.
  • List point number 3.
  • List point number 4.

This is what will result in the work of this example:

Figure 1. The operation of Example No. 5.

Good evening Dear colleagues, today we will explore with you two additional selectorcalled cSS subsidiaries and adjacent selectors. To a greater extent, it is possible to do without them, however, for your development, they must be assimilated, and sometimes apply that they remain in your memory. Therefore, we will discuss what selectors They are adjacent, and what are her subsidiaries and understand the example.
CSS subsidiaries - Elements that are located inside the parent element. An example of this may be as follows. We have a block in which there is a paragraph, another block and picture. These are these three elements are subsidiaries. If other elements are also located in the child block, then they are no longer subsidiaries at the first block, but are subsidiaries in the block in which they are directly. I hope you caught the essence.
Neighboring CSS selectors - Elements are located in each other in the document code. An example of this is. We have a paragraph and the following tag span.. Everything is pretty clear, but we just have to disassemble all this in real examples.








Text in paragraph


Text in span

No longer child text in paragraph






With the help of styles add the same result for the tag span.

Div \u003e Span (
font-Size: 200%;
}

p. + span (
color: Red;
}

The result of the code in both cases will be applied to the tag span.because it is a subsidiary in relation to the tag div and next after the tag p.. Therefore, the font has become two times more and red. Now we fully dealt with subsidiaries and adjacent selectors in CSSAnd you can only strengthen your knowledge in practice, to the soon!