MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
  • Home
  • About
  • Contact
  • Privacy
  • Sitemap
  • BLOG
  • AFFILIATE MARKETING
    • LeadsArk
  • MORE
    • MAKE MONEY
      • ONLINE BUSINESS
      • ADSENSE
      • AFFILIATE MARKETING
    • BLOGGING
      • SEO
      • BlogSpot
      • WORPRESS
      • GOOGLE
      • COMPUTER TIPS
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DEVELOPMENT
      • PHP
      • WORDPRESS
  • DOWNLOADS
    • Blogger Template
    • Wordpress Theme
    • PDF

Tuesday, April 23, 2019

How to use css attribute selector? CSS Tutorial 16

 MyBlogHelp     April 23, 2019     css     1 comment   

css attribute selector

 

  CSS Attribute Selector

  1. Introduction to CSS attributes
  2. Different type of CSS attribute selectors with syntax and simple example

Introduction to CSS Attribute Selector

CSS provides you, the ability to design elements according to attribute and attribute values. That is, an element can be targeted which has not defined specific attribute or attribute values. For this, many CSS attribute selectors are available in CSS.  

As you can understand by the name only, CSS attribute selector select attributes of an element. The element whose attribute matches the attribute selector, the same element is targeted.

For example, if you want to target all the anchor tags on a webpage for which the target attribute has been defined and that attribute has been defined as value _blank, so you can not do it by the normal CSS selectors. But it is very easy to do this by attribute selectors. Let us now try to know all about the attribute selector in detail by CSS.

Different type of CSS Attribute Selector

This CSS selector targets such HTML elements in which the attribute defined in the angular brackets. The general syntax of this CSS attribute selector has been given below.

[attribute-name]
{
    property: value;
}

As you can see in the above syntax, The attribute you want to match is defined in angular brackets. The example of this selector has been given below.


<!DOCTYPE html>
<html>
<head>
<title>CSS Attribute Selectors</title>
<style>
[target]
{
   background-color: hotpink;
}
</style>
</head>
<body>
<a href="https://www.mybloghelp.com" target="_blank"> MyBlogHelp </a><br>
<a href="http://www.google.com"> Google </a><br>
<a href="http://www.yahoo.com" target="_top"> Yahoo </a>
</body>
</html>

The above example will produce the below output.


CSS-attribute-selector-example

CSS [attribute = "value"] Selector

This CSS selector targets such HTML elements in which the attribute specified in the angular bracket and its same value is defined. In this selector, the value of the attribute is also defined by the assignment operator before its name.

When defining this selector, the value of the attribute is defined in double quotes. The general syntax of this selector has been given below.

[attribute-name = "value"]
{
    property: value;
}

This attribute selector only affects those elements, whose attribute and the value defined for it matches exactly. This has been explained by the below example.


<!DOCTYPE html>
<html>
<head>
<title>CSS Attribute Selectors</title>
<style>
body{
width: 25%;
}
[class = "p1"]
{
    color: green;
    background-color: pink;
}
</style>
</head>
<body>
<p class = "p1"> This the paragraph which background and color will be changed. </p>
<p> This paragraph will not be changed. </p>
</body>
</html>

The above example will produce the below output.


CSS-attribute-value-selector-example

CSS [attribute ~ = "value"]


This CSS attribute selector targets such HTML elements, which contain the word specified in the attribute's value. When defining this selector, the tilde (~) symbol is defined after the name of the attribute and after that by assigning the assignment operator, that word is specified in double quotes which can be included in the value of an element.

For example, if you want to target some div in which the class attribute is defined and the value of that attribute includes fruits and flowers word, then in this situation, you can use this selector.

The general syntax of this selector has been given below.

[attribute-name = "contain-word"]
{
   property: value;
}

The example of this selector has been given below.


<!DOCTYPE html>
<html>
<head>
 <title>CSS Attribute Selectors</title>
