Leverage the power of the General Sibling Selector to easily traverse through deeply nested elements in your

Hi there, I have a query regarding the General Sibling Selector.

Let's start by looking at the HTML:

<div id="layout-middle">
 <div id="Center">
   <a class="col Picture1">Title1</a>
   <a class="col Picture2">Title2</a>
   <a class="col Picture3">Title3</a>
   <a class="col Picture4">Title4</a>
   <a class="col Picture5">Title5</a>
   <a class="col Picture6">Title6</a>
 </div>
</div>

<div id="bg"></div>

CSS:

 #layout-middle {
  background: #d3d1ce url('Images/middle.jpg') top center repeat-x;
  border-bottom: 4px solid #777674;
}
 #Center {
 margin: 0 auto;
 display: table;
}
.col {
 border: 1px solid #3E414A;
 text-align: center;
 float: left;
 width: 160px;
 height: 375px;
 padding-top: 16px;
 margin-top: -93px;
}

#bg {
 position: absolute;
 top: 0;
 right: 0;
 bottom: 0;
 left: 0;
 width: 100%;
 height: 100%;
 z-index: -1;
 background-image: url('Images/bg-top.jpg');
 transition: .25s;
 pointer-events: none;
}

#Bg serves as a background image behind all elements.

All images are formatted like this:

.Image1{
 background: url('Images/PictureXY.png') 0 0 no-repeat;
 }

Now my question is, when I hover over, for example, Picture1, how can I change the #bg background image to a new one without using Javascript, only CSS?

Normally, I would do something like:

.col:hover ~ #bg{
  background-image: url('newimage.png')
}

However, this doesn't work with the nested elements. I am aware that this could be easily achieved with Javascript, but my requirements specify the use of only HTML & CSS.

If you have a better solution than the General Sibling Selector, please let me know.

Answer №1

Unfortunately, the current CSS setup makes it impossible to achieve this.

The .col elements do not have a direct hierarchical relationship with the #bg div, making it difficult to target them with CSS3 selectors.

Your best bet would be to reorganize your HTML by placing the #bg element inside the #Center div. This way, you can utilize the general sibling selector successfully.

If restructuring the HTML is not an option, you may have to resort to using javascript to accomplish this task.

Answer №2

After reviewing your current HTML structure, it appears that achieving the desired result is not possible based on the specifications:

The general sibling combinator consists of the "tilde" (U+007E, ~) character which separates two sequences of simple selectors. The elements represented by the two sequences must share the same parent in the document tree and the element represented by the first sequence must precede (though not necessarily immediately) the element represented by the second one.

However, with the provided HTML code below, you can achieve your intended outcome:

#layout-middle {
   background: #d3d1ce url('Images/middle.jpg') top center repeat-x;
   border-bottom: 4px solid #777674;
 }
 #Center {
   margin: 0 auto;
   display: table;
 }
 .col {
   border: 1px solid #3E414A;
   text-align: center;
   float: left;
   width: 160px;
   height: 375px;
   padding-top: 16px;
   margin-top: -93px;
 }
 #bg {
   position: absolute;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
   width: 100%;
   height: 100%;
   z-index: -1;
   background-image: url('Images/bg-top.jpg');
   transition: .25s;
   pointer-events: none;
 }

.col:hover ~ #bg{
  background-image:url('https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97100&w=200&h=100')
}
<div id="layout-middle">
  <div id="Center">
    <a class="col Picture1">Title1</a>
    <a class="col Picture2">Title2</a>
    <a class="col Picture3">Title3</a>
    <a class="col Picture4">Title4</a>
    <a class="col Picture5">Title5</a>
    <a class="col Picture6">Title6</a>
    <div id="bg"></div><!-- Move this div here -->
  </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

What's the best way to fill in content for textarea and radio buttons?

