CSS exclusive - achieving a horizontal scrolling list with all list items in a single row

I am working with a div that has a fixed width. Inside this div, there is an unordered list (ul) containing a random number of list items (li) with varying widths.

My project utilizes the Bootstrap framework.

Below is an example of the code structure:

<div class="parent">
    <div class="child">    
        <ul id="inner" class="nav nav-tabs">
            <li>Test</li>
            <li>Test</li>
            <li>Test</li>
            <li>Test</li>
            <li>Test</li>
        </ul>         
    </div>  
</div>

<div class="parent">    
   <div class="child">        
        <ul id="inner-new" class="nav nav-tabs">
            <li>Test</li>
            <li>Test</li>
            <li>Test</li>
            <li>Test</li>
            <li>Test</li>
        </ul>        
    </div> 
</div>

<style>
.parent {
    background: green;
    width: 200px;
    padding: 20px;
    overflow: hidden;
    display: inline-block;
}

.child {
    background: white;
    padding: 10px;
    overflow: auto;
}

li {
    padding: 30px;
}

#inner {
    background: yellow;
    padding: 10px;
    width: 500px;
}

#inner-new {
    background: yellow;
    padding: 10px;
    width: auto;
}
</style>

A demonstration of the above code can be viewed on jsfiddle: http://jsfiddle.net/4dvkbtpg/2/

I am seeking a solution to have all the li elements in one row, similar to the first example, without setting a fixed width for the ul element. I do not know the exact number or width of the li elements in advance, and I must achieve this using only CSS.

I have attempted various approaches involving float:left and different display properties, but have not been able to find a suitable solution. Can anyone suggest a method to accomplish this?

For better clarity, refer to this image:

Answer №1

Feel free to take a look at this example

.container{
width:100%;
}

Next, make sure to check out this example

If I understand correctly, it seems like you're looking for something similar to this alternate example

Based on the image you provided, here is the final example

Answer №2

Take a look at this new update:

http://jsfiddle.net/4dvkbtpg/3/

CSS snippet provided below:

.container {
    background-color: green;
    width: 200px;
    height:400px;
    padding: 20px;
    overflow: scroll;
    display: inline-block;
}

Answer №3

To fix it, simply delete the width property (width: 200px;) from the ".parent" class.

Answer №4

If you want to make changes to the #inner class, follow these steps:

-Include these properties

max-height: 150px; // this sets the maximum height of the specific div, showing a scrollbar if it exceeds that height overflow-y: auto; // allows scrolling once the maximum height is reached

Remove these properties

width : 500px; // this sets the width of the div to 500 px, extending automatically due to the parent's "display : inline-block" property you may need to adjust one of them to suit your needs..

.parent {
    background: green;
    width: 200px;
    padding: 20px;
    overflow: hidden;
    display: inline-block;
}

.child {
    background: white;
    padding: 10px;
    overflow-y: auto;
}

li {
    padding: 30px;
}

#inner {
  background: yellow;
  padding: 10px;
  /* width: 500px; */
  /* max-width: 200px; */
  overflow-y: auto;
  max-height: 150px;
}

#inner-new {
    background: yellow;
    padding: 10px;
    width: auto;
}

Feel free to customize the max-height to your liking.

Answer №5

It seems like I understand your predicament, as I've been stuck in a similar situation before. Avoid using ul and li elements. Take a look at this demo, or refer to the code snippet below:

.wrapper{
    width:200px;
    white-space: nowrap;
    overflow:auto;
}

.wrapper span{
  display: inline-block;
  line-height: 50px;
  margin: 0px 5px;
  padding: 0px 5px;
  border: 1px solid;
}
<div>
   <p class="wrapper">
       <span>Child 1</span>
       <span>Child 2</span>
       <span>Child 3</span>
       <span>Child 4</span>
       <span>Child 5</span>
       <span>Child 6</span>
   </p>
</div>

Answer №6

After making some changes to your code, I believe the main issue was with Bootstrap applying float:left; and display:block properties to the li element. To address this, I switched these to display: inline-block; removed the float property, and included list-style: none for li, along with white-space: nowrap; and overflow-y: hidden; which may align more closely with your requirements.

.parent {
    background: green;
    width: 200px;
    padding: 20px;
    overflow: hidden;
    display: inline-block;
}

.child {
    background: white;
    padding: 10px;
    overflow: auto;
}

li {
    padding: 30px;
    list-style: none;
    display: inline-block !important;
    float: none !important;
}

#inner {
    padding: 10px;
    white-space: nowrap;
    overflow-y: hidden;
    background: yellow;
}

#inner-new {
    background: yellow;
    padding: 10px;
    width: auto;
}

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

Guide on implementing two submission options in an HTML form using JavaScript

Currently, I am working on a form that includes two buttons for saving inputted data to different locations. However, I am facing an issue with the functionality of the form when it comes to submitting the data. Since only one submit function can be activa ...

