I am encountering unexpected results when utilizing the CSS child combinator with a list

I am puzzled by the fact that the letter H is not displaying in red, while D E F are showing up as red. Shouldn't the CSS path for H be .pages > li > ul > li ?

What I have observed is that if I remove G or change the tag of G to something else, like a <p>, then H shows up in red;

.sitemap li {
  color: teal;
}

.pages > li > ul > li {
  color: red
}
 <aside class="sitemap">
          <ul class="pages">
            <li>A</li>
            <li>B</li>
            <li>C
              <ul>
                <li>D</li>
                <li class="featured">E</li>
                <li>F</li>
              </ul>
              <li>G</li>
              <ul>
                <li>H
                  <ul>
                    <li class="featured">I</li>
                    <li>J</li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
        </aside>

Answer №1

You overlooked a couple of lines in the HTML code. Once you add them, your style should work correctly, but keep in mind that it will also affect the color of D, E, and F as they are at the same level as H.

<aside class="sitemap">
  <ul class="pages">
    <li>A</li>
    <li>B</li>
    <li>C
      <ul>
        <li>D</li>
        <li class="featured">E</li>
        <li>F</li>
      </ul>
    </li> <!-- don't forget this line -->
    <li>G</li>
    <li> <!-- make sure to include this line -->
      <ul>
        <li>H
          <ul>
            <li class="featured">I</li>
            <li>J</li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</aside>

Answer №2

To ensure correct nesting, you need to properly handle the <ul> and <li> elements in your code.

The error lies in having <li>G</li> directly nested within <li> C. Placing an <li> within another <li> is not valid; <li> elements should only be children of <ul>, <ol>, or <menu>.

Refer to the following code snippet for a correctly structured list item example.

.sitemap li {
    color: teal;
}
.pages > li > ul > li {
    color: red
}
<aside class="sitemap">
    <ul class="pages">
        <li>A</li>
        <li>B</li>
        <li>C
            <ul>
                <li>D</li>
                <li class="featured">E</li>
                <li>F</li>
            </ul>
        </li> <!-- point c <li> is complete here after completing your subpoints of main point c -->
        <li>G
            <ul><!-- now for H is a sub point for G so just move this <ul> inside of the <li> of point G -->
                <li>H
                    <ul>
                        <li class="featured">I</li>
                        <li>J</li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</aside>

Answer №3

I'm not sure why you're formatting your text in that manner. It's not the correct way to create an unordered list using HTML. The parent of a list item li should be the unordered list element ul. However, if you wish to continue with your current approach, you can use the following CSS:

.pages > ul > li {
  color: red;
}

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

Checkbox and radio buttons in JQuery UI are stacked vertically on top of each other, rather than adjacent to each other

Here is the HTML code snippet: <div id="container"> <div id="signals-summary-parameters-radio"> <fieldset> <legend>Row Filter : </legend> <label for="signals-summary-radio-all">All</label> ...

I'm curious if there is a specific regex pattern that can be used for validating custom page ranges in JavaScript

I'm in need of a regular expression for page sequences that should match: 1 1, 4-5 1, 4-5, 8, 11, 13-14 but should not match: 1,,,, 1, 4-5- 1, 4--------2233-----,,,, I've experimented with the following patterns but they haven't been su ...

Create a dynamic HTML table as the page loads

Customize for vhinn This is the desired output: I am currently attempting to dynamically create an HTML table on pageload using variables retrieved from a database. Here is a simple example of HTML code: http://jsfiddle.net/jdv590/daCum/1/ Code snippet ...

Troubleshooting HTML Label Problems

