Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to give a max-height to flex-direction column?

I want to give a max-height to the flex-box when below 700px. I tried overflow:hidden which shows that max-height is working but I’m not able to figure out why it isn’t being applied. How can I achieve it?
Any help is much appreciated.

*{
  margin:0;
  padding:0;
  box-sizing: border-box;
}
.inner-div{
  display: flex;
  max-height: 250px;
}
.img-container{
  flex:1;
}

img{
  width:100%;
  height: 100%;
  object-fit: cover;
  flex:1;
  display: block;
}
.text{
  background: silver;
  padding: 2em;
  flex:1;
}
@media only screen and (max-width: 700px) {
  .inner-div{
    flex-direction: column;
  }
}
<div class="outer-div">
  <!-- child div below -->
  <div class="inner-div">
    <div class="img-container">
      <img src="https://images.ctfassets.net/hrltx12pl8hq/3AnnkVqrlhrqb9hjlMBzKX/693a8e5d40b4b6c55a7673ca4c807eef/Girl-Stock?fit=fill&w=480&h=270" />
    </div>
    <div class="text">
      <h2>hello</h2>
      <h3>subheading</h3>
    </div>
  </div>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The problem come from your image. You should add overflow:hidden; to your image container.

After if you want to align your image, you can simply add display: flex; align-items: center; to your .img-container.

So add to your media query:

/* ADDED */
  .img-container{
    overflow:hidden;
    display: flex;
    align-items: center;
  }

DEMO

*{
  margin:0;
  padding:0;
  box-sizing: border-box;
}
.inner-div{
  display: flex;
  max-height: 250px;
}
.img-container{
  flex:1;
}

img{
  width:100%;
  height: 100%;
  object-fit: cover;
  flex:1;
  display: block;
}
.text{
  background: silver;
  padding: 2em;
  flex:1;
}
@media only screen and (max-width: 700px) {
  .inner-div{
    flex-direction: column;
  }
  /* ADDED */
  .img-container{
    overflow:hidden;
    
    display: flex;
    align-items: center;
  }
}
<div class="outer-div">
  <!-- child div below -->
  <div class="inner-div">
    <div class="img-container">
      <img src="https://images.ctfassets.net/hrltx12pl8hq/3AnnkVqrlhrqb9hjlMBzKX/693a8e5d40b4b6c55a7673ca4c807eef/Girl-Stock?fit=fill&w=480&h=270" />
    </div>
    <div class="text">
      <h2>hello</h2>
      <h3>subheading</h3>
    </div>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading