I have two <div>
elements, one next to the other. Both have the CSS attribute display: inline-block;
. Now the second <div>
element has a fixed width of 100 px, whereas the first <div>
element doesn't have a fixed width: it depends on the size of the window.
My problem is that the first <div>
element will spread over 100% vertically if the window is narrow. I would like to restrict its width to 100% minus 100px, so that both <div>
elements can align one next to the other at all times.
I've looked at posts with similar questions, but none really dealt with the case of inline-block
.
EDIT: Here is a jsfiddle: http://jsfiddle.net/y3sXu/ I want the first <div>
to provide some room for the second <div>
on the same line.
There's no particular reason to use display: inline-block
to do this.
Here's a clean implementation using floats instead: http://jsfiddle.net/y3sXu/14/
<div id="container">
<div id="DivB">b</div>
<div id="DivA">a</div>
</div>
#container {
overflow: hidden;
}
#DivA {
overflow: hidden;
}
#DivB {
float: right;
width: 100px;
}
This is an old question but has some weight in Google so I thought I'd update it with a new answer. A more modern way to accomplish this is to stick with display:inline-block;
and use calc
for the width of the variable element.
So long as you have one fixed width inline element width: 150px
, you can offset the variable width element by the fixed width calc(100% - 150px)
.
Your code should look like this:
.fixed-width-element {
display: inline-block;
width: 150px;
}
.variable-width-element {
display: inline-block;
width: calc(100% - 150px);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With