Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is css "[class*=my-class] .my-subclass" doing?

I inherited some css and I have searched everywhere online to understand what is being expressed by a block of css that looks like:

 [class*=wrapper] .logo {
                padding-top: 32px !important;
            }

What is the asterisk and square brackets doing?

It is hard to search for [ and * on google... Sorry if the question is dumb.

like image 678
metalaureate Avatar asked Dec 07 '14 16:12

metalaureate


People also ask

What does class *= mean in CSS?

[class*="button_type"] is CSS class Selector (equivalent to CSS attribute selector) means that will select all elements whose class contains at least one substring "button_type".

What is a CSS class used for?

A CSS class is an attribute used to define a group of HTML elements in order to apply unique styling and formatting to those elements with CSS.

How do I apply a CSS to a specific class?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

What is CSS class example?

CSS Class Example You can also select elements (or classes) that's nested within another element or class. For example, div. css-section p will select all <p> elements that are nested within a <div> element that uses the . css-section class.


2 Answers

It selects an element with class logo that has an ancestor that has wrapper somewhere in its class attribute. For example note that the class burgerwrapper also leads to the element being selected below.

[class*=wrapper] .logo {
  color: #f99;
}
<div class="logo">Not selected</div>

<div class="wrapper">
  <div class="logo">
    Selected
  </div>
</div>

<div class="burgerwrapper">
  <div class="logo">
    Selected
  </div>
</div>

See http://css-tricks.com/attribute-selectors/ for some background information on attribute selectors.

like image 119
ckuijjer Avatar answered Oct 01 '22 15:10

ckuijjer


what square brackets doing

Attribute selectors

CSS 2.1 allows authors to specify rules that match elements which have certain attributes defined in the source document.

Attribute selectors w3

What is the asterisk

Substring matching attribute selectors

[att*=val] Represents an element with the att attribute whose value contains at least one instance of the substring "val". If "val" is the empty string then the selector does not represent anything.

Substring matching attribute selectors

To sum it up in you example:

[class*=wrapper] .logo {
  color: red;
}
<div class="wrapper">
  <div>not this</div>
  <div class="logo">this</div>
  <div class="logo">this</div>
</div>
<div>
  <div>not this</div>
  <div class="logo">not this</div>
  <div>not this</div>
</div>

Select child elements with class .logo that their parent element has attribute class with value wrapper appears somewhere in that attribute.

like image 22
Alex Char Avatar answered Oct 01 '22 14:10

Alex Char