The tag indicates the language of the style sheet. What is CSS, connecting a CSS file. and set the style for the specified class. Styles created with the class can be applied to other elements, not necessarily of this type

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

Related Styles

The most convenient, because with their help you can easily design an entire site in a single style. The linked style assumes that all CSS styling is in separate file with extension .css.

This approach is also convenient in that it separates page layout rules from their content, CSS code can be easily changed without interfering with HTML files, and the principle of code separation is very important, especially in large projects.

To associate rules from a CSS file with an HTML page, use the tag added to container , and its attributes.

Here is the line linking the rules from the file mystyle.css with HTML page:

rel \u003d "stylesheet" specifies that the tag refers to a stylesheet file, href \u003d "mysyle.css" sets its address. The addressing rules are the same as for regular links - the path can be absolute, relative, etc.

Imported styles

Using the command @importyou can add styles from a CSS file to the current table. This may be required, for example, if you want to complement the individual document layout specified using global styles with universal rules from a separate file. The method cannot be used with inline styles.

The code below will import the file table into the document any.css, which is located in the folder with the HTML document being edited:

@import url (any.css);

The command is written on the line below the opening tag

There are several ways to add styles to a web page, which differ in their capabilities and purpose. Let's consider them in more detail below.

External style sheet

Styles are located in a separate file with the css extension; the element is used to link an HTML document to a CSS file. ... It is located inside as shown in example 1.

Example 1. Connecting external styles

Styles

Heading

Rel attribute value will always be stylesheet and remain unchanged. The href value is the path to the CSS file; the path can be specified both relatively and absolutely. Note that this way you can include a style sheet that is located on another site. In the example above, we include the Cyrillic font Lobster from the Google Fonts site.

The content of the style.css file is shown in example 2.

Example 2. Content of the style.css file

H1 (font-family: "Lobster", cursive; color: green;)

As you can see from this example, the style file is a regular text file and contains only CSS syntax. In turn, the HTML document contains only a pointer to the file with the style, in this way the principle of code separation and site design is fully implemented. Therefore, using an external style sheet is the most versatile and convenient method of adding style to your site. This allows you to independently edit hTML files and CSS.

Internal style sheet

Styles are written in the HTML document itself inside the element

Heading

IN this example element style is set

which can then be used universally on this web page (Figure 1). Please note that we can safely combine with

Heading 1

Heading 2

In this example, the first heading is set to red and the size is 36 pixels using the style attribute, and the second heading is set to green through the element

Heading 1

Heading 2

This example shows the import of a style file from the Google Fonts site to include the Cyrillic font Lobster.

From the author: hello dear readers of the webformyself portal. In site building, the design of a web resource is of great importance. This is what the css language (cascading style sheets) is responsible for, which we will talk about today. Or rather, about its connection and use. Consider some css styles for the site, which are used in the design of web pages.

Connecting css

Today, you can hardly find a web page anywhere that would have been created purely using html capabilities. Indeed, appearance such sites would be just awful, therefore, when the markup of the necessary elements is ready, they immediately need to be decorated, and this is done using css in a separate file.

I must say that in general there are also possibilities for including styles in the html file. For example, they can be defined using the style attribute or the same tag. These are called inline styles. This approach is not welcomed today and violates modern standards development.

And how to do it right, you ask? To do this, you need to create a new file with the css extension, and then connect it to html. This is done very simply using the link tag, which is responsible for connecting external files. This is done like this:

< link rel = "stylesheet" type = "text/css" href = "style.css" >

The tag is single, as you already understood. I'll tell you a little more about its attributes:

rel \u003d "stylesheet" - generally the rel attribute is written in order to define the role of the file on the page. In our case, the role is a style sheet, which is indicated.

type \u003d "text / css" - the so-called MIME-type, which is usually indicated to all included files so that the browser interprets them correctly. In modern web browsers, you do not need to write this attribute.

href \u003d "style.css" is a standard attribute indicating the address of our external file. In this case, it is written based on the fact that the file has the name style, css extension and is located in the same folder as the html document.

As you can see, in connecting css files, only the href attribute will change, nothing else needs to be changed. You can connect as many style sheets as you want to a page, but no more than 2-4 are considered optimal, because too many requests to the server are not very good either.

Where can I get ready-made css styles for the site?

Well, okay, with the connection, I hope, everything is more or less clear. But you are creating an empty file, and in order to see the effect of adding it, you need to either write the rules yourself, or take them from somewhere.

There are a lot of free html templates on the web, for example. If you download any of them, then inside there will certainly be a ready-made style sheet with the necessary rules for website design. But it will work correctly only for its own html document. The point is that these two languages \u200b\u200bare linked using so-called selectors.

The selector is a feature of the css language that is unique to it. What are they needed for? Well, imagine this code:

Title text

Text in a paragraph

And here's how to style a paragraph and a heading in css, for example? For this, selectors are created to refer only to those elements that you need. For example:

p (font-size: 12px;) .title (font-size: 36px;)

font - size: 12px;

