What is causing the discrepancy in CSS line-height when text spans multiple lines?

Can anyone explain why there is extra space above the first paragraph in this HTML and CSS code, but not the second or third? The line-height is consistent for all three paragraphs. How can I adjust it so that there is equal spacing around all three paragraphs?

I am encountering this issue on Chrome, not sure if it appears differently on other browsers.

.wrapper {
  width: 175px;
  border: 1px solid black;
  font-size: 20pt;
}

.wrapper p {
  margin: 0.5em 0 0 0;
  background-color: orange;
  line-height: 1.2;
  word-wrap: break-word;
}

.wrapper p span {
  display: inline-block;
}
<div class="wrapper">
  <p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">AAAAA &amp; AAAAA</span></span></p>
  <p style="text-align:center"><span style="font-size:0.5em">BBBBB · BBBBB · BBBBB BBBBB BBBBB</span></p>
  <p style="text-align:center"><span style="font-size:0.5em"><span style="color:#00bcd4;">CCCCCCCC &amp; CCCCCCCCCC</span></span></p>
  <p>This is a paragraph without any spans, it needs to look correct too.</p>
</div>

Could someone verify if this is a browser-specific issue by looking at the provided image of the problem?

edit: Inserted another p tag without any contained span tags. This paragraph also should be correctly formatted.

Answer №1

To solve the issue at hand, you should eliminate the font-size:0.5em from the span's style attribute in your html code and instead include it in the styling of .wrapper p in your css.

<div class="wrapper">
  <p style="text-align:center"><span style="color:#00bcd4;">AAAAA &amp; AAAAA</span></p>
  <p style="text-align:center"><span>BBBBB · BBBBB · BBBBB BBBBB BBBBB</span></p>
  <p style="text-align:center"><span style="color:#00bcd4;">CCCCCCCC &amp; CCCCCCCCCC</span></p>
</div>
<!-- Also removed the extra spans in the first and third p's, as they would now serve no purpose. -->
.wrapper p {
  margin: 0.5em 0 0 0;
  background-color: orange;
  line-height: 1.2;
  word-wrap: break-word;
  font-size:0.5em; /* This is added */
}

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

This explanation delves into the connection between the font-size in your span and the parent element's font-size.

The unit em is relative to the parent element's font-size. In this case, the parent of the span is the p element, which inherits the font-size of the .wrapper, set at 20pt. Consequently, the span's minimum height is set to 10pt, while the p's minimum height is 20pt.

Hence, the disparity arises because the span is half the size (0.5em) of the p element, leading to text wrapping within the span and filling up the remaining space in the parent p element, creating the observed gap.

Answer №2

For a quick solution without the need for a lengthy explanation, scroll to the end.

When dealing with line height in CSS, there are three key considerations to keep in mind. Firstly, the line-height property applied to block elements determines the minimum height of the line box.

On a block container element consisting of inline-level elements, 'line-height' sets the minimum height of line boxes within the element source

In certain cases, such as yours, the minimum height may equate to 22pt = 1.2*20pt. Even when the p element is empty, this minimum height will still be present.

The second consideration involves understanding that an inline-block element occupies one line box, regardless of text wrapping inside it. Therefore, even if multiple line boxes exist within the element, each span element will take up just one line box from the parent p.

Inline-level boxes like replaced inline-level elements and inline-blocks are considered atomic inline-level boxes due to their behavior as a single opaque box within the formatting context source

The final aspect to consider is vertical alignment, typically set as baseline by default, which dictates how elements align within the line box.


To gain a better comprehension of these aspects, let's delve into what's occurring:

In the first scenario, an element with a small size relative to the line box's height is aligned at the baseline within the p element. The subsequent cases involve elements with similar font sizes but heights matching or exceeding the line box's height, resulting in varied alignments.

A visual representation can aid in understanding these concepts better. Notably, all three elements are aligned at the bottom (baseline), with slight variations observed in the latter two.

An additional demonstration showcasing the impact of vertical alignment follows:

By adjusting the line-height to cater to only one line of text, you ensure consistent alignment within the containers. This can be achieved either by setting a low line-height value, or specifying the desired font size explicitly.

Additional examples and detailed discussions on similar topics can be found here: Line height issue with inline-block elements

Answer №3

By including the style display: inline-block within the <span> tag, you inadvertently changed its default behavior from inline to inline-block.

To correct this issue, simply update your code by removing:

.wrapper p span {
  display: inline-block;
}

and replacing it with:

.wrapper p span {
  display: inline;
}

This adjustment should resolve the problem at hand.

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

Toggle visibility of div content when hovering over a link by leveraging the data attribute, with the div initially visible

I have a collection of links: <p><a href="#" class="floorplan initial" data-id="king"><strong>King</strong></a><br> 4 Bedrooms x 2.5 Bathrooms</p> <p><a href="#" class="floorplan" data-id="wood">< ...

