Top-align the background of the inline element

This particular inquiry pertains to a curious discrepancy in the computed height of an inline nonreplaced element across different web browsers. The question can be found here: Why the computed height of inline nonreplaced element differs between browsers?

In my case, I have an inline element where I apply a background color to create a highlighting effect (specifically for the legend of an image).

The challenge arises when trying to precisely control the rendering of the background height, given that font rendering varies among browsers. While slight differences are acceptable, what I really need is for the top of the first line background to align perfectly with an image on the left side. In the provided jsfiddle example, the white and blue backgrounds should align. Is there a way to achieve this alignment using CSS?

HTML

<div class="main">
    <div class="float"></div>
    <span>Et quoniam mirari posse quosdam peregrinos existimo haec lecturos forsitan, si contigerit, quamobrem cum oratio ad ea monstranda deflexerit quae Romae gererentur, nihil praeter seditiones narratur et tabernas et vilitates harum similis alias, summatim causas perstringam nusquam a veritate sponte propria digressurus.</span>
</div>

CSS

div.main {
    min-height: 100px;
    background-color: red;
    padding: 10px;
} 

div.float {
    min-height: 50px;
    float: left;
    width: 10px;
    background-color: white;
}

span {
    text-transform: uppercase;
    line-height: 1.5;
    background-color: blue;
    padding-left: 10px;
    padding-right: 10px;
}

http://jsfiddle.net/0ne3pay2/4/

Answer №1

In my opinion, the most effective way to achieve this is by utilizing display:table-cell; for the float div and span elements. Here is an example:

div.main {
    min-height: 100px;
    background-color: red;
    padding: 10px;
} 

div.float {
    min-height: 50px;
    float: left;
    width: 10px;
    background-color: white;
    display:table-cell;
}

span {
    text-transform: uppercase;
    line-height: 1.5;
    background-color: blue;
    padding-left: 10px;
    padding-right: 10px;
    display:table-cell;
}
<div class="main">
    <div class="float"></div>
    <span>Et quoniam mirari posse quosdam peregrinos existimo haec lecturos forsitan, si contigerit, quamobrem cum oratio ad ea monstranda deflexerit quae Romae gererentur, nihil praeter seditiones narratur et tabernas et vilitates harum similis alias, summatim causas perstringam nusquam a veritate sponte propria digressurus.</span>
</div>

This configuration will enhance the display of the span, making it appear as a block rather than individual lines.

UPDATE:

If you prefer not to render your span as a block, simply add margin-top: 3px; to your float div. This adjustment addresses the issue caused by the line-height: 1.5; property of the span element. You can choose to either remove this property or apply the margin to the div instead.

Take a look at the revised Fiddle.

Answer №2

div.content {
  min-height: 200px;
  background-color: blue;
  padding: 15px;
  text-align: center;
}

Answer №3

div {
    font-weight: bold;
    text-transform: uppercase;
    line-height: 2.0;
    background-color: red;
    display: flex;
    padding-top: 30px;
    padding-bottom: 15px;
    text-align: center;
}

http://jsfiddle.net/exampleuser/1a2b3c4d/9/

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

Move the div with jQuery

