What is the reason behind flex causing the hr element to have a width of 0?

Whenever I attempt to place an hr element inside a container that has display: flex, it mysteriously disappears. (Removing display: flex makes it reappear, leading me to believe that this is indeed the cause.)

I stumbled upon a blog post that suggested flex causing the hr to have zero width, which I tested by assigning it 100% width (and sure enough, it reappeared), but no explanation was provided.

This made me curious if using flexbox could make other random elements vanish as well? For now, I've simply moved the hr outside the flex container as a quick fix, but I wonder why this issue occurs in the first place?

Although I don't suspect anything else in my code triggering this glitch, here's the full snippet for reference:

Answer №1

The default styles for the hr element are set by the browser, with Chrome as an example:

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

When in a flex container, using auto margins will take up all available space in the margin direction.

For instance, setting -webkit-margin-start: auto (equivalent to margin-left: auto) and -webkit-margin-end: auto (equivalent to margin-right: auto) will consume the free space on the left and right of the hr element, resulting in its compression to width: 0 due to lack of content inside.

To counter these auto margins, you can either use width: 100%, or more efficiently, just override it with margin: 0.

Even after removing the auto margins, the parent's align-items: center property will come into effect.

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

This change overrides the default

align-items: stretch</code behavior, performing the same function as the left/right <code>auto
margins. Consequently, the hr element is compressed to width: 0.

To adjust your hr rule accordingly, make the following tweaks:

hr {
  margin: 0;
  align-self: stretch;
}

* {
  /* Makes width and height include padding, border */
  box-sizing: border-box;
}

body {
  font-family: "Quicksand", sans-serif;
  margin: 0;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  color: #2d3c49;
  text-transform: uppercase;
}

h1 {
  font-weight: 200;
  /* Browsers typically display h1 as 2em */
  font-size: 2.6em;
  margin-bottom: 0;
}


/* Adds a bit of space above subtitle (set h1 bottom margin to 0) */

header h4 {
  margin-top: 7px;
}


/* Top content */

header {
  display: flex;
  max-width: 1000px;
  margin: 0 auto;
  margin-top: 1em;
  /* Vertically centers logo  */
  align-items: center;
}


/* logo img */

.logo {
  height: 90px;
  width: auto;
  margin-right: auto;
}


/* Only subtitle isn't aligned all the way to the right; this fixes it. TODO: figure out why doesn't apply to h1 text */

.header-text {
  display: flex;
  justify-content: flex-end;
}

hr {
  background-color: #7d97ad;
  max-width: 1000px;
  /* margin-bottom: 1em; */
  border: none;
  height: 3px;
  margin: 0 0 1em 0;    /* NEW */
  align-self: stretch;  /* NEW */
}

.main-image {
  max-width: 1000px;
  height: auto;
}


/* Applies to content within <main> element (excludes header, footer) */

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  max-width: 1000px;
  margin: 0 auto;
}


/* Applies to project section (including header text) */

.container-projects {
  display: flex;
  /* Parent container needs this for flex-item to take full width in row */
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: space-between;
  margin: 2em 0;
}

