Elements centered within a flex container are expanding and overflowing above

It seems like I'm missing a key point when it comes to my flexbox setup for vertical and horizontal centering.

The issue arises when the container's height exceeds that of its parent, causing the content to extend beyond the top but remain fixed at the bottom due to the presence of a vertical scroll. Give it a try by adjusting the view's height or adding more lines of text.

body,
html {
  height: 100%;
  width: 100%;
  margin: 0;
}

* {
  box-sizing: border-box;
}

#wrapper {
  background: grey;
  height: 100%;
  width: 100%;
  max-height: 100%;
  display: flex;
  overflow-y: auto;
  align-items: center;
  justify-content: center;
}

#box {
  margin: 30px 0;
  background: white;
  border: 1px solid #dfdfdf;
}
<div id="wrapper">
  <div id="box">
    First line
    <br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br> Last linje
  </div>
</div>

I need help preventing the clipping issue. Additionally, I aim to maintain a 30px margin above and below the container. Any suggestions would be greatly appreciated!

Thank you!

Answer №1

It's not that you forgot something, but rather a matter of understanding what is going on. Initially, you set your wrapper to be 100% height of the screen and then centered the box both vertically and horizontally. However, when the box has a large height, it can result in a layout like this:

https://i.sstatic.net/ZlCbT.png

By adding overflow-y: auto, you are creating a scroll within the wrapper that starts from the top and allows you to scroll through the overflowing content at the bottom, leading to a visualization like this:

https://i.sstatic.net/tkgBQ.png

This explains why you can scroll to see the bottom part but struggle to view the top part. To resolve this issue, it's recommended to utilize margin:auto for centering your element, which results in two scenarios:

  1. If box-height < wrapper-height, the space will be evenly distributed on each side due to margin:auto, thereby centering your element as expected.
  2. For box-height > wrapper-height, the normal behavior occurs where the element overflows and its top edge aligns with the top edge of the wrapper.

https://i.sstatic.net/UHMB8.png

You might also observe similar issues with horizontal alignment, which is why utilizing margin for centering in both directions can be beneficial.

body,
html {
  height: 100%;
  width: 100%;
  margin: 0;
}

* {
  box-sizing: border-box;
}

#wrapper {
  background: grey;
  height: 100%;
  width: 100%;
  max-height: 100%;
  padding:30px 0;
  display: flex;
  overflow-y: auto;
}

#box {
  margin: auto;
  background: white;
  border: 1px solid #dfdfdf;
}
<div id="wrapper">
  <div id="box">
    First line
    <br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br> Last linje
  </div>
</div>

Answer №2

To achieve the desired effect of giving your flex item (#box) a specific height and setting its overflow, rather than modifying the flex container, you can make some adjustments to your styles. Additionally, to incorporate the 30px spacing above and below, it's recommended to remove the margin from the box and instead apply padding to the container.

Here is how the updated styles would appear:

#wrapper {
  background: grey;
  height: 100%;
  width: 100%;
  max-height: 100%;
  display: flex;      
  align-items: center;
  justify-content: center;
  padding: 30px 0; /*added*/
}

#box {
  background: white;
  border: 1px solid #dfdfdf;
  overflow-y: auto; /*added*/
  height: 100%; /*added*/
}

body,
html {
  height: 100%;
  width: 100%;
  margin: 0;
}

* {
  box-sizing: border-box;
}

#wrapper {
  background: grey;
  height: 100%;
  width: 100%;
  max-height: 100%;
  display: flex;      
  align-items: center;
  justify-content: center;
  padding: 30px 0;
}

#box {    
  background: white;
  border: 1px solid #dfdfdf;
  overflow-y: auto;
  height: 100%;
}
<div id="wrapper">
  <div id="box">
    First line
    <br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br> Last linje
  </div>
</div>

Answer №3

It appears that the top margin within the box class is causing the container's height to increase. Consider adjusting it to padding instead of margin for a better result. I hope this suggestion proves helpful. Many thanks.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When I hover my mouse over the items on the Navigation bar, they shift forward

Seeking assistance with a coding issue. When hovering over TOYOTA, the HONDA and CONTACT US sections move forward unexpectedly. This movement is unwanted as I would like only the next list item to display when hovering over TOYOTA and HONDA. How can I corr ...

Adjusting the rover effect in HTML can alter the size of an image

I'm trying to create a rollover effect in HTML with the following code: <img src="images/facebook.png" onmouseover='this.src="images/facebookActive.png"' onmouseout='this.src="image/facebook.png"' height="32px" width="32px"/&g ...

accessing data from a web service using HTML

