Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering Items with Flexbox and Overflow

Tags:

html

css

flexbox

Problem Summary

My desired layout is to be able to center (both vertically and horizontally) an image of unknown size on the page. If the image is too big to fit in either direction, I want to show scroll bars so the user can scroll to see the full image. The issue I've run in to is that when the image is too big to fit, the top and left of the image (depending on what portion is cut off) will never be able to be scrolled to.

Attempted Solution

I am attempting to use flexbox to achieve the desired layout, but flexbox is not a requirement. Here is a small example that reproduces the problem (note that I haven't put any browser prefixes in the CSS, so you will want to view this in Chrome [or maybe Firefox, too?]):

.body {
  height: 600px;
}

.container {
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid red;
  height: 100%;
  overflow: auto;
}

img {
  border: 5px solid black;
}
<div class="body">
  <div class="container">
    <img src="http://placehold.it/700x700" />
  </div>
</div>

You should be able to see the border completely around the image, but the left and/or top part of the image will get cut off as the window shrinks. More and more of the image will become un-viewable as the window continues to shrink.

Note that it doesn't seem to matter if there is an image in there. Here's a snippet just using plain-old divs to show the problem:

.body {
  height: 600px;
  margin-top: 80px;
}

.container {
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid red;
  height: 100%;
  overflow: auto;
}

.content {
  border: 5px solid black;
  width: 600px;
  height: 600px;
  background-color: gray;
}
<div class="body">
  <div class="container">
    <div class="content"></div>
  </div>
</div>

Again the entire div should always be reachable by scrolling, but it is not.

Question

How can I achieve the desired layout described above using only HTML and CSS (JS answers not accepted)? Flexbox is not required to solve the problem, but it would be a nice-to-have.

Thanks!

like image 749
Chandler Kent Avatar asked Sep 11 '15 16:09

Chandler Kent


People also ask

How do you center a div vertically and horizontally with flexbox?

To center a div vertically and horizontally using flexbox, you need to wrap the div or div's inside a container with properties ' display: flex; flex-direction: column; justify-content: center;align-items: center; ', then just make the div ' text-align: center; ' if it has text.

Can't scroll to top of flex item that is overflowing container?

To fix this problem use flexbox auto margins, instead of justify-content . With auto margins, an overflowing flex item can be vertically and horizontally centered without losing access to any part of it.

How do I center elements vertically in Flexbox?

The power of Flexbox really shines when we also need to center elements vertically. Here’s our example, but with the flex items also centered vertically: arrr! yeehaw! If you just want specific flex items to be centered vertically, you can set align-self on the items instead: arrr! yeehaw!

How to justify centring Flexbox items using justify-content?

Centring flexbox items using justify-content results in a quriky behaviour when the content overflows the container. Even after scrolling, some items may not be fully visible. Don't use justify-content on the container. Add margin-left: auto; to the first child item. Add margin-right: auto; to the last child item. <!--

How do I Center a box horizontally in HTML?

To get the box to center horizontally, we need to set the parent container to display: flex;. Then we can use justify-content to center horizontally! justify-content is the X axis and the property name we need for the Y axis is align-items.

What is a Flexbox in CSS?

Flexbox is just a new CSS display type that is designed to help us craft those CSS layouts much much easier. It lets us control the position of elements the size of them, and the spacing of them relative to their parent container elements and each other, and it also works great responsively too.


1 Answers

Just remove the justify-content from your .container and add a margin: auto to your image.

.body {
  height: 600px;
}

.container {
  margin: auto;
  display: flex;
  /* align-items: center; // no need for this anymore */
  /* justify-content: center; // remove this */
  border: 1px solid red;
  height: 100%;
  overflow: auto;
}

img {
  border: 5px solid black;
  margin: auto;  /* add this */
}
<div class="body">
  <div class="container">
    <img src="http://placehold.it/700x700" />
  </div>
</div>
like image 71
Dennis Avatar answered Sep 23 '22 08:09

Dennis