.portfolio-header {
  /* Puts header in its own row without removing from container with row...


It's important to note that when using flex, auto margins take precedence over keyword alignment properties such as justify-content and align-items. The presence of an auto margin will utilize all available free space on the line before any alignment properties can come into play. If there is no remaining free space after applying auto margins, align-items won't have any impact.

Hence, in the given code snippet, align-items has no effect until the auto margins are removed.

8.1. Aligning with auto margins

Prior to alignment using justify-content and align-self, any positive free space is allocated to auto margins in that dimension.

If the free space is distributed to auto margins, alignment properties will not be effective in that dimension as the margins would have already consumed all available free space.

For further insights on how flex auto margins work, consider the following resources:

  • In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

Answer №2

When using Flex, it's important to note that it can override the margin settings for certain elements such as the HR tag. One solution is to add this CSS rule:

hr {margin-left:0;margin-right:0}

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

Using HTML5 and CSS transitions to scale an image within a 960 grid layout

Having some trouble achieving a smooth transition for the height and width of images on my webpage. I believe if you take a look at my page, not much explanation is needed: Click here to see the image transition The goal is to enlarge the image to double ...

What steps can be taken to eliminate the 1-pixel line between the background and gradient?

https://i.sstatic.net/nbtkK.png How can I remove the line that appears between the block and the gradient background when zooming in, especially noticeable in Safari (After zooming a couple times, Cmd +) For more details, please refer to this jsfiddle li ...

The pseudo-element ::before did not function as expected in the CSS code

I attempted to utilize the ::before element in HTML directly. Below is my code: <ul class="clear toogled" style="height:188pxl"> <li tabindex="0" style="display: block;"> <label for="MAP-IA_1_1_OR_CB_1" aria-label="Filter by product"> :: ...

What is the technique for implementing a slide animation on hover to move a div identified by the class `box`?

How can I create a div element with the class of box that already has some transitions applied to it, and also include a slide animation on hover effect for the same div? .box { background-color: red; width: 70px; height: 70px; transition-propert ...

Error in jQuery: Null property causing TypeError when reading 'text'

Issue: I have a modal form that is loaded via an ajax call. Inside the modal, there is a span element containing an email address. I am trying to capture the value of this span element using JavaScript. Currently, I have a button click event that triggers ...

Tips for relocating a CSS button

I have designed two buttons using css and I am looking to align them below the title "forecast customer activity". Due to extensive styling in css, the code might appear lengthy. Requesting assistance with a solution after reviewing the following code snip ...

Prevent textArea from reducing empty spaces

I am facing an issue with my TextEdit application set to Plain Text mode. When I copy and paste text from TextEdit into a textarea within an HTML form, the multiple spaces get shrunk. How can I prevent the textarea from altering the spacing in the text? T ...

The scss function is returning an invalid property value

In my Angular project, I have organized my scss files/folders in the following way: 1) Settings - containing color settings and functions for the project 2) Files and folders defining variables such as fonts, widths, etc. for components and pages 3) Fil ...

Removing Delivery Address and Method From Checkout Page in OpenCart 2

Is there a way to remove "Step 2, Step 3: Billing Details" from the Checkout Page in OpenCart 2.0? I have searched for solutions online but haven't found one specifically for OpenCart 2.0. This is what I tried: <div class="panel panel-default" s ...

spill the elements from one div into another div

I'm facing a situation where I have 2 divs on a page, with the first div containing text content only. The issue is that when the content of the first div overflows, it gets cut off due to the CSS applied to it: .one { overflow: hidden; width: 1 ...

Eclipse compatibility issue with JavaFx Css causing styling problems

Recently delving into JavaFx, I decided to try my hand at setting up a custom style. Following a tutorial, I encountered a stumbling block that I just can't seem to overcome. No matter what changes I make in the CSS file, it simply doesn't reflec ...

Select options with disabled sorting feature

Is there a way to use CSS to sort disabled options in a select element? I would like all disabled options to appear at the bottom, with enabled options at the top. In the screenshot attached, you can see that enabled options are black and disabled options ...

Experiencing problem with hover:after effect on Internet Explorer 8

I've been working on incorporating the CSS effect found on this website: Everything seems to be functioning properly except for the fix provided for IE8. I added some conditional code so that the fix is only applied to <= IE8, and as a result didn ...

Repeated Outcomes in SQL Query

Looking for help with PHP coding! I've been stuck on this problem for a few days and need some assistance. I have two tables in my database: "projects_2016" and "attachment". My goal is to display data from "projects_2016" in the top table and then ...

Is it possible to move the Material UI TableSortLabel Arrow to the opposite side?

My table has columns that are both right aligned and left aligned, and I need them sorted. When the column is right aligned, I would like the sorting icon to be on the left side of the header. Otherwise, there is an awkward empty space where the icon shoul ...

Invoke data-id when the ajax call is successful

Initially, there was a smoothly working "like button" with the following appearance: <a href="javascript:void();" class="like" id="<?php echo $row['id']; ?>">Like <span><?php echo likes($row['id']); ?></span ...

Position two in-line divs at the bottom, one on each side

I am in search of a way to dynamically position two elements at the bottom of a container, making sure they are at opposite corners. Much like how the Stackoverflow Logo and the Ask Question are aligned at the bottom but on different ends of their containe ...

What is the best way to validate adjusted tags within the html editor of Visual Studio 2012?

The html editor in Visual Studio 2012 keeps giving me warnings for tag names that are not recognized in the detected or configured html schema. Since I am using AngularJs by Google, this is quite inconvenient as it relies on the ability to enter custom ta ...

Challenges with looping in Jquery animations

I've been working on an animation function that I want to run in a loop. So far, I've managed to get it to work for one iteration, but I'm struggling to figure out how to repeat the loop a specific number of times. Below is the code for my a ...

Switching from a click event to a hover event in JavaScript

I've been experimenting with animating a burger bar on hover and came across an example online that I managed to adapt for mouseenter functionality. However, I'm struggling to make it revert back to the burger bar shape once the mouse leaves on m ...