I am facing an issue with my custom labels. .label-ras{ padding:3px 10px; line-height:15px; color:#fff; font-weight:400; border-radius:5px; font-size:75%; } .label-primar{background-color:#5c4ac7} <span style="background- ...

The URL in $.mobile.changePage has not been updated

One of the challenges I encountered was switching between two pages - a menu list page and a menu detail page. The transition from the menu to the menu detail page was achieved using $.mobile.changePage. While everything seemed to be working correctly, the ...

What are some ways to eliminate the excess white space beneath the footer in HTML?

After creating a responsive webpage that works flawlessly on mobile devices and tablets, I noticed a problem with the footer when viewed on desktop. There is an empty white space at the end of the webpage, and upon inspecting the browser, it focuses on the ...

Ways to update div content following a successful AJAX call

I have a form that utilizes AJAX requests, and when the input fields are not filled correctly and the submit button is clicked, error messages are displayed without refreshing the page. While this functionality works, a problem arises when the form is subm ...

Adjust the appearance of an element in a different parent when hovering over a specific element with a matching index

I have implemented a menu on my site in two different ways - one using text and the other using pictures. Users can click on both types of menus. Now, I am looking to create an interactive feature where hovering over a specific item in the text menu (such ...

The items are misaligned in a horizontal position

The 4 divs are styled with a width of 23% and displayed using inline-block. They have a fixed height of 220px and no margin applied. However, the issue arises as they are not aligning horizontally Despite attempting to adjust the height and width paramete ...

Using jQuery to trigger a Bootstrap modal when a button is clicked

I've been attempting a simple task, but it appears to be unsuccessful. I'm trying to handle the click event of a button inside a bootstrap modal, and so far, nothing happens when clicking the button. My guess is that my jQuery selector for select ...

Countdown malfunction: wrong date displayed

Utilizing the Countdownjs library in my project is resulting in an incorrect day count. Incorporating AngularJS, here is the custom directive I've implemented for the countdown: .directive('tempoPercorrido', function($interval){ ret ...

Transferring data from AJAX to PHP class methods

Q. Is it feasible to transfer data from ajax to a specific php class with functions? For instance, verifying a username on the registration form to check if the user already exists. This form is straightforward and will gather a username input along with ...

PHP script for image upload is malfunctioning

<? if(isset($_POST['upload'])) { if(empty($_FILES['image'])) { echo "<p class=\"error\">Please upload an image...</p>\n"; } else { $file_name= $_FILES['image']['name' ...

Smoothly scroll to the bottom of a dynamically updated div using native JavaScript

I am in the process of developing a chat application and my goal is to achieve a smooth scrolling animation that automatically moves the page down to the bottom of the div when dynamic messages are loaded. <ul id="messages"> <li ...

Utilizing Angular 10 to Transform a JSON Data into a Custom String for HTML Rendering

I have received a JSON response from my backend built using Spring Boot. The "category" field in the response can either be 1 or 2, where 1 represents Notifications and 2 represents FAQs. { "pageNumber": 0, "size": 5, "totalPages&q ...

Problem with jQuery Window Resize Trigger Not Reactivating

I am facing a challenge in integrating a slider within multiple jquery tabs. As the slider requires specific width and height to display correctly, I have to trigger a window resize event when the tabs are switched. Surprisingly, the code I implemented onl ...

Achieving perfect alignment in a CSS grid for form elements vertically

One of my current projects involves creating a form layout using HTML and CSS that follows a Material Design style: <div class="form-group"> <input type="text" id="feedName" name="name" value={nameValue} onChange={this.handl ...

React Bootstrap ToggleButton firing function multiple times unnecessarily

I've encountered an issue with my react-bootstrap ToggleButtons. Whenever I click on them, the handlePlatformChange() function is triggered twice - first with the correct id and then immediately after with null. I tried including e.preventDefault() in ...

Require assistance in designing a webpage using DIV elements

I have a vision for how I want my webpage to look: Check out my mockup here: http://img64.imageshack.us/img64/5974/pagedh.jpg But, I'm not quite there yet. Here's my progress so far: I'm still learning about using <div> elements ins ...

How can I position the close button correctly in a bootstrap popup modal?

I have a basic bootstrap popup modal. Currently, the close X button is centered within the popup window, but I would like to align it to the right corner instead. Is there a way to achieve this? <div class="modal fade" id="checkNameSurvey-modal" data ...