Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep child divs on one line?

I've tried every suggestion I could find (most of them involving white-space:nowrap and display:inline-block) but so far nothing has worked to get these child divs to stay on one line and scroll horizontally.

My code:

<div id="list">
    <a href="javascript:show('shown','id1','a1');"><div id="a1" class="inactive">link1</div></a>
    <div id="spacer"></div>
    <a href="javascript:show('shown','id2','a2');"><div id="a2" class="inactive">link2</div></a>
    <div id="spacer"></div>
    <a href="javascript:show('shown','id3','a3');"><div id="a3" class="inactive">link3</div></a>
    <div id="spacer"></div>
    <a href="javascript:show('shown','id4','a4');"><div id="a4" class="inactive">link4</div></a>
    <div id="spacer"></div>
    <a href="javascript:show('shown','id5','a5');"><div id="a5" class="inactive">link5</div></a>
    <div id="spacer"></div>
    <a href="javascript:show('shown','id6','a6');"><div id="a6" class="inactive">link6</div></a>
</div>

Essentially this is a navigation bar for mobile devices that scrolls horizontally.

The normal version has this bar vertical (working fine) and the "spacer" div is used as a divider, switching from a horizontal rule to a vertical rule.

like image 507
Calvin P. Avatar asked Apr 29 '14 00:04

Calvin P.


People also ask

How do you keep divs on the same line?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.

How do I stop div from going to the next line?

Set size and make inline Because they're block elements, when reducing the size of Div one to make room for the other div, you're left with space next to Div one and Div two below Div one. To move the div up to the next line both div's need to have the inline-block display setting as shown below.

Can 2 divs be in the same line?

By default, if we have multiple div tags it will be displayed one below the other. In othere words, each div will be displayed in a separate line. To display multiple div tags in the same line, we can use the float property in CSS styles. The float property takes left, right,none(default value) and inherit as values.


1 Answers

You're looking for white-space: nowrap;

http://jsfiddle.net/ySMdY/1/

#list {
    white-space: nowrap;
}
#list a, #list a div, #list .spacer {
    display: inline-block;
}
#list a {
    /* just some styles so I can see it working */
    background-color: rgba(0, 0, 0, 0.5);
    padding: 0 50px;
}

ALSO: IDs are supposed to unique per page. You can't have multiple #spacer divs, you should only have one. If you want multiple, class is the way to go.

like image 105
Ming Avatar answered Sep 17 '22 15:09

Ming