search
HomeWeb Front-endHTML TutorialBasic knowledge of CSS (Cascading Style Sheets)_html/css_WEB-ITnose

CSS refers to Cascading Style Sheets. Styles define how HTML elements are displayed. It is usually stored in a style sheet and adds styles to HTML 4.0 to solve the problem of separation of content and presentation.

When the same HTML element is defined by more than one style, which style will be used?

Generally speaking, all styles will be cascaded in a new virtual style sheet according to the following rules, with number 4 having the highest priority.

  1. Browser default settings
  2. External style sheet
  3. Internal style sheet (located inside the tag)
  4. Inline styles (Inside an HTML element)

Therefore, an inline style (inside an HTML element) has the highest priority, which means it will take precedence over the following style declaration: in the

tag style declaration, a style declaration in an external style sheet, or a style declaration in the browser (the default).


CSS Syntax

CSS rules consist of two main parts: the selector, and one or more statement.

selector {declaration1; declaration2; ... declarationN }

A selector is usually an HTML element that you need to change the style of.

Each declaration consists of an attribute and a value.

The property is the style attribute you wish to set. Each attribute has a value. Properties and values ​​are separated by colons.

selector {property: value}

The following line of code sets the text color within the h1 element to red and sets the font size to 14 pixels.

In this example, h1 is the selector, color and font-size are attributes, and red and 14px are values.

h1 {color:red; font-size:14px;}

The diagram below shows you how the code above is structured:

Tip: Use curly braces to surround declarations.


Group of selectors

You can group selectors , so that grouped selectors share the same declaration. Use commas to separate selectors that need to be grouped. In the example below, we have grouped all heading elements. All title elements are green.

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

Inheritance and its problems

According to CSS, child elements inherit properties from their parent element. But it doesn't always work this way. Consider the following rule:

body {     font-family: Verdana, sans-serif;     }

According to the above rule, the body element of the site will use the Verdana font (assuming the font is present on the visitor's system).

Through CSS inheritance, child elements will inherit the properties owned by the top-level element (in this case, body) (these child elements such as p, td, ul, ol, ul, li, dl, dt, and dd). No additional rules are needed, all child elements of the body should display the Verdana font, as well as the child elements of the child element. And in most modern browsers, this is indeed the case.

But in the bloody days of the browser wars, this might not have happened, when support for standards was not a priority for enterprises. For example, Netscape 4 does not support inheritance and not only ignores inheritance, but also ignores rules that apply to the body element. There is still a related issue in IE/Windows up to IE6 where font styles in tables will be ignored. What should we do?


Derived selector

by basing the element on the context of its position By using relationships to define styles, you can make your markup more concise.

In CSS1, selectors that apply rules in this way are called contextual selectors because they rely on context to apply or avoid a rule. In CSS2, they are called derived selectors, but no matter what you call them, they do the same thing.

Derived selectors allow you to style a tag based on the context of the document. By judiciously using derived selectors, we can make our HTML code cleaner.

For example, if you want the strong element in the list to be in italics instead of the usual bold, you can define a derived selector like this:

li strong {    font-style: italic;    font-weight: normal;  }

Please note that the tag The context of the blue code of :

<p><strong>我是粗体字,不是斜体字,因为我不在列表当中,所以这个规则对我不起作用</strong></p><ol>
<li><strong>我是斜体字。这是因为 strong 元素位于 li 元素内。</strong></li>
<li>我是正常的字体。</li>
</ol>

In the above example, only the strong element in the li element is styled in italics, and there is no need to define a special class or id for the strong element. , the code is more concise.



id selector

id selector The browser can specify specific styles for HTML elements marked with specific ids.

The id selector is defined with "#".


It should be noted that each id can only appear once in the same html document.


In CSS, the class selector is displayed with a period:

.center {text-align: center}

In In the above example, all HTML elements with the center class are centered.

在下面的 HTML 代码中,h1 和 p 元素都有 center 类。这意味着两者都将遵守 ".center" 选择器中的规则。

<h1 id="This-heading-will-be-center-aligned">This heading will be center-aligned</h1><p class="center">This paragraph will also be center-aligned.</p>

注意:类名的第一个字符不能使用数字!它无法在 Mozilla 或 Firefox 中起作用。


要灵活运用各种类型CSS的使用,以及结合。



如何插入样式表

当读到一个样式表时,浏览器会根据它来格式化 HTML 文档。插入样式表的方法有三种:

外部样式表

当样式需要应用于很多页面时,外部样式表将是理想的选择。在使用外部样式表的情况下,你可以通过改变一个文件来改变整个站点的外观。每个页面使用 标签链接到样式表。 标签在(文档的)头部:

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

浏览器会从文件 mystyle.css 中读到样式声明,并根据它来格式文档。

外部样式表可以在任何文本编辑器中进行编辑。文件不能包含任何的 html 标签。样式表应该以 .css 扩展名进行保存。下面是一个样式表文件的例子:

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

不要在属性值与单位之间留有空格。假如你使用 “margin-left: 20 px” 而不是 “margin-left: 20px” ,它仅在 IE 6 中有效,但是在 Mozilla/Firefox 或 Netscape 中却无法正常工作。

内部样式表

当单个文档需要特殊的样式时,就应该使用内部样式表。你可以使用

<style type="text/css">  hr {color: sienna;}  p {margin-left: 20px;}  body {background-image: url("images/back40.gif");}</style>

内联样式

由于要将表现和内容混杂在一起,内联样式会损失掉样式表的许多优势。请慎用这种方法,例如当样式仅需要在一个元素上应用一次时。

要使用内联样式,你需要在相关的标签内使用样式(style)属性。Style 属性可以包含任何 CSS 属性。本例展示如何改变段落的颜色和左外边距:

<p style="color: sienna; margin-left: 20px">This is a paragraph</p>

多重样式

如果某些属性在不同的样式表中被同样的选择器定义,那么属性值将从更具体的样式表中被继承过来。

例如,外部样式表拥有针对 h3 选择器的三个属性:

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

而内部样式表拥有针对 h3 选择器的两个属性:

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

假如拥有内部样式表的这个页面同时与外部样式表链接,那么 h3 得到的样式是:

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

即颜色属性将被继承于外部样式表,而文字排列(text-alignment)和字体尺寸(font-size)会被内部样式表中的规则取代。


Statement
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
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

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

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

See all articles

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment