If you try to give margin-left
to C1 div, it moves and overflow is hidden. But if you try to give margin-left
to C2 div, it moves towards right, but overflow is not hidden, rather it moves in next line (behavior of inline-block
).
So why is it not working on C2 div? Is there any way to solve this problem?
(Basically I want C1 and C2 div to be placed together and overflow should be hidden if I increase their widths, or if I give them margins).
Here's what I'm trying:
.c1 {
width: 220px;
height: 200px;
background-color: #666666;
display: inline-block;
}
.c2 {
width: 200px;
height: 220px;
background-color: #CCCCCC;
display: inline-block;
}
.c3 {
width: 180px;
height: 210px;
background-color: #333333;
display: block;
}
.wrapper {
background-color: red;
width: 500px;
height: 500px;
display: inline-block;
overflow: hidden;
}
<div class="wrapper">
<div class="c1">C1</div>
<div class="c2">C2</div>
<div class="c3">C3</div>
</div>
What is the fix? Start by opening the developer console and inspect what it is you are working with and/or against. In this case, the solution was as easy as targeting the body element as well as the child div content wrapper and applying css overflow: hidden styles to both.
Making a div vertically scrollable is easy by using CSS overflow property. There are different values in overflow property. For example: overflow:auto; and the axis hiding procedure like overflow-x:hidden; and overflow-y:auto;.
To change this, set the min-width or min-height property.” This means that a flex item with a long word won't shrink below its minimum content size. To fix this, we can either use an overflow value other than visible , or we can set min-width: 0 on the flex item.
Use overflow-x : hidden and overflow-y : scroll , or overflow: hidden scroll instead. Use overflow: clip instead. As of Firefox 63, -moz-scrollbars-none , -moz-scrollbars-horizontal , and -moz-scrollbars-vertical are behind a feature preference setting.
Add white-space: nowrap
to the container (.wrapper
).
white-space
The
white-space
property is used to describe how whitespace inside the element is handled.
nowrap
Collapses whitespace as for normal, but suppresses line breaks (text wrapping) within text.
source: https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
To understand why, with white-space: normal
, C2 wraps but C1 does not, see these posts:
Here's an excerpt from an answer by @BoltClock:
The value of
overflow
on a container doesn't influence whether or when its contents overflow; it only changes how it and its contents are rendered, when overflow does occur.So you have to force the inline-blocks to actually overflow the container.
Since an inline-block has the same rigid physical structure as a block container box, it's impossible for an inline-block to "break apart" or wrap when it's the only inline-level box on a given line box.
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