Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS attribute selectors

CSS Attribute Selectors


CSS attribute selectors are used to select HTML elements based on their attributes or attribute values.

Attribute selectors allow you to style elements without having to add a class or ID to them.

**Note:** IE7 and IE8 require a !DOCTYPE declaration to support attribute selectors! IE6 and earlier versions do not support attribute selectors.

The following are common CSS attribute selectors:

1、[attribute]

Selects all elements with the specified attribute (regardless of the attribute value).

/* 选择所有具有 `type` 属性的元素 */

[type] {

  border: 1px solid red;

}

2、[attribute="value"]

Selects elements with the specified attribute and an exactly matching value.

/* 选择所有 `type` 属性等于 `text` 的元素 */

[type="text"] {

  background-color: yellow;

}

3、[attribute~="value"]

Selects elements whose attribute value contains the specified word (one of a list of words separated by spaces).

/* 选择属性值中包含 `button` 的元素 */

[class~="button"] {

  font-weight: bold;

}

4、[attribute|="value"]

Selects elements whose attribute value is exactly the specified value or begins with the specified value immediately followed by a hyphen -, often used for language codes.

/* 选择所有 `lang` 属性是 `en` 或者以 `en-` 开头的元素 */

[lang|="en"] {

  color: green;

}

5、[attribute^="value"]

Selects elements whose attribute value begins with the specified value.

/* 选择所有 `href` 属性以 `https` 开头的链接 */

[href^="https"] {

  text-decoration: none;

}

6、[attribute$="value"]

Selects elements whose attribute value ends with the specified value.

/* 选择所有 src 属性以 .jpg 结尾的图片 */

[src$=".jpg"] {

  border: 2px solid blue;

}

7、[attribute*="value"]

Selects elements whose attribute value contains the specified value.



/* Select all elements whose `title` attribute contains `tutorial` */

[title*="tutorial"] {

  font-style: italic;

}

With attribute selectors, you can easily select elements and apply specific styles based on their attributes, increasing flexibility and maintainability.


Attribute Selectors

The following example turns all elements with a title attribute blue:

Example

[

title

]

{

color:

blue

;

}

Try it »


Attribute and Value Selectors

The following example changes the border style of elements with title='runoob':

Example

[

title

=

runoob

]

{

border:

5

px

solid

green

;

}

Try it »


Attribute and Value Selectors - Multiple Values

The following is an example of styling elements with a title attribute containing a specified value, using (~) to separate the attribute and value:

Example

[

title

~=

hello

]

{

color:

blue

;

}

Try it »

The following is an example of styling elements with a lang attribute containing a specified value, using (|) to separate the attribute and value:

Example

[

lang

|=

en

]

{

color:

blue

;

}

Try it »


Form Styles

Attribute selector styles do not require the use of class or id:

Example

input

[

type

=

"

text

"

]

{

width:

150

px

;

display:

block

;

margin-bottom:

10

px

;

background-color:

yellow

;

}

input

[

type

=

"

button

"

]

{

width:

120

px

;

margin-left:

35

px

;

display:

block

;

}

Try it »