1. Introduction
Cascading style sheets are a computer language used to represent file styles such as HTML (an application of the standard Universal Markup Language) or XML (a subset of the standard Universal Markup Language).
CSS is currently the latest version of CSS3, is to be able to truly achieve Web page performance and content separation of a style design language. Compared to the performance of traditional HTML, CSS can control the placement of objects in the Web page with pixel-level precision, support almost all font size styles, have the ability to edit Web objects and model styles, and be able to conduct preliminary interaction design, is currently based on the best performance design language text display. CSS can be based on the understanding of different users to simplify or optimize the writing, for all types of people, have a strong legibility.
2. Basic syntax
A CSS rule consists of two main parts: a selector, and one or more declarations.
selector {declaration1; declaration2; ... declarationn}
Each declaration consists of an attribute and a value.
selector {Property:value}
In the following example, H1 is a selector, color and font-size are attributes, and red and 14px are values.
h1 {color:red; font-size:14px;}
3. Selector type
a). Tag selector: The selector is usually an HTML tag, such as P, H1, EM, a, or even the HTML itself.
HTML {color:black;} h1 {Color:blue;} h2 {Color:silver;}
b). class selector: Class selectors typically begin with a. Number and are referenced in an HTML file by the class attribute.
. important {color:red;} <H1 class= "Important" >this heading is very important.
c). ID selector: Usually begins with a # and is referenced in an HTML file by an id attribute, unlike a class selector, where the ID selector can only be used once in a document.
#mostImportant {color:red; background:yellow;} <H1 id= "Mostimportant" >this is important!
d). Property selector: Typically enclosed in brackets, you can use the Simple property selector if you want to select an element that has a property, regardless of the property value. It has several uses:
[Attribute] used to select an element with the specified attribute.
[Attribute=value] used to select an element with the specified attributes and values.
[Attribute~=value] used to pick a list of words separated by a space, with one equal to value.
[Attribute|=value] used to pick a list of words separated by a hyphen with the attribute value, starting with value.
[Attribute^=value] matches each element of the property value to the beginning of the specified value.
[Attribute$=value] matches each element of the property value to the end of the specified value.
[Attribute*=value] matches each element of the property value that contains the specified value.
4.css use in HTML documents
a). Direct style sheet: inline styles lose many of the benefits of stylesheets because of the mix of performance and content. Use this method with caution, such as when the style only needs to be applied once on one element.
<p style= "Color:sienna; margin-left:20px ">this is a paragraph</p>
b). Internal style sheet: An internal style sheet should be used when a particular style is required for a single document. You can use the <style> tag to define an internal style sheet at the head of the document, just like this:
c). Appearance style sheet: An external style sheet is an ideal choice when the style needs to be applied to many pages. With an external style sheet, you can change the appearance of the entire site by changing a file. Each page uses <link> tags to link to the style sheet. <link> tags at the head of (document):
CSS Basic syntax