<style>
[class~="Fruit"]
{
   width: 200px;
   height: 100px;
   background-color: green;
   border: 3px solid red
}
[class~="Flower"]
{
    width: 200px;
    height: 100px;
    background-color: yellow;
    border: 3px solid blue;
}
</style>
</head>
<body>
<div class="Div1 Fruit"> This is div1. <img src="fruit1.jpg" style="float: right;width: 50px;"> </div>
<div class="Div2 Flower"> This is div2. <img src="flower1.jpg" style="float: right;width: 50px;"> </div>
<div class="Div3 Fruit"> This is div3. <img src="fruit2.jpg" style="float: right;width: 50px;"> </div>
<div class="Div4 Flower"> This is div4. <img src="flower2.png" style="float: right; width: 50px;"> </div>
</body>
</html>

The above example will produce the below output.


CSS-attribute-selector-example

CSS [attribute | = "value"] Selector


This CSS selector targets such elements where the value of the attribute specified in the angular brackets starts with the specified word. After the name of the attribute in the selector, the vertical (|) line symbol is defined and then it is written the word in double quotes which you want to match the attribute’s value.

It is important to keep in mind that the word should be complete. If you define the attribute, and the hyphen is used before, also hyphen should be applied before the word. If you do not write a complete word, you will not be able to target any element. 

For example, if you want to select such a paragraph element whose value of class attribute starts with first, then you can do this by the selector. The general syntax of this selector has been given below.

[attribute-name | = "containing-word"]
{
    property: value;
}

The example of this selector is being given below.


<!DOCTYPE html>
<html>
<head>
<title>CSS Attribute Selectors</title>
<style>
[class|="First"]
{
   color: blue;
   background: pink;
}
[class|="Second"]
{
color: red;
background: yellow;
}
[class|="Third"]
{
color: green;
background: lime;
}
</style>
</head>
<body>
<p class = "First-Paragraph"> This is the first paragraph. </p>
<p class = "Second-Paragraph"> This is second paragraph. </p>
<p class = "Third-Paragraph"> This is third paragraph. </p>
</body>
</html>

The above example will produce the below output.


CSS-attribute-selector-example

CSS [attribute ^ = "value"] Selector


This CSS selector also targets such elements which value of the attribute specified, starts with the specified word. The only difference is that in this selector you do not need to define the complete word. This means even if define some letters, then it targets selectors elements.

When defining this selector, Circumflex (^) accent symbol is defined after the name of the attribute, and after assignment operator, the word is written in double quotes that you want to match the attribute’s value.

The general syntax of this selector has been given below.

[attribute-name ^ = "contain-word"]
{
    property: value;
}

The example of this selector has been given below.


<!DOCTYPE html>
<html>
<head>
 <title>CSS Attribute Selectors</title>
<style>
body{
width: 25%;
}
[class^="best"]
{
    color: blue;
    background: #ccc;
}
</style>
</head>
<body>
<p class="best-design"> This is paragraph about best designing. </p>
<p class="best-color"> This is a paragraph about best color. </p>
<p class="best-content"> This is a paragraph about best content. </p>
</body>
</html>

The above example will produce the below output.


CSS-attribute-selector-example

CSS [attribute $ = "value"] Selectors


This CSS selector targets such elements which have been specified as the value of the attribute ends the specified word.

When defining this selector, the dollar ($) symbol is defined after the name of the attribute and after assignment (=) operator, the word is defined in the double quotes which you want to match the attribute value. It is not necessary that the word is complete.

The general syntax of this selector has been given below.

[attribute-name $ = "value"]
{
    property: value;
}

The example of this selector has been given below.


<!DOCTYPE html>
<html>
<head>
 <title>CSS Attribute Selectors</title>
<style>
body{
width: 25%;
}
[class$="ing"]
{
color: blue;
background: yellow
}
[class$="graph"]
{
   color: green;
   background-color: pink;
}
</style>
</head>
<body>
<h3 class="first-heading">This is the first heading</h3>
<p class = "first-paragraph"> This is the first paragraph </p> 
<h3 class="second-heading">This is the second heading</h3>
<p class = "second-paragraph"> this is the second paragraph </p>
<h3 class="third-heading">This is the third heading</h3>
<p class = "third-paragraph"> This is the third paragraph </p> 
<h3 class="fourth-heading">This is the fourth heading</h3>
<p class = "fourth-paragraph"> This is fourth paragraph </p>
</body>
</html>

