Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force inline divs to stay on same line?

I'm trying to make a three-column layout. I'd like the width of the left and right columns to be only as wide as their children content. I'd like the center column to expand to fill the remaining space.

I'm trying the following (overview, jsfiddle link included below):

#colLeft {   display: inline;   float: left; } #colCenter {   float: left;   display: inline;   overflow: none;   white-space: nowrap; } #colRight {   display: inline;   float: right; }  <div id="parent" style="width:100%">   <div id="colLeft">left</div>   <div id="colCenter">Some really long text in the center. Some really long text in the center.</div>   <div id="colRight">right</div> </div> 

fiddle: http://jsfiddle.net/5kszQ/

but the center column pushes the right column below it when its content is too long. I'd like all three columns to be inline, and have the center column shrink as necessary. This is what the above is giving me:

enter image description here

instead I would like:

enter image description here

Thanks for any help

like image 281
user1219278 Avatar asked Mar 16 '13 03:03

user1219278


People also ask

How do I force a div to stay 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 put divs on the same line?

You can use display:inline-block . This property allows a DOM element to have all the attributes of a block element, but keeping it inline. There's some drawbacks, but most of the time it's good enough.

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

css prevent new line div // You should use a <span> element instead, which is inline. // Although it's bad form, you can add style="display: inline;" to a div.

How do I keep my div inline?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.


1 Answers

Here's one method using inline-block for the left and middle and position:absolute for the right element.

Example

jsFiddle

HTML

<div id="parent" style="width:100%">     <div id="colLeft">left</div><!--     --><div id="colCenter">Some really long text in the center. Some really long text in the center.</div>     <div id="colRight">right</div> </div> 

CSS

html, body {     margin: 0px;     padding: 0px; } #parent {     background-color: #eee;     height: 48px;     position:relative;     overflow:hidden;     white-space: nowrap; } #colLeft {     background-color: #ff8b8b;     height: 48px;     display: inline-block; } #colCenter {     background-color: orange;     height: 48px;     display: inline-block;     overflow: hidden; } #colRight {     background-color: #c3d0ff;     height: 48px;     display: inline;     float: right;     position:absolute;     top:0;     right:0; } 

Since it relys on inline-block, there is a comment in between the <div>s to get rid of the spacing illustrated in this image:

Ugly spacing

text-overflow:ellipsis

To achieve this when using text-overflow:ellipsis you may need to fallback on JavaScript, here is a possible solution (jsFiddle).

Ellipsis using JavaScript

window.onresize = updateDimensions;  function updateDimensions() {     var parent = document.getElementById('parent');     var left = document.getElementById('colLeft');     var right = document.getElementById('colRight');     var middle = document.getElementById('colCenter');      middle.style.width = (parent.offsetWidth - right.offsetWidth - left.offsetWidth)  + 'px'; } 
like image 69
Daniel Imms Avatar answered Sep 28 '22 03:09

Daniel Imms