What is preventing the CSS selector from functioning for <li> elements nested within a <p> tag?

Studying for my exam, I decided to practice answering some past questions without looking at the markscheme. One question in particular has me stumped:

The question asks to explain the effect and identify which elements will be affected by the following rule:

p ul li {
   color:blue;
}

I created a simple HTML layout on JSFiddle: http://jsfiddle.net/2nkmzgv2/

Here is the code snippet:

<p>
  <ul>
      <li>Should this text be blue?</li>
  </ul>
</p>

As I tested it out, I noticed that the text did not change to blue as expected. This left me wondering if nothing was meant to happen at all or if there was an issue with my syntax or method in the HTML content.

Answer №1

It seems that ol and ul items are not allowed inside of p elements.

Should ol and ul be placed inside or outside of <p> tags?

This question may be a bit tricky for your exam. It's important to review the HTML specifications as well, as CSS knowledge alone might not be sufficient.

The behavior you're experiencing is because <p> cannot contain block-level elements like <ul> or <ol>. Browsers (e.g., Chrome) recognize this limitation and try to handle the illegal structure by placing the block element between two paragraphs:

https://i.sstatic.net/FaC2p.png

Due to this handling, the rule declared for the li element does not take effect.

p ul li {
  color: blue;
}
<p>
  Hello
  <ul>
    <li>I'm never styled in blue.</li>
  </ul>
  Goodbye
</p>

Be mindful of other quiz questions like:

a a { color:red }
p p { color:blue; }

Answer №2

The HTML is considered invalid because having a list inside paragraph tags is not allowed as shown in this source.

This also means that the CSS styling will not affect the li element. To apply the CSS correctly, use the following code:

ul li {
    color: blue;
}

Make sure your HTML looks like this:

<ul>
    <li>Should this text be blue?</li>
</ul>

Check out the latest version of JSFiddle for more details.

Answer №3

"According to the HTML specification, it is not valid for list elements such as ol and ul to be nested within paragraph (p) elements. This rule applies specifically when a sentence includes a bulleted list." Referenced from HTML spec

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

How can a bootstrap gallery maintain a consistent size despite variations in picture dimensions?

I am currently working on creating an image gallery for our website using the latest version of Bootstrap. However, we are facing an issue with the varying sizes of our images. Each time the gallery switches to a new image, it disrupts the overall size and ...

Horizontal scroll through Bootstrap 4 navigation tabs

I'm currently using the newest Bootstrap 4 nav-tabs feature to create tabs and I'm aiming to keep all the tabs in a single horizontal line that can be scrolled. I've attempted to use various JavaScript plugins, but none of them seem to func ...

The Bootstrap Carousel unfortunately does not provide the option for responsive images

There appears to be an issue with the bootstrap carousel images where they are being assigned a fixed height/width attribute by either views or bootstrap_views. As a result, when the page is resized or viewed on a smaller screen or device, the image gets ...

Is there a way to show an alert indicating the location of the element that was clicked on the screen?

Here is an example I am working on: link HTML CODE: <ul> <li id="#bar"> <img src="http://theimageconference.org/wp-content/uploads/2014/01/images-50x50.png"width=50 height=50></img> <p>ELEMENT 1</p&g ...

Create a dynamic webpage that accepts user input, processes it using R code, and then displays the resulting output back on the webpage

I am in the process of creating a front end interface to showcase the results of my R code, which requires two files: master.csv and detail.csv. The master.csv file contains reference data with two columns, Commodity Category and Commodity Name, listed as ...

Creating a basic image carousel with JavaScript, CSS, and HTML

After running the code, I encountered an issue where the first image displays but then after one second, only a white blank screen appears with no further action. It seems like there may be an error in the JavaScript code. Below is the code that was attemp ...

What is the reason for the lack of an applied CSS selector?

.colored p{ color: red; } article > .colored{ color:powderblue; } .blue{ color: white; } <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initi ...

Pair up balls that have matching numbers

A software application was designed with the following features: It can create 4 balls on the screen, each containing a numerical value Users have the ability to input values for new circles to be generated Upon clicking a button, 4 new circles with num ...

Running a method at any given time within an ngFor loop in Angular 5

On my angular page, I am facing a challenge with updating a variable and displaying it in the HTML within an *ngFor loop. Here is an example of what I need: HTML: <table *ngFor="let data of Dataset"> somehowRunThis(data) <div>{{meth ...

Building a Multiple Choice Text Box with HTML

I am looking to create a section on my website where I can display text, and I have 9 buttons on the page that I want to stay within the same page without redirecting. The goal is for the text to update based on which button is clicked, with a fading ani ...

Tips on utilizing jquery slideDown alongside appendTo

I am currently utilizing an ajax request to retrieve records from the database and then appending the data in HTML. However, I want the HTML to slide down once the data has been appended, but I'm unsure of how to achieve this. Below is the ajax metho ...

Using AngularJS to bind to the 'src' property of an <img> tag

There is a table on my website with numerous rows, and each row contains a preview image that should appear in the top right corner when the mouse hovers over it. I attempted to insert an image tag with AngularJS binding for the URL in the src attribute l ...

Bootstrap 5's ScrollSpy functionality seems to be malfunctioning

I attempted to implement the scroll-spy feature of Bootstrap 5 in order to highlight the corresponding navbar item as active while scrolling through different sections of my HTML page. However, I encountered an issue where the navigation-link item would be ...

Is it possible to eliminate the sticky class as you scroll down?

Check out this jQuery code I wrote to remove the sticky class while scrolling down: $(window).scroll(function (e) { if ($('.main_form_wrapper').length != 0) { var window_scroll = $(window).scrollTop(); console.log(window_scro ...

Reposition all other elements of the HTML body after collapsing the Bootstrap section

I am facing an issue with Bootstrap where every time I click on a collapse element, the rest of the body moves up or down. Is there a way to prevent this from happening? Here is an example of my code: <body> <div class="panel-group"> ...

Ways to resolve the issue of iOS Safari showcasing 'a' tag styling differently compared to other browsers

Here's a simple question for you. Take a look at the images below for reference. Check out the iOS Safari browser on my phone https://i.sstatic.net/o9jOe.jpg and compare it to my desktop browser, both screenshots were taken live from the server. ...

Trouble with video embedding not adjusting properly within a Bootstrap 5 carousel

I'm currently facing an issue with two video embeds in a carousel. One has a standard bootstrap size of 16x9, while the other has a custom size of 4x5. Even though I'm using the embed-responsive code for both videos, none of them scale responsiv ...

Where should I place my custom menu script in Wordpress and how do I do it?

I am currently exploring the realms of PHP, Wordpress, and Javascript as I endeavor to transform my website, crafted with CSS and HTML, into a dynamic theme for Wordpress using PHP. One particular feature on my site is an off-screen menu that smoothly ope ...

Text alignment from the lower edge

Imagine having a title inside an image, positioned near the bottom with a negative margin-top. The issue arises when the text orientation expands from top to bottom as more content is added. Is there a way to reverse this, so that the text div grows toward ...

Tips for creating a hover-activated dropdown menu

How can I create a drop-down menu in my horizontal navigation bar that appears when hovering over the Columns tab? The drop-down menu should include options such as Articles, Videos, Interview, and Fashion. To better illustrate what I am looking for, here ...