Potential Issue with CSS General Sibling Selector

It seems like I have encountered a potential bug with the General Sibling Selector. When I use p ~ div, the selector does not work as expected. However, when I replace p with the specific class name text_paragraph, it works fine. You can view my code and fiddle below to see the issue.

Fiddle https://jsfiddle.net/t2Ljmgar/

.outer_container {
  margin: 20px;
  padding-bottom: 10px;
  background-color: rgba(216, 23, 27, 1);
  border: 1px solid rgba(0, 0, 0, 1);
}
.text_paragraph {
  margin: 10px 10px 0px 10px;
  padding-left: 10px;
  font: 2em/4em Arial, Helvetica, "sans-serif";
  color: rgba(64, 64, 64, 1.00);
  border: 1px solid rgba(0, 0, 0, 1);
  background-color: rgba(180, 180, 120, 1);
}
.text_container {
  margin: 10px 10px 0px 10px;
  padding-left: 10px;
  font: 1.5em/3em Arial, Helvetica, "sans-serif";
  color: rgba(64, 64, 64, 1.00);
  background-color: rgba(215, 215, 251, 1);
}
p ~ div {
  background-color: rgba(0, 255, 255, 1);
}
<div class="outer_container">
  <p class="text_paragraph">Important header up top.</p>
  <div class="text_container">This is the first text on the page.</div>
  <div class="text_container">This is the second text on the page.</div>
  <div class="text_container">This is the third text on the page.</div>
</div>

By changing P to the specific class name, the selector starts working correctly.

.outer_container {
  margin: 20px;
  padding-bottom: 10px;
  background-color: rgba(216, 23, 27, 1);
  border: 1px solid rgba(0, 0, 0, 1);
}
.text_paragraph {
  margin: 10px 10px 0px 10px;
  padding-left: 10px;
  font: 2em/4em Arial, Helvetica, "sans-serif";
  color: rgba(64, 64, 64, 1.00);
  border: 1px solid rgba(0, 0, 0, 1);
  background-color: rgba(180, 180, 120, 1);
}
.text_container {
  margin: 10px 10px 0px 10px;
  padding-left: 10px;
  font: 1.5em/3em Arial, Helvetica, "sans-serif";
  color: rgba(64, 64, 64, 1.00);
  background-color: rgba(215, 215, 251, 1);
}
.text_paragraph ~ div {
  background-color: rgba(0, 255, 255, 1);
}
<div class="outer_container">
  <p class="text_paragraph">Important header up top.</p>
  <div class="text_container">This is the first text on the page.</div>
  <div class="text_container">This is the second text on the page.</div>
  <div class="text_container">This is the third text on the page.</div>
</div>

Answer №1

Your specific class is taking precedence over the generic element selector due to its higher specificity.

Try commenting out the .text_container background-color property and observe how the generic one assumes control:

.outer_container {
  margin: 20px;
  padding-bottom: 10px;
  background-color: rgba(216, 23, 27, 1);
  border: 1px solid rgba(0, 0, 0, 1);
}
.text_paragraph {
  margin: 10px 10px 0px 10px;
  padding-left: 10px;
  font: 2em/4em Arial, Helvetica, "sans-serif";
  color: rgba(64, 64, 64, 1.00);
  border: 1px solid rgba(0, 0, 0, 1);
  background-color: rgba(180, 180, 120, 1);
}
.text_container {
  margin: 10px 10px 0px 10px;
  padding-left: 10px;
  font: 1.5em/3em Arial, Helvetica, "sans-serif";
  color: rgba(64, 64, 64, 1.00);
  /* background-color: rgba(215, 215, 251, 1); */
}
p ~ div {
  background-color: rgba(0, 255, 255, 1);
}
<div class="outer_container">
  <p class="text_paragraph">Important header up top.</p>
  <div class="text_container">This is the first text on the page.</div>
  <div class="text_container">This is the second text on the page.</div>
  <div class="text_container">This is the third text on the page.</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 is the best way to "leap" a sole transform value within an animation?

I need to create an animation that makes a transform function value "jump" in an animation. Within my @keyframes rule are three percentages with the transform property in each one. I specifically want only the scale to transition from 0.5 to 1 between the ...

CSS transition effect to show content: display: block

After experimenting with different styles for another element, I added padding, height, and opacity to the div. However, there seems to be no transition effect with the current CSS code. Can anyone explain why? When a button is clicked, the class .show is ...

ajax with names that are alike

I've set up a text input field that searches for file names on my server. However, it currently requires an exact match to display the file. I'm looking for a solution that can show me files even if the input text isn't an exact match. Here ...

