Selectors in CSS. CSS Property Options


Igor. Update: October 30, 2018.

The starting article of this section provided general information regarding, including a description of how to connect styles to the HTML code of a website page (document), which determines the presence of certain elements on a web page.

In this publication, we will look at how to correctly compose and write CSS properties for all elements so that the browser can identify them and display them correctly on the page.

In particular, let's look at what a single style rule is in general, which includes a selector, as well as properties and their values.

Syntax and options for writing CSS rules

Since cascading style sheets are, albeit a specialized, but still a language, it is permissible to draw an analogy between the set of CSS rules and in plain text, where each word consists of letters, a sentence consists of words, and the text fragment itself consists of sentences.

Given this clear association, we can deduce logical sequence, which concerns the CSS structure (it is the most optimal, in my humble opinion):

  • values ​​(or parameters), which is expressed in numbers, color code, font name, etc., in accordance with our analogy, can be considered letters;
  • The structural unit of CSS is property, which defines its part of the appearance of an element on the page (its color, text font, size, indents, position, etc.), to which specific values ​​(one or more) correspond. Here, the combination of a property name and its parameter can be compared to a word (which can also consist of one or more letters) in a standard text fragment;
  • CSS rule - selector(style name) along with set CSS properties for each element it completely determines its design and location on the page. Structurally associated with a sentence (a set of individual words in ordinary text);
  • a complete collection of all CSS rules for a specific document (web page) ensures its correct display in the browser. Let's compare this with a completed passage of text.

Perhaps the association I proposed is a little far-fetched, but I think it will still help beginners understand the general structure of the CSS language.

So, each property is assigned a specific value (parameter), and there may be several of them. The corresponding set of properties (rule) is applied to a specific selector. Schematically, a CSS rule can be displayed like this::

A selector is placed in front, then curly braces (opening and closing) are placed, between which there are CSS properties with their assigned values, the name of each property is separated from its corresponding parameter by a colon. All properties along with their values ​​are separated by a semicolon. Let's look at a live example:

