HTML add css--style Selector

Source: Internet
Author: User

How to add a style to HTML. Two methods:

First, create a new CSS style sheet, the same directory as the original HTML, and then through the link tag links. such as: <link type= "Text/css" rel= "stylesheet" href= "lounge.css" > (link tag is a void element with no end tag.) )

Second, use the style tag directly in the HTML source code. such as the:<style> style </style>,style tag embedded in the head.

Inserting a style into HTML will involve five style selectors.

1. Element selector to add a style to an element

As follows: the P element and the text within the H1 element apply the style inside the curly braces

<style>

P,h1{color:grey;

background-color:red;

Font-family:sans-serif;}

</style>

2. Class Selector, classes property

<style>Create a CSS class named in the label blue-text , and then apply the class to the HTML element.

Such as:

<style>

. Blue-text{color:black;}

</style>

<p class= "Blue-text" >

Note that in CSS style elements, the class selector should be added . as a prefix. In the class declaration of an HTML element, the class attribute cannot be added . as a prefix.

The class selector does not have to be specific to a label, and this CSS class is used whenever the element has a class tag applied to it.

3. ID property setting Style

Like class, an ID is defined for an element, and a declaration is created for the ID. Cases:

<p id= "Important" ></p>

<style>

#important {color:black;}

</style>

Note: Unlike class, you should add # as a prefix in CSS style elements, and there is no prefix in HTML. An ID corresponds to a CSS property.

4. Attribute selector, select an element to apply a style

such as: *[title]{color:red;}

All elements that contain the title attribute can apply the above style.

5. Descendant selectors are also called include selectors. Descendant selectors can select elements that are descendants of an element.

Select elements based on context

You can define descendant selectors to create rules that make these rules work in some document structures, but not in other structures.

For example, if you want to apply a style only to the EM element in the H1 element, you can write this:

H1 em {color:red;}

The above rule turns the text of the EM element as the descendant of the H1 element into red. Other EM text (such as EM in a paragraph or block reference) is not selected by this rule:



p>em{color;red;} indicates that the EM element next to P uses this style.
p+em{} uses this style with the EM element adjacent to the P element.
This style is applied to the p adjacent to the H1+P{}H1.


HTML add css--style Selector

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.