Suppose I have an outer container of unknown fixed width, and an inner element, like so:
<div id="outer"><div id="inner">hide me when #outer is too small</div></div>
Is there a way I can make #inner
entirely hidden (not just clipped) when #outer
is not wide enough to show it in its entirety using pure CSS?
The following style rule hides an element on a web page: display: none; When you set the value of display to none, the affected element will disappear. This means the element will no longer take up any space on the web page.
Completely hiding elements can be done in 3 ways: via the CSS property display , e.g. display: none; via the CSS property visibility , e.g. visibility: hidden; via the HTML5 attribute hidden , e.g. <span hidden>
To hide an element in a responsive layout, we need to use the CSS display property set to its "none" value along with the @media rule. The content of the second <p> element having a "hidden-mobile" class will be hidden on devices smaller than 767px.
CSS Demo: visibility To both hide an element and remove it from the document layout, set the display property to none instead of using visibility .
This is possible without JS through floats and adding a helper inner element:
<div class="outer">
<div class="helper"></div>
<div class="inner">hide me when .outer is too small</div>
</div>
and css:
.outer {
overflow: hidden;
}
.helper {
width: 1px;
height: 100%;
float: left;
}
.inner {
float: left;
}
The inner element will now wrap under the helper if it doesn't fit within the width of the outer element, which with .helper
having height: 100%
and .outer
having overflow: hidden
results in the inner element not being visible.
You could remove the 1px helper width by using width: 0.01px
instead, but I'm not sure if that runs into browser compatibility issues.
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