I have three <svg>
s side by side in a container <div>
(inside a container <div>
).
Each <svg>
should always take up one-third of its immediate parent's width and have a height equal to its width.
This means that as the window is resized, and the <svg>
s' width become smaller or larger, so should their heights, and so should the height of the container <div>
.
#container {
background-color: grey;
}
.svg-wrapper {
background-color: red;
display: inline;
}
svg {
background-color: white;
width: 33%;
}
<div id="container">
<div class="svg-wrapper" id="sw1">
<svg id="svg1"></svg>
</div>
<div class="svg-wrapper" id="sw2">
<svg id="svg2"></svg>
</div>
<div class="svg-wrapper" id="sw3">
<svg id="svg3"></svg>
</div>
</div>
On the advice of this answer, I tried adding .svg-wrapper { padding-bottom: 100% }
, but to no avail.
EDIT: Basically, two conditions should always be satisfied:
- The three
<svg>
s should always be square: their height is equal to their width - The width of each
<svg>
should be one-third of their parent's width.
When the width of the parent changes because the page is resized, the width of each <svg>
should also change so that it remains one-third of the parent. The height of each <svg>
should also change, by the same amount, so height and width are always equal.
Voted to Close: My question was really the same as this. I didn't see the part of Web_Designer's great answer that specified making the position of the wrapper relative and the position of the inside-div absolute.