Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll if element is longer than parent, center if it's shorter with CSS only?

Tags:

css

I'm in a situation where a certain element can have very short or very long content without me having any way to know which is going to be the case.

The way I would like to handle this is that if the element is shorter than its parent, I'd like the element to be centered horizontally and vertically within the parent; if the element is longer, I'd like it to be naturally positioned within the parent and to be able to scroll the parent (not the element itself) to see the content out of view. Here's a quick image I made to illustrate the case:

enter image description here

Is there anyway that I can do this with CSS only? I'll take any solution that is compatible with IE11.

What I tried

So far I tried centering the element within the parent with flexbox and adding overflow: scroll to the parent, but when the content is longer than the parent it still gets centered and gets cut both on top and at the bottom...

#parent {
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: scroll;
}
like image 361
grazianodev Avatar asked Sep 11 '25 16:09

grazianodev


1 Answers

I think this is what you are looking for:

* { box-sizing: border-box; } body { margin: 0; background: #ddd; }
.parent {
  display: flex;
  flex-wrap:wrap;
  height: 80vh;
  max-height: 100vh;
  width: 40%;
  margin: 5%;
  background: white;
  float: left;
  padding: 15px;
  overflow: auto;
  align-items:center;
  justify-content: center; /*Horizontally centers child in this case*/
}
.child {
  background: #eee;
  padding: 15px;
  max-height: 100%;
  overflow: auto;
  /* width: 100%;  you could give full width as well */ 
}
<div class="parent">
  <div class="child">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.</p>
  </div>
</div>
<div class="parent">
  <div class="child">
    <p>Dummy text</p>
  </div>
</div>

Update

What you needed to solve?

Ans: You needed was to give your child maximum height limit, max-height:100%, so that it won't get cut out at top and bottom.

Also you need to give the child overflow: auto no need for parent. Not overflow: scroll, as we don't need scroll tracks for child that doesn't need scrolls.

Codepen link to play around


Hope this helped you!

Feel free to ask, if any queries. :)

like image 138
divy3993 Avatar answered Sep 13 '25 06:09

divy3993