What is the best way to target a specific class to exclude in CSS styling?

Here is the css code snippet:

.single-post .container {
    width: 60%;
}

not(.single-post) .container{
            min-width:92%;
    }

The goal I am aiming for is to have the container width set to 60% on single post pages and 92% on all other pages. To achieve this, I'm trying to exclude the .single-post class from the second line of code by using not(.single-post), but it doesn't seem to be working as expected.

On the other hand, the first line of code works perfectly fine.

Answer №1

One technique is to declare the general case first and then get more specific later on. The order of declarations is important as it affects cascading styles. Give it a try with the code snippet below.

.container{
    width:92%;
    background-color: blue;
}

.single-post .container {
    width: 60%;
    background-color: red;
}
<!-- normal page -->
<div>
  <div class="container">
     
  </div>
</div>

<p> </p>

<!-- single post post page -->
<div class="single-post">
  <div class="container">
     
  </div>
</div>

Answer №2

:not(.single-post) .container{
        min-width:92%;
}

: is required.

Additionally, consider inserting !important after 92%

the revised code will look like this

:not(.single-post) .container{
        min-width:92% !important;
}

Answer №3

It seems that using :not() on nested classes is not possible: nesting inside css :not() selectors

One alternative could be to apply !important to individual posts?

.container {
  background-color: lightblue;
  margin: 1em 0;
  padding: 1em;
  width: 92%;
}

.single-post .container {
  width: 60% !important;
}

Here's a working fiddle for reference: https://jsfiddle.net/rmgawnu8/1/

Keep in mind that the same CSS reference needs to be used.

Answer №4

If the class .single-post is present on the body element or another parent element, you can simplify your CSS like this:

.container {
  width: 92%;
}
.single-post .container {
  width: 60%;
}

This code will make the width of the .container element 92% on most pages, but it will be 60% on the .single-post page due to the specificity and order of the rules in the style sheet.

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

Are there any methods to personalize the CSS transition?

I have a question about modifying the style of an element with the transition property. It seems that any changes made are done gradually. Is there a way to monitor these style changes, prevent immediate modifications, and instead replace them with a cus ...

Is the form being submitted with the href attribute?

Does this code ensure security? <form id="form-id"> <input type='text' name='name'> <input type='submit' value='Go' name='Go'/></form> <a href="#" onclick="docume ...

Positioning elements to the right inside a Bootstrap 4 thumbnail behaves in a unique way when compared to Bootstrap

<div class='img-thumbnail'> <img src="a.jpg" class='img-fluid'> </div> This particular code snippet is from an HTML file using Bootstrap 4 CSS. It displays an image inside a styled box. Adding an h4 heading places ...

Why doesn't Select2 function properly in Bootstrap 3 when the tabindex is removed?

There is a bug with Select2 that causes it to malfunction in a Bootstrap 3 modal unless the tabindex element is removed. I have successfully resolved this issue in several modals on my page, but one specific modal still cannot get Select2 to function. Eac ...

The second tab on Bootstrap5's tab content feature seems to generate an endless amount of white

I am working on a project where I need to display statistics for both the current month and year. To organize this data, I created a card with tabs for each - one tab for the month and another for the year. By default, the month tab is loaded first. Howev ...

Implementing a dropdown menu within a Bootstrap navbar

Currently, I am working on developing a website and decided to kickstart the project by using the dashboard example provided on the Bootstrap website. However, I encountered an issue when trying to integrate a dropdown into the navbar. It seems that the dr ...

Creating an Android game with the utilization of HTML5 and Javascript involves the adaptation of images to various screen resolutions

Currently, I am working on creating a basic game for android devices by utilizing HTML5, javascript, and cordova for wrapping it up. One of the key features of my game will involve using an image as a background and animating it to create a dynamic visua ...

Discovering an Improved Method for Implementing a Functional jQuery Menu Bar

Currently, I have a functioning Menubar where each button on the menu triggers a secondary Menubar using jQuery. The code snippet is as follows: <script> $(document).ready(function(){ $("#homeButton1").mouseover(function(){ $(".secondMen ...

Having trouble with video playback in the HTML5 media player on Codeigniter platform

I'm attempting to incorporate a video into my web application using the HTML5 media player within the Codeigniter framework. Below is the code snippet I have been working on: public function view($settings, Screen $screen) { ...

Adjusting the color of a cell based on its value

Currently, I am in the process of converting a CSV file to an HTML table by utilizing a tool available at . However, I am facing a challenge in modifying the background color of cells based on their values. I would greatly appreciate any help or guidance w ...

Design a sophisticated background using CSS

https://i.sstatic.net/yILwL.png I am wondering if there is a way to create a CSS3 background similar to the one shown in the image link above. The gradient should smoothly transition from white to black, spanning across a header div, and remain consistent ...

Adaptive Table Layout that Creates a Page-breaking Design

I have a page layout with three columns: a fixed-width left-hand navigation, a responsive content column, and a fixed-width right-hand navigation. The issue arises when the content in the middle column includes HTML tables that sometimes exceed the availab ...

Unlock the Potential of Bootstrap5 Carousel: Mastering Icon Movement

I'm utilizing the carousel from Bootstrap 5, and here is the image. https://i.sstatic.net/dpmjf.jpg The previous and next icons are overlapping with the text. I would like to reposition them, if possible, just above the upvote/downvote buttons. Howe ...

When the dropdown form options in HTML are clicked, they appear disproportionately large compared to the initial select box

Sorry if my wording is a bit off, I'm still relatively new to working with CSS and HTML. I've encountered an issue where the dropdown options appear much larger than the select box itself when clicked. I can't seem to figure out how to fix i ...

Having trouble with Nodejs Express failing to load script files?

I am attempting to launch an Angular application through node-express. 1 File Structure AngularNode public /core.js /index.html project.json server.js 2 server.js var express = require('express'); var app = expres ...

Transfer data from a Django template populated with items from a forloop using Ajax to a Django view

Struggling to implement a click and populate div with ajax in my django e-commerce app. The function involves clicking on a category in the men's page which then populates another div. gender.html {%for cate in cat%} <a href="javascript:getcat()" ...

Understanding the Camera's View in Three.js

I am currently developing a game using html5 and THREE.js, where I have implemented a camera that rotates with the euler order of 'YXZ'. This setup allows the camera to rotate up, down, left, and right – simulating a first person view experien ...

Simple method for swapping out an HTML string with image tags solely using JavaScript

In our Ionic 2 App, we are displaying content from a backend API in the form of Questions and Answers. The Answers are generated using summernote, allowing us to render raw HTML in the app. However, we need to replace all instances of img tags in the answe ...

Customize Angular Material's Mat-Dialog background blur/darkening effect

Greetings, dear community members, I am currently utilizing angular along with angular material in my projects. By default, when a material dialog is opened, it slightly darkens the background. However, I am interested in having a blurred background inst ...

What sets apart Material UI's gutterBottom from a paragraph?

Can you explain the distinction between these two options? While exploring the Typography Component API, I noticed that both the gutterBottom and paragraph props seem to serve the same purpose. If set to true, the margin bottom will be 0. You can find mor ...