I have a main wrapper div that is set 100% width. Inside that i would like to have two divs, one that is fixed width and the other that fills the rest of the space. How do i float the second div to fill the rest of the space. Thanks for any help.
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
- Adding a timeout to a render function in ReactJS
CSS3 introduced flexible boxes (aka. flex box) which can also achieve this behavior.
Simply define the width of the first div, and then give the second a
flex-grow
value of1
which will allow it to fill the remaining width of the parent.jsFiddle demo
Note that flex boxes are not backwards compatible with old browsers, but is a great option for targeting modern browsers (see also Caniuse and MDN). A great comprehensive guide on how to use flex boxes is available on CSS Tricks.
This will be cross browser compatible. Without the margin-left you will run into issues with content running all the way to the left if you content is longer than your sidebar.
I don't know much about HTML and CSS design strategies, but if you're looking for something simple and that will fit the screen automatically (as I am) I believe the most straight forward solution is to make the divs behave as words in a paragraph. Try specifying
display: inline-block
You might or might not need to specify the width of the DIVs
If you're not tagetting IE6, then float the second
<div>
and give it a margin equal to (or maybe a little bigger than) the first<div>
's fixed width.HTML:
CSS:
The margin accounts for the possibility that the 'rest-of-space'
<div>
may contain more content than the 'fixed-width'<div>
.Don't give the fixed width one a background; if you need to visibly see these as different 'columns' then use the Faux Columns trick.
You can use CSS grid to achieve this, this is the long-hand version for the purposes of illustration:
Or the more traditional method using float and margin.
I have included a background colour in this example to help show where things are - and also what to do with content below the floated-area.
Don't put your styles inline in real life, extract them into a style sheet.
Give the first div a float left and fixed with, the second div 100% width an float left. That should do the trick. If you want to place items below it you need a clear:both on the item you want to place below