Ensuring Flexbox items are aligned vertically without creating any mysterious empty spaces between them

I'm encountering an issue with Flexbox's flex-wrap: wrap that appears to be causing additional white space below the div.

This is how my setup looks (with class names for clarity):

<div class="viewheight-background">

    <div class="header"></div>
    <div class="vertically-centered-text"></div>

</div>

My CSS code is quite simple:

html, body{
    height: 100%;
}

.viewheight-background {
    min-height: 100%; /* Ensures a section of 100% screen height */
    background-size: cover;

    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
}

.vertically-centered-text {
    align-self: center;
}

The reason why vertically-centered-text is within a div is because it contains a main heading and some subtext.

Removing "align-self: center" results in proper wrapping of flex-items. However, there seems to be invisible white-space causing "align-self: center" to sit below the center of its containing div.

Any suggestions on how to solve this?

(Let me know if more details are needed)

EDIT:

Here is a reference Fiddle. I forgot to include html, body {height: 100%;}

Answer №1

Finally discovered a viable solution.

It seems that:

float, clear and vertical-align do not impact a flex item.

I identified the issue with your:

align-self: center;

on the second flex item.

Therefore, your css should be modified to:

.vertically-centered-text {
    /*align-self: center;*/
    background-color:blue;
    display: -webkit-flexbox;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
    text-align: center;
}

Refer to this LIVE DEMO or execute this snippet (in full page view):

html,
body {
  height: 100%;
}
.viewheight-background {
  min-height: 100%;
  /* To ensure a section of 100% screen height, could be done with 'vh' */
  background-size: cover;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}
.vertically-centered-text {
  /*align-self: center;*/
  background-color: blue;
  display: -webkit-flexbox;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
  text-align: center;
}
.header {
  background-color: red;
}
<div class="viewheight-background">
  <div class="header">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>
  <div class="vertically-centered-text">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
    sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
    incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
    velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
</div>

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

The div swiftly clicks and moves beyond the screen with animated motion

Here is a code snippet that I am working with: $(document).ready(function(){ $(".subscriptions").click(function (){ if($(".pollSlider").css("margin-right") == "-200px"){ $('.pollSlider').animate({"margin-right": '+ ...

Removing classes from multiple cached selectors can be achieved by using the .removeClass

Currently working on enhancing some JavaScript/jQuery code by storing selectors in variables when they are used more than once to improve performance, for example: var element = $("#element"); element.hide(); However, I am facing difficulties while tryin ...

Determining the optimal time to utilize the addClass function in jQuery and CSS

One of my HTML elements has the class "article": <article> Content </article> In my jQuery script, I want to add specific classes that have been predefined: $('article').addClass('rounded box-shadow gradient-bright'); I ...

Retrieve the nearest attribute from a radio button when using JQuery on a click event

I have a query regarding the usage of JS / JQuery to extract the data-product-name from a selected radio button, prior to any form submission. Although my attempt at code implementation seems incorrect: return jQuery({{Click Element}}).closest("form" ...

Why doesn't the float extend across my entire background?

One issue I am facing is with two divs in my HTML file, both set to 50% width. The left div has a fixed position while the right div should be scrolling. I have set it to float, but there seems to be a white space around the background of the floating div: ...

Reposition the span element to the right of the div tag

I need help adjusting the positioning of the elements in this HTML code. How can I move the span element with the image to the right of the div tag? Here is the code snippet: <div style='width:600px;padding-top: 2px;padding-bottom: 1px'& ...

Challenges in the functionality of PHP form submission utilizing GET parameters

I'm currently working with PHP forms and the form tag I have looks like this: <form name="adv_search_form" action="<?php echo $targetpage; ?>" method="GET"> When I submit the form, it directs me to this URL: http://localhost/projectcode ...

Registering a change event for a table's value

I am a beginner in Angular and struggling with writing an event that can successfully pass the changed value from a table cell to my component. Below is the HTML code for the table cell, where the user should be able to change the value and have it passed ...

Embedding content within various ng-template elements

I'm currently working on developing a button component (app-button) that can utilize multiple templates based on the parent component using it. <div class="ds-u-margin-left--1 ds-u-float--left"> <ng-container *ngTemplateOutlet="icon">< ...

The -moz-use-text-color property does not function properly in Chrome and IE versions 9 and above

I have added -moz-use-text-color for border-color in the following CSS code: .booksline{ border-image: none; margin: 0 auto 0px; width: 96%; height:220px; padding:20px 20px 20px 0; border-width: 1px 1px medium; border-style: solid solid none; border-colo ...

The media does not need to be applied to the ".nav__btn" class

How can I hide an element with the ".nav__btn" class by setting its display to none when the screen width is at least 768px? I tried the following media code but it doesn't work. Media Code @media (min-width: 768px) { .nav__btn { display: ...

When a user clicks on buttons other than the submit button, the input field redirects to another page

A helpful user named Vittore assisted me in displaying the first 5 list items of the unordered list and implementing page navigation with next and previous buttons flawlessly. The issue arises with an input field, which functions correctly by navigating ...

Is there a more efficient way to optimize my coding for this Cellular Automata project?

As a beginner in programming, I wanted to delve into the world of cellular automata and decided to create my own using JavaScript. The project involves a simple binary (black and white) 2D CA where each cell updates its color based on the colors of its 8 ...

aligning a trio of button pieces to seamlessly merge in CSS

I have a total of 6 files that I need to work with. They are as follows: button_01.png (left side of the button) button_01-RO.png (rollover effect) button_02.png (middle part of the button) button_02-RO.png (rollover effect) button_03.png (right side ...

Tips for avoiding the need to reload a single page application when selecting items in the navigation bar

I am in the process of creating a simple Single Page Application (SPA) which includes a carousel section, an about us section, some forms, and a team section. I have a straightforward question: How can I prevent the page from reloading when clicking on nav ...

Choosing multiple classes in CSS 3

As I reviewed some sample code today, I noticed the developer opted to use a "+" instead of a "," to select two classes. .region + .region{ border-left: 1px solid lightgray; padding-left: 3.5%; margin-left: 4%; } I'm curious about what t ...

Consider implementing a fixed position to the navigation bar

Currently, I am facing an issue with my menu layout as shown in this demo The problem arises when the menu goes outside of its container, similar to the image below: https://i.sstatic.net/nQA2K.png This issue occurs because I have applied position: fixe ...

Strange problem with CSS - Unable to load a specific style based on the browser

Within my main.jsp page, I have the following code: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <script src="/targets/js/jquery.uploadify.min.js" type="text/javascript"></script> ...

JQuery - Triggering mouseenter/hover event only on top-level menu items, excluding the nested submenu items (list items that are not within nested lists)

I have a complex navigation menu structure with nested lists, and I'm struggling to trigger an event only on the top-level items when hovered. Despite trying different jQuery selectors and methods, such as using the NOT selector or targeting direct ch ...

"Enhance your website with a dynamic animated background using Bootstrap container

I'm struggling to understand why the Bootstrap container is blocking the particles-js animation behind the text. I want the background surrounding the text to be animated as well... :/ Example Code: .gradient-bg { background: rgba(120, 87, 158, ...