Adding CSS. What is CSS, what are CSS styles for? Own css styles
We already know that in order for the website to look beautiful and stylish, we need to work with the CSS file. And for our styles to be applied we need to combine HTML file and a CSS file.
There are several options for performing this operation: using nested style sheets, external style sheets, and inline style.
Today we will talk about the latter method.
Injecting style into an HTML tag
The essence this method thing is necessary registration we embed inside the tag. A separate attribute - style. This attribute can be applied to any tag, but only within the body of the site, that is within body. The value of this attribute is the operators of those styles that need to be applied to a given element.
For example, let's set different font sizes for two different paragraphs of text:
< p style= "font-size:25px;" >Set this piece of text to a letter height of 25 pixels. p> < p style= "font-size:15px; color:#2400ff;"> And this text will have letters, 15 pixels high, and we will also tint it blue to demonstrate the use of several styles at the same time. p>
Flaws
The example perfectly demonstrates how such styling clogs the page code.
You can also note a couple more disadvantages of using this stylization technique. The first of them is the spread of style throughout the document, which in the future of editing will complicate the process significantly.
It will also be difficult to format large amounts of text. I think no one is happy with the prospect of specifying a font size for each paragraph, especially if there are 40 such paragraphs.
Even when using built-in styles, it becomes impossible to use pseudo-classes, which largely ties the hands of the web designer.
It is also worth noting the confusion that is sure to emerge in the use of the style attribute. This confusion will arise due to the use of different quotes when entering styles.
For clarity, let's look at the example below:
< div style= "font-family: "Roboto Condensed", sans-serif"> The entry is correct. div> < div style= "font-family: "Roboto Condensed", sans-serif"> That's also correct. div> < div style= "font-family: " Roboto Condensed ", sans-serif" >This is not a correct entry. div> < div style= "font-family: " Roboto Condensed ", sans-serif" >And this is also not true div>
Looking at the calculations provided above, the logical conclusion suggests itself that the use of built-in styles is associated with a number of significant complications and inconveniences.
When to Use Inline Style
Despite all the shortcomings, the built-in style was not invented in vain. Webmasters often turn to them in case of programmatic style assignment. For example, let's look at this code
< div id= "productRate" > < div style= "width: 40%" > div> div>
Recording the required width for this block will be a simple operation.
A similar situation may arise if it is necessary to replace the background image (for example) of a user with the administrator role. In this case, the img tag may not be suitable. Therefore, it is worth referring to the style attribute:
< div style= "background:url(fon.jpg)" > div>
Also, programmers often turn to built-in styles when creating pop-up windows. Often this process goes like this: the block whose design is being worked on is assigned display:block, and the remaining windows are hidden using display:none so that they do not visually interfere with the programmer’s work. Here's an example:
< div class = "element" style= "display:block" >A pop-up window that is designed as this moment div>
Bottom line
Using built-in styles is associated with a number of difficulties and inconveniences, however, in the process of designing certain elements, webmasters often turn to this method to optimize their work.
In this chapter we will talk about how to implement CSS into an HTML document, that is, link the style description of an element directly to the element itself, some HTML tag.
Implement this task possible in three ways:
- Write a style description directly in the element itself. This method is good only if there is only one such element in HTML document e which needs a separate style description.
- Write a style description for all identical elements of the HTML document. This method is justified if the style of the page is fundamentally different from the overall design of the site (a group of interconnected pages).
- Extract the style description of HTML elements into a separate CSS file. This will allow you to manage the design of the entire site, each page of the site in which a reference to a CSS file is indicated. This method is the most effective use of a cascading style sheet.
Let's take a closer look at each option, and at the same time get acquainted with the rules of CSS writing syntax.
The style attribute.
Almost every HTML tag has an attribute style, which indicates that a certain style description is applied to this tag.
It is written like this:
style="">
Everything that will be written between the attribute quotes style and will be a style description for this element, in this case the element
Well, for example:
style="color: #ff0000; font-size:12px"> this is a paragraph with a personal style
In this case, we specified that this paragraph should be displayed in red and have a font size of 12 pixels. In subsequent chapters I will talk in detail about what is written in quotes, but now we are talking about how to apply CSS to any HTML tag.
Using the same principle, you can specify an individual style for almost every HTML element.
style="background-color: #c5ffa0">
style="color: #0000ff; font-size:18px">All about elephants
Buy an elephant
style="color: #ff0000; font-size:14px">
style="color: #0000ff; font-size:16px">Rent an elephant
style="color: #ff0000; font-size:14px">
But once again, this method of introducing CSS is good only if you need to set a certain style for a small number of HTML elements.
Tag
Для того, что бы описать необходимые элементы одновременно на всей странице в заголовок HTML документа внедряют тег (not to be confused with the attribute of the same name) in which the elements we need are described.
Take a look at the example, there will be comments below.
All about elephants
On this site you will find any information about elephants.
Buy an elephant
With us you can purchase the best elephants at competitive prices!!
Rent an elephant
Only here you can rent any elephants!!
As can be seen from the example, we achieved exactly the same result as in the first case, only now we do not assign a style to each element individually, but placed it in the “head” of the document, thereby indicating that all headings ,
- the paragraphs will be blue
- red. Imagine how much easier our work would be if there were a hundred such paragraphs and about fifteen headings on a page, and the document itself would weigh less by “removing” all repeating style descriptions for each individual element.
Now the promised comments:
Tag the styles of certain HTML elements are directly declared according to the following syntax:
If several properties of an element are specified in a style declaration block, they are separated by a semicolon.
CSS in a separate external file.
How long does it take to short, we come to the main, in my opinion, advantage of CSS, namely the ability to put all information related to the design of the site into a separate external file.
So, open notepad (or another editor) and write the following text in it:
Body (background-color: #c5ffa0)
a (color:#000060; font-weight: bold;)
a:hover (color:#ff0000; font-weight: bold; text-decoration:none)
h1 (color: #0000ff; font-size:18px)
h2 (color: #ff00ff; font-size:16px)
p (color: #600000; font-size:14px)
I will try to tell you in detail about what we wrote about this strange thing in the subsequent chapters of this textbook.
All! The style description file has been created! Now all that remains is just a little bit, namely to force the necessary pages of our site to draw information from this file.
This is done using the tag (connection). Tag multi-purpose and serves to “link” an HTML document with additional external files that ensure its proper operation. Tag is a kind of link, only intended not for users, but for browser programs (browsers). Because carries exclusively official information it is located in the head of the HTML document between the tags and is not displayed on the screen by browsers.
Tag has attributes:
href- The path to the file.rel- Defines the relationship between the current document and the file being referenced.
- shortcut icon - Specifies that the included file is a .
- stylesheet- Specifies that the included file contains a style sheet.
- application/rss+xml - A file in XML format to describe the news feed.
Since we connect as external file cascading style sheet, then our service link takes the following form:
I repeat, to certainly dispel possible misunderstandings. Attribute rel assign a value stylesheet Since we are connecting a cascading style sheet as an external file, we indicate the path to the css file (in this example the file is called mystyle.css and lies next to the HTML document in which it is written this link) we also indicate that this file text and contains a style description type="text/css"
Now we insert this line into the page headers of our site and enjoy the result..
mystyle.css file
body (background-color: #c5ffa0)
a (color:#000060; font-weight: bold;)
a:hover (color:#ff0000; font-weight: bold; text-decoration:none)
h1 (color: #0000ff; font-size:18px)
h2 (color: #ff00ff; font-size:16px)
p (color: #600000; font-size:14px)
index.html file
Menu:
All about elephants.
Buy an elephant.
Rent an elephant.
All about elephants
On this site you will find any information about elephants.
File elephant.html
Menu:
All about elephants.
Buy an elephant.
Rent an elephant.
Buy an elephant
With us you can purchase the best elephants at competitive prices!!
File elephant1.html
Menu:
All about elephants.
Buy an elephant.
Rent an elephant.
Rent an elephant
Only here you can rent any elephants!!
In the example above, "site about elephants", there are currently three pages, each associated with one single external css file - mystyle.css. Thus, we significantly “unloaded” it and made the design of the entire site “mobile-friendly”. Imagine how many kilobytes we would win if this site had a hundred full-fledged pages!? And also, how much time would we save if we needed to change anything in its design!?
In this chapter, we looked at three ways to embed CSS in an HTML document. Which one is better to use?
- Use attribute style for any element, if this element with a different style from other elements is the only one on the entire site.
- Use tag
This is a heading
This is a paragraph.