The above example will produce the below output.


CSS-attribute-selector-example

CSS [attribute * = "value"] Selector


This attribute selector targets such elements whose are specified the value of the attribute with the specified word. It's the same as selector [attribute = "value"] selector. The only difference is that you do not need to specify the exactly complete value.

You can only define some of the letters, which will match the attribute's value, the same element will be the target. 

When defining this selector, the asterisk (*) symbol is defined after the name of the attribute, and after assignment (=) operator, the word is specified in double quotes which you want to match.

The general syntax of this selector has been given below.

[attribute-name * = "contain-word"]
{
     property: name;
}

The example of this selector is being given below.


<!DOCTYPE html>
<html>
<head>
  <title>CSS Attribute Selectors</title>
<style>
body{
width: 25%
}
[class*="ing"]
{
   color: blue;
   background-color: yellow;
}
</style>
</head>
<body>
<h3 class = "heading-first"> This is the first heading. </h3>
<p class = "first-paragraph"> This is the first paragraph. </p>
<h3 class = "heading-second"> This is the second heading. </h3>
<p class = "second-paragraph"> This is the second paragraph. </p> 
</body>
</html>

The above example will produce the below output.

CSS-attribute-selector-example
  
         
         
        <<-- PREVIOUS                                                                  NEXT -->>


  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

1 comment:

  1. Sadhana RathoreJuly 15, 2019 at 5:37 PM

    Excellent post, thanks for this. I gathered lots of information from this and I am happy about it. Do share more updates.
    PHP Training in Chennai
    Best PHP Training Institute in Chennai
    PHP Institute in Chennai
    Angular Training in Chennai
    Web Designing Training in Chennai
    Salesforce Training in Chennai
    Tally course in Chennai
    PHP Training in Anna Nagar
    PHP Training in Vadapalani
    PHP Training in Thiruvanmiyur

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Thank you for reading this post. Please do not enter any spam link in the comment box.

Followers

Blog Archive

  • ►  2020 (4)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
  • ▼  2019 (52)
    • ►  December (1)
    • ►  November (2)
    • ►  October (1)
    • ►  August (1)
    • ►  July (1)
    • ►  May (11)
    • ▼  April (10)
      • How to use css box-shadow property? CSS Tutorial 19
      • How to use css z-index property? CSS Tutorial 18
      • How to use css outline property? CSS Tutorial 17
      • How to use css attribute selector? CSS Tutorial 16
      • How to use the css float property? CSS Tutorial 15
      • How to use the css visibility property? CSS Tutori...
      • How to use css cursor property? CSS Tutorial 13
      • How to use css display property? CSS Tutorial 12
      • How to use css position property? CSS Tutorial 11
      • How to use css overflow property? CSS Tutorial 10
    • ►  March (6)
    • ►  February (3)
    • ►  January (16)
  • ►  2018 (54)
    • ►  December (27)
    • ►  November (5)
    • ►  May (5)
    • ►  April (4)
    • ►  February (3)
    • ►  January (10)
  • ►  2017 (17)
    • ►  December (9)
    • ►  November (8)

Popular Posts

  • How to use the main tag in html5?
  • How to use keygen tag in html5?
  • How to use html5 mark tag to highlight text?
  • How to start Online Business without money from home?
  • What is a search engine?

Contact Us

Name

Email *

Message *

About MyBlogHelp

Most of the posts, we share on this blog related to Affiliate Marketing, Organic Lead Generation, Digital Marketing, Social Media Marketing, Make Money Online, SEO, and web design.

Featured Post

LeadArk - Affiliate Marketing | Qualified Organic Lead Generation | LeadsArk Review

Copyright © MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
Design by Md. Abdussamad | MyBlogHelp.com