We've successfully developed a web service for temperature conversion using Netbeans. @WebMethod(operationName = "tempConverter") public Double tempConverter(@WebParam(name = "temp") double temp, @WebParam(name = "choice" ...

What is the best way to display fewer pages in Pagination on a mobile view?

Issue We need to display fewer pages in the mobile view so that it can align with the heading (My Orders) on the same line. https://i.sstatic.net/JROvk.png Resource material-ui/pagination Current Progress I have successfully removed the Next and Prev ...

Connecting buttons to JavaScript functions that interact with MySQL database entries

I am working on a task involving rendering a database table using XMLHttpRequest in JavaScript to a PHP page. My goal is to display each entry from the table as an HTML row/cell with two buttons within each "entry". These buttons should trigger specific Ja ...

Entering destinations into the Google Maps search bar

I am currently working on a project that involves creating a Google Maps window. The goal is for the user to input their location (marker A) and receive directions to a specified destination (marker B), ideally using an input box and submit button. So far, ...

Ensure that both the top row and leftmost column are fixed in a vertical header using JQuery DataTable

Check out the implementation of vertical header flow in Datatables by visiting: https://jsfiddle.net/2unr54zc/ While I have successfully fixed the columns on horizontal scroll, I'm facing difficulty in fixing the first two rows when vertically scroll ...

How can I pass a URL into the <img> tag using WebAPI to load an image encoded in base64 format?

Is there a way to insert a URL from Webapi into an <img> tag? My approach involves converting the image to a base64 string and storing it in the Database as a varbinary. <img src="https://localhost:44381/api/Account/get-resource-image;data:imag ...

Modifying the maximum length of input fields in HTML, Javascript, and PHP is possible by adjusting the value through

Hi there, I'm facing an issue that should be easy for you guys to help me with. I recently discovered a security flaw while using the maxlength function in HTML. Users can easily modify the maxlength attribute by inspecting elements on the website, wh ...

You are unable to move the image to the top of the screen after zooming it with CSS

I implemented an image viewer component with interactive buttons for rotating, zooming in, and zooming out. Upon clicking a button, CSS transform is applied to the image. However, I encountered an issue where, when zooming the image, it cannot be scrolled ...

Listening for changes in a variable using a YUI event listener

/* Creating an event listener for the submit button */ YAHOO.util.Event.addListener(webserver.result_form, 'submit', webserver.result_submit); In my main.js, I currently have this event listener set up. I was curious to know if in YUI there is ...

Using Jinja2 to loop through and create Bootstrap accordion items for each element

I have integrated the Bootstrap accordion element into a Jinja template for a loop, with the code looking like this: <div class="accordion" id="accordionExample"> {% for article in articles %} <div class="accor ...

Angular Material: div not being filled by layout-fill

<!-- Container Div --> <div layout-fill> <!-- Image Div --> <div layout="column" layout-align="center center" class="coverImage" layout-fill> <md-content class="md-padding"> Sample Text Here ...

The hover effect in CSS brings life to list items when filled with content

I am attempting to create an animation for an <li> element that gives the illusion of a fill effect moving from left to right when hovered over. This is my current progress: ul { list-style-type: none; } .hoverTest { float: left; margin-right: 1 ...

Tips for displaying and concealing a toggle button based on screen size changes

My goal is to display my userlist without a toggle button by default when the screen is in full size, and only provide a toggle button when the screen is in mobile size to allow users to show/hide the list. Currently, I am utilizing the following code: & ...

Is it possible to convert nested CSS into Material-UI's CSS in JS method?

Here is the code for embedding an iframe: <style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: abso ...

Adjust the dimensions of the dropdown menu

Objective: How can I adjust the width of a select dropdownlist that is utilizing bootstrap v2? Challenge: I am uncertain about how to modify the width in the context of bootstrap. Additional Information: Keep in mind that there are three dropdownli ...

What is the best way to make my navbar adjust seamlessly to accommodate the largest element?

Currently, I'm facing an issue where my logo is not fitting properly inside my navbar and it's "falling out." You can see the problem here: https://i.sstatic.net/l4T1B.png Here is the relevant code: HTML: <nav class="container-fluid navbar ...

observing numerous directories in Sass

Is there an efficient way to monitor multiple directories in sass using a shell script? This is what I currently have in my shell script: sass --style compressed --watch branches/mysite/assets/css/sass:branches/mysite/assets/css & sass --style compre ...

Steps for modifying the look of a button to display an arrow upon being clicked with CSS

Looking to enhance the visual appearance of a button by having an arrow emerge from it upon clicking, all done through CSS. Currently developing a React application utilizing TypeScript. Upon clicking the next button, the arrow should transition from the ...