Table of Contents
How to insert a style sheet
External style sheet
Internal style sheets
Inline styles
Multiple Styles
Home Web Front-end HTML Tutorial Use css_html/css_WEB-ITnose

Use css_html/css_WEB-ITnose

Jun 24, 2016 am 11:55 AM

How to insert a style sheet

When a style sheet is read, the browser will format the HTML document according to it. There are three ways to insert a style sheet:

External style sheet

An external style sheet is ideal when styles need to be applied to many pages. With external style sheets, you can change the look of your entire site by changing one file. Each page links to the style sheet using the <link> tag. The <link> tag is in the head (of the document):

&lt;head&gt;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;mystyle.css&quot; /&gt;&lt;/head&gt;
Copy after login

The browser will read the style declaration from the file mystyle.css and format the document according to it.

External style sheets can be edited in any text editor. The file cannot contain any html tags. Style sheets should be saved with a .css extension. Here is an example of a stylesheet file:

hr {color: sienna;}p {margin-left: 20px;}body {background-image: url(&quot;images/back40.gif&quot;);}
Copy after login

Do not leave spaces between attribute values ​​and units. If you use "margin-left: 20 px" instead of "margin-left: 20px" it will only work in IE 6, but not in Mozilla/Firefox or Netscape.

Internal style sheets

When a single document requires special styling, an internal style sheet should be used. You can define an internal style sheet at the head of the document using the <style> tag, like this:

&lt;head&gt;&lt;style type=&quot;text/css&quot;&gt;  hr {color: sienna;}  p {margin-left: 20px;}  body {background-image: url(&quot;images/back40.gif&quot;);}&lt;/style&gt;&lt;/head&gt;
Copy after login

Inline styles

Since presentation and content are mixed together, inline styles Coupling styles loses many of the advantages of style sheets. Use this approach with caution, for example when the style only needs to be applied once to an element.

To use inline styles, you need to use the style attribute within the relevant tag. The Style property can contain any CSS property. This example shows how to change the color and left margin of a paragraph:

&lt;p style=&quot;color: sienna; margin-left: 20px&quot;&gt;This is a paragraph&lt;/p&gt;
Copy after login

Multiple Styles

If certain properties are defined by the same selector in different style sheets, Then the property value will be inherited from the more specific style sheet.

For example, the external style sheet has three properties for h3 selectors:

h3 {  color: red;  text-align: left;  font-size: 8pt;  }
Copy after login

while the internal style sheet has two properties for h3 selectors:

h3 {  text-align: right;   font-size: 20pt;  }
Copy after login

If this page with an internal style sheet is linked to an external style sheet at the same time, then the style obtained by h3 is:

color: red; text-align: right; font-size: 20pt;
Copy after login

That is, the color attribute will be inherited from the external style sheet, and the text arrangement (text -alignment) and font-size are replaced by rules in the internal style sheet.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

How do I use HTML5 form validation attributes to validate user input?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

What is the purpose of the <iframe> tag? What are the security considerations when using it?

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

What is the purpose of the <meter> element?

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

What is the purpose of the <datalist> element?

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

What are the best practices for cross-browser compatibility in HTML5?

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

What is the purpose of the <progress> element?

See all articles