Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed width div on right, fill remaining width div on left [duplicate]

Tags:

html

css

Im searching for a way to have 2 divs as columns where div on right has a fixed width and div on left fill remaining space.

Does anyone happen to know if this can be done?

My attempt (renders block2 underneath block1):

<style>
.block1 {
   width: auto;
   height: 200px;

   background-color: green;
}
.block2 {
   float: right;
   height: 200px;
   width: 200px;

   background-color: red;
}
</style>

<div class="block1">test1</div>
<div class="block2">test2</div>
like image 661
Thomas Avatar asked Nov 11 '11 13:11

Thomas


People also ask

How do I make a div fill the remaining width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do I display the div content on the right side?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

How do you cover space in HTML?

To insert blank spaces in text in HTML, type &nbsp; for each space to add. For example, to create five blank spaces between two words, type the &nbsp; entity five times between the words. You do not need to type any spaces between the entities.


2 Answers

You can do it like this:

HTML:

<div class="right">right</div>
<div class="left">left</div>

CSS:

.left{
    background:red;

}
.right{
    float:right;
    width:200px;
    background:green
}

Check this live example http://jsfiddle.net/QHTeS/2/

like image 86
sandeep Avatar answered Oct 27 '22 00:10

sandeep


Float Both of the elements left:

<style>
.block1 {
   width: auto;
   height: 200px;
   float: left;
   background-color: green;
}
.block2 {
   float: left;
   height: 200px;
   width: 200px;

   background-color: red;
}
</style>

<div class="block1">test1</div>
<div class="block2">test2</div>

You should wrap them in a container as well to prevent messing up the rest of your layout. :)

http://jsfiddle.net/tcFjN/


That was wrong!

Use display: table; on parent and display: table-cell; on children:

<div id="wrapper">
    <div class="block1">test1</div>
    <div class="block2">test2</div>
</div>


#wrapper
{
    display: table;
    width: 100%;
}

.block1 {
       width: auto;
       height: 200px;
       display: table-cell;
       background-color: green;
}
.block2 {
       display: table-cell;
       height: 200px;
       width: 200px;
       background-color: red;
    }

http://jsfiddle.net/tcFjN/1/

like image 41
Kyle Avatar answered Oct 27 '22 00:10

Kyle