Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ".class element" and "element.class"?

Tags:

css

Is there a difference between .class element and element.class in a CSS selector?

I had always been shown element.class but just the other day came across a CSS file at work that had .class element and wanted to know if this was just a style choice (in which case I would make my changes match), or if there was a specific reason (in which case I would not necessarily want to make my changes match).

like image 583
Covar Avatar asked May 16 '11 21:05

Covar


People also ask

What does .class mean in CSS?

In CSS, a class is a group of elements that are the same or similar. You can have as many elements as you want in a class. And each element can be the member of multiple classes. Every class has CSS attributes (like color and font-size) that are specific to that class. CSS classes are similar to a real-life class.

What is an element of a class?

HTML Classes and IDs Giving an element a class Classes are identifiers for the elements that they are assigned to. Use the class attribute to assign a class to an element. <div class="example-class"></div> To assign multiple classes to an element, separate the class names with spaces.

What is the difference between an element and class selector?

HTML elements are things like <h1>, <p>, <div>, <nav>, etc. They are the building blocks of the page. Classes are names/conventions the developer includes that will go inside the angle brackets of an element and allow for more specific, or general styling in CSS or functionality in JavaScript.

What is the difference between div and class?

A div is a container tag that is used to define a division or a section in an HTML document, whereas a class is an attribute that specifies actions to be taken to one or more elements. When using a class in CSS you have to specify the HTML elements you want it to affect by using a dot(.)


2 Answers

element.class selects all <element />s with that class. .class element selects all <element />s that are descendants of elements that have that class.

For example, HTML:

<div class='wrapper'>   <div class='content'></div>   <div></div>   <div class='footer'></div> </div> 

For example, CSS:

div.wrapper {   background-color: white; /* the div with wrapper class will be white */ }  .wrapper div {   background-color: red;   /* all 3 child divs of wrapper will be red */ } 
like image 168
glortho Avatar answered Oct 20 '22 00:10

glortho


"element.class" selects elements that have the given class.

".class element" selects all elements that are children of anything with the given class.

Example:

<div class="foo">     <p>...</p> </div> 

div.foo would select the div, while .foo p would select the child paragraph. It should be noted that without specifying direct child via the ">" selector, this will traverse the entire document tree when looking for children.

like image 37
Doug Stephen Avatar answered Oct 20 '22 00:10

Doug Stephen