attribute selector

Set styles for HTML elements with specified attributes.

You can set styles for HTML elements with specified attributes, not just class and id attributes.

Note: IE7 and IE8 support attribute selectors only when !DOCTYPE is specified. In IE6 and lower, attribute selection is not supported.


Attribute Selector

The following example sets the style for all elements with the title attribute:

[title]{color:red;}

Attribute and value selectors

The following example sets the style for all elements with title="php":

[title=php]{border:5px solid blue;}


Attributes and value selectors - multiple values

The following example sets a style for all elements that contain a title attribute with a specified value. Applies to attribute values ​​separated by whitespace:

[title~=hello] { color:red; }

The following example styles all elements with a lang attribute that contains the specified value. Applies to attribute values ​​separated by hyphens:

[lang|=en] { color:red; }


Set the style of the form

Attribute selectors are used without Particularly useful when setting styles on forms of class or id:

input[type="text"]{  
    width:150px;  
    display:block;  
    margin-bottom:10px; 
    background-color:yellow; 
    font-family: Verdana, Arial;
}
input[type="button"]{  
    width:120px;  
    margin-left:35px; 
    display:block;  
    font-family: Verdana, Arial;
}

QQ截图20161012090827.png

Continuing Learning
||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <style type="text/css"> [title] { color:red; } </style> </head> <body> <h1>可以应用样式:</h1> <h2 title="Hello world">Hello world</h2> <a title="php" href="http://php.cn">php中文网</a> <hr /> <h1>无法应用样式:</h1> <h2>Hello world</h2> <a href="http://php.cn">php中文网</a> </body> </html>
submitReset Code