Ways to adjust the height of a separate container based on various class attributes

Check out the HTML structure provided below:

<div>
   <div class="custom-bar custom-bar-minimal"></div>
</div>
<div class="custom-border">
   <div class="container"></div>
</div>

Here's the corresponding CSS:

.custom-bar { height: 50px; }
.custom-bar-minimal {height: 25px; }

.container { height: ~"calc('100vh - 50px')"; }

The goal is to adjust the height of the container when using the custom-bar-minimal class.

I've already attempted this method:

div:has(.custom-bar) + .custom-border .f8-wi-container {
  height:  ~"calc('100vh - 50px')";
}
div:has(.custom-bar-minimal) + .custom-border .f8-wi-container {
  height:  ~"calc('100vh - 25px')";
}

If anyone has a solution, I would greatly appreciate it. Thank you!

:has selector does not seem to be effective in this scenario.

Answer №1

Within CSS, there is no < Selector available to style a parent element based on its child. As of now, using vanilla CSS, this functionality is not possible. It remains uncertain whether preprocessors like Sass, Less, or Stylus can achieve this.

However, the > Selector does exist and targets only immediate children of a parent node. For more information, refer to the MDN Documentation.


An alternative approach is using the :has pseudo-class, as shown in the code snippet below. Unfortunately, this feature is currently unsupported by all browsers:

.parent:has(> child) {
    /* Style Rules */
}

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

Preventing Div items from rearranging during size transitions using toggleClass

When I click on an element with the ID "id", I use toggleClass to resize a div from 10px to 500px in width. This is done partly to show/hide content. However, the issue arises when the transition occurs and the contents of the div start rearranging, overfl ...

Ensure that the URL is updated correctly as the client navigates between pages within a Single Page Application

Seeking a solution for maintaining the URL in a Single Page application as the client navigates between pages, with the URL changing in the background. I attempted using @routeProvider but it doesn't seem to be suitable for my situation. Any suggestio ...

The font is functioning properly in digital screen formats, however, it is not displaying correctly in

I have encountered an issue where the code below works on screen media but not in print media: <style type="text/css"> @font-face { font-family: dinregular; src: url('/images/templateImages/template9/din-regular-italic.ttf') } < ...

Eliminate CSS property with Jquery

I have found that most solutions only remove the attribute setting rather than removing the attribute entirely. My goal is to switch the positioning of an element from absolute to fixed. In order to position the element correctly within its parent DIV, I ...

Replacing Bootstrap CSS with a custom stylesheet

Within my bootstrap.css file, the following styling is defined: .form-control { display: block; width: 100%; height: 34px; padding: 6px 12px; font-size: 14px; line-height: 1.428571429; color: #555555; ve ...

React component not displaying styles applied with jQuery

While jQuery appears to be functioning properly within a React component, I am facing issues when trying to apply styling using jQuery in the same component. The console.log(eachVisitedTopic) statement within the loop is providing the expected results. to ...

Preserve the location of a moveable div using jQuery

I have implemented draggable divs in my JSP page, allowing users to move them around freely. After dragging the divs, I would like to save their positions either in a cookie or database for future reference. Could you please advise on the best way to ach ...

How can I reset the alternating table row in a new table using CSS?

I am encountering an issue with alternating row colors in my tables. Even though I have two separate tables, the tr:odd rule is being applied across both tables. Below is the HTML for my tables: <table class='myTable'> <tr class=&a ...

Using jQuery, generate a div element and display it in full screen mode

Is it possible to dynamically create a full-screen div using jQuery directly from JavaScript without the need to first define it in the HTML and then make it visible with jQuery or CSS? UPDATE After some troubleshooting, I found a solution to my issue: ...

What is the best way to create a hover effect for Material Icons?

Having issues with Material Icon not displaying the specified changes when using CSS modules in my project import SettingsIcon from "@mui/icons-material/Settings"; import css from "./LandingPage.module.css"; <SettingsIcon className= ...

Functionality Issue: Submit Button Not Working on Designed Form Select

Through dedication and hard work, I managed to create a customized form with images that display correctly in Firefox, Chrome, and Internet Explorer's compatibility mode. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

Ways to display values below the slider?

I'm currently working on a data visualization project and am facing an issue with adding values to a slider. The slider itself is functioning correctly, but I can't seem to display any values below it on the webpage. My goal is to show dates like ...

The constricted styles are causing the whole page to bounce (scroll) up and down

On my SPA frontend, I have a parent div with a height of 580 containing 9 smaller divs (about 190px in height each). The parent div has an overflow set to hidden so that only 3 elements are visible at one time. Every 5 seconds, I change the styles by addin ...

Conceal the Navigation Bar Upon the Pop Up's Appearance

I Designed a Landing Page with a Fixed Mobile Nav Bar at the Bottom. There is also a Pop-Up Form that appears when clicked. How can I make sure the Nav Bar is closed when the form pops up? This issue is blocking the Send Button. Is it possible to achiev ...

Designing the button outline variant with Material-UI styling

I've recently started using Material UI and I'm facing some difficulties. One of the challenges I'm encountering is with a button component export default function TheButton(props: PropsWithChildren<Props>) { const { className, hover ...

Troubleshooting a problem with HTML table styling

Could someone please help me identify the issue with this code snippet? I am aiming to style the table with a border, background color, and align it to the top. I appreciate any assistance. Thank you. <td style="border: solid 2px #111111;" bgcolor="#d ...

What is the method for positioning the cursor at the beginning of a center-aligned placeholder within a contenteditable element?

Here is the code snippet I am working with: HTML: <div class="title" contenteditable="true" placeholder="Title"></div> CSS: .title { width: 500px; margin: 0 auto; text-align: center; } [contenteditable=true]:empty:before{ conte ...

External CSS file for Demandware platform

My boss was supposed to train me this year on demandaware knowledge, but ended up quitting, leaving me in the dark. I am scheduled to attend workshops later this year, but for now I am relying on Google and stackoverflow for guidance. Currently, I am wor ...

The alignment of divs can be disrupted in a Nested Grid system

I am working with a 16 grid system where I have two 8 column divs positioned side by side. In one of the 8 column divs, I am trying to include a 5 column div on the left and two three columns divs stacked on top of each other on the right. I have tried v ...

What is the best way to ensure Font-Face loads fonts successfully on iOS devices?

In the process of constructing a website with Gatsby.js, Styled-Components, and a custom font named 'Montserrat', I encountered an issue regarding font loading. The font appears as expected on desktop browsers during both build and hot-reloading, ...