Mysterious space found within flexbox elements

There seems to be a mysterious gap between the top red and blue bars.

What could I be doing incorrectly? Removing main eliminates the bar, but then the footer moves up?

If you'd like to take a look at what's happening, here is a link to my fiddle: https://jsfiddle.net/v9yrmafw/1/#

.strip {
  height: 20px;
  background: red;
}
.bar {
  text-align: center;
  background: blue;
  color: white;
  height: 100px;
}
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.footer {
  margin-top: auto;
}
.footer__body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 70px;
  background: yellow;
  color: white;
}
main {
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: auto;
}
<div class="container">
  <main>
    <div class="strip"></div>
    <div class="bar">
      <h1>Home</h1>
    </div>
  </main>

  <footer>
    <div class="footer">
      <div class="footer__body">
        <p>© {{ copyright }} {{ now }}</p>
      </div>
    </div>
  </footer>
</div>

Answer №1

This issue arises from the margin collapsing of the h1 element with the bar div - to address this, you have two options:

  1. Reset the margin to zero for the h1

  2. Add padding or a border to the bar

Check out the demo below where I've included some padding to the bar:

.strip {
  height: 20px;
  background: red;
}
.bar {
  text-align: center;
  background: blue;
  color: white;
  height: 100px;
  padding: 10px;
}
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.footer {
  margin-top: auto;
}
.footer__body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 70px;
  background: yellow;
  color: white;
}
main {
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: auto;
}
<div class="container">
  <main>
    <div class="strip"></div>
    <div class="bar">
      <h1>Home</h1>
    </div>
  </main>

  <footer>
    <div class="footer">
      <div class="footer__body">
        <p>© {{ copyright }} {{ now }}</p>
      </div>
    </div>
  </footer>
</div>

Answer №2

The issue at hand arises from the phenomenon of margin collapsing, which was identified in @Kukkuz' response.

In addition to the remedies provided in that response – adjusting the margin or introducing border / padding to prevent margin collapsing, there is a third solution: converting the parent into a flex container

An implementation example in your code could be:

.bar {
    display: flex;
}

Within a flex formatting context, margins no longer collapse.

As per the specification:

3. Flex Containers: the flex and inline-flex display values

A flex container establishes a new flex formatting context for its contents.

This functions akin to setting up a block formatting context, except that it employs flex layout instead of block layout.

For instance, floats are restricted from penetrating the flex container, and the margins of the flex container do not merge with the margins of its contents.

updated code snippet

.bar {
   [...] /* This is an excerpt */
}
<div class="container">
[...] /* This is an excerpt */
</div>

Answer №3

experiment with this for your h1 tag

.highlight {
    height: 25px;
    background: purple;
    
  }
  
.navbar {
    text-align: center;
    background: green;
    color: black;
    height: 80px;
   
  }
  
.wrapper {
  display: flex;
  flex-direction: row;
  height: 90vh;
  
}  

.footer {
  margin-top: auto;
}
.footer__info {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 60px;
  background: orange;
  color: black;
}

section {
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: auto;
  
  
}
<div class="wrapper">
  <section>
    <div class="highlight"></div>
    <div class="navbar"><h1 style=" padding:5px;
    margin:5px;">About</h1></div>
  </section>
  
  <footer>
    <div class="footer">
      <div class="footer__info">
        <p>© {{ year }} Company Name</p>
      </div>
    </div>
  </footer>
</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

Altering the background colour of dropdown fields when clicking on a checkbox

How can I make the colors of my dropdown fields match the colors of the text (non-dropdown) fields when they are disabled using a checkbox? This is how my code looks: <form v-on:submit.prevent :disabled="isDisabled(opti ...

Utilizing the JSON File in Your HTML Webpage: A Comprehensive Guide

I'm currently trying to integrate the following JSON file into my HTML page. <html> <head> <title> testing get</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> ...

Calculating the time gap between two consecutive "keyup" occurrences

I am in need of creating a basic point of sale system using Javascript. I have a barcode scanner that functions like a keyboard. My goal is to automatically identify when the input is coming from the barcode scanner and then generate a report with the sc ...

How can I ensure that buttons in a row utilize the full 100% of available space?