The 'background-size:cover' property does not function properly on iPad devices

Greetings! I have been working on developing a parallax website, and everything seems to be running smoothly across all browsers except for iPad. It appears that the background-size: cover property is not functioning properly on this device. Below is the ...

Enhance your dynamic php page with the use of a light box feature

Hey, I have created a PHP page that dynamically reads images from a folder and displays them on a gallery page. However, I am facing some issues - I am unable to link an external CSS file and I have to include all the CSS within the HTML. Additionally, I c ...

Getting rid of all CSS properties currently set for the Mobile view

Utilizing third-party library components in my project. The library includes components that come with predefined styles for mobile devices. I am seeking to completely UPDATE these properties within my own code. When I say "update," I mean removing all th ...

The compatibility issues with the textarea and text-indent: placeholder feature on EDGE are causing functionality problems

I'm facing a peculiar problem with placeholders when using the Edge browser. In my current project, I need to implement a textarea with a specific text-indent property. However, on Edge, the placeholder text appears displaced more than expected. To h ...

Methods for altering the color of a div using addEventListener

Why doesn't the color change when I click on the div with the class "round"? Also, how can I implement a color change onclick? var round = document.querySelector(".round"); round.addEventListener("click", function() { round.style.backgroundCol ...

Utilizing flexbox for dynamic width adjustment with flex-grow functionality

Currently, I am in the process of configuring a menu bar using this particular template I have been attempting to achieve this layout utilizing FlexBox, but it appears that there is an issue. Here is the HTML code: <nav> <ul> < ...

Creating personalized styles for my React components with the help of styled-components

Exploring the power of styled-components for custom styling on child components has been an interesting journey for me. For instance, I recently crafted a unique card component named myCard. Take a look at how it's structured: import React from "rea ...

Alignment of text fields in a vertical manner

How can I vertically align text input fields using CSS? <form action='<?= $_SERVER['PHP_SELF']; ?>' method='post'> <p><label for='username' class=''>Username:</label& ...

Ways to align the navigation icon to the left side

I need help positioning my help icon at the bottom of the screen, regardless of the user's monitor size. I've tried using margin and margin-top, but it doesn't adjust properly when changing monitor sizes. Any suggestions or assistance would ...

`Customizing Switch Colors on Bootstrap: A Guide`

https://i.sstatic.net/alsdy.png I obtained the code from Get Bootstrap. <div class="form-check form-switch"> <input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked> & ...

Looking for solutions to issues with Style errors and Visibility problems?

< table border="1" cellpadding="0" cellspacing="0" width="100%"> < tr> < td>1< /td> < td>1< /td> < td>1< /td> < td>1< /td> < td>1< /td> ...

Ways to ensure next/image takes up all available grid space

Is there a way to make the next/image element span from grid 1 through 8? It seems to work fine with the imgtag but not with next/image. .project { display: grid; margin-bottom: 4rem; grid-template-columns: repeat(12, 1fr); align-items: center; } ...

Expand the contents inside a div without affecting the actual div

Is there a way to zoom out the contents of a div similar to how a browser zooms out a page? I'm working on an application that loads a page into a div. However, I want to load the content without scroll bars. This would require zooming out the conten ...

Reducing the Size of Sprite Images on Websites

I am working on a web page that contains an image file called sprites.png. This file holds multiple images within it. Specifically, there is one image in the file that is 48px x 48px. In my CSS, I am referencing this image like so: .cell-back { height:3 ...

Tips for making a background image responsive using ng-style in AngularJS

Can anyone assist me in fixing the gap space issue in a responsive view with an attached jpg background image? Follow this link for more details: enter image description here ...

React-app size has grown larger post-deployment

I recently created my second app clone using React. I have noticed that after deployment, the size of the app increases. Everything looks normal on localhost:3000, but it significantly grows once deployed. Any assistance in resolving this issue would be gr ...

A step-by-step guide on coding a slider image

<?php $query1="SELECT * FROM user_photos_offline WHERE ssmid='$ssmid' AND status='1' ORDER BY date_uploaded DESC LIMIT 0,5"; $sql=mysql_query($query1); $count=mysql_num_rows($sql); ...

Dividers afloat - expanding current script with multiple additions

I am currently utilizing this website as a hub for showcasing my various web projects. The jsfiddle code provided in response to this particular inquiry is exactly what I need, however, I am unsure of how to have multiple divs moving simultaneously acros ...

Enhance your title bar with an eye-catching image

Is there a way to add an image to the title bar? I currently have the title set as "Webnet". I attempted to combine it with a FontAwesome Glyphicon's icon image like this: <title><i class="icon-user icon-black"></i>Webnet</titl ...