I have a container that I don't want to set as inline-block and that contains inline-block elements. These elements will overflow out of the container instead of extending it. How can I manage to prevent this behaviour ?
Demo
#inline-block-container {
background-color: red;
white-space:nowrap;
}
.inline-block-element {
background-color: blue;
width: 50px;
height: 30px;
margin: 10px;
display:inline-block;
}
In this demo, I would like the red rectangle to expand (even out of viewport) to contain (visually) all the blue rectangles.
-Edit-
On my real page :
You can see the background (gradient light gray) stopping at the right border of the window (before scrolling to the right). There is also a bootstrap tooltip (in black) that is wrongly positionned and same for a context menu. The container, the body and the html tag won't extand outside of the initial view because the inline-block elements overflow outside of their container.
I can't set the size in css because the content of the elements (dimgray) can change.
I tried to set the container as display:table : same result. I tried position:absolute : it brakes things and doesn't solve the issue. I tried inline-flex : same result.
Thanks in advance.
Best regards,
The default value of overflow is visible . With this default, we can see content when it overflows. To crop content when it overflows, you can set overflow: hidden . This does exactly what it says: it hides overflow.
An inline element does not start on a new line and it only takes up as much width as necessary. The <div> element is a block-level and is often used as a container for other HTML elements. The <span> element is an inline container used to mark up a part of a text, or a part of a document.
The display: inline-block Value Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.
Positioning the parent div absolutely will enable this but this may not be suitable for your requirements.
#inline-block-container {
background-color: red;
white-space: nowrap;
position: absolute;
width: auto;
}
#inline-block-container {
background-color: red;
white-space: nowrap;
position: absolute;
width: auto;
}
.inline-block-element {
background-color: blue;
width: 50px;
height: 30px;
margin: 10px;
display: inline-block;
}
<div id="inline-block-container">
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
</div>
Or as mentioned by Nenad Vracar...display:table
#inline-block-container {
background-color: red;
white-space: nowrap;
display: table;
width: auto;
}
#inline-block-container {
background-color: red;
white-space: nowrap;
display: table;
width: auto;
}
.inline-block-element {
background-color: blue;
width: 50px;
height: 30px;
margin: 10px;
display: inline-block;
}
<div id="inline-block-container">
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
</div>
Or as also mentioned by Nenad Vracar...display:inline-flex
#inline-block-container {
background-color: red;
white-space: nowrap;
display: inline-flex;
width: auto;
}
#inline-block-container {
background-color: red;
white-space: nowrap;
display: inline-flex;
width: auto;
}
.inline-block-element {
background-color: blue;
width: 50px;
height: 30px;
margin: 10px;
display: inline-block;
}
<div id="inline-block-container">
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
<div class="inline-block-element"></div>
</div>
Essentially, these methods are virtually indistinguishable from using display:inline-block
and it's "shrink to fit" properties. The line boxes and white-space:nowrap
work in basically the same way to maintain the background color.
Knowing why you are averse to inline-block
would go a long way to identifying the most suitable alternative.
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