# CSS Vertical Centering

**URL:** <https://forum.freecodecamp.org/t/css-vertical-centering/421651>\
**Category:** General\
**Created:** [September 19, 2020, 1:54pm UTC](https://forum.freecodecamp.org/t/css-vertical-centering/421651 "2020-09-19T13:54:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobsdaz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacobsdaz/32/136128_2.png) [@jacobsdaz](https://forum.freecodecamp.org/u/jacobsdaz)\
**Post date:** [September 19, 2020, 1:54pm UTC](https://forum.freecodecamp.org/t/css-vertical-centering/421651/1 "2020-09-19T13:54:52Z")

</div>

Hi, I’m having problems centering elements in the viewport, and I’m struggling to find the specific solution to the problem using google.  
So I have an h1 header with responsive text, and a figure with a nested image in my project.  
I would like to use flex to center the image in the viewport, while having the h1 banner at the top, and have all this fill the viewport. However, when using display - flex, and setting the container with these two elements, height to 100vh and reducing the height in the browser, the image overflows into my content, h2 and paragraph at the bottom.  
Any help would greatly be appreciated, really struggling with basic layouts in CSS.

Here is the Codepen link to the project : [https://codepen.io/darryn-jacobs/pen/NWNdZOY](https://codepen.io/darryn-jacobs/pen/NWNdZOY)

_Update_  
Okay so I just noticed the problem, I have a position - relative property set on my figure element to float in a figcaption over the image nested in my figure element, the flex property seems to be centering things fine. So now I’m unsure how to keep the image/figure from overflowing into the elements below it, while maintaining the floating caption?

---

<div class="post-metadata">

**Author:** ![miku86](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miku86/32/95580_2.png) [@miku86](https://forum.freecodecamp.org/u/miku86)\
**Post date:** [September 20, 2020, 6:45am UTC](https://forum.freecodecamp.org/t/css-vertical-centering/421651/2 "2020-09-20T06:45:25Z")

</div>

Hey Darryn,

can you show as an image of the problem?

I can’t reproduce your problem or I do not understand it fully. 🙂

---

<div class="post-metadata">

**Author:** ![jacobsdaz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacobsdaz/32/136128_2.png) [@jacobsdaz](https://forum.freecodecamp.org/u/jacobsdaz)\
**Post date:** [September 20, 2020, 9:00am UTC](https://forum.freecodecamp.org/t/css-vertical-centering/421651/3 "2020-09-20T09:00:49Z")

</div>

![FCC](https://global.discourse-cdn.com/freecodecamp/original/3X/5/0/501887c6fbed333acffc5485e8fa1f1aba6b6350.png)

Hey Miku  
Thanks for the response. Here is an image, as the height of the viewport is reduced, the image flows over the header and paragraph. It’s only noticeable at very small viewport heights.

---

<div class="post-metadata">

**Author:** ![jacobsdaz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacobsdaz/32/136128_2.png) [@jacobsdaz](https://forum.freecodecamp.org/u/jacobsdaz)\
**Post date:** [September 20, 2020, 9:31am UTC](https://forum.freecodecamp.org/t/css-vertical-centering/421651/4 "2020-09-20T09:31:08Z")

</div>

So I don’t know if I’m over-complicating things, but to try and summarize:

I have an h1 heading with responsive text.  
A figure with a nested image that fills the container.  
The figure is set to position: relative to allow the figcaption, which is set to position: absolute to be hidden and then displayed on hover.

So I basically just want these elements to be displayed for the height of the viewport and then center the figure vertically and horizontally.  
The way I tried to do this was by placing everything in a div container with a with a height: 100vh.  
I then placed the figure in another nested div with a height: 80vh  
To center the figure horizontally in this container I put a display: flex property on the div.  
And then put a display: flex property on the div.  
And used align-self: center on the figure to align it vertically.

I hope this explanation is less convoluted, not sure if there is an easier way to have elements fill the viewport and center them vertically, but this is the solution I came up with.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [September 20, 2020, 2:46pm UTC](https://forum.freecodecamp.org/t/css-vertical-centering/421651/5 "2020-09-20T14:46:23Z")

</div>

Changing the `height` to `min-height` might fix the problem:

```auto
div.flex {
  min-height: 80vh;
}

.viewport {
  min-height: 100vh;
}

```

---

<div class="post-metadata">

**Author:** ![jacobsdaz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacobsdaz/32/136128_2.png) [@jacobsdaz](https://forum.freecodecamp.org/u/jacobsdaz)\
**Post date:** [September 20, 2020, 5:12pm UTC](https://forum.freecodecamp.org/t/css-vertical-centering/421651/6 "2020-09-20T17:12:32Z")

</div>

Thanks appreciate it. That seems to have fixed it.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:38pm UTC](https://forum.freecodecamp.org/t/css-vertical-centering/421651/7 "2021-06-01T18:38:53Z")

</div>

Closed on June 1, 2021, 6:38pm UTC


