I am facing an issue with a set of three inline divs that need to be displayed either in a row or in a column if they do not fit within their container width. In cases where the container is too narrow for them to fit in one row, but not narrow enough to collapse into a single column, they are currently displayed as 2 on top and 1 below.
How can I ensure that they are always displayed either all inline or all in one column, considering the following constraints:
- Avoid using media queries.
- The inner divs have fixed widths in pixels.
CSS:
.container {
text-align: center;
background-color: #FFFFFF;
margin-bottom: 20px;
}
.narrow {
width: 200px;
}
.medium {
width: 400px;
}
.wide {
width: 600px;
}
.element {
width: 100px;
margin: 20px;
padding: 20px;
display: inline-block;
background-color: #FF0000;
border-radius: 5px;
}
HTML:
<div class="container narrow">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
<div class="container medium">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
<div class="container wide">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
JS Fiddle: https://jsfiddle.net/5wLeggy4/1/