Why aren't the general sibling combinator & adjacent sibling combinator effective for selecting ul + li & tr + td elements?

I have recently started learning development, and I am practicing HTML & CSS by following tutorials on YouTube.

In one of the tutorial videos, the instructor explained advanced selectors, including adjacent and general sibling combinators.

For example: h2 + a {}, h2 ~ p {}

When I tried these selectors as demonstrated, they worked. However, when I attempted to use them on ul + li, tr ~ td, it did not work as expected.

Can someone please explain why this is?

h2 ~ p {
  color: red;
 }
  ul + li {
  color: purple;
  }
 <h2 class="subtitle">About Me</h2>
    
    <img src="../img/img-1.jpg">
    
    <p>Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    
    <span>Lorem ipsum dolor sit amet</span>, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. 
    
    Cillum dolore eu fugiat nulla , pariatur:
    
      <ul><!--parent-->
        <li>Excepteur sint occaecat</li><!--first child-->
        <li>My numbered list item</li><!--second child-->
        <li>Nulla excepteur irure</li><!--third child-->
      </ul>
    </p>

h2 + a {
  color: red;
  }
  tr ~ td {
    color:green;
    }
  
<h2 class="subtitle" id="subtitle-id">Services</h2>
    
    <a href="http://www.google.com" id="google-link">Click here to go to Google</a>
    
    <table border="1">
      <thead>
        <tr>
          <td>Lorem</td>
          <td>Ipsum</td>
          <td>Dolor</td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Sit</td>
          <td>Amet</td>
          <td>Consectetur</td>
        </tr>
        <tr>
          <td>Adipisicing</td>
          <td>Elit</td>
          <td>Sed</td>
        </tr>
        
      </tbody>
    </table>

Answer №1

Brothers and sisters are two elements born of the same parent.

An item in a list cannot stand as a sibling to another item, it must be a child within the list.

A cell in a table cannot exist as the sibling of a row, it must be a child of that row.


x and y are siblings

<x></x>
<y></y>

m and n share a parent-child bond

<m> <n></n> </m>

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

Utilizing jQuery to Extract HTML Data Attributes

Is there a way to access and extract the values stored in the data attributes with jQuery? <div class="sm-tot" data-ts-speed="500" data-ts-interval="4000" data-ts-newVal="5000" > ...

CSS: element misplaced at the top instead of the bottom where it belongs

The component with the ID of "#bottom-nav" actually appears at the top. I just designed this on CSS-board, so feel free to review the HTML and CSS code Take a look at it on CSS-desk here It is positioned at the very end of the page and includes a ' ...

Exploring the possibilities of Jquery cycle combined with creative CSS webkit animations

I am currently using jQuery cycle for my slideshow. I want the numbers to stop and then roll whenever the next slide appears. I have the numbers rolling in an infinite sequence so they only fade in and out within the slideshow. However, I actually want to ...

What are the steps to create a responsive form using CSS?

I have a screenshot below that needs to be recreated using HTML/CSS. This design should be consistent in both desktop and mobile views. https://i.stack.imgur.com/cnfHt.png Currently, I have managed to replicate this in a JSFiddle which looks good on desk ...

CSS-powered tabs which necessitate the conventional display of radio buttons and labels inside each tab

Creating CSS-only tabs involves hiding the radio button and styling the tab label accordingly. .tabs input[type="radio"] { position: absolute; top: -9999px; left: -9999px; } .tabs label { display: block; min-height: 150px; padding: 4px 4px; ...

What is the best way to place a div inside a navbar element inline?

Having a bit of trouble with my HTML code. I'm working on creating an offcanvas navbar. Here's the code snippet: <nav class="navbar navbar-dark bg-dark"> <div class="container-fluid"> <a cl ...

jQuery failing to recognize multiple selection form elements

<select class="js-example-basic-multiple categories form-control" name="categories[]" style="width: 100%" multiple="multiple"> <option value=""></option> <option value="fresher">fresher</option> <option value=" ...

Dynamic sliding box jumps instead of simply fading in/out

My app features both a navigation bar and a sub-navigation bar. Within the sub-navigation bar, users can click on a button that triggers the appearance of another sub-bar while hiding the original one. The new sub-bar should smoothly slide out from behind ...

Sending emails to multiple groups in Opencart can be easily achieved with the

I am interested in customizing Opencart's admin panel to allow for the selection of multiple customer groups when sending emails. However, I am unsure of where and how to make these modifications as I am relatively new to this type of task. https://i ...

Rotating SVG graphics with a growth effect

Check out my CSS below: /* -------------------------- Base --------------------------- */ body { padding-top: 60px; background: #0f4e7a; } svg { margin: auto; display: block; width: 28%; } .star{ fill: #FFF; } /* ------------------ ...

obtaining data from an HTML input field

I'm struggling to retrieve the value from an HTML input element, and despite my own research, it seems like I have the correct syntax. However, I'm still unable to get it to work, leaving me confused. When I log the value to the console, it appe ...

Using an HTML5 image icon as an input placeholder

Is there a way to incorporate an image icon into an input placeholder and have it disappear when the user starts typing, across all browsers? In trying to achieve this, I successfully implemented a solution for webkit (Safari+Chrome) using ::-webkit-input ...

Customizing the appearance of individual columns in the Material-UI DataGrid

My goal is to center an IconButton within a DataGrid table, all of which are MUI components. Despite reading the documentation and trying various methods, I haven't achieved the desired result yet. In my latest attempt, I implemented the following: G ...

Challenges arise with the safari navigation bar within a PWA platform

Struggling with the formatting and spacing issues of a PWA application on IOS devices due to notches and navbar heights. One specific problem is that a chat input with absolute positioning and bottom: 0 does not display properly in Safari because of the n ...

Issues with retrieving the subsequent anchor element using Jquery Slider

(Apologies for the lengthy post. I like to provide detailed explanations.) Just starting out with Jquery, trying my hand at creating a custom image slider from scratch. Got the slider working with fade effects using this code: Javascript: $(document).re ...

Update the form action URL when a specific option is selected from the dropdown menu upon clicking the submit

I would like my form to redirect to a specific page on my website based on the selection made in the <select> tag. For instance, if '2checkout' is selected, it should go to gateways/2checkout/2checkout.php. Similarly, if 'Payza' i ...

Tips for creating an HTML table that fills the entire width of its parent element:

One of the rows in my table contains a nested table. I want both tables to fill up 100% of the width of the parent element. How can I achieve this? Check out the demo here Here's the HTML code: <div class="container"> <table> <tr ...

Tips for transforming an HTML website into a WordPress site

After choosing the default WordPress theme, for example twenty thirteen, and customizing it with my own CSS, I was able to achieve the look I wanted for my header and footer. However, I encountered difficulties when creating pages as the default CSS of t ...

My goal is to retrieve and print the duplicated values only once from an associative array

Given an associative array, I need to print all the department names without any repetitions. <h3>2. List out all department names</h3> <div class="all"> </div> Here is my JavaScript code: var employee=[{"firstName":"Zahir","last ...

Looking to create applications for Android and iOS that can integrate with a module designed in Adobe Captivate

I have created an interactive e-learning module using Adobe Captivate, which has been published in both HTML5 and SWF formats. The module includes some interactive elements/actions that can be accessed by clicking on them. It works perfectly fine in a web ...