Home Web Front-end HTML Tutorial Three ways to place CSS in HTML and CSS selectors_html/css_WEB-ITnose

Three ways to place CSS in HTML and CSS selectors_html/css_WEB-ITnose

Jun 24, 2016 am 11:40 AM

(1) There are generally three ways to use CSS styles in HTML:

1 Inline reference

2 Internal reference

3 External reference.

The first type: inline reference (also called inline reference)

is to apply CSS styles directly to HTML tags.

 

Use CSS inline references to represent paragraphs.

 < ;/p>

Features: Inline styles are more flexible than other methods, but they need to be confused with the displayed content. Inline styles will lose some of the advantages of style sheets.

The second type: internal reference (also called inline)

Use the style tag to directly load the content of the CSS file into the HTML document in the tag.

 

 …

 ​​The text in the p tag is in the following style*/ >

  

 

 Features: Suitable for use when an HTML document has a unique style.

The third type: external reference

CSS external reference uses external CSS files. General browsers have caching functions, so users do not need to Download this CSS file every time.

External references are efficient and save bandwidth compared to internal references and inline references.

External references are recommended by W3C

There are two ways to implement external references:

(1) Use the link tag to reference CSS

(2) Use @import to import CSS

 

 …

                                         /css">

@import "mystyle2.css"

.... /*Other CSS definitions*/

/head>

Note: If you use several different style sheets on the same selector, this attribute value will overlap several style sheets and encounter conflicts. The final definition will prevail.

(2) CSS selector has six types of selectors:

1 HTML selector

2 types of selectors

3 ID selector

4 Association selector

5 Combination selector

6 Pseudo element selector

1 HTML selector: It is an HTML tag, used to change the style of a specified tag. Any HTML element can be a CSS selector.

Syntax: HTML tag name {attribute: value}

 p { text-indent:3em; } /*The selector is p*/

h1{ color:red; } /*The selector is h1*/

2. Class selector: Match the element whose class attribute value is classname of element E in the document

Syntax: tag name. class name {attribute: value} or . class name {attribute: value}

  类选择符名称的定义方式是,"."符号,英文"dot",后加类名称classname

  类选择符的定义需要有.符号(.符号标明是类选择符),但是HTML文档中的标签的class属性名不能出现.符号,见下面示例:

  p.dark-row{ background:#EAEAEA; }    /*设置p标签中class属性为dark-row的*/

  .note{ font-size:small }            /*为note的类可以被用于任何元素*/

  

第一段

             

  第二段    

  第三段           

  
第四段
 > Syntax: ID name {attribute: value}

The ID selector name is defined as the # symbol, English "pound", followed by the ID name idname

ID selector The definition needs to have the # symbol (the # symbol indicates the ID selector), but the # symbol cannot appear in the id attribute name of the tag in the HTML document, see the example below

The special thing about the id attribute is that a document Only one element can use an ID selector (just the opposite of the class attribute). The id attribute can be used to uniquely identify an element.

Text indentation 3em

4 association selectors: also It is called the inclusion selector. It can be used to define a style sheet for a certain element inclusion relationship. Element 1 contains element 2. This method is only defined for element 2 in element 1. There is no definition for individual element 1 or element 2.

Syntax: selector 1 selector 2...{attribute: value}

table a{font-size:12px}

Links within the table The style has been changed so that the text size is 12 pixels, while the text for links outside the table remains at the default size.

5 Combination selectors: also called selector groups, you can combine selectors with the same attributes and values ​​to write, and separate the selectors with commas, which can reduce the style Repeat definition.

Syntax: selector 1, selector 2,.,..{attribute: value}

h1, h2, h3, h4, h5, h6 { color: green }

p, table{ font-size: 9pt }

The effect is completely equivalent to:

p { font-size: 9pt }

table { font-size: 9pt }

6 Pseudo-element selectors refer to a way of defining different states of the same HTML element. For example, the normal state, access state, selected state, and the state when the cursor moves to the hyperlink text of the tag can be defined using a pseudo-element selector.

Syntax: tag: pseudo-element {attribute: value;}

a:link {color: #FF0000; text-decoration: none} /* Unvisited link*/

a:visited {color: #00FF00; text-decoration: none} /* Visited links*/

a:hover {color: #FF00FF; text-decoration: underline} /* Mouse over link*/

a:active {color: #0000FF; text-decoration: underline} /* Activate link*/

Tip: a:hover must be placed It is only valid after a:link and a:visited.

Tip: a:active must be placed after a:hover to be effective.

If there are any mistakes, please correct me.

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

See all articles