The navigation bar's background color remains static and doesn't update as

Struggling with HTML/CSS and unable to get the NAV bar to display the specified background color. Tried putting the nav in a class, commented out parts of the code, but still couldn't make it work. Despite looking for solutions, I can't figure ou ...

Ways to automatically refresh a page in Javascript after a short period of user inactivity

Similar Question: How Can I Modify This Code To Redirect Only When There Is No Mouse Movement I am looking to update a web page automatically if the user is inactive, specifically through key presses or mouse clicks using Javascript. ...

Conceal the Angular Bootstrap modal instead of shutting it down

I am utilizing Angular Bootstrap Modal to launch multiple modals, including a video player. http://angular-ui.github.io/bootstrap/#/modal My goal is to maintain the video's current position even when the modal is closed. This way, when I reopen the ...

BeautifulSoup: Retrieve text from HTML lists that are designated exclusively with bullet points

Seeking assistance on how to extract the text specifically within a bulleted list from a Wikipedia article using BeautifulSoup. I am aware that list items are enclosed in <li> tags in HTML, regardless of the type of marker used before each item in t ...

What is the best way to display SQL query results in a list upon clicking a tab?

I have imported an SQL file containing a table named student_tb. This table consists of three columns: student_id, student_name, and class_name. To create this SQL table, you can follow the script below: CREATE TABLE `student_tb` ( `student_id` int(11) ...

When a div tag containing PHP is empty, it should either be hidden or show specific text based on your requirements

Among the plethora of unanswered queries regarding hiding empty divs, I find myself unable to make any of the suggested solutions work. Hence, I am putting forth my own question. On my webpage, there is a specific section dedicated to showcasing various it ...

The onclick event for a Bootstrap .btn-group checkbox functions correctly in JSFiddle, but encounters issues when tested

The toggle button group in the form below utilizes Bootstrap and checkboxes. I am looking to track click events on the checkbox inputs. <html> <head> <title>Example: Bootstrap toggle button group</title> <meta charset="UTF-8 ...

Preloading videos for optimal performance on mobile devices

Looking for a solution where 4 MP4/video files, each 5MB in size, can be played consecutively without gaps between them on all browsers and devices. Preferably not looking for a solution involving the replacement of video source files. The current approac ...

How to dynamically adjust font size using Javascript

Is there a way to adjust the font size of the left-column using JavaScript? <div id="left-column"> <a href="">1</a><br> <a href="">2</a><br> <a href="">3</a><br> <a href=""> ...

How to customize the page background color in Next JS

I am currently working on a project using Next Js and have encountered an issue. I have a global.css file set up in the project, but I need to change the background color of a specific page without affecting the rest of the project. Although I have tried u ...

What is the reason behind using '-webkit-backface-visibility: hidden;' to resolve problems with negative margin transitions on iOS/iPad 5.1?

During the process of resolving my issue, I accidentally discovered a solution - but I always prefer to understand the fixes I apply. <ul> <li><img src="something.jpg" /></li> <li><img src="somethingElse.jpg" />< ...

Engage the PROTRACTOR refresh function

I'm just getting started with automation and Protractor. I successfully automated the login page, but now I'm facing a challenge with accessing a menu that I need in order to navigate to a different page. Within the menu, there is an href="#/dom ...

Concealing the pathway to a background image

Currently, I have a large grid made up of divs that display different sections of an image. By using background-image and background-position, I am able to showcase parts of the image. However, one issue is that users can easily view the complete image by ...

A fresh javascript HTML element does not adhere to the css guidelines

While attempting to dynamically add rows to a table using Javascript and jQuery, I encountered an issue. Here is my code: <script> $(document).ready(function(){ for (i=0; i<myvar.length; i++){ $("#items").after('<tr class="item- ...

Show a table with rows that display an array from a JSON object using JavaScript

My current code includes JSON data that I need to display in table rows, but I'm struggling to understand how to do so effectively. The output I am currently seeing is all the rows from the array stacked in one row instead of five separate rows as in ...

Css technique for changing color on mouse hover

I currently have a social media bar on my website with icons for Facebook, Twitter, Google+, and RSS. Here is how it looks: When I hover over the icons, I want the circle around the image to change color to blue. However, every attempt I've made end ...

The event listener is failing to trigger the JavaScript function upon click

I am experiencing an issue with an onClick listener triggering an AJAX call to a PHP script. It seems that the click is not being recognized. Any suggestions on what could be causing this? Index.html <html> <head> <script src="//ajax.googl ...