首页 > 其他分享 >css07 CSS Attribute Selectors

css07 CSS Attribute Selectors

时间:2024-05-29 21:45:27浏览次数:24  
标签:elements attribute value Selectors yellow background Attribute CSS

https://www.w3schools.com/css/css_attribute_selectors.asp

Style HTML Elements With Specific Attributes

It is possible to style HTML elements that have specific attributes or attribute values.


CSS [attribute] Selector

The [attribute] selector is used to select elements with a specified attribute.

The following example selects all <a> elements with a target attribute:

Example

a[target] {
  background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
a[target] {
  background-color: yellow;
}
</style>
</head>
<body>

<h2>CSS [attribute] Selector</h2>
<p>The links with a target attribute gets a yellow background:</p>

<a href="https://www.w3schools.com">w3schools.com</a>
<a href="http://www.disney.com" target="_blank">disney.com</a>
<a href="http://www.wikipedia.org" target="_top">wikipedia.org</a>

</body>
</html>

 


CSS [attribute="value"] Selector

The [attribute="value"] selector is used to select elements with a specified attribute and value.

The following example selects all <a> elements with a target="_blank" attribute:

Example

a[target="_blank"] {
  background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
a[target="_blank"] {
  background-color: yellow;
}
</style>
</head>
<body>

<h2>CSS [attribute="value"] Selector</h2>
<p>The link with target="_blank" gets a yellow background:</p>

<a href="https://www.w3schools.com">w3schools.com</a>
<a href="http://www.disney.com" target="_blank">disney.com</a>
<a href="http://www.wikipedia.org" target="_top">wikipedia.org</a>

</body>
</html>

 


CSS [attribute~="value"] Selector

The [attribute~="value"] selector is used to select elements with an attribute value containing a specified word.

The following example selects all elements with a title attribute that contains a space-separated list of words, one of which is "flower":

Example

[title~="flower"] {
  border: 5px solid yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
[title~="flower"] {
  border: 5px solid yellow;
}
</style>
</head>
<body>

<h2>CSS [attribute~="value"] Selector</h2>
<p>All images with the title attribute containing the word "flower" get a yellow border.</p>

<img src="klematis.jpg" title="klematis flower" width="150" height="113">
<img src="img_flwr.gif" title="flower" width="224" height="162">
<img src="img_tree.gif" title="tree" width="200" height="358">

</body>
</html>

 

The example above will match elements with title="flower", title="summer flower", and title="flower new", but not title="my-flower" or title="flowers".


   

CSS [attribute|="value"] Selector

The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-).

Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".

Example

[class|="top"] {
  background: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
[class|="top"] {
  background: yellow;
}
</style>
</head>
<body>

<h2>CSS [attribute|="value"] Selector</h2>

<h1 class="top-header">Welcome</h1>
<p class="top-text">Hello world!</p>
<p class="topcontent">Are you learning CSS?</p>

</body>
</html>

 


CSS [attribute^="value"] Selector

The [attribute^="value"] selector is used to select elements with the specified attribute, whose value starts with the specified value.

The following example selects all elements with a class attribute value that starts with "top":

Note: The value does not have to be a whole word!

Example

[class^="top"] {
  background: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style>
[class^="top"] {
  background: yellow;
}
</style>
</head>
<body>

<h2>CSS [attribute^="value"] Selector</h2>

<h1 class="top-header">Welcome</h1>
<p class="top-text">Hello world!</p>
<p class="topcontent">Are you learning CSS?</p>

</body>
</html>

 


CSS [attribute$="value"] Selector

The [attribute$="value"] selector is used to select elements whose attribute value ends with a specified value.

The following example selects all elements with a class attribute value that ends with "test":

Note: The value does not have to be a whole word!  

Example

[class$="test"] {
  background: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style> 
[class$="test"] {
  background: yellow;
}
</style>
</head>
<body>

<h2>CSS [attribute$="value"] Selector</h2>

<div class="first_test">The first div element.</div>
<div class="second">The second div element.</div>
<div class="my-test">The third div element.</div>
<p class="mytest">This is some text in a paragraph.</p>

</body>
</html>

 


CSS [attribute*="value"] Selector

The [attribute*="value"] selector is used to select elements whose attribute value contains a specified value.

The following example selects all elements with a class attribute value that contains "te":

Note: The value does not have to be a whole word!  

Example

[class*="te"] {
  background: yellow;
}
<!DOCTYPE html>
<html>
<head>
<style> 
[class*="te"] {
  background: yellow;
}
</style>
</head>
<body>

<h2>CSS [attribute*="value"] Selector</h2>

<div class="first_test">The first div element.</div>
<div class="second">The second div element.</div>
<div class="my-test">The third div element.</div>
<p class="mytest">This is some text in a paragraph.</p>

</body>
</html>

 


Styling Forms

The attribute selectors can be useful for styling forms without class or ID:

Example

input[type="text"] {
  width: 150px;
  display: block;
  margin-bottom: 10px;
  background-color: yellow;
}

input[type="button"] {
  width: 120px;
  margin-left: 35px;
  display: block;
}
<!DOCTYPE html>
<html>
<head>
<style>
input[type="text"] {
  width: 150px;
  display: block;
  margin-bottom: 10px;
  background-color: yellow;
}

input[type="button"] {
  width: 120px;
  margin-left: 35px;
  display: block;
}
</style>
</head>
<body>

<h2>Styling Forms</h2>

<form name="input" action="" method="get">
  Firstname:<input type="text" name="Name" value="Peter" size="20">
  Lastname:<input type="text" name="Name" value="Griffin" size="20">
  <input type="button" value="Example Button">
</form>

</body>
</html>

 

Tip: Visit our CSS Forms Tutorial for more examples on how to style forms with CSS.




All CSS Attribute Selectors

SelectorExampleExample description
[attribute] [target] Selects all elements with a target attribute
[attribute=value] [target="_blank"] Selects all elements with target="_blank"
[attribute~=value] [title~="flower"] Selects all elements with a title attribute that contains a space-separated list of words, one of which is "flower"
[attribute|=value] [lang|="en"] Selects all elements with a lang attribute value starting with "en"
[attribute^=value] a[href^="https"] Selects all <a> elements with a href attribute value starting with "https"
[attribute$=value] a[href$=".pdf"] Selects all <a> elements with a href attribute value ending with ".pdf"
[attribute*=value] a[href*="w3schools"] Selects all <a> elements with a href attribute value containing the substring "w3schools"

 

标签:elements,attribute,value,Selectors,yellow,background,Attribute,CSS
From: https://www.cnblogs.com/emanlee/p/18221124

相关文章

  • css08 How To Add CSS
    https://www.w3schools.com/css/css_howto.aspWhenabrowserreadsastylesheet,itwillformattheHTMLdocumentaccordingtotheinformationinthestylesheet.ThreeWaystoInsertCSSTherearethreewaysofinsertingastylesheet:ExternalCSSInte......
  • at-rule or selector expectedcss(css-ruleorselectorexpected)
    问题如图所示,在使用scss和less是使用//注释就会出现红色报错,这个不影响运行,但是看着很不舒服。解决没有找到什么好的解决办法,一般的解决方法是,把验证关闭,然后重启。但是后面我开启这个设置,再进行注释,没有报错,感觉还是重启的作用。先放着不管吧。......
  • 7. CSS 网格布局
    CSS3引入了强大的网格布局(GridLayout),它提供了一种二维的布局方式,使得创建复杂的网页布局变得更加简单和直观。通过定义行和列,我们可以精确控制网页元素的排列和对齐。本章将详细介绍网格布局的基本概念和属性,并通过具体示例帮助读者掌握如何使用网格布局。7.1网格布局简......
  • css06 CSS Pseudo-elements
    https://www.w3schools.com/css/css_pseudo_elements.aspWhatarePseudo-Elements?ACSSpseudo-elementisusedtostylespecifiedpartsofanelement.Forexample,itcanbeusedto:Stylethefirstletter,orline,ofanelementInsertcontentbefore,or......
  • 8. CSS弹性布局基础
    第8章弹性布局基础CSS3引入了一个强大的布局模块——弹性布局(Flexbox),它提供了一种更加高效、直观的方式来排列和对齐元素,使复杂布局的实现变得更加简单。本章将详细介绍弹性布局的基本概念和属性,并通过具体示例帮助读者掌握如何使用弹性布局。8.1弹性布局简介弹性布局(F......
  • 第三方css动画库
    https://animate.style///安装npminstallanimate.css--save//引入import'animate.css';//使用<h1class="animate__animatedanimate__bounce">Ananimatedelement</h1>//样式名称必须加animate__animated之后    ......
  • vite配置自动引入全局scss变量文件
    全局自动引入scss变量文件当定义了全局的scss变量文件并且而其他很多页面都需要使用的时候,都需要显式的使用@import或者@use引用一遍全局scss文件,很是麻烦。使用以下配置这样能有效避免造成大量重复工作,可以在任何scss文件中任意使用全局变量。定义全局scss变量文件配置vi......
  • CSS3 超实用属性:pointer-events (可穿透图层的鼠标事件)
    1、是什么pointer-events 直译为指针事件,该属性指定在什么情况下某个DOM可以成为鼠标事件的target。简而言之,就是允许/禁止DOM的鼠标事件(click事件、hover事件、mouse事件等鼠标事件)2、具体属性分析用法分析:pointer-events:auto|none|visiblePainted|visibleFill|......
  • css通过子元素选择父元素的实现示例
    在CSS中,直接通过子元素选择其父元素并不直接支持,因为CSS的选择器是从上到下(从父元素到子元素)进行选择的,而不是相反。但是,你可以使用其他方法或技术来间接实现这一效果,比如使用JavaScript、jQuery或其他脚本语言,或者通过调整你的HTML结构和CSS样式来达到类似的效果。不过,我可以给......
  • css 特殊进度条
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=......