CSS stands for Cascading Style Sheets, which means Cascading Style Sheets. Used to decorate web pages. If the HTML code contains content (what the browser will show), then the CSS determines its appearance (how the browser will show it). The beauty of CSS is that with the help of one style you can style all the same type elements of a page or an entire site at once (all links, paragraphs, lists at once). With a CSS style, you define once how a particular element should look like, for example, pictures, and they change their appearance in all documents at once. To change the formatting of text throughout your site, you only need to change the CSS code once.
Basic CSS elements
Just as HTML is made up of tags, attributes, and values, so CSS is made up of its own elements. The essence of CSS constructions can be explained as follows: "Indicate which page element to style, and indicate how to style it." Here are the building blocks of CSS.
- Selector... An identifier that tells the browser which element on the page to apply the design to. Thanks to it, the browser "understands" that the style is intended, for example, to decorate lists or tables.
- Style declaration block... It is written after the selector and enclosed in curly braces. It sets the style of the element (its design). The style declaration block consists of two parts.
- Property... Analogue of an attribute in HTML. Determines which particular styling property will be changed.
- Value... It is set to the property, separated by a colon, and determines how exactly the property will be changed.
There can be several properties and values \u200b\u200bin the style declaration block, in which case they are listed separated by semicolons.
Selector types
There are different types of selectors depending on the properties of which page elements they define.
- Universal... Sets rules for all elements of the page for which no other rules are set.
The code * (font-size: 14px;) sets the font size to 14 pixels for all elements in the document, for which no other rules are specified using other selectors. - Tega. This type of selector sets the formatting rules for the content of a particular HTML tag. The name of the selector is the same as the name of the descriptor, only it is written without angle brackets: p, h1, ul.
For example the code h2 (color: red;) sets the text color to green for all second level headings, that is, the content of the tags . - Attributes... With this group of selectors, you can define the style of the content of all tags that are assigned a specific attribute. Selectors can be specified more precisely by specifying not only the name of the attribute, but also the value assigned to it, as well as the name of the tag containing it. This can be used to personalize the design.
- Class... The kind of selectors in case you need to style the content of tags of the same type in different ways. For example, you want to make the links at the bottom of the site red, while all others should remain blue, as they were. To apply class rules to a site element, you need to specify the class name in the attribute class corresponding tag.
Suppose using the class step individual elements need to set a left indent of 15 pixels.
The CSS code will be like this:
Step (margin-left: 15px;)
The HTML code that will bind the element to the rule will be as follows:
Indented text
- ID... Used in conjunction with the attribute id HTML tag and is used when properties need to be set on a single element. Unlike a class selector, which is preceded by a period, it is written through a hash:
#exclusive (color: orange;)
- Contextual... In HTML, some tags are often found inside others, and contextual selectors help define rules for just such a case. Using them, for example, you can format items inside numbered lists, or italic text inside paragraphs:
P i (fint-family: Century;)
The remaining groups of selectors are based on combining the listed types, as well as on the principle of inheritance, when child element takes the properties of the parent.
Combining and grouping selectors is useful in many situations. For example, to define class rules step only for links, you need to specify both selectors separated by a dot (first the tag, then the class):
A.step (margin-left: 15px;)
How do I connect CSS to an HTML page?
There are a number of ways you can get the website building tools to interact with each other. Styles are divided into the following categories by the method of adding.
Inline Styles
They are set in the document directly inside the HTML tag using the attribute style... Have the highest priority. This means that if a different design is specified for the same element, then preference will be given to the rule that is written inside the tag. A selector for the inline style is not needed, since the relationship between the style and the tag is obvious - the design of the tag is set in it.
The following code sets the font size and color of the text inside the tag
Inner styled text.
Global Styles
Set by tag …