Utilizing the :not selector in combination with adjacent elements

Here's my HTML:

<div class="parent">
    <div class="thumb">...</div>
    <div class="text">...</div>
</div>

I need the "text" div background to be white, but only if there is no preceding "thumb" div.

I could use this CSS:

.parent .text {background-color: #fff;}
.parent .thumb + .text {background-color: transparent;}

This sets all text backgrounds to white and then changes those with a thumb div before it. But I'm wondering if I can simplify it like this:

.parent :not(.thumb +) .text {background-color: white;}

This way, the style only applies to the "text" div when there is no preceding "thumb" div. Any thoughts?

Answer №1

To implement conditional statements (if/else) in CSS, you can leverage SASS:

$type: monster;
p {
  @if $type == ocean {
    color: blue;
  } @else if $type == matador {
    color: red;
  } @else if $type == monster {
    color: green;
  } @else {
    color: black;
  }
}

For more information, refer to the official documentation.

Alternatively, JavaScript/jQuery can be utilized to dynamically add or remove classes based on specific conditions.

Keep in mind that traditional CSS does not support native if/else functionality.

Answer №2

To effectively target an element using CSS, you can use the .thumb + .text {} selector but keep in mind that this style will only be applied when there is a .thumb element right before the .text element:

.parent {
    background: grey;
    border: 1px solid red;
    margin-bottom: 30px;
}

.text {
    background: purple;
}

.thumb + .text {
    background: white;
}
<div class="parent">
    <div class="thumb">...</div>
    <div class="text">White bg</div>
</div>

<div class="parent">
    <div class="text">White bg</div>
</div>

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

Tips to stop excessively long text from overflowing its containing div

Let's fit as many text lines as the div's height allows. <div class="row g-0"> <div class="col-md-3 align-content-center"> <div class="d-flex justify-content-center"> <!-- svg image - ...

How can I use jQuery to reposition an inner element to the front?

Imagine you have a collection of elements: <ul id="ImportantNumbers"> <li id="one"></li> <li id="two"></li> <li id="topNumber"></li> <li id="four"></li> </ul> Every five seconds, the ...

Is there a way to change tailwincss into regular CSS?

I recently received an HTML file that was created using Tailwind CSS and now I am looking for a way to convert everything to regular CSS. I found a tool that converts individual classes, but it would require me to extract and paste thousands of classes o ...

Delay in dropping while using React Beautiful DnD

While incorporating react-beautiful-dnd into my project, I faced an issue where the dragged item is initially positioned incorrectly in one of the lists. There is a brief delay before it jumps to its correct position. You can see the problem in action by ...

When using CSS float:left and overflow:visible, the text may get cropped-off at

I'm currently experimenting with creating a color gradient in javascript using numerical values within some of the divs to indicate scale. However, I've run into an issue where as the values get larger, they are cut off due to the float:left prop ...

Python: Leveraging lxml to find content not within a span tag

Upon encountering some HTML that is malformed, I am faced with the task of parsing it. Notice how the text "Cowabunga" is not enclosed within any HTML element. from lxml.html import fromstring from lxml.cssselect import CSSSelector stuff = ''&a ...

Footer content encroaching on main body text

Thank you so much for your assistance. If you want to check out the content, visit it online The issue I'm facing is with my footer. It doesn't stay below the content as I'd like it to. I tried using Matthew James Taylor's technique, ...

Select an image from the browser using JavaScript, adjust its size, and then transmit it to the server. Finally, utilize PHP to properly save the image

I am currently tackling the challenge of implementing a classical feature where users can select a file in their browser ("Browse"), have JavaScript resize it to a maximum width/height of 500 pixels, upload it to the server, and then save it to disk using ...

Creating a scrollable div in Electron

I'm currently developing a basic Markdown application using Electron. At the moment, all I have is a textarea, a div, and some Javascript code that retrieves the text from the textarea, processes it with Marked (an NPM module), and updates the content ...

Hide the button with jQuery Ajax if the variable is deemed acceptable upon submission

I need to figure out how to hide the submit button if the email entered is the same as the one in the database from action.php. Can anyone help me with integrating this functionality into my existing code? <form onsubmit="return submitdata();"> ...

Is the Addthis widget displaying incorrectly in a fixed element?

I'm currently using addthis as a vertical toolbox with the hover popup feature on a fixed element. However, when scrolling, the popup appears in a different location. I've tried adjusting the offset top and left configurations, but they haven&apo ...

The button in Bootstrap 4 that triggers the modal stays in a highlighted or clicked state even after the modal has been

I am currently utilizing the button below to trigger the opening of a modal window However, upon closing the modal, it seems to maintain a highlighted or clicked appearance with a shadow effect. Initially, I assumed I could address this issue by implement ...

The modal is functioning perfectly with the initial row in the table

Is there anyone who can help me fix this issue? I've spent a lot of time trying different solutions, but none of them seem to work. I'm using a tailwindcss template for the modal and JavaScript to show or hide it. I'm having trouble findin ...

Assertion error: 1 does not match the expected value of 6

I am faced with a challenging test that requires me to write code that will pass successfully. However, no matter what I try, all I get is the following error messages: "Failed asserting that 1 matches expected 6" when I return 6 and "Too few arguments t ...

Animating an upward movement of a div nested inside another div

Recently delving into the world of web development with HTML, CSS, and JavaScript, I've encountered an issue that has me stumped. I can't seem to figure out how to move a div upwards when the mouse hovers over it within its containing div. .prof ...

The text on the asp:Button displays incorrectly in IE8+

The content inside the button slightly shifts or moves downward by a few pixels. <div> <asp:Button runat="server" ID="Button1" CssClass="ButtonClass" Text="" runat="server" /> </div> ascx.cs: Button1.Text = LocalizationResourceManager. ...

displaying outcomes as 'Indefinite' rather than the anticipated result in the input field

I need to automatically populate values into 4 text fields based on the input value entered by the user. When the user exits the input field, a function called getcredentials() is triggered. This function, in turn, executes Python code that retrieves the r ...

activating the submit button depending on the user input

My goal is to create a form with a textarea and a submit button that can be enabled or disabled based on whether there is any input in the textarea. I have confirmed that both the submit button and content are being selected correctly (I'll provide a ...

Chrome's CSS columns cannot contain iframes without causing them to escape their

I'm currently attempting to incorporate Iframes within a 3-column grid. HTML <div id="blogPosts"> <div class="blogPost"> <iframe width="100%" src="https://www.youtube.com/embed/YqeW9_5kURI" frameborder="0" allowfullscre ...

The data-toggle function is not functioning properly with the code provided

Could someone shed some light on why my tabs and navigation bar dropdown button are not functioning properly? <div class="col-12"> <h2>Corporate Leadership</h2> <ul class = "nav nav-tabs&q ...