I have a row of buttons in my HTML code like this: HTML : <div class="buttons"> <div><input type="button"/></div> <div><input type="button"/></div> <div><input type="button"/></div& ...

Minimum width compared to Maximum Width

Exploring media queries for the first time and seeking some guidance. I initially developed a Desktop version of my website, now considering making it responsive as well. While researching, I came across suggestions to use max-width in this scenario, but ...

What could be causing the empty space along the right edge of my website?

I've encountered an issue with my website where there's a white space on the right side of the page, causing horizontal scrolling. I attempted to resolve this by adding overflow: hidden to the CSS, which worked perfectly in Chrome. However, the p ...

Error encountered while uploading image on django through jquery and ajax

Looking for help on posting a cropped image via Jquery and ajax. I've been trying to implement a solution from a question on cropping images using coordinates, but I'm facing issues with receiving the image on Django's end. The CSRF token an ...

React isn't updating the on-change value despite changes being made

In my React application, there is a file called EditTodos.js that is responsible for updating the to-do list. When I click on the "Edit" button, it triggers a pop-up modal component. import React, { useState } from "react"; import { Button, Modal } from " ...

Sliding Menu with jQuery

My goal is to create a dynamic menu that reveals sub-menu items by sliding them down and changing the background color upon clicking. I am currently using the HTML code displayed below, with the sub1 and sub2 elements initially hidden using display:none; ...

Tips for developing screen reader-friendly AJAX areas and managing updates to the DOM?

My interactive user process operates in the following manner: Users encounter a dropdown menu featuring a selection of cities. Upon picking a city, an AJAX request retrieves all buildings within that city and inserts them into a designated div (the AJAX ...

Random margins appearing between list items

While working on my menu design, I've encountered an issue with unexplained margins between the list items that cannot be found in the developer's console. Here is the code snippet: HTML <div class="navbar"> <ul class="n ...

Sharing Variable Across Multiple Pages

I have successfully passed a variable from a link on my first page to the second page, but I am encountering difficulties when trying to pass that variable into the third page. How can I accomplish this? The First Page is an HTML Page <a href="SecondP ...

Is there a method for redirecting my page to a specific href link without triggering a page reload?

This is my HTML code. <a href="http://127.1.1.0:8001/gembead/emstones.html?car=36">Car</a> I am trying to redirect to a specific page with parameters without fully reloading the current page. Is there a way to achieve this task? I believe the ...

Looking for assistance with accessing elements using the "document.getElementById" method in Javascript

Below is the code snippet I am working with: <html> <head> <script> function adjustSelection(option) { var selectList = document.getElementById("catProdAttributeItem"); if (option == 0) { ...

Troubleshooting issues with Jquery fadeIn and fadeOut - tips for implementing fadein animation using addclass and mouseover

My concept involves a shop menu displayed as a list: <li class="shop-menu"><a href="{{url('shop')}}">shop</a></li> When the shop-menu is hovered over (using a mouseover jquery function), I aim to make a div named shop-m ...

Apply styles specifically to elements that contain a child element

I have a scenario where there are multiple <p> elements with the same class names, but only one of them has a child element. My objective is to highlight only the <p> that contains a child, however, my current code ends up highlighting all of t ...

Can a piece of code be created that generates the XPath for a specific element?

I'm interested in finding out if there is a way to automatically generate an XPath by simply selecting an element, eliminating the need for manual updates when changes occur in HTML files on websites. I welcome any suggestions or alternatives that can ...

Utilizing a Jquery plugin for enhanced form validation with the inclusion of supplementary techniques

I am looking to integrate a jQuery plugin into my form validation process, specifically to ensure that only letters are allowed in the name section. If a user enters special characters or numbers, I want the form to display an error message. Additionally, ...

Discover the method for selecting an element within a table that includes a style attribute with padding using Jquery

Looking for a way to identify a td element with the padding style in a table I've attempted to locate a td element with the padding style attribute in a table $(Table).find('td[style="padding:"]') or $(Table).find('td[style] ...

Guide to showcasing two spring models within a single HTML table row utilizing Thymeleaf

I need help formatting a row to display in an HTML table: "$num out of $totalCount" For example, if num=5 and totalCount=8, I want it to show as 5 out of 8. This is the code I have so far: ... <tr> <td th:text=&quo ...