How can I create a horizontal list within a vertical list, while also incorporating a vertical list within a horizontal list using CSS?

I'm facing a challenge with two lists, one vertical and the other horizontal. I need to wrap one list inside the other, but the user can decide this at runtime. Setting the lists as display:block or display:inline-block doesn't seem to work in this case. Below is an example of the HTML structure:

<ul id="list1">
    <li>
        <ul id="list2"></ul>
    </li>
</ul>

There are also two classes available for the lists - .horizontal and .vertical with specific CSS rules.

.horizontal li {
    display: inline-block;
}

.vertical li {
    display: block;
}

The issue arises when the user assigns these classes at runtime. The outermost class seems to override the innermost class, impacting the layout of the nested list. How can I ensure that the CSS properties of the inner list remain intact regardless of which class is assigned by the user?

Answer №1

Instead of assigning classes to the outer list, consider using this approach

.horizontal li,
.vertical li li {
  display:inline-block;
  vertical-align:top;
}

.vertical li,
.horizontal li li {
  display:block;
}
<ul class="horizontal">
  <li>
    this is horizontal outer
    <ul>
      <li>this is vertical inner</li>
      <li>
        1 inner
      </li>
      <li>
        2 inner
      </li>
      <li>
        3 inner
      </li>

    </ul>
  </li>
  <li>
    1 outer 
  </li>
  <li>
    2 outer
  </li>
  <li>
    3 outer
  </li>
</ul>
<ul class="vertical">
  <li>
    this is vertical outer
    <ul>
      <li>this is horizontal inner</li>
      <li>
        1 inner
      </li>
      <li>
        2 inner
      </li>
      <li>
        3 inner
      </li>

    </ul>
  </li>
  <li>
    1 outer 
  </li>
  <li>
    2 outer
  </li>
  <li>
    3 outer
  </li>
</ul>

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

Using .htaccess to Conceal Directories with Documents

It seems that my website is being targeted by individuals seeking to obtain all the code. I have implemented an .htaccess file that will display nothing when someone visits domain.com/images, but if they specifically request domain.com/images/logo.png, the ...

Creating a dynamic scene with three.js within an HTML div element

I've been searching for solutions but can't seem to figure it out for this particular scenario. https://codepen.io/rkimo/pen/vdwxJj Essentially, I just want to include this blob in a div so that I can add content before and after it and be able ...

Navigate only within the tbody section of the Material UI Table by scrolling

Utilizing the material-ui Table component to build a table with a sticky header presents a challenge. The default behavior is for the scroll to encompass both the thead and tbody of the table. However, I specifically require the scroll functionality to be ...

There appears to be an unusual white line running along the right side of the page

While working on a document, I noticed an odd white line appearing in the right margin of the page. Despite setting the width to 100% or using margins to center the content, this line persists and I can't figure out why. I've read suggestions on ...

Choosing elements in jQuery

Having some trouble with the subnav on my current website project. I think the issue lies in how I am selecting items in my jquery code. It seems like a small fix that needs to be made, but I'm unsure of the correct approach. http://jsfiddle.net/ZDEr ...

The selector By.PARTIAL_LINK_TEXT in Selenium is failing to meet the expected condition

I am trying to store the friend list of a Facebook ID. When I click on the Friends tab on the profile, the friend list should be opened if it's not blocked; otherwise, something else should open. To determine if the friend list is opened or blocked, ...

In HTML, the size and position of an <a> element cannot be altered or adjusted

I am having trouble with an anchor element that is contained within a div. No matter what I try, I cannot resize or move it up or down without it covering its sibling element. I have attempted using transform-translate and margin properties, but to no avai ...

Guide to animating the height of a div using jQuery:

I'm hoping to create a cool animation effect where a certain part of a div expands to 100% width when clicked, and then reverts back to its original height on a second click. I've tried writing some code for this, but it's not working as exp ...

Spacing between division elements and a footer that is positioned with float property

I have searched extensively, but none of the solutions I've found address my specific issue. There is a persistent 14px gap between vertical DIV elements on my page. Even when inspecting the code, there seems to be no margin or padding causing this g ...

Is there a way to get rid of the "bouncing effect" on my button while using an inline floating-label text input?

When an input with a floating label (Bootstrap 5) is inline with other elements, the elements may appear to jump up and down depending on the position of the floating label. https://i.sstatic.net/kDxBo.gif <link href="https://cdn.jsdelivr.net/npm/bo ...

What is the best way to display my icon <div>s side by side instead of on top of each other?

I am facing an issue with the following code: <a href="#"><div class="iconFriends"></div></a> <a href="#"><div class="iconFavorite"></div></a> <a href="#"><div class="iconPM"></div></a&g ...

Show off the images once they have finished loading completely?

Is there a way to ensure that the image is shown on the webpage only once it has been completely loaded? <ul id="listcontainer"> <li class="li1"> <img src="images/m1.png"> </li> </ul> ...

selection menu and advancement gauge

While working on my code, I have a task where I need to make the progress bar move a specific amount when a name is clicked based on the option's value. <!DOCTYPE html> <html> <head> <title>testinggg</title> &l ...

"Creating a fixed header with jQuery by manipulating the offset and scrollTop

My goal is to create a smooth scrolling website that scrolls vertically using jQuery. I found a helpful tutorial called Smooth Scrolling Website and have been following it closely to build my site. However, I'm facing an issue with a fixed header - t ...

Header fixed vertically

I am currently working on a webpage with a minimum width requirement of 1124px. This means that whenever the browser window is smaller than that, the content of the page needs to be scrolled horizontally instead of smoothly transitioning into a responsive ...

Can someone help me understand how to change the structure in order to identify which class has a body?

I have a small example to share with you: link HTML CODE: <div class="body"> <div class="test">TEST</div> </div> JS CODE: switch (className) { //instead of n, I need to write className case body: alert("my cla ...

Is there a way to save an HTML page and its accompanying files on an Android device?

I am currently working on a project that requires downloading the source code of a webpage along with all internal files such as images, CSS, and JavaScript from a given link. Once downloaded, I will need to open this HTML file in a webview while offline. ...

What is the correct way to properly insert a display none attribute

I'm experiencing some alignment issues with the images in my slideshow. I used this example as a reference to create my slide: https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_dots When clicking on the next image, it seems to mo ...

When clicking on ASP.NET sidebar styles, they continuously refresh

This is the code snippet I have in the sidebar: <div class="sidebar w3-collapse" id="showside"> <div class="sidebarbg"> <center> <img id="image" class="w3-round w3-margin-rig ...

Looking to adjust the positioning of text being inserted into a column - unable to utilize text-align or float properties

I'm struggling to position my text in a specific area within a column using CSS. Our app displays a price when a quantity is selected, but I can't seem to center the $14 under 'Item Total'. Even using float: center; hasn't worked f ...