Body ( font-size: 14px; background: #00ff00 url("west.png") repeat-x; )

Please note that the background property has several values: the background color (#00ff00), the path to the background image ("west.png") and the resolution to repeat it horizontally (repeat-x). You probably noticed that this recording option is somewhat different from the diagram presented just above.

In fact, there is no inconsistency here. Since CSS is not sensitive to spaces, line breaks, or tabs, style rules can be written in a variety of ways to suit specific situations.

For example, an extended rule entry, a sample of which has just been presented, is the most convenient for perception, and therefore is usually used for studying styles, or in the “raw” style.css file, which was created for a new site and therefore has not yet been finalized . There is another variation of this same CSS rule entry:

Body ( font-size: 14px; ) body ( background: #00ff00 url("west.png") repeat-x; )

As you can see, the same selector is repeated for each property. This is not an error for the reasons mentioned above, but this format for writing a rule clutters the code and complicates its perception, and therefore is practically not used.

An extended version of the notation can be presented without hyphenation:

Body ( font-size: 14px; background: #00ff00 url("west.png") repeat-x; )

But that's not all. All spaces, as well as the separating character (semicolon) after the last property (before the closing curly brace), can be omitted. This is also quite legitimate CSS shape rules, less voluminous, which is good for site optimization, but less readable:

Body(font-size:14px;background:#00ff00 url("west.png") repeat-x)

Once the site design is ready and all necessary changes have been made to the CSS style files, they are adjusted accordingly in order to optimize the web resource, and all spaces are automatically removed. This can be done manually, but why waste precious time so irrationally? After all, even for a small site, the style.css file can contain many style rules.

Selectors, Properties, and Applying Styles

I have already noted that over time there is an increasingly closer convergence of HTML and CSS. If at the dawn of the Internet in its current form appearance elements on a web page were described using hypertext markup tag attributes, these days it is too frequent use This method is considered since overloaded HTML code often does not contribute to proper optimization of site pages, which also harms its SEO promotion.

Block with green text surrounded by a frame

CSS (the identifier is preceded by a hash icon):

#area (color: green; border: 2px solid #d6c99a)

The following options can contain both tag selectors and classes with identifiers.

Why are these sizing options needed? The fact is that it is not always the best solution is the use of standard relative values, including percentages. Sometimes it is more beneficial to link the font size to the height and width of the browser window. Let's say, if the viewport height is 800px, then 1vh = 8px, and if the width is 1500px - 1vw = 15px.

The units vmin and vmax are determined by the minimum and maximum dimensions areas in height and width. Sample: Browser window height is 700px and width is 1300px. In this case vmin = 7px and vmax = 13px.

When setting dimensions, you must specify the units (for example, height: 55px), otherwise the browser will display the corresponding elements incorrectly. Values ​​equal to zero do not apply to this rule; they can be designated in two ways (padding: 0 or padding: 0px); for obvious reasons, the first option is overwhelmingly in the lead.

Interest

I decided to mention this type of units separately. Above I already talked about the advantage of using relative values ​​when setting properties. This fully applies to interest.

Table (width: 100%; /* Table width in percentage */)

Percentage entries are used quite often, especially when it is necessary to set the value relative to the parent or when the sizes depend on certain external circumstances. For example, an element width equal to 100% will adapt to the most screens. different devices and display correctly there.

Keywords

Let's look at some words that play the role of property parameters and determine one or another position or appearance of an element. Here are some illustrative examples.

1. Values text-align properties , which help align text horizontally: center | justify | left | right | start | end. Here is an example with the parameter justify:

P (text-align: justify)

This notation means that the paragraph text is aligned both left and right:

2. CurrentColor- matches the value of the color property of the current element and specifies a color for areas that do not receive one by default. Let's say a container with class "bl-2" is nested in "bl-1":

The currentColor value specifies the background color

And styles are specified for both:

Bl-1 ( padding: 10px; /* Margins around the inner container */ background: currentColor; /* Background color */ ) .bl-2 ( color: blue; /* Text color */ padding: 10px; /* Margins around text */ background: #ccc; /* Background color */ )

If there is no text color specified for an element (the “color” property), then by default the text fragment will be colored black (in most popular browsers). In our example, for the outer container (“bl-1”) color is missing. As a result, when specifying the “currentColor” value, the background of this container will become black:

3. Inherit is a keyword as a parameter of the corresponding values ​​of parent elements. Sample:

The inherit value determines the inheritance of properties

And the CSS rules for it:

Bl-1 ( border: 4px solid red; padding: 10px; ) .bl-2 ( border: inherit; padding: inherit; )

As a result, the border design and padding were moved from the parent block (bl-1) to the internal block (bl-2):

4. Initial— used in cases where it is necessary to set the initial parameters of properties. Let's say it helps to restore values, installed by browser default, or define initial parameters that were changed as a result of inheritance. Example:

CSS Property Options

There are a wide variety of meanings that determine certain styles, which determine the appearance of elements on a web page.

Read more details

And styles for it:

H2 ( color: #2e15d1; font-family: Verdana, sans-serif; ) p ( color: green; ) .init ( color: initial; font-family: initial; )


Using the "init" class, the appearance of the p paragraph fragment enclosed in the span tag and the H2 bottom header are changed to match their default styling settings.

5. Unset is a parameter that is essentially a “hybrid” of inherit and initial. Defines the property parameter as inherit if the property is inherited from a parent, otherwise as initial.

Example 1(acts like inherit):

Text color green - inherits the value of the "color" property for the "zzz" class

The text color is red according to the value of the "color" property for the p tag

P (color: red) .zzz (color: green) .xxx p (color: unset)

In accordance with CSS rules the color property is inherited, so "unset" in our example returns green color text for the container "xxx" which is a child of "zzz":

Example 2(works like initial):

Frame color is black - default value

The border color (blue) is set in the properties of the div container

Div ( border: 2px solid blue; margin-bottom: 10px; margin-top: 10px; padding: 10px; ) .xxx ( border-color: unset; )


Since according to the rules of style sheets border property not inherited, then “border-color: unset” returns the border color to its default value, that is, it becomes black.

URL or address

This parameter contains and is used when you need, for example, to point to an image file (read using HTML img tag). In this case it is used keyword url(), where between the brackets and the path to the graphic file is indicated:

Body ( background: url(https://site/images/star.jpg) no-repeat; /* Absolute path */ ) div ( background: url(images/globus.jpg) no-repeat; /* Relative path * / )

Time

It is used, for example, when implementing animation effects (yes, you can now create such things using pure CSS), where the parameters are specified in seconds (s) or milliseconds (ms). Values ​​can be either integers or fractional numbers. At the same time, we must remember that there should be no space between the number and the unit of measurement (2s, 50ms)./p>

Let's look at an example.

And, accordingly, styles for it:

Loading ( width: 440px; margin: auto; padding: 20px; background: #c49746; color: #fff; text-align: center; ) .spin ( display: inline-block; width: 70px; height: 70px; border: 10px double #ccc; border-right: 10px double transparent; border-radius: 70px; /* Animation */ animation: spin 5s linear 0s infinite normal; ) /* Set rotation */ @keyframes spin ( from ( transform: rotate( 0deg); ) to ( transform: rotate(360deg); ) )

On this page You can not only look at the result, but also try to edit some properties, so to speak, in practice, in general, experiment. Naturally, it is still difficult for beginners to study cascading style sheets to understand and take into account the subtleties of animation, but a continuation will definitely follow, so be sure to subscribe to fresh materials in order to get what you want on time. But let's continue.

Strings

This type of option allows you to add text fragments. Strings must be enclosed in quotes (single or double). Moreover, if you need to put a word or phrase in quotes inside a string, then these types of quotes can be combined.

For example, if you put the entire line in single quotes, then the internal text fragment is in double quotes, and vice versa. It is also allowed to use the same type, only in this case it is necessary to escape the internal quotes by adding a backslash “\” in front of them:

  • property: "string 'content'"
  • property: "string 'content'"
  • property: "string\"content\""

To get started, the HTML code:

Current version of the article.

And now the styles:

New::after ( content: "updated"; color: red; )

Color

CSS can be used to indicate colors for properties when creating CSS rules. various parameters. Let's look briefly at each of them:

1. Name. True, not all shades can be specified in this way, but only some of the shades (“red” - red, “green” - green, “black” - black, “orange” - orange, “yellow” - yellow, “olive” - olive and some others). They can also be classified as keywords.

P (color: green)

2. Hex (hexadecimal code). basis hexadecimal system, unlike the well-known decimal, there are 16 characters: 10 numbers (from 0 to 9) and the first 6 letters of the Latin alphabet (A, B, C, D, E, F). Letters A to F correspond to numbers 10 to 15.

P (color: #f8f9e3)


3.1. RGB— you can set the color value based on the decimal system. Name this method is an abbreviation for the three main colors involved: R ed (red), G reen (green), B lue (blue). The shades of each of them are in the range from 0 to 255. To use this system, you need to write rgb in front, and indicate each of the color components in brackets: rgb (240, 0, 180). It is also possible to use percentages:

Div ( background-color: rgb (210, 0, 90); color: rgb (100%, 50%, 15%); )

3.2. RGBA- extended version previous method An RGB containing an alpha channel that sets the element's transparency, ranging from 0 to 1. A value of "0" is completely transparent, and "1" is completely opaque.

Body (background-color: rgba (255,255,255,.9))

4.1. HSL— the name of this method is also nothing more than an abbreviation that includes the first letters of three words: H ue (hue), S ataturate (saturation) and L ightness (lightness). It is these characteristics that together determine the final color. In this case, the shade is located at a specific place on the color wheel:

Since the entire circle is 360°, each hue (hue) corresponds to a very specific value in degrees ranging from 0° to 359°. In this case, the parameters of the main shades are as follows: 0° - red, 120° - green, 240° - blue.

Saturation and lightness are measured as percentages (from 0 to 100%). For saturate, parameter 0 means no color range, and 100% is the most saturated color. The higher the lightness value, the lighter the tone, 0 is black and 100% is white.

Div (background-color: hsl(40,90%,15%))

4.2. HSLA- similar to RGBA, it is an extended version of HSL, to which a transparency indicator is added.

Div (color: hsla(120,100%,50%,0.6))

Angle of rotation or tilt

In CSS, angular characteristics (based on a circle) can be used as parameters for certain properties. Here are the units used:

  • degrees - deg(1 full circle is 360deg);
  • grads - grad(full circle 400grad);
  • radians - rad. A whole circle is equal to 2π (approximately 6.2832rad);
  • turns - turn(1 turn is equal to a full turn).

It is important to note that negative values ​​can be used. Below are illustrative examples with parameters of some angles (rotations):


For clarity, we will use the rotation characteristics to implement a gradient fill of the background and, as one of the components of our sample, we will place certain text on the page:

Gradient background fill.

Now let’s create a CSS rule for this background:

Body ( background: #f5e573; background: linear-gradient(-5deg, #f5e573, #f7f4db 50%, #f7f6eb); )

You can evaluate the result at this page and practice there, editing the styles as you wish. This will also be useful for acquiring practical skills.

CSS contains style rules that are interpreted by the browser and then applied to appropriate elements in your document. A style rule consists of three parts: