Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I hide the element if it doesn't fit the available width using css only?

Tags:

html

css

I have a header and an image that should be horizontally laid out on the absolutely positioned div. I'm showing the text in the subdiv with the fixed width. I'm trying to hide the image if it doesn't fit into the absolutely positioned container by the means of CSS3, but I can't. I know it's possible to do with JS, but maybe there is a CSS3 way?

.container {
  display: -ms-flexbox;
  -ms-flex-direction: row;
  -ms-flex-pack: justify;
  overflow-x: hidden;
  position: absolute;
  left: 60px;
  right: 60px;
  bottom: 0px;
  height: 400px;
}

.part-one {
  left: 0px;
  bottom: 100px;
  width: 300px;
  font-size: 22pt;
  color: white;
  min-width: 300px;
}

.part-two {
  max-width: 400px;
  width: 400px;
  min-width: 50%;
}

.header {
  font-size: 50pt;
  color: blue;
  margin-bottom: 10px;
}
<div style="position: relative; width: 500px; height:500px;">
  <div class="container">
    <div class="part-one">
      <h1 class="header">This is some header</h1>
    </div>
    <div class="utilizationContainer" class="part-two">
      <img src="">
    </div>
  </div>
</div>

here is the fiddle: http://jsfiddle.net/8r72g/5/

It's ok if it works only in IE10.

like image 749
Shalom Aleichem Avatar asked Sep 13 '13 00:09

Shalom Aleichem


People also ask

How do I completely hide an element in CSS?

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>

How do you hide an element in CSS without affecting the layout?

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 .

How can you hide an element but still keep its space in document?

visibility:hidden hides the element, but it still takes up space in the layout. display:none removes the element from the document. It does not take up any space.

How do I hide elements based on screen size?

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.


1 Answers

I've found how this can be done. If I use display:block on the .container and float:left in the inner containers, the image will be wrapped if it doesn't fit the width. And if I use overflow: hidden, the image will be moved under the text and hidden.

Here is the fiddle that proves this trick works: http://jsfiddle.net/8r72g/8/

like image 88
Shalom Aleichem Avatar answered Sep 17 '22 15:09

Shalom Aleichem