Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select an element that has a certain class?

My understanding is that using element.class should allow for a specific element assigned to a class to receive different "styling" than the rest of the class. This is not a question about whether this should be used or not, but rather I'm trying to understand how this selector is intended to work. From looking at a ton of examples on the internet, I believe the syntax is correct and do not understand why this is not working.

Here is an example:

CSS:

h2 {     color: red; }  .myClass {     color: green; }  h2.myClass {     color: blue; } 

HTML:

<h2>This header should be RED to match the h2 element selector</h2> <div class="myClass">     <h1>This header should be GREEN to match the class selector</h1>     <h2>This header should be BLUE to match the element.class selector</h2> </div> 
like image 708
Carolyn Avatar asked Jun 05 '13 18:06

Carolyn


People also ask

Which selector will finds elements with a specific class?

The class selector selects HTML elements with a specific class attribute.

How do you select an element that is not a class?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

How do you select a class attribute?

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".

How do I select a specific h2 in CSS?

You can just add this line to your CSS: h2 { background-color: #000000; color: #123123; //You can change these properties to whatever you want. }


2 Answers

It should be this way:

h2.myClass looks for h2 with class myClass. But you actually want to apply style for h2 inside .myClass so you can use descendant selector .myClass h2.

h2 {     color: red; }  .myClass {     color: green; }  .myClass h2 {     color: blue; } 

Demo

This ref will give you some basic idea about the selectors and have a look at descendant selectors

like image 118
PSL Avatar answered Sep 25 '22 21:09

PSL


h2.myClass refers to all h2 with class="myClass".

.myClass h2 refers to all h2 that are children of (i.e. nested in) elements with class="myClass".

If you want the h2 in your HTML to appear blue, change the CSS to the following:

.myClass h2 {     color: blue; } 

If you want to be able to reference that h2 by a class rather than its tag, you should leave the CSS as it is and give the h2 a class in the HTML:

<h2 class="myClass">This header should be BLUE to match the element.class selector</h2> 
like image 41
ASGM Avatar answered Sep 24 '22 21:09

ASGM