This is bit hard to explain but am trying to size an SVG in my page so that it can grow in width according to the width of the container but only so far as it doesn't exceed a certain percentage of the page vh. This part works, but am also trying to lay this out with a side bar, and whenever the max height restriction starts to kick in left and right padding is introduced in the SVG, whereas I want the layout to be as compact as possible in this situation, avoiding "letterboxing" of the SVG.
The requirements for the SVG are:
- Preserve aspect ratio
- Use 100% width of container but do not exceed given max height
- Reduce width (of element) when height is restricted
#container {
width: 800px;
margin: 0px auto;
text-align: center;
background: #aaaaaa;
}
#flex {
display: inline-flex;
align-items: stretch;
background: red;
}
#main {
flex-grow: 1
}
svg {
background: green;
width: 100%;
max-height: 75vh;
}
#sidebar {
background: blue;
color: white;
min-width: 100px;
}
<div id="container">
<div id="flex">
<div id="main">
<svg viewBox="0 0 870 690">
<rect x="0" y="0" width="100%" height="100%" fill="purple" />
</svg>
</div>
<div id="sidebar">
SIDEBAR
</div>
</div>
</div>
View when vertical height restriction not exceeded:
View when vertical height restriction in force:
I would like to avoid the green vertical bars and have the SVG and sidebar take up less horizontal space but still be centered in their container.
Am not sure this behaviour is really to do with it being an SVG, and also am pretty sure setting width 100% on the SVG element is part of the issue, but if I don't set a width it has zero width and doesn't appear at all!
Help appreciated.