Title (

font - size: 36px;

We set all paragraphs to a font size of 12 pixels, and elements with the title class (in this case, h1) got a much larger size - 36 pixels. Note, in the case of paragraphs, the styles apply to all of them, no matter how many of them are on the page.

The second selector addressed only one element - the title class. Of course, you can create other elements with exactly the same class, this is not prohibited, and they will receive the same rule, but if we just write the h1 tag in html (without the title class), then no rules will be added for it.

This is how it conveniently allows css to select the desired elements and leave alone those that are not needed. What does this have to do with the ready-made styles that you can find on the internet? Everything there is tied to specific classes and identifiers, so you can't just connect these tables to any files like that, they won't work if the required elements don't have the appropriate classes in the html.

For example, in the stylesheet we see the following code:

Nav (width: 300px; background: aqua; ...) .nav a (display: block; color: #ccc; ...)

Nav (

width: 300px;

Already from this we can conclude that somewhere on our html page there should be an element with the nav class, and links should be in it. If this is the case, then the decoration will be applied to them and their appearance will change accordingly. If no such elements are found, then the code is simply ignored.

Again, on the net you can find a lot of ready-made templates in which you can see this class relationship, so if you change something in the html, the corresponding operations must be performed in the stylesheet.

How to make css styles for the site yourself?

Css is not scary and extremely difficult. It is a common technology created by humans to design web pages and has its own rules. It is enough to understand them, and you too can create the appearance of web resources using this language.

The most important condition for rapid development is constant practice, coupled with new knowledge that you will learn for yourself. Ours can give you such practice.

In addition, you can learn and master the basics of css in our premium section. There is one of the courses that is completely devoted to the basic properties of this language. ...

Most importantly, be determined to study and set aside the prejudice that it is very difficult. Also, free materials on our website, articles and video tutorials can help you. If you wish, you can also acquire the basic knowledge that is needed when working with css using them.

On this I say goodbye to you. Read our blog webformyself to improve your site building knowledge.

CSS- Cascading Style Sheets (cascading style sheets) is a tool that allows you to set various visual properties of HTML tags.

Official site of the developers: http://www.w3.org/TR/CSS21/cover.html.

CSS is made up of rules. The rules are name and meaning. They are separated by a colon (rule name: rule value). Do not put any quotes in the values \u200b\u200bof the rules! Example:

Color: red; background: #cccccc;

But how do you tie CSS to HTML? How to write document styles in html-code? There are 3 solutions for this:

  • Attachment (inline).
  • Embeding.
  • Linking.

The basis of the document is html, and css is already applied to it. Specifically to the document, css itself does not mean anything.

Nesting method (inline) CSS

The style attribute is used. It can be placed in any visually displayed element. Style contains css rules (rule name: rule value), several rules are separated by semicolons (; is the end of the rule in css):

Plain text

"color: red; background: #cccccc"\u003e This paragraph has been styled inline.

Embeding CSS

A special “style” element is used, inside of which css rules are written. Inside style, we write only the css language. Example:

< style type= "text/ css"> p (color: red; background: #cccccc) The style is applied here by the nesting method. Plain text

In this example, we have written a rule for the “p” paragraph element. On the 2nd line, we have written a selector (which tags the css rule will apply to). The rule itself is written in curly braces “()”.

The "style" element is placed only in the "head" element.

Linking CSS

In order not to write styling every time in the document (not to load the document), it is necessary to write css rules in one place and apply them to different documents (to an unlimited number).

To do this, put all the css rules in a separate text file... Usually this file is given the extension .css. And then you can immediately see that the style rules are spelled out in it. Can't be confused with anything.

How then can we link this separate css file to our html document? To do this, we use the following code in the head section:

Those. specify the file with styles via a hyperlink to it. The browser loads the style file and applies the rules from it to the entire document. In essence, this works the same as the “style” element, but it is located in a separate file. And we can put a link to a css file in any document. This is very convenient because I edit the styles in one single file; having once loaded the style sheet, when switching to the next pages, the browser will no longer go after this css file, it will get it from the cache (such as temporary storage of uploaded files). Thus, I save traffic and speed up page display.

Attention! The "link" element will fail with a single href attribute. Because this hyperlink is used for various purposes. This is a service hyperlink that may point to different types files that can be used for different purposes for your pages. The rel (rel \u003d ”stylesheet”) attribute is used to signal to the browser that this is a stylesheet. It specifies the type of relationship between the loaded document and our main document. The browser now understands that the loaded document is a stylesheet.

CSS (Cascading Style Sheets), or cascading style sheetsare used to describe the appearance of a web document written in a markup language. CSS establishes style rules that change the appearance of elements placed on web pages, perform fine tuning their details such as color, font, size, borders, background, and location in the document.

You can inline CSS directly into a markup element as an attribute value style... This attribute is available to everyone hTML elements... FROM using CSS you can specify a number of style properties for a given HTML element. Each property has a name and value, separated by a colon (:). Each declared property is separated by a semicolon (;).

This is how it looks for an element

:

Ways to add CSS styles

The CSS standard offers three options for applying a style sheet to a web page:

  • External style sheet - defining style sheet rules in a separate .css file, and then including this file into an HTML document using a tag
  • Internal Style Sheet - Define style sheet rules using a tag

    Example: Internal style sheet

    • Try it yourself "

    Heading

    First text

    Second text

    Third text

    Internal style sheet

    Heading

    First text

    Second text

    Third text

    In this example, we use CSS to set the background color for the element : background-color: palegreen, color and font type for headings

    : color: blue; font-family: verdanaas well as font size, color and center alignment for paragraphs

    : font-size: 20px; color: red; text-align: center.

    Inline style

    When it is necessary to format a separate element of an HTML page, the style description can be placed directly inside the opening tag using the already specialized style attribute. For example:

    Paragraph

    Such styles are called inline, or inline styles. Rules defined directly within the opening tag of an element override rules defined in external file CSS as well as rules defined in the element

    Heading

    First text

    Second text

    Third text

    Tasks

    • Center align text

      Center the text using the inline paragraph style.

    Programs and games