Make sure the text fits perfectly without any extra spaces

When working with CSS/HTML, I noticed that using a simple text within a div without any margin or padding, and specifying a font-size of 36px with a line-height also set to 36px doesn't fit perfectly - there is always some spacing at the top of the line. By reducing the line-height by 20% (to around 29.8), I was able to achieve the desired effect.

I'm curious as to why this happens. Is there something related to browsers that I might be overlooking? I initially thought that setting the line-height equal to the font-size would eliminate any extra spacing around a line.

Update: It turns out that the spacing issue I am experiencing is known as "half-leading". Despite setting the line-height to match the font-size, browsers still calculate half leading which results in some additional spacing.

Update 2: I came across information in the CSS specification detailing line-height: http://www.w3.org/TR/CSS2/visudet.html#line-height. However, I'm still puzzled about how to prevent the inclusion of ascender/descender spacing altogether.

Answer №1

In response to the second update you provided:

It's interesting to note that ascender/descender height varies depending on the font used, with some fonts not even having ascenders or descenders at all, such as all caps monospace fonts. Simply using text-transform: capitalize wouldn't solve this issue if the font doesn't include any characters with ascenders/descenders.

While it may be a bit of a workaround, as you mentioned, it seems like this limitation is more of a feature than a bug in this case.

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

Guide on attaching an onclick event to a button created with a string in Next.js?

<div onClick={(event) => this.addToCart(event)}> <ReactMarkdownWithHtml children={this.props.customButton} allowDangerousHtml /> </div> In my current situation, I am facing an issue with the code above. The button is being rendered ...

Hero Sticky Transition with 100vhDivElement

My code is giving me some trouble. I want the div with text to stay at the bottom of the hero section with a transparent background. When that div reaches the top, it should stick with a black background. The issue is that I have set it in the javascript v ...

Can custom HTML elements be designed to function similarly to standard ones?

What is the best way to create a custom HTML element that functions as a link without relying on inline Javascript? For instance: <x-button href="...">...</x-button> Can I develop an element like <x-button> that can accept an attribute ...

Tips for optimizing Angular source code to render HTML for better SEO performance

Our web platform utilizes Angular JS for the front-end and node js for the backend, creating dynamic pages. When inspecting the code by viewing the source, it appears like this: For our business to succeed, our website needs to be SEO-friendly in order to ...

Understanding the functionality of sessions in CodeIgniter when using AJAX

I am experiencing an issue with the Session in my code. It only works when the page is refreshed. I have a controller called ravi and the view named myview. Here is the code snippet: <?php class Ravi extends CI_Controller { public funct ...

Tips for preventing an element from scrolling horizontally along with the page body

This is the structure of my HTML: <div class='header-wrapper'> <div class='title'>Title</div> </div> <div class='some-wide-content'> </div> Let's imagine a scenario where the br ...

Why is this strange red dot character 🔴 appearing in the URL in FireFox?

While browsing the web, I came across a website with a strange red blob in the URL. It's something I've never seen before. What could possibly be the reason for this unique feature?! Note that you have to visit the page to see it for yourself. T ...

Confirming the accuracy of multiple fields in a form

Looking for help with validating both password and email fields on a registration form. I've successfully implemented validation for passwords, but need assistance adding validation for the email section as well. Can both be validated on the same form ...

Evaluate numerical values using xsl:choose function

Using XSL version 1.0, I am populating an HTML table with percentage values from XML. My goal is to display the highlighted color of the percentages based on a condition, but the condition is not being processed as expected, leading to the exclusion of the ...

Exploring the Power of jQuery Event Attributes

Can someone explain the distinct difference between the keys delegateTarget and currentTarget in the jQuery event object? $(this).on('click',function(event){ console.log(event.delegateTarget); console.log(event.currentTarget); ...

Guide on incorporating data from a text file into an HTML table

I'm attempting to showcase the records from upload.txt in an HTML table. The location of the upload.txt file is at this URL: http://localhost:3000/upload. When accessing this URL, it retrieves the upload.txt file. Currently, I am using node.js for r ...

The background image fails to display on the button

Why isn't the background image for my button appearing when set using a CSS rule? I have a button with the class "todo" and although other parts of the rule work, the image itself does not show up even after trying "background: url" and "background-im ...

Conceal flexbox item depending on neighboring element dimensions or content

I've encountered an issue while using a flexbox to display two elements side by side. The left element contains text, while the right one holds a number. When the value in the right element has at least 7 digits ( > 999,999 or < -999,999), I need to ...

The automated Login Pop Up button appears on its own and does not immediately redirect to the login form

Hey guys, I'm struggling with modifying the jquery and html to ensure that when the login button is clicked, the login form pops up instead of displaying another login button. Another issue I am facing is that the login button seems to pop up automati ...

Tips for showcasing the individual product page in full width on woocommerce

Here is a link to the single product page: I am wondering if it's possible to extend the product summary drop-down menus all the way to the right without impacting the mobile site? Thank you. ...

Jssor's dynamic slider brings a touch of sophistication to preview upcoming images

Incorporating the jssor nearby slider to create a nearly fullscreen display. The goal is to set the opacity of upcoming images to 0.25 when they are not in the main viewport, giving the edges of the upcoming and previous slides a slight transparency. < ...

What is the best way to determine if a child component's property has changed within a method?

The child component is app-google-maps-panel and has 2 properties in the parent component: <div class="col-6"> <app-google-maps-panel [longitude]="longitude" [latitude]="latitude"></app-google-maps-panel> & ...

Discovering the window.scrollTop, ScrollY, or any other distance value while utilizing CSS scroll snap can be achieved by following these

I am currently utilizing css scroll snap for smooth scrolling through sections that are 100vh in height. The functionality of the scroll snap is quite impressive. However, I need to find a way to determine the exact distance the user has scrolled down the ...

Combining various arbitrary values, like :has in Tailwind, can be achieved by following these steps

I'm facing an issue where I need to hide an element if it has children. In regular CSS, this is achieved with the following code: &:not(:has(*)){ display: none } However, when trying to implement this in Tailwind, I'm struggling to figure ...

PHP Lightweight HTML Content Scraper

I have been experimenting with a basic web crawler and here is the simple HTML code I used for learning purposes: input.php <ul id="nav"> <li> <a href="www.google.com">Google</a> <ul> <li&g ...