Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to force horizontal scrolling in an HTML list using CSS?

Tags:

I have a list like this:

<div>    <ul>      <li>one</li>      <li>two</li>      <li>three</li>      <li>four</li>    </ul>  </div> 

and the following CSS:

ul {       width: 160px;      height: 100px;      overflow: auto;       } li {       width: 80px;      display: inline-block;      float: left       } 

I'm trying to force the list items to display from left to right, that is

 one - two - three - four 

My problem:
Doing it like this gives me two rows with two items each.

Question:
Is there a CSS way to force the list items to all be in a single row so I can use horizontal scrolling? Right now if I set overflow:auto I'm only getting vertical scrollbars, which I don't want.

I don't want to set this on the wrapping div. I'm just curious if there is a CSS solution I can use within the list alone.

Thanks for help!

like image 468
frequent Avatar asked Mar 14 '12 18:03

frequent


People also ask

How do you horizontally scroll in HTML CSS?

To enable horizontal scrolling, we can use the CSS property overflow-x. If we assign the value scroll to the overflow-x property of the container element, the browser will hide horizontally overflowing content and make it accessible via horizontal scrolling.

How do I make a horizontal scroll bar in CSS?

For horizontal scrollable bar use the x and y-axis. Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line.

How do I enable horizontal scrolling?

Basic Horizontal Scroll Box To make a scroll box with a horizontal scroll, you need to use the overflow-x property. Specifically, you need to use this code: overflow-x:scroll; . This tells your browser to create scroll bars on the x (horizontal) axis, whenever the contents of the container is too wide.

How do you make a list scrollable in HTML?

(1) List all of your items in individual div s. (2) Put all items into a containing div , and limit the height of that div to a fixed size. (3) Then, style the containing div to set overflow: scroll .


1 Answers

You can't really scroll floated content. Once it's floated, it's not calculated in the width or height of the parent container by default. Really the <ul> is just expanding to its set width and then not doing anything else.

Removing the float: left will make them scrollable. The only problem you'll have then is that there is the extra "space" between each inline-block. You can remove that by removing the line-breaks between each list item. It's not the prettiest thing. Normally I'd use a font-size: 0 and then reset the font-size in the list item.

You also need to make sure the items don't wrap to a new line when they hit the width of the element.

jsFiddle Examples:

  • Removing line breaks
  • Using font-size: 0
like image 167
animuson Avatar answered Nov 02 '22 01:11

animuson