Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

CSS How to style an unordered list without styling another one nested within it

Hi all,

This is my first time posting, so I hope I’m doing it correctly.... please bear with me.
I’m very new to coding, so this is probably a super easy question to answer, but it’s driving me nuts.

I have an <ul> nested within another <ul> I need to style the direct children of that FIRST list, but NOT the elements in the second unordered list.

As a part of the exercise I’m doing for my course, we need to place a bottom border under list items 1, 2, 3 but not under the two bullet points in the second . See photo attached for end goal.

I’ve tried everything. Closest I could get was using a child combinator...

This is the CSS I’ve tried:

[CODE lang="css" title="CSS I’ve tried"]ul > li {border-bottom: solid;
border-width: 1px;
border-color: grey}[/CODE]

here is the code in the html (Exercise instructs not to change the html to accomplish what we need to do):

HTML:
<ul class="list">
<li>One</li>
<li>Two
   <ul>
     <li>2.1</li>
     <li>2.2</li>
   </ul>
</li>
<li>Three</li>
</ul>

here is the end goal featuring the bottom border under 1, 2, and 3
image.webp
 
Look at my last reply 😉. You were applying the styles to ALL <li> inside <ul>. The selector I wrote selects only the <li> that are directly inside a <ul> that is directly inside the element <body>.
 
What is the parent of the first <ul>? If it is <body>, you could change the selector for:
CSS:
body > ul > li
{
}
HTML:
<div class="container">
    <h2>This is a heading</h2>
        <p>This paragraph comes after the heading.</p>
        <p>This is the second paragraph.</p>

        <h2>Another heading</h2>
        <p>This paragraph comes after the heading.</p>

        <ul class="list">
          <li>One</li>
          <li>Two
            <ul>
              <li>2.1</li>
              <li>2.2</li>
            </ul>
          </li>
          <li>Three</li>
        </ul>

  </div>
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom