What impact does using display: flex have on the line-height of an element?

Have you noticed the peculiar behavior of display flex? Sometimes it takes into account the line-height of an element, causing the height to be dependent on it. Other times, it disregards the line-height and sets the minimum size based on the content inside.

Consider this example where the defined styles ignore the line-height - the height of the element will match the size of the SVG:

<div style="display:flex; line-height: 5">
  <div style="display: inline-flex; line-height: 10">
    <svg>...</svg>
  </div>
</div>

If we remove the display:flex style from the outer div, the size of the element will align with a line-height of 5:

<div style="line-height: 5">
  <div style="display: inline-flex; line-height: 10">
    <svg>...</svg>
  </div>
</div>

Similarly, removing the display:inline-flex style from the inner div will result in the size matching a line-height of 10:

<div style="line-height: 5">
  <div style="line-height: 10">
    <svg>...</svg>
  </div>
</div>

The question remains: why does it work this way? It seems to operate as expected with text, consistently applying the line-height for text elements.

svg {
  width: 1rem;
  height: 1rem;
}

.block {
  display: flex;
  border: solid 1px;
  line-height: 10;
}

.inline {
  display: inline-flex;
  line-height: 5;
}
  <div class="block">
      <span class="inline">
       <svg viewBox="0 0 24 24" focusable="false" xmlns="http://www.w3.org/2000/svg">
       <path d="M11 7h2v2h-2zm0 4h2v6h-2z">
       </path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z">
       </path>
       </svg>
      </span>
 </div>

<div class="block">
    <span class="inline">
      ------
    </span>
 </div>

Answer №1

Functionality remains consistent with text, ensuring line-height is always applied.

When applying inline-flex and flex, the scenario changes as text is no longer present in your case. You are now dealing with flex-level elements instead of inline-level elements.

A flex item establishes an independent formatting context for its contents. However, flex items themselves are flex-level boxes source

Additionally,

The display value of a flex item is blockified: if the specified display of an in-flow child of an element generating a flex container is an inline-level value, it computes to its block-level equivalent

In this example:

<div style="display:flex; line-height: 5">
  <!-- I am a flex item since my parent has display:flex and my inline-flex will get transformed to flex -->
  <div style="display: inline-flex; line-height: 10"> 
    <!-- I am a flex item since my parent is has display:inline-flex -->
    <svg>...</svg> 
  </div>
</div>

The property line-height will not have any effect here due to the absence of inline-level elements and the lack of an IFC (inline formatting context).

On a block container element whose content consists of inline-level elements, 'line-height' specifies the minimal height of line boxes within the element. source

Furthermore,

For a non-replaced inline element, 'line-height' denotes the height used in calculating the line box height.


The second example involving text presents a slightly different situation where a flex item creates an inline formatting context for its content.

Every in-flow child of a flex container becomes a flex item, and each continuous sequence of child text runs is enclosed in an anonymous block container flex item. source

Hence, your text is enveloped within a hidden block container that inherits the line-height, allowing line-height logic to take effect.

An illustration follows:

<div class="block"> <!-- flex container -->
    <span class="inline"> <!-- flex item & flex container -->
      <anonymous> <!-- Browser-generated anonymous block container flex item inheriting line-height -->
        ------
      <anonymous>
    </span>
 </div>

In the SVG example provided, adding text triggers the application of line-height:

svg {
  width: 1rem;
  height: 1rem;
}

.block {
  display: flex;
  border: solid 1px;
  line-height: 10;
}

.inline {
  display: inline-flex;
  line-height: 5;
}
<div class="block">
  <span class="inline">
       <svg viewBox="0 0 24 24" focusable="false" xmlns="http://www.w3.org/2000/svg">
       <path d="M11 7h2v2h-2zm0 4h2v6h-2z">
       </path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z">
       </path>
       </svg> a
      </span>
</div>

<div class="block">
  <span class="inline">
      ------
    </span>
</div>

In this scenario, there are 2 flex items - the SVG and an anonymous block container flex item for the text, both subjected to the line-height attribute.

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

Manage the selection of multiple items when clicking on and off a button or component

I am currently working on implementing a feature with multi-select buttons using a custom component. My goal is to require that at least one option is selected at all times. This means that users can select multiple options, but the last remaining option c ...

How can I position a div column-10 to the right and make it stay fixed in place

I'm having an issue trying to position a div on the right side without causing content to overlap with the navbar. I've attempted using `position:absolute top:0 right:0`, but it's not working as intended. <link href="https:// ...

What is the best way to create a unique transition for every component?

I have three elements that I would like to display with a transition effect when they appear or disappear. There is one main element and the other two show up when you click the corresponding button. You can view my current code sample here: https://jsfid ...

Steps for adding products to your shopping cart without using a JavaScript framework:

As a newcomer to web development, I took on the challenge of creating an e-commerce website using only HTML, CSS, and vanilla JavaScript (without any frameworks). I've encountered a roadblock while trying to implement the "Add to Cart" functionality ...

javascript guide to dynamically update the glyphicon based on value changes

I am attempting to implement an optimal level feature where a glyphicon arrow-up will be displayed if a value falls within the optimum range, and it will switch to a glyphicon arrow-down if the value is lower. <div class="card-body" ng-repeat="item i ...

Design a vibrant call-to-action button that enlarges with a mouse hover

I came across some cool buttons on this site Here is the code I found: @import "https://fonts.googleapis.com/css?family=Didact+Gothic&subset=greek"; @import "https://cdn.linearicons.com/free/1.0.0/icon-font.min.css"; body { font-family: 'Dida ...

The presence of a border in CSS creates additional room around the content

My issue is that when I apply a border to a div, the content inside no longer fills up 100% of the width and height. I have already tried adding padding: 0;, margin: 0;, and box-sizing: border-box;, but the background still shows at certain zoom levels. ...

Locate the value of every selected radio button on individual forms

Currently, I have two forms with radiobuttons stored in separate div containers. I am looking to compare the selected radiobuttons from the left and right div containers. My current code checks for the selected values in both div containers, but it ends ...

What could be the reason for my click event not being triggered?

Having trouble with opening a link in a new tab using jQuery <a class="tt-link" data-link="report.html?id=0">Open</a> I attempted to open it in a new tab by attaching a handler within my ready() function $('.tt-link').click(funct ...

Encountering difficulties accessing XML file on server via anchor tag

I have an XML file on the server that I am attempting to open in a browser when the user clicks on a link. Below is how I have set up the link, but it is not opening the file: Code: <a title="View XML" href="file://///90.0.0.15/docmgmtandpub/PublishD ...

Switch the alignment of Bootstrap checkboxes from vertical to horizontal

I'm looking to have my checkbox elements aligned horizontally instead of the default vertical alignment in bootstrap. To achieve this, I've added the following CSS code: .checkboxinsameline{ display:inline; } Here's the HTML structure ...

Styling PHP Input Buttons with CSS

I'm facing an issue with styling the button on a working form. Even though I added CSS and declared the button class, it's not applying the styles. The CSS is defined at the top of the HTML file, and the PHP code is embedded within. Both classes ...

Transfer the output of a function to an event listener

Currently, I am delving into self-teaching Javascript and recently crafted some code to delve a deeper understanding of event handlers. <html> <head> <script language=Javascript> function sum(first, second) { // return first ...

Using PHP, eliminate all hyperlinks from the DOM HTML

I have a string called $processhtml that contains some HTML. My goal is to remove all link tags and their contents from the HTML using PHP. For example: "This is some text with <a href="#">link</a>" should become: "This is some text with" ...

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, ...

Obtain Data Grid from Website

I need to extract a table with the following columns: "Date|Open|High|Low|Close|No.of Shares|No.of trades|Total Turnover|Deliverable Qty" from the website "" Below is the code I am using: Sub Macro_BSE() Application.ScreenUpdating = False Dim File ...

How can a fixed size block and a responsive block be aligned next to each other?

I am looking to create a centered table that is 900px wide with one row and two cells. The right cell should always be 200px wide, while the left cell should fill up the remaining space. However, I am facing an issue where the right cell jumps below the le ...

GWT 2.5.1 - How to Restrict the Amount of Items Displayed in a Dropdown Menu (ValueListBox)

Currently, I am utilizing a ValueListBox to showcase a range of values for users to choose from. The main issue I am encountering pertains to IE11 and its subpar behavior when interacting with the control: The dropdown list appears all over the place (in ...

What is the method used by Chrome dev tools to create CSS selectors?

When setting a new style rule for an element, Chrome generates a selector that includes the entire hierarchy of elements instead of just the class name. For instance: <body> <div class="container"> <span class="helper"> ...

Strategies for manipulating the position of an SVG polyline

Is there a way to move the chevron symbol to the beginning of each accordion header instead of it being on the right side of the words? Any help with this would be appreciated. The code snippet is provided below. Please advise on how to reposition the pol ...