What are styles in html. Embedding CSS in an HTML Document
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 is that we implement the necessary design 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 the this element.
For example, let's set different sizes font 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 design large volumes 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 will certainly arise in use 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 img tag may not fit. 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 as follows: to the block, on the design of which work in progress, display:block is written, 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" >Pop-up window that is currently being designed 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.
CSS (Cascading Style Sheets), or cascading style sheets, are used to describe appearance web document written in markup language. CSS sets 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 embed CSS code directly into a markup element as an attribute value style. This attribute is available for all HTML elements. WITH using CSS You can specify a number of style properties for a given HTML element. Each property has a name and a value, separated by a colon (:). Each declared property is separated by a semicolon (;).
This is what it looks like 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 - Define style sheet rules in separate file.css, and then including this file in the HTML document using the tag
- Internal Style Sheet - Define style sheet rules using a tag
Example: Internal Style Sheet
- Try it yourself "
Heading
Text first
Text two
Text three
Internal style sheet Heading
Text first
Text two
Text three
IN in this example we set the background color for the element using CSS
: background-color:palegreen, color and font type for headings: color:blue; font-family:verdana, as well as font size, color, and center text alignment for paragraphs
: font-size:20px; color:red; text-align:center.
Built-in style
When to format separate element HTML pages, the style description can be placed directly inside the opening tag using the already specialized style attribute. For example:
Paragraph
Such styles are called built-in (inline), or embedded. Rules defined directly within an element's opening tag override rules defined in the outer CSS file, as well as the rules defined in the element
Heading
Text first
Text two
Text three