What sets apart a .class element from element.class in CSS?

I was browsing through this tutorial http://www.w3schools.com/css/tryit.asp?filename=trycss_nesting and noticed a confusing point. Why did they use .marked p instead of p.marked? When I tried changing it to p.marked, the text didn't turn white as expected.

I also went through some previous posts like what is the difference between ".class element" and "element.class"?, but the explanations were filled with terminology that I couldn't grasp, such as children and descendants.

If someone could provide a simple example to explain how these concepts work, that would be greatly appreciated. Thank you.

Answer №1

It's actually quite straightforward.

Here is a snippet of HTML code

<div class="wrapper">
  <div class="box"></div>
  <div class="box"></div>
</div>

The first kind is element.class

For example:

div.wrapper{
  background: green;/*This will set the background color for the div WITH the class "WRAPPER"*/
}

The second type is .class element

For example:

.wrapper div{
  background: yellow;/*This will set the background color for the div INSIDE the WRAPPER class*/
}

Answer №2

To gain a better understanding of CSS selectors, it's important to familiarize yourself with some fundamental rules. Here are a few examples:

.foo --> targets all elements with the class name foo
#foo --> selects an element with the ID of foo
p.foo --> specifies all paragraph elements with the class foo
p#foo --> identifies a paragraph element with the ID of foo
.foo p --> styles all paragraph elements that are descendants within elements with the class foo
and the list goes on...

Answer №3

element.class assigns the attribute directly to the element, whereas .class element applies it to elements within the specified class.

In simpler terms, if you use element.class and write <element class="class" />, the defined CSS will affect that specific element. Conversely, with .class element and the following structure:

<element class="class">
<element />
</element>

the CSS rules will target the element nested inside.

Answer №4

One important distinction to note is that using the .class selector assigns the property to all elements regardless of their type, such as div, span, p, etc.

On the other hand, when using the element.class selector, the property is only applied to that specific element.

Answer №5

Whenever you use the syntax .class elementname, you are specifically directing your code to target the element name within the designated class tag. For example, if you have ".test p", your corresponding HTML code would look like this:


<div class="test">Happened<p>Something</p></div>

On the other hand, when you write element .class, you are indicating that you want to focus on the element with the specified class present inside the parent element's tag. So, for the selector "p .test", your HTML code should be:

<p>Something, <div class="test">Happened</div></p>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Placing the navigation bar on the right side of the section

.middle-site-content-container { width: auto; height: auto; border: 2px red solid; display: flex; /*Le pot pozitiona in line*/ flex-wrap: wrap; justify-content: center; flex-direction: row; margin: auto; } .middle-site-content-1, .middle ...

If one sibling requires flexing, apply flex to all siblings

Illustrative Issue: large: https://i.sstatic.net/oq0jA.png small: https://i.sstatic.net/WXqmV.png When space is limited, one row will start to flex while others remain static if they have enough space. Is there a way to make all rows flex when at least ...

The inner HTML functionality in Angular 2 seems to be malfunctioning when dealing with HTML tags

I am facing an issue with an array that includes displayName with HTML tags: this.topicsList = [ {id: "173", name: "Discussion1", displayName: "Discussion1", status: 1}, {id: "174", name: "discussion123", displayName: "discussion123", status: 1}, {id: "19 ...

Prevent the Spread of an Event for a Particular Controller

tl;dr Summary Develop a function that is able to handle a specific event on multiple elements within a hierarchy. The function should execute when the event is triggered on the first element reached during bubbling, but should not continue executing or re ...

Position a button freely on the grid layout of bootstrap4

I need help creating a sidebar in Bootstrap 4 with a floating arrow button to collapse it, similar to this example. Currently, I'm using a `container-fluid` and dividing the row into a 2-8-2 layout for left sidebar, main content, and right sidebar. I ...

What is the best way to insert a vertical column separator in a Material-UI table?

I have designed a table with a specified height (such as 70vh). I am looking to add a vertical column divider that spans the entire height of the table. I can achieve this using CSS for the TableCell component. However, I want the vertical column divider t ...

Displaying directory contents with JavaScript XHR

When I inquired whether javascript has the ability to list files on the server, the general response was that "javascript cannot access the server filesystem since it is a client-side scripting language". However, I believed this answer was only partially ...

Modify content or display picture within accordion panel based on its identifier

In my view, I have a model representing a list of items. Each item has a unique ID, which is included in the H2 header tag. The details of each item are displayed in a div below the header. Initially, there is an image within the header that is set to disp ...

Encountering an error while submitting form via Ajax to PHP backend

Currently, I am in the process of developing a form that solicits information from a user, including their name, address, amount1, amount2, and a comment. It seems that the radio inputs for amount1 and amount2 are causing issues with my form. Upon submiss ...

Tips for creating a responsive navbar image alongside your navbar

On my desktop, the navbar appears perfectly. However, when I switch to mobile view, the navbar image does not fit properly and the hamburger menu is missing. See the image below: View Image Here After refreshing the page, the hamburger menu finally shows ...

Discover the best way for child components to utilize parent components' methods in VueJs

I am facing an issue with a button in my parent component that handles form validation. This button triggers the display of a child component, which then makes an API call. The problem arises when I modify the input as the display changes simultaneously. M ...

Is there a way to alter the appearance of an HTML element without an ID using a JavaScript function?

I have a specific goal in mind, but I'm struggling to articulate it concisely. Despite conducting extensive research, none of the suggested solutions seem applicable in my case. Objective: I aim to change the background color of a div based on the da ...

Transforming an HTML jQuery contact form into an ASP.NET solution

I have developed a contact form using HTML5, Bootstrap, and jQuery. The form includes hidden fields that are only displayed when the user selects a radio button. I have implemented validation to ensure that the hidden fields are not required to be filled o ...

Can we convert a PHP frontend into a Vue node?

I'm currently working on transitioning a legacy PHP application to Vue, and I want to do it gradually. The current PHP app is quite messy, with HTML and JavaScript files intertwined in a complicated manner like this: foo.js.php ... <script src=&quo ...

A guide to creating smiley emojis using solely HTML and CSS

Is there anyone who can assist me in constructing this happy face? https://i.sstatic.net/yyYYQ.png ...

MD-List Equilibrium Elevation

Seeking a solution for implementing a simple chat using the md-list template. The issue arises when new items are added to the md-list, causing it to expand. Desiring the list to behave similarly to other chat platforms, where the height remains constant ...

I am encountering challenges with submitting the form

I am encountering an issue where I want to submit the form on button click and navigate to the next page, but instead I get an error message saying: "Form submission canceled because the form is not connected". Does anyone have a solution for this problem ...

Embedded HTML code within a table cell

How can I dynamically build an HTML string and display it inside a cell in reactJS' ag grid? Here's my example: function generateHtmlString(props) { let myHtmlString = "<span>a</span>"; myHtmlString += "--"; myHtmlString += "&l ...

Issues arise when a questionnaire is duplicated

Currently, I am working on a questionnaire that involves the cloning feature in jQuery using .clone(). The questions are answered using checkboxes with options for yes or no. These checkboxes are placed within a table and controlled by the following script ...

Item on the menu has been pushed lower in the list

There seems to be an issue with the layout of my menu as one item is displaying lower than the others without any clear reason. <html> <head> <title> My Website Homepage </title> </head> <body> ...