I retrieved data from a database and stored it in the variable $tableRe. Now, I need to display these values in a textarea and also check the radio button. Below is my code snippet: $sql = "select (address, gender) from stud table"; if($result=mysqli_que ...

CSS styling for the dropdown list in the content/option section

Can someone help me with a simple question I've been researching for hours without success? I'm trying to figure out how to style the content/option/popup of a dropdownlist on the right side of the 'open button' like in this screenshot ...

Deciphering the elements in the periodic table using PHP and HTML

Recently, I encountered a problem that got me thinking. I was given the task of posting people's bios online (as discussed in another question) and I chose to use XML to create elements for each section of the bio. However, when some bios contained ...

Conceal the choice if its value matches that of another option

My goal is to create a code that will hide an <option> if its value matches the value of the first option (which is retrieved dynamically with PHP). Here's the initial code snippet: <select onChange="this.form.submit()" name="cart[<?php e ...

Clear SELECT After Submission

I have a jQuery script and need a function to reset the SELECT input after it has been submitted. <script> $(document).ready(function() { //elements var progressbox = $("#progressbox"); var progressbar = $("#progressbar"); var statustxt = ...

Converting text/plain form data to JSON using Node.js - step by step guide

I am currently working on a Node.js application to execute a POST call to an API for placing an order. However, the data I receive in our app is in text/plain format and not JSON. This is the current format of the data: TypeOrder=buy Coin=BTC AmountCoin= ...

Script to pop up cancel alert box

There's a dilemma with this code - if you click CANCEL the first time the box pops up, it continues to run. I'm unsure of how to make it redirect to the underlying page when clicked on cancel for the first time. var numero= prompt ("Enter a nu ...

What is the best way to utilize jQuery for deleting the final <li> within a <ul> element?

One of my web pages contains an unorganized list similar to this: <ul id="myList"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> </ul> How can I target the last ...

Creating a JavaScript object and retrieving the values of numerous input fields with identical classes

I have encountered an issue that I need assistance with: <input title="1" type="text" class="email"> <input title="2" type="text" class="email"> <input title="3" type="text" class="email"> The HTML code above shows my attempt to extract ...

What is the best way to create a button that will trigger a modal window to display a message?

I am looking to create a button that will open a modal window displaying a message. However, when I tried to add a label and viewed the page, the desired window appeared on top of the rest of the content. But unfortunately, clicking the button did not prod ...

Apply a Fade In transition to the ul li elements following a JavaScript toggle

I'm currently experimenting with implementing a Fade in from the right animation for the links displayed in the menu after toggling the menu body in the browser. Despite my efforts, I am unable to determine why the animation is not functioning as expe ...

Tips on snapping pictures using an html5 camera without encountering security prompts

Is there a way to capture an image from a webpage without triggering security warnings? The webpage where I need webcam functionality cannot be switched to HTTPS protocol. I have already installed root certificates and made them trusted, but still no succ ...

I noticed a significant space below the footer on my webpage. As a first-time website creator, are there any solutions to this

I'm currently in the process of working on my very first website for a study project using HTML and CSS. There's an issue where there is a large gap below the footer that I can't seem to eliminate. I've only completed 3 pages so far, bu ...

Transform a dropdown menu into an inverted footer

I stumbled upon a unique dropdown menu design that I want to tweak and reverse, making it an innovative "dropup" menu that opens from the bottom to the top. Currently, this is what I have: HTML <div class="get-started"> <a href="#" id="get-s ...

Passing an undefined value to the database via AJAX upon clicking a button

Hi there, I'm currently working on a table where I'm trying to perform an inline edit and update the value in the database by clicking on a button (an image). I've attempted to use an onclick function, but it seems to show "value=undefined&a ...

Is there a restriction on the maximum size of a CSS file?

Currently, I am working on building a site using ASP.NET MVC. The CSS file that I have been working on has now grown to 88KB in size and contains over 5,000 lines of code. Recently, I have noticed that some styles that I added towards the end of the file a ...

differences in rendering of flexbox align-items: center between Chrome and Firefox

As I was attempting to center a div inside another div using flexbox, an inconsistency in rendering between Firefox and Chrome caught my attention. In the scenario, there are 3 divs, each with properties display:flex, justify-content:center, and align-ite ...

When a radio button is chosen, multiple text fields must be completed in order to validate the form

I am working with a set of 3 radio buttons. If the last option, labeled "In Home," is chosen, then all 4 of the text fields must be filled in for the form to validate. While I have found information on how to achieve this with checkboxes or a single text f ...

Ways to align two divs side by side using CSS and HTML

Here is a question that needs solving: I have two elements that need to be displayed in one row at a specific ratio, with the same pattern repeating in subsequent rows. However, the content of the next row is appearing in the unused space of the previous r ...

Easily collect form information from an HTML <form> element

I've been struggling to figure out how to retrieve data from an HTML form and display it in the console. What step am I overlooking? Here is the HTML code I'm working with: <!DOCTYPE html> <html> <head> <title>U ...