CSS (Cascading Style Sheets, Cascading Style Sheets) is a style sheet language used for HTML (Hypertext Markup Language, Hypertext Markup Language) documents. CSS can make the appearance of web pages more beautiful and clear, and improve the readability and maintainability of web pages.
CSS style sheets usually include three parts: selectors, attributes and values. The selector specifies the HTML element to which the style is to be applied, the attribute defines the style to be applied to the element, and the value specifies the specific value of the attribute.
Let’s learn more about how to use CSS.
First, we need to create a CSS style sheet for the HTML document. Usually, we store the CSS style sheet in a separate .css file and introduce it through the tag within the
tag of the HTML document.For example:
<head> <link rel="stylesheet" type="text/css" href="style.css"> </head>
A selector is an identifier that specifies the HTML element to which the CSS rule applies. Common selectors include:
Example:
/* 标签名选择器 */ p { color: blue; } /* 类选择器 */ .red { color: red; } /* ID选择器 */ #green { color: green; } /* 属性选择器 */ [class="yellow"] { color: yellow; } /* 伪类选择器 */ a:hover { color: purple; }
The attribute specifies the style of the HTML element to which the CSS rule applies. Common attributes include:
Example:
/* 文本颜色 */ p { color: blue; } /* 背景颜色 */ body { background-color: #eee; } /* 字体大小、字体类型、字体粗细 */ h1 { font-size: 32px; font-family: Arial, sans-serif; font-weight: bold; } /* 文本对齐方式 */ div { text-align: center; } /* 外边距、内边距 */ .box { margin: 10px; padding: 20px; } /* 边框 */ .img { border: 1px solid black; }
The value is the specific value of the attribute. The range of possible values for a property depends on the property's type. For example, colors can use predefined color names (like "red", "blue", etc.) or use hexadecimal or RGB values (like "#ff0000", "rgb(255,0,0)", etc.).
Example:
/* 颜色 */ p { color: blue; } /* 背景颜色 */ body { background-color: #eee; } /* 字体大小、字体类型、字体粗细 */ h1 { font-size: 32px; font-family: Arial, sans-serif; font-weight: bold; } /* 文本对齐方式 */ div { text-align: center; } /* 外边距、内边距 */ .box { margin: 10px; padding: 20px; } /* 边框 */ .img { border: 1px solid black; }
Styles in CSS can be inherited. For example, we can set a font attribute for all paragraphs in an HTML document, and the values of these attributes can be automatically inherited by the text contained in each paragraph.
Example:
/* 字体 */ body { font-family: Arial, sans-serif; }
When multiple CSS rules are applied to the same HTML element, they cascade. This means that some rules have a higher priority and take precedence over other rules. The cascading principle used in CSS is:
Example:
<!DOCTYPE html> <html> <head> <style> /* 优先级为1,文件顺序为1 */ p { color: red; } </style> <style> /* 优先级为10,文件顺序为2 */ .green { color: green; } </style> <style> /* 优先级为100,文件顺序为3 */ #blue { color: blue; } </style> </head> <body> <p class="green" id="blue">This text is blue.</p> </body> </html>
It is generally considered to link a CSS stylesheet to an HTML document using the tag is best practice. This has the following benefits:
Example:
<head> <link rel="stylesheet" type="text/css" href="style.css"> </head>This text is red.
This text is blue.
This text is green.
/* style.css */ .red { color: red; } .blue { color: blue; } .green { color: green; }
Sometimes, saving a CSS stylesheet into an HTML document can improve page load speed. If the style sheet applies only to the current page, you can place the style sheet in a