when a div contains another div and is then followed by another div, apply the following style

Is there a way to create a rule that functions like the following:

.container .unit:first-child(if it contains div.box1.extra) + .box2 { top: 50px;}

<div class="container">
   <div class="unit">
       <div class="box1 extra"><!-- content --></div>
   </div>

   <div class="unit">
       <div class="box1"><!-- content --></div>
   </div>

   <div class="box2"><!-- content --></div>                 
</div>

Answer №1

Currently, CSS does not support DOM traversal. However, you can still declare a rule even if it cannot be achieved with CSS.

.container .unit:first-child .box1.extra + .box2 { top: 50px;}

If the .box2 element is not present, the layout will remain unchanged. But for units that contain a .box2, the style rule will be applied as expected.

NOTE: This method only works when .box2 is a child of .unit. If they are siblings, this approach is unfortunately not possible.

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

Adjust Navbar Header Color Based on Screen Size

I am completely new to front-end development. I am in the process of building my own responsive website using Bootstrap 3. One issue I am facing is that when I resize my browser window to simulate a phone screen, I want the navigation bar color to change ...

Discover the power of uploading images using HTML5 with PHP

Is there a way to retrieve values of uploaded images after clicking on the submit button using $_POST in PHP (with image upload through HTML5)? For an example of HTML5 image upload, you can check out this link You can access the demo at the following lin ...

Creating an automated HTML tag for a form input

https://i.sstatic.net/ZBDk2.png Is there a plugin to create form inputs like the one shown above? How can I add this feature to my own website? I would like for every user input to automatically add the < br > tag. ...

Node.js error: HTML audio file returns 404 on server but plays fine when accessed directly as an HTML file

I'm facing an issue with an HTML page that contains an autoplay tag for a song (the file is in mp3 format). When I open the page as a local file on my Mac, the song plays fine. However, when I try to run it using a node.js file with socket.io, the son ...

Tips on creating animations for a background with a value of 2 using CSS3

Hey everyone, I'm currently working with this CSS style: body { width:100%; height:100%; background:#fff; background-image: url(bg.png), url(clouds.png); background-repeat: repeat; font-family: tahoma; font-size: 14px; ...

Guide to deactivating a control within a User Control

I anticipated that making this change would be straightforward, but I am encountering some difficulties. Initially, my asp.net markup for the server control appears like this: <ucTextArea:UserControlTextArea ID="taRemarks" runat="server" /> However ...

It appears that the font in style.css is not being updated properly and seems to resemble

My issue lies within my CSS code. The text on my website is not displaying correctly and seems to be ignoring the styling that I have applied. Even though I have used similar styling on other buttons which look fine, this specific text element is causing p ...

What is the best way to position the header at the top and the footer at the bottom of the page, with the main content in between using

Recently, I started using Bootstrap and incorporated Bootstrap 5.3 into this HTML page. My goal is to align the header at the top, footer at the bottom, and adjust the size of the main content in between. Here's the code I attempted: <!DOCTYPE html ...

Arranging of the fetch_assoc

As a complete beginner in programming, I am excited to share that I have recently embarked on this journey and had only a few classes so far. Currently, I am working on developing a new website just for fun. It is intended for me and my colleagues at work ...

Make the HTML element expand to the remaining height of the viewport

Need help with creating a section that automatically fills the remaining viewport height below the navigation bar. The section includes a centered div and background image, along with a button at the bottom for scrolling down by one full viewport height. H ...

Mistake in Timestamp Separation - JavaScript

I have a Timestamp retrieved from MSSQL and displayed on a Webclient. The timestamp reads: Thu Jan 01 18:00:00 CET 1970, however, I am only interested in the time format: 18:00 or 18:00:00 (preferably the former). <script> var timestamp = ...

Problem with special characters in Localstorage for Internet Explorer 8

After developing a jQuery script that retrieves data from a service, stores it in local storage, and displays it on the page upon a specific event, I encountered an issue with displaying Chinese characters only on IE8+ browser. Unfortunately, IE is unabl ...

Exploring methods to validate a Reactive Form in Angular 10+ by incorporating two groups within the formBuilder.group

I am looking to implement form validation with two separate groups within the main formBuilder.group. I am unsure of how to access the 'errors' value in my HTML [ngClass]. component.ts: createForm(): void { this.myForm = this.formBuilder ...

The value returned when using $.css('transform') is incorrect and displays as "rotate"

Possible Duplicate: Retrieve -moz-transform:rotate value using jQuery Firefox 15 - Chrome: $('#img2').css('transform'); return => "rotate(90deg)" Firefox 16 $('#img2').css('transform'); return => mat ...

Steps for creating a fade effect between two different elements when the mouse is interacting with one of them

I want to create a unique effect with an image in the background and a button above it. When the mouse hovers over the button, I want its opacity to decrease while the image's opacity increases simultaneously. I have experience applying fade effects ...

Tips for customizing the CSS file of a React component imported from a UI framework

As I embark on building a large application utilizing React, I find myself navigating the new territory of React philosophy coming from a background of Css+Js+jQuery methods. One key requirement for my project is a reliable UI framework that aligns with Go ...

Arrangement of asymmetrical interactive forms

I am in need of creating a design featuring numerous non-rectangular shapes that are interactive and reveal an image when clicked. The concept is to transform an image of stained glass into a webpage where each pane can be clicked to unveil its colorful ve ...

Clicking on the mat-icon-button with the matSuffix in the password field will always trigger a

Seeking assistance with implementing mat-icon-button matSuffix in Angular for a login page. This is my first time working with Angular and I am facing an issue with the password field. I have used mat-icon-button matSuffix, but whenever I click on the ico ...

Having trouble with cc email addresses causing problems in Outlook

I'm encountering an issue where there is a '&' symbol in one of the cc email addresses. Due to the default behavior of the browser, the complete email address is not being displayed properly in Outlook. Here's an example: let to='a ...

Photo displayed above the carousel using Bootstrap

I'm currently in the process of building a website using the template found at . However, I'm having trouble placing an image in the center of the carousel, above the slides. Here is the code snippet I have tried so far: <div style="positi ...