I have two divs inside a wrapper div of width 880px.
The first div contains an image which can be any width, I’d like the second width to scale with it i.e:
Div 1 = 300px
Div 2 = 580px << this div scales to fit the parent div.
Setting a width of 100% on the second div ignores the fixed width on the parent div.
Is this possible using only css or would I have to use javascript/jquery (jquery is already loaded so it’s not too big a hassle)?
Thanks!
With CSS, you can only set dimensions that are directly relative to an element’s content, one if its parents, the font size, or to the screen resolution, but not to the dimensions of its siblings.* This means that, in order for the second block to be the same width as the image, it should “inherit” the width from a parent, or have it set dynamically. For example:
The
floatproperty on the#wrapperblock makes its width fit its contents. The width of the#fits_imgblock isautoor100%by default (since it’s adivelement), so its width will fit#wrapperand therefore be indirectly relative to the width of the image.If you can’t make the
#wrapperelement float, you could also mirror the width with jQuery like so:* Note that there are also absolute units like ‘cm’, but they’re relatively useless and rarely used.