Issue with unordered list item styling in CSS

Encountered a peculiar problem:

Within my .cshtml file, there is another element:

<ul>
    <li>Test</li>
    <li>Test2
        <ul>
            <li>Test3</li>
        </ul>
    </li>
    <li>Test4
        <ul>
            <li>Test5</li>
        </ul>
        <ul>
            <li>Test6</li>
        </ul>
    </li>
</ul>

It is supposed to display like this:

Test
Test2
     Test3
Test4
     Test5
     Test6

However, it actually looks like this:

Test
Test2
Test3
Test4
Test5
Test6

The only styling applied in css is list-style: none;. The main <ul> is contained within a <div> with text-align: left; (removing this centers all items).

I am new to css, so I can't figure out what is missing.

UPDATE: I forgot to mention that this displays correctly in jsfiddle.

Answer №1

Make sure to reset all your styles and properly close the </li> tag. Don't use <li> solely for opening and closing:

<ul>
  <li>Test</li>
  <li>Test2
    <ul>
      <li>Test3</li>
    </ul>
  </li>
  <li>Test4
    <ul>
      <li>Test5</li>
    </ul>
    <ul>
      <li>Test6</li>
    </ul>
  </li>
</ul>

Common Error

<ul>
    <li>Test<li> <!-- Should be </li> not <li> -->
    <li>Test2
        <ul>
            <li>Test3</li>
        </ul>
    <li><!-- Should be </li> not <li> -->
    <li>Test4
        <ul>
            <li>Test5</li>
        </ul>
        <ul>
            <li>Test6</li>
        </ul>
    <li><!-- Should be </li> not <li> -->
</ul>

Preview:

Answer №2

Take a look at this...

OR

Show me the CSS you utilized...

div {
  text-align: left;
}
ul {
  list-style: none;
}
<div>
  <ul>
    <li>Test</li>
    <li>Test2
      <ul>
        <li>Test3</li>
      </ul>
    </li>
    <li>Test4
      <ul>
        <li>Test5</li>
      </ul>
      <ul>
        <li>Test6</li>
      </ul>
    </li>
  </ul>
</div>

Answer №3

It appears that this could be the root of your problem.

  • padding:0px;
  • margin:0px;

ul li ul
{
    padding:0px;
}
<ul>
    <li>Sample</li>
    <li>Example
        <ul>
            <li>Illustration</li>
        </ul>
    </li>
    <li>Demo
        <ul>
            <li>Trial</li>
        </ul>
        <ul>
            <li>Experiment</li>
        </ul>
    </li>
</ul>

SOLUTION

Reminder: Please eliminate the padding property in your ul CSS.

ul li ul
{
   /* padding:0px; REMOVE IT If It's 0PX' */
}
<ul>
    <li>Sample</li>
    <li>Example
        <ul>
            <li>Illustration</li>
        </ul>
    </li>
    <li>Demo
        <ul>
            <li>Trial</li>
        </ul>
        <ul>
            <li>Experiment</li>
        </ul>
    </li>
</ul>

Answer №4

My solution involves wrapping your un-ordered list with a div tag that has a class name. For example, use

<div class="test"></div>

Here is the HTML code:

<div class="test">
    <ul>
        <li>Test</li>
        <li>Test2
            <ul>
                <li>Test3</li>
            </ul>
        </li>
        <li>Test4
            <ul>
                <li>Test5</li>
            </ul>
            <ul>
                <li>Test6</li>
            </ul>
        </li>
    </ul>
</div>

And here is the CSS code:

.test ul li {
    list-style: none;
    text-align: left; 
}

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

Interactive Material Icons with Hover Effects in CSS

I'm having trouble adding a hover effect to material icons in my code. Despite everything else looking good, the hover effect is not working as expected. Below is the code snippet I've been using: import ManageAccountsIcon from '@mui/icons-m ...

How to create a responsive image that appears over a button when hovering in Bootstrap?

My goal is to display an image over a button when hovering. I've tried adding the .img-responsive class in Bootstrap while including the image in the HTML, but it's not working as expected with my current method of loading images. The buttons the ...

Abnormal scrolling issues observed on handheld devices like mobile phones and tablets

I recently added a parallax effect to the hero section of my website, and while it works perfectly on desktop, I encountered some scrolling issues when viewing it on mobile or tablet devices. Sometimes the scrolling behaves as intended, and other times it ...

