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.
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 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.
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; }
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?
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 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 class="center">This heading will be center-aligned</h1><p class="center">This paragraph will also be center-aligned.</p>
注意:类名的第一个字符不能使用数字!它无法在 Mozilla 或 Firefox 中起作用。
要灵活运用各种类型CSS的使用,以及结合。
当读到一个样式表时,浏览器会根据它来格式化 HTML 文档。插入样式表的方法有三种:
当样式需要应用于很多页面时,外部样式表将是理想的选择。在使用外部样式表的情况下,你可以通过改变一个文件来改变整个站点的外观。每个页面使用 标签链接到样式表。 标签在(文档的)头部:
<head><link rel="stylesheet" type="text/css" href="mystyle.css" /></head>
浏览器会从文件 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 中却无法正常工作。
当单个文档需要特殊的样式时,就应该使用内部样式表。你可以使用