CSS selectors

CSS selector


#CSS selector is divided into two types

Id and Class Selectors

#If you want to set CSS styles in HTML elements, you need to set the "id" and "class" selectors in the element.


#id selector

The id selector can specify a specific style for HTML elements marked with a specific id.

The HTML element uses the id attribute to set the id selector, and the id selector in CSS is defined with "#".

The following style rules apply to the element attribute id="para1":

    php中文网(php.cn)  

php中文网(php.cn)

这一段不受css样式影响。

Run the program to try it


The ID attribute should not start with a number. IDs starting with numbers do not work in Mozilla/Firefox browsers.lamp.gif


class selector

class selector is used to describe the style of a group of elements. The class selector is different from the id selector. , class can be used in multiple elements.

The class selector is represented by the class attribute in HTML. In CSS, the class selector is displayed with a dot ".":

In the following example, all objects with the center class HTML elements are all centered.

Example

    php中文网(php.cn)  

标题居中

段落居中。

Run the program to try it


You can also specify specific HTML elements using classes.

In the following example, all p elements use class="center" to center the text of the element:

Example

    php中文网(php.cn)  

这个标题将不会受到影响

这一段将居中对齐

Run the program and try it


The first character of the class name cannot use numbers! It won't work in Mozilla or Firefox.lamp.gif


Continuing Learning
||
php中文网(php.cn)

php中文网(php.cn)

这一段不受css样式影响。

submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!