What is the best way to attach multiple files in PHP using mail.php and mime.php from Pear library?

I am having trouble attaching multiple files. When I try to attach more than one file, only one file gets sent in the email even though I attached multiple files. if(isset($_POST['upload'])){ include('config.php'); include_once ...

Automatically generating new lines of grid-template-columns with CSS grid

I am new to using grid in CSS and there are still some concepts that I don't quite understand. Below is the container I am working with: <section class="css-grid"> <div class="css-item"><img src="1.jpg" / ...

What is the best way to keep the menu bar in place as you scroll?

Can someone help me figure out how to keep the menu bar fixed at the top of the page when the user scrolls down? I've tried various methods but haven't had any luck. Any assistance would be greatly appreciated. Here is the CSS code: And here is ...

The alignment of my Navbar Brand is off in my Navigation Bar and won't align to

<body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container-fluid"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs ...

What could be causing these cards to not show up correctly?

I am currently incorporating Angular Material's cards in a material grid. Here is the code snippet that I am working with: http://codepen.io/anon/pen/YWwwvZ The issue at hand is that the top row of images extends off the screen at the top, and the b ...

Animate a box moving continuously from the right to the left using jQuery

I'm trying to create a continuous motion where a box moves right, then left, and repeats the cycle. However, I can only get it to complete one cycle. $(document).ready(function() { function moveBox() { $('#foo').css({ 'ri ...

Menu with dropdown navigation

Looking to create a dynamic and responsive navbar menu with items and subitems on my website. Bootstrap has proven to be challenging when adding submenus. Are there any tools, frameworks, codes or plugins that could assist me in achieving this? It's ...

Place the blockquote in the middle, but maintain the text's alignment to

I recently designed a webpage using Bootstrap and CSS. My main goal is to have my two blockquotes centered on the page while keeping the text left-aligned (which is the default behavior for the selected classes in Bootstrap). To see the full code, take a ...

Steps for adding a div after a specified number

To display the following div after getting a value greater than or equal to the specified value and retrieving it through ajax: 1. How can I show the below div with the given value? .mainbox{ width:auto; height:auto; padding:20px; background:#f00; } .i ...

Malfunctioning carousel

I've been attempting to set up a carousel, but it's not functioning properly. I'm unsure of where the issue lies. The navbar section is working fine, but the carousel isn't appearing at all. Should the carousel be placed above the navba ...

What is the best way to eliminate the unattractive white border surrounding dialog boxes?

Is there a way to remove the unsightly white outline around the UI Dialog? Check out this picture of the dialog I've tried various solutions I found on Google and Stack Overflow, such as: .ui-widget-content { border: none !important; outlin ...

My Django function doesn't get triggered by the form submission using post method

I'm facing a frustrating dilemma and issue. Currently, I am working on a form written in HTML using Django, which is meant to update information in my database related to the specific page it is on. The problem lies in the fact that the Django termi ...

Building a drop down menu with HTML and CSS: a step-by-step guide

Looking for a way to create a dropdown menu for the list item with the id "gameDesignPage" in my HTML structure: <div class="MenuContainer"> <div class="Menu"> <div class="MenuContents"> <ul> ...

Dompdf is outputting the HTML Bootstrap code as col-sm-x instead of col-lg-x

Currently, I am utilizing Dompdf in my PHP project to generate PDF reports. However, I have encountered an issue where the HTML bootstrap code does not render as expected on the PDF document. Specifically, when viewing the grid on a smaller device, it coll ...

Access SCSS variable values in Angular HTML or TypeScript files

So, I've been looking into whether it's feasible to utilize the SCSS variable value within HTML or TS in Angular. For instance: Let's say I have a variable called $mdBreakpoint: 992px; stored inside the _variable.scss file. In my HTML cod ...

Tips for inserting an image within two divs within the .row class (leveraging Bootstrap 4)

Take a look at my code snippet: <div class="row"style=""> <div class="panel panel-success col-sm-3" style="background-color: #d16b55; "> <div class="panel-body" style="height: 1000px; margin-left: 50px;overflow: hidden !important;"> ...

Issue with displaying Bootstrap modal on top of another modal

Upon clicking the "Click me here" button, a modal pops up and displays a toast message expressing gratitude. The thank you modal should remain open on top of the save modal even after clicking save. However, upon saving, an error message appears: Cannot r ...