CSS CSS Syntax

Source: Internet
Author: User

Introduction to CSS

CSS (cascading style Sheet, layer style sheets) defines how HTML elements are displayed.

When the browser reads a stylesheet, it formats (renders) the document according to the style sheet.

CSS Syntax CSS instance

Each CSS style consists of two components: a selector and a declaration. The declaration also includes attributes and property values. Each deity then ends with a seal.

CSS Annotations
/* This is a comment */
CSS hit introduction in-line style

Inline is the CSS style that is set in the Stykle attribute of the tag. Large-scale use is not recommended.

<style= "color:red">Hello World. </ P >
Internal style

Embedded is a set of CSS styles that are written in the

<Head>    <MetaCharSet= "UTF-8">    <title>Title</title>    <style>P{Background-color:#2b99ff;        }    </style></Head>
External styles

The external style is to write the CSS in a separate file and then introduce it to the page. This is the recommended method.

<href= "Mystyle.css"  rel= "stylesheet"  type = "Text/css" />
CSS Selector basic selector element Selector
{color: "Red";}
ID Selector
{  background-color: red;}
Class Selector
{  font-size: 14px;}  {  color: red;}

Attention:

Style class names do not start with numbers (some browsers do not recognize them).

If you have more than one class attribute in the tag, use a space to split it.

Universal Selector
{  color: White;}
Combo Selector descendant Selector
/* the A label inside Li sets the font color */  {  color: green;}
Son selector
/* Select all the parent is the <p> element of the <div> element */  {  font-family: "Arial Black", arial-black, cursive;}
Adjacent selector
/* Select all <p> elements immediately following the <div> element */  {  margin: 5px;}
Brother Selector
/* I1 behind all the brothers P tag */  {  border: 2px solid royalblue;}
Property Selector
/* used to select an element with the specified attribute.  */{  color: red;} /* used to select an element with the specified attributes and values.  */{  color: green;}
/*find all elements with the title property beginning with Hello*/[title^= "Hello"]{Color:Red;}/*find all elements with the title property ending with Hello*/[title$= "Hello"]{Color:Yellow;}/*Find all elements of the title property that contain (string contains) Hello*/[title*= "Hello"]{Color:Red;}/*Find all the title attributes (with multiple values or values separated by spaces) with an element with a value of Hello:*/[title~= "Hello"]{Color:Green;}
Grouping and nesting groupings

When the styles of multiple elements are the same, we do not need to repeat the style for each element, we can set the element style uniformly by the comma-style grouping selector among multiple selectors.

For example:

{  color: red;}

The above code uniformly sets the font to red for div tags and p tags.

{  color: red;}
Nesting

Multiple selectors can be mixed to use, for example:. C1 all P tags within the class set the font color to red.

{  color: red;}
Precedence CSS inheritance for selectors

Inheritance is a major feature of CSS, and it is dependent on the ancestor-descendant relationship. Inheritance is a mechanism that allows styles not only to be applied to a particular element, but also to apply

To the offspring of it. For example, a body-defined font color value is also applied to the text of a paragraph.

{  color: red;}

All tags on the page inherit the body font color. However, the weight of CSS inheritance is very low, it is 0 lower than the weight of ordinary elements.

We just set the font color for the corresponding tag to overwrite the style it inherits.

{  color: green;}

In addition, inheritance is an important part of CSS, and we don't even have to think about why it's possible, but there are limitations to CSS inheritance. Some properties cannot be inherited, such as: border, margin, padding, background, etc.

The priority of the selector

We have learned a lot of selectors, that is, in an HTML page there are many ways to find an element and set its style, the browser according to what determines which style should be applied?

In fact, according to the weight of the different selectors to decide, the specific choice of weight calculation method such as:

In addition, you can force the style to take effect by adding the!import method, but it is not recommended. Because excessive use of!import can make the style file confusing and difficult to maintain.

Don't use!import unless you have to.

CSS CSS Syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.