Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep floating divs on same line

How do i keep two elements in the same row with fixed right column? I want right div to be with fixed size, and left column fluid, but when in insert long text to left one, then right one goes to the next column..

Example: http://jsfiddle.net/Jbbxk/2/

Is there any pure CSS solutions?

NB! Wrap div must have dynamic width! For demostration purposes it has fixed witdh, so it will wrap.

Cheers!

like image 435
Kristian Avatar asked Jul 12 '12 11:07

Kristian


People also ask

How do I 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.

How do I keep HTML elements on the same line?

To get all elements to appear on one line the easiest way is to: Set white-space property to nowrap on a parent element; Have display: inline-block set on all child elements.


1 Answers

This is one common way of doing what you want:

.wrap {
    position: relative;
    margin: 5px;
    border: 1px solid red;
}
.left {
    float: left;
    background-color: #CCC;
    margin-right: 48px;
}
.right {
    position: absolute;
    top: 0;
    right: 0;
    width: 48px;
    height: 48px;
    background-color: #888;
}

Explanation:

  • The fluid left column fills the whole width but saves space for the right column with margin-right: [right column width];
  • The fixed right column is placed at an absolute position at top 0, right 0 (its correct place)
  • The wrap div is assigned position: relative so the right column position is determined according to it.
like image 149
pacha Avatar answered Sep 25 '22 05:09

pacha