Development Journey (+): CSS inheritance-in-depth profiling

Source: Internet
Author: User

CSS inheritance refers to the style nature of labels that are wrapped inside the label that will have an external label. The most typical application of inheritance features typically plays in the entire Web page style presets, the overall layout declaration. You need to specify the other styles of the part set in the individual elements to achieve the effect. This feature gives web designers a better place to play. But inheritance also has a lot of rules, the application of time is easy to confuse people.
CSS allows us to set the appearance of a richer and more easily modifiable document. We now mainly want to work with friends on the inheritance of CSS a bit in-depth discussion.

One of the main features of CSS is inheritance, which is dependent on the ancestor-descendant relationship. Inheritance is a mechanism that allows a style to be applied not only to a particular element, but also to its descendants. For example, a body-defined color value is also applied to the text of a paragraph. The following examples illustrate:
Example Source Code
Style definition: body{color:blue;}
Application example code:<p>css<strong> inheritance </strong> in-depth analysis </p>

Take a look at the following example:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >< HTML xmlns= "http://www.w3.org/1999/xhtml" >

Running the above code, we see the result is: "CSS inheritance in-depth analysis" this paragraph is blue. "Inheritance" is bold because of the strong element applied. This is in line with the author's intentions and why CSS inherits part of the reason.

In the actual work, we write code, often in the front of the CSS document, first defined:

Example Source Code

*{margin:0; padding:0; border:0;}


The real intent of the code is that all the non-vegetarian margin, padding, and border values are zero, in the case of default definitions. When you need to apply a different style, you can define the element individually. That is, the entire Web page style presets, the overall layout declaration. The part that needs to be specified as a different style can be set in an individual element to achieve the effect.

In CSS, inheritance is a very natural behavior, and we don't even have to consider whether we can do it, but inheritance has its limitations.

First, some properties are not inherited. There is no reason for this, just because it is set up, as is the standard. For example: Border attribute, we all understand that the function of the border property is to set the border of the element, it has no inheritance. If you inherit the border property, the document will look strange. For example we define a container div with a border of 1px, and in this container the UL Li under normal circumstances we do not want it to have a border, if the border have inheritance, we will have to remove their borders. This is obviously unreasonable.

The properties of most border classes, such as: Border (Border), Padding (padding), margin (boundary), background, etc., are not inherited.

At some point inheritance also brings some errors, such as the following CSS definition:

Body{color:blue}

This defines the color of the text in the body as blue. If the body contains a table, in some browsers this definition makes the text in addition to the table blue, and the text color inside the table is not blue. Technically, this is not true, but it does exist. So we often need to help with some techniques, such as defining CSS like this:

Body,table,th,td{color:blue}

The text in this form will also turn blue.

In CSS, inheritance is a very natural behavior, and we don't even have to consider whether we can do it, but inheritance has its limitations.

First, some properties are not inherited. There is no reason for this, just because it is set up, as is the standard. For example: Border attribute, we all understand that the function of the border property is to set the border of the element, it has no inheritance. If you inherit the border property, the document will look strange. For example we define a container div with a border of 1px, and in this container the UL Li under normal circumstances we do not want it to have a border, if the border have inheritance, we will have to remove their borders. This is obviously unreasonable.

The properties of most border classes, such as: Border (Border), Padding (padding), margin (boundary), background, etc., are not inherited.

At some point inheritance also brings some errors, such as the following CSS definition:

Body{color:blue}

This defines the color of the text in the body as blue. If the body contains a table, in some browsers this definition makes the text in addition to the table blue, and the text color inside the table is not blue. Technically, this is not true, but it does exist. So we often need to help with some techniques, such as defining CSS like this:

Body,table,th,td{color:blue}

The text in this form will also turn blue.

Development Journey (+): CSS inheritance-in-depth profiling

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.