What is the best way to adjust a <div> element so that it stays centered on the webpage while also keeping its size attributes intact?

"How To" Can you help me center a single element within a "div" on my webpage without changing its current size attributes? Here is the page I need assistance with: I would like the "Interior Painting" element under the "Residential Services" header to ...

What is the best way to style a tooltip in an ASP.NET application using CSS?

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { foreach (TableCell cell in e.Row.Cells) { ...

Ways to gradually reveal all elements within a header section

Is there a way to gradually reveal all the components in my header once the window has finished loading by utilizing jQuery's fadeIn function? Below is the pertinent code snippet: HTML <div class="banner"> <header class="header"> < ...

Why is there a CSS reference failure in Express and how can it be resolved?

Below, you'll find a simple express server I've put together: // check out my basic Express server var express = require("express"); var app = express(); var bodyparser = require("body-parser"); app.use("/public/", express.static("./public/")); ...

Can information be transferred to a CSS document?

Currently working on developing a content management system (CMS) using PHP and exploring the possibility of passing a URL to an image. In my PHP pages, I am utilizing Twig templates and the {{ image1url }} to display the image URL. Is there a way to pas ...

Tips for ensuring that jQuery is the first script to load in the header of a WordPress website

My website and plugins are inserting some jQuery code between the HTML tags, along with a jQuery dependent plugin as the first script tag. No matter what I do to insert jQuery as the first script in the head section, it doesn't seem to work. I have a ...

The Bootstrap dropdown menu is cut off and not fully visible on mobile devices

I am experiencing an issue with the mobile version of a website where the dropdown menu is not fully visible due to the position of a specific menu item and the number of items in the dropdown. https://i.sstatic.net/HmXeq.png The image below shows how I ...

Striking a balance between innovation and backward compatibility in the realm of Web Design/Development

Creating websites is my passion, and I enjoy optimizing them for various platforms, devices, and browsers. However, lately, I've been feeling frustrated. I'm tired of facing limitations in implementing my creative ideas due to outdated technolog ...

Converting SQL database tables into MVC webpages using JavaScript

Currently, I am working on populating an MVC webpage by utilizing a Controller and a View using C#, HTML, and Javascript exclusively. It is crucial that I avoid using a model or PHP due to the existing format in use. Thus far, all the data has been succes ...

Issue encountered while trying to access PHP script within table data utilizing Ajax

I have a small project that involves displaying different news articles. I need to incorporate the news_all.php file into the table data in dashboard.php without using the include('news.php) function. Instead, I am utilizing Ajax methods to achieve th ...

Learn how to efficiently apply styles to multiple objects within an array using the material-ui library

Currently, I'm utilizing the functional component of React.js. Within this component, I have an array of objects that contain specific styles. const data = [ { id: 1, color: '#000' }, { ...

Navigate to specific element from bootstrap navigation bar

I am in the process of creating a website for a small organization. The website is being constructed using bootstrap 4, and there is a navbar that connects to various flex-containers. I want these connections to smoothly scroll to their respective elements ...

What is the best way to customize the appearance of the initial row in each tr cluster?

Looking to add a button for expanding child rows within a datatable, with each group in the table having the same child elements.... The provided CSS currently displays the icon on every row throughout the entire table. td.details-control { backg ...

How can one HTML form be used to request a unique identifier from the user, retrieve a record from the database, parse it into JSON format, and then populate an HTML page form using the

As someone who isn't an expert in any specific coding language, I have a knack for piecing things together to make them work. However, my current challenge is stretching my technical abilities a bit too far. Here's what I'm trying to achieve ...

Troubleshooting Issue: jQuery Scroll Feature Unresponsive

I'm having an issue with my website where it's not scrolling down when I click on an arrow. You can see the site here. Despite trying on different divs, nothing happens when I click. I've also tested it on other sections with no success. & ...

Create two grid components for Material-UI and use Flexbox to ensure that both items have equal height

I'm currently working on ensuring that two grid items have the same height using flex in Material UI, but I've hit a roadblock. It seems like my approach with the Grid element might be incorrect. I've experimented with adjusting the height a ...

Navigating the rollout of a fresh new design

When implementing a new design on a website, how can you bypass the need for users to clear their browser cache in order to see the updated changes? Is there a way to automatically refresh the cache once a new version of the website is uploaded, or are th ...

How to swap one image for another using jQuery on click

I have a question about updating images on an HTML page using jQuery. Below is the code I am currently using: $(document).ready(function() { $("#img-1").click(function() { var imgsrc = $(this).attr('src'); $(".click-img").attr('s ...

Fluidly move through spaces where subsequent elements align to the top level

In the black container, there is an {overflow:hidden;} CSS property applied The yellow blocks are ordered in code according to the numbers and have {float:left;} I am aiming for the 5th element to align with the element above it: However, currently this ...