I have a fixed-size inner div within a variable-width outer div.
#outer { width: 100%; }
#inner { width: 80px; }
<div id="outer">
<div id="inner"></div>
</div>
I want the inner div to be placed inside outer div as follows:
[Npx of empty space][80px fixed-size inner div][2Npx of empty space]
where N is some number. As the window size changes, N will change, but the right will remain twice the size of the left. Is it possible to do with CSS?
Required Margins: The top, bottom, and right margins are required to be 1 inch, but the left margin can either be 1 inch or 1.25 inches.
The syntax for the CSS margin property (with 2 values) is: margin: top_bottom left_right; When two values are provided, the first value will apply to the top and bottom of the element. The second value will apply to the left and right sides of the element.
Left is the position of your entire element, margin-left is the amount of your left margin. For example if your are not in a normal document flow, your left margin is not going to let anything occupy that space.
As an alternative to Mr Lister's positioning option we can use calc
on the margins.
#outer {
width: 100%;
}
#inner {
width: 80px;
height: 35px;
background: rebeccapurple;
margin-top: 1em;
margin-left: calc((100% - 80px) / 3);
margin-right: calc((100% - 80px) / (3 * 2));
}
<div id="outer">
<div id="inner">
</div>
</div>
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