Using CSS: Leveraging the power of CSS selectors for styling child elements

I attempted to implement a CSS style that targets a child element of the body based on the active state of another div class.

Here is the HTML structure:

<body class="restrict-body-scroll">
    <div class="main-wrapper">
        <div class="global-banner hide">GLOBAL BANNER ITEM HERE</div>
        <div class="alter-banner hide">ALTER BANNER ITEM HERE</div>
        <header class="header">
            <div class="item-left"></div>
            <div class="item-center"></div>
            <div class="item-right">
                <div class="overlay">
                    <div class="overlay-container">
                        <h3>Welcome Guest!</h3>
                    </div>
                </div>
            </div>
        </header>
    </div>
</body>

This is the CSS code I tried to use:

.global-banner.hide {
    display: none !important;
}
.global-banner .item-right .overlay-container {
    height: 0;
}
body.restrict-body-scroll .global-banner.hide + .header .item-right .overlay-container.active {
    height: 100vh;
}

Unfortunately, it seems like this implementation is not functioning as expected!

Answer №1

+ will choose elements that come directly after the selected elements: in your case, you should use ~

Take a look at the code snippet to see both examples.

.global-banner.hide {
  display: none !important;
}

.global-banner .item-right .overlay-container {
  height: 0;
}

body.restrict-body-scroll .item-center+.active {
  height: 100vh;
  color: red;
}

body.restrict-body-scroll .global-banner.hide~.header .item-right .overlay-container.active {
  font-size: 100px;
}
<body class="restrict-body-scroll">
  <div class="main-wrapper>
    <div class="global-banner hide>GLOBAL BANNER ITEM HERE</div>
    <div class="alter-banner hide>ALTER BANNER ITEM HERE</div>
    <header class="header>
      <div class="item-left></div>
      <div class="item-center></div>
      <div class="item-right active>
        <div class="overlay>
          <div class="overlay-container active>
            <h3>Welcome Guest!</h3>
          </div>
        </div>
      </div>
    </header>

    </div>
  </body>

Answer №2

+ is used as an immediate sibling selector, to select other siblings you can use ~

.global-banner.hide{
    display:none !important;
}
.global-banner .item-right .overlay-container{
    height:0;
}
body.restrict-body-scroll .global-banner.hide ~ .header .item-right .overlay-container.active{
    height:100vh;
}

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

CSS: Dynamically adjusting height based on parent element's current height

I'm seeking clarification on the topic at hand. Why am I unable to simply use the following code: <div style="min-height: 100vh"> <div style="height: 100%"> </div> </div> When I implement this code, ...

`Why is the color of the <select> element not changing in Firefox?`

I seem to be facing some difficulty replicating this issue accurately. However, I do have a jsfiddle link where the selected text color does not match the selected option text. http://jsfiddle.net/kralco626/9xJvF/8/ (similar to: seting <select> col ...

What is the best way to choose all the checkboxes in a checkbox list, such as a ToDo List?

I am currently working on developing a to-do list where each item includes a checkbox. My goal is to be able to select these checkboxes individually by clicking on them. Furthermore, I would like the checkboxes to toggle between being checked and unchecked ...

Tips for creating a button that maintains consistency across different screen sizes

One of the images linked below shows a button displayed on two different sized screens, one 24 inches and the other 13 inches. The button on the 24-inch display appears normal, while the "Add to Cart" button on the 13-inch display is broken. Check out th ...

"Utilize Ajax to load PHP content and dynamically refresh a specific div

I have implemented an image uploading system, but now I want to incorporate a feature that allows users to rotate the uploaded images using Ajax. The challenge I'm facing is that if the session variable is lost during a full page update, I need to ens ...

Do I have to include the HTML audio type when writing code?

Do we really need to specify the audio type in HTML, such as .mp3? I tried it without specifying the audio type and it seems to work just fine. So why is it important to include it? ...

Getting an 'undefined function' error while calling PHP in two separate includes?

My website template consists of four PHP files: - functions.php (contains main functions for the website) - index.php (main template file to bring other documents together) - header.php (website header) - footer.php (website footer - this is where ...

Can someone guide me on how to align text to the bottom border of a page?

Is there a way to adjust the position of the header text ("bottom text") so that it appears just above the bottom border of the page? I have attempted modifying styles and classes within the footer, style, and h3 tags but have not had any success. Below ...

Tips for creating responsive content within an iframe

I have inserted a player from a website that streams a channel using an iframe. While I have managed to make the iframe responsive, the video player inside the iframe does not adapt to changes in viewport size. Despite trying various solutions found online ...

Guide on retrieving the initial value from HTML and passing it to the controller in AngularJS during page load

<ul> <li ng-init="formData.mdpSunday='5'"> <input ng-model="formData.mdpSunday" name="mdpSunday" type="radio" value="5"> </li> </ul> app.controller(&a ...

Expanding a list of object arrays through iteration: A step-by-step guide

// Initializing with one object let posi_val=[{top: '3px', left: '2px'}]; // Adding another object in the loop for (let i = 1; i < n; i++) { posi_val.push({ top: `${posi_val[i - 1].top.slice(0, -2) * 2}px`, l ...

Issue with floating navigation bar functionality when resizing the page

After much tinkering, I have managed to create a floating navigation bar for my website. Most of the time, it works perfectly fine, but there's an issue when the window size is adjusted in the floating mode. Take a look at the image below for referenc ...

Guide to filling out select dropdowns in HTML with information from a database

I'm new to PHP and HTML, so please be patient with me. I've been attempting to populate a select box using data from a MySQL database, but all I see is an empty textbox. Here's my current code: <select name="cargo"> <?php ...

Is it recommended to add the identical library to multiple iFrames?

Here's a quick question I have. So, my JSP page has 4 different iFrames and I've included JQuery UI libraries in it: <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> <script src="http://c ...

Craft a stunning transparent border effect using CSS

I am trying to achieve a unique border effect for an element using CSS, where the transparency is maintained against the background: This is the desired outcome: https://i.stack.imgur.com/6Z1MU.png However, the border I am currently able to create looks ...

Troubles with Fixed Navigation Bar in JavaScript-Enabled Scrolling

I've implemented a JavaScript-enabled scrolling navigation bar that starts below a hero graphic and then sticks to the top when it reaches the top of the page. The functionality works as intended, but there is one issue - when the navigation bar reach ...

Having difficulty integrating JSON data from a PHP page into jQuery

I'm having trouble parsing JSON data sent from a PHP page using jQuery. The result I'm getting is just an opening curly brace: { I'm not sure how to fix it. In my PHP code, I'm creating the JSON data like this: $load_op_cm = $DBM -> ...

Show the modal content in Bootstrap on mobile and tablet screens

I have exhaustively searched the web for a solution to my issue, but unfortunately, I have come up empty-handed. I am hoping that you can offer some guidance in this matter. I am currently utilizing modals to showcase details on my website. However, when ...

What causes the discrepancy between a website's source code and the code viewed in the browser inspection tool? (Regarding web scraping)

This is my first programming project and I apologize in advance for any mistakes in terminology. My Objective: I am currently working on a project to scrape data from my local library's website. My main goal is to automate the process of renewing bo ...

Transform JSON array containing identical key-value pairs

My array is structured as follows: [ { "time": "2017-09-14 02:44 AM", "artist": "Sam", "message": "message 1", "days": 0 }, { "time": "2017-09-14 02:44 AM", " ...