I am currently working on making a div draggable without relying on jQuery UI. Here is the HTML code: <div class="wrapper"> <div class="toddler"></div> </div> And here is the script I have written: $.fn.slider = function () { ...

What steps can I take to ensure my HTML5 mobile web app is optimized for compatibility across a variety of devices

I recently developed an HTML5 mobile web app and conducted tests on various devices including iPhones, iPads, Android phones, and tablets. What steps can I take to optimize the size and resolution of this app for seamless performance across all these dif ...

What is the best way to add padding to each line within a block of text containing multiple lines

My <span> tag has a background color and left and right padding applied to it. However, the padding is only visible at the beginning and end of the <span>, not on each line when the text wraps onto multiple lines. Is there a way to add padding ...

Steps to import shared CSS using Styled-components

In my project using react, I've implemented styled-components for styling. One requirement is to have a shared loading background. Here is the code snippet of how I defined it: const loadingAnimation = keyframes` 0% { background-position: 95% 95%; } ...

Is it possible to customize the color of an SVG image within Next.js using next/image?

import Image from 'next/image' ... <Image src="/emotion.svg" alt="emtion" width={50} height={50} /> I am trying to change the color of the SVG using next/image. However, applying the following CSS rule does not seem ...

Unable to utilize navigator.camera.getPicture in iOS while using PhoneGap Adobe Build

I've spent hours searching, trying to troubleshoot my PhoneGap app (compiled by Adobe PhoneGap Build) and I suspect there's something crucial about PhoneGap that I'm missing. I've added the following lines to the config.xml file: <f ...

CSS not being applied to Next.js HTML pages

Currently, I am utilizing Nextjs and including the flaticon css in _app.js as follows: import '../public/assets/css/flaticon.css'; In the upcoming section, an error is being encountered: @font-face { font-family: 'Flaticon'; src: ...

Angularv9 - mat-error: Issue with rendering interpolated string value

I have been working on implementing date validation for matDatepicker and have run into an issue where the error messages do not show up when the start date is set to be greater than the end date. The error messages are supposed to be displayed using inter ...

How can I feature an image at the top of the page with large 3 and jQuery in FireFox?

I am interested in showcasing a single image at the forefront of the page when it is selected. While there are numerous plug-ins that offer this feature, many come with unnecessary extras like galleries, video support, and thumbnails which I do not requir ...

Align select and number inputs vertically in Firefox

How can I achieve vertical alignment for these inputs on Firefox? The alignment works perfectly in Chrome, Safari, and Opera, but not in Firefox (Version 57 tested on macOS, Windows, and Linux Mint). While removing type="number" from the input resolves th ...

Tips for aligning a search bar to the right position

How can I align the search bar to the right? I've attempted various methods but nothing seems to be working. I would like the 'Transaction' title to be on the left side of the card and the search bar on the right side. This is the code snip ...

Leveraging PHP functions for form validation

Previously, the code worked flawlessly until I integrated it into functions. Now, I am struggling to get this form to function correctly with the functions I created. It seems that passing variables and implementing the main logic are key steps, but I&apos ...

What is the method for accessing an image in JavaScript?

Currently, I am developing a currency converter for a gaming marketplace and I would like the users to be able to generate images using JavaScript. While most of the work is completed, I am facing an issue where the images are not displaying properly and i ...

Java - Avoid overwriting files by renaming duplicated files instead of the selected file

Situation - I have a script called TableToCSV that is designed to convert a .html table file to a .csv file. However, it requires the user to input a file with a .html extension through the console. The issue is that the files selected often have a .xls ex ...

Using Javascript to dynamically add SVGs from an array

Having issues with displaying SVG images in a quiz I'm building. I have a folder full of SVGs that correspond to each multiple choice option, but I can't seem to get the pathway right for them to show up properly. I've tried using template l ...

What is the process of editing a webpage to affect the display on a different webpage?

I am currently working on creating two webpages. One will serve as a customization page where users can upload images and input text, which will then be displayed on another page. I am wondering if it is possible to achieve this functionality using CSS and ...

Tips for adjusting the progress bar to 100% and back to 0%

My goal is to increase the progress bar percentage with each click. Currently, it only goes up to 75%. When saving, it should show 100%, but it's not displaying that properly. On clicking the back button, it should reset to 0% on the third click. HTM ...

Which is more recommended to use in AJAX (XMLHttpRequest) - eventListener or readyStateChange method?

As I revisited a video from WWDC12 discussing advanced effects with HTML5, I couldn't help but notice that for the demo they utilized req.addEventListener("load",callback,true) instead of the usual onreadystatechange. This made me wonder: what differ ...

Ways to customize hover color of Bootstrap 5 Dropdown menu

I'm having trouble modifying the hover color for a dropdown menu using Bootstrap 5. I'm unsure of the correct method to achieve this. The desired outcome is as follows: However, at the moment it appears like this: Apologies for the oversight, h ...

Utilizing HTML and JavaScript to Download Images from a Web Browser

I'm interested in adding a feature that allows users to save an image (svg) from a webpage onto their local machine, but I'm not sure how to go about doing this. I know it can be done with canvas, but I'm unsure about regular images. Here i ...