What is the reason behind the additional height created by line-height?

<div><iframe style="height: 100px"></iframe></div>
<div style="line-height: 0"><iframe style="height: 100px"></iframe></div>

Click here to view the code snippet on jsFiddle.

Upon inspecting the DOM, it is evident that the first div has a height of 108px while the second one's height is 104px. Despite the iframe with borders being 104px, it inexplicably adds 4 extra pixels when line-height is set.

This behavior was observed and tested on Chrome version 28.0.1500.71 for Mac OS.

Answer №1

Because the iframes are positioned on the baseline. However, the div must be sufficiently tall to encompass the strut, which extends beyond the baseline and is determined by the line-height.

When the line-height is 0, the strut height is also 0, resulting in no part of the strut extending below the baseline.

To prevent this issue, you can use iframe { display:block; } or iframe { vertical-align:top; } or

iframe { vertical-align:bottom; }
.

(Using display:block prevents the creation of a line box, eliminating the strut. Setting vertical-align to top or bottom allows the strut to move away from the iframe's baseline, ensuring that none of it extends below the iframe. It is important to note that if the line-height exceeds the height of the iframe, the strut will dictate the line-box height and therefore the div height will remain greater than the iframe height, regardless of the vertical-align setting.)

Answer №2

It appears that both divs are 109 pixels tall when viewed in Chrome version 28. It's important to note that the default vertical-align setting for an <iframe> element and all inline elements is baseline. Modifying this setting to bottom will result in both elements aligning as you might have anticipated.

http://jsfiddle.net/p6SD4/1/

Answer №3

The optimal height can vary depending on the web browser you are using. In my testing, both Firefox and Chrome display IFRAME elements with a height of 104px (100px content height + 4px border).

Which specific browser are you currently using? Have you attempted adding the attribute border="0" to see if that alters the display?

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

Is it possible to manipulate the content inside a div tag using JavaScript in HTML?

What is the most efficient way to dynamically adjust a data offset value within an HTML tag using JavaScript? For example: <div class="box" data-offset="2s"></div> I am looking to update the value within the data-offset attribute based on s ...

When a button is clicked, unleash the magic of music using JavaScript

Why isn't the button onclick working for the mp3 file I uploaded to GitHub? I even tried using the link through https://voca.ro/1dcHxW3v1oQA, but it still wouldn't work. function play() { var audio = new ...

How to customize the arrow color of tooltips in Bootstrap 4

After spending an hour searching online, I still can't figure out how to change the tooltip arrow color to red. None of the code snippets I found seem to work for me. My latest attempt was: .tooltip-arrow { border-right-color: red; border-left-c ...

Angular - ngbDropdownMenu items are hidden from view, but their presence is still detectable

Hey there! I'm currently working on a school project and I'm aiming to implement a drop-down menu. Surprisingly, it's proving to be more challenging than expected. <div class="parrent"> <div class="row"> ...

Issue with zeroLineColor and zeroLineWidth not functioning properly for the x-axis within Chartjs

Looking to customize the width and color of the x-axis in Chartjs? While it's straightforward for the y-axis using zeroLineColor and zeroLineWidth, achieving the same effect for the x-axis seems trickier. If you try adding: ticks: { beginAtZero: tr ...

Encountering difficulties including Bootstrap in React-app after attempting global installation

After using the command npm i -g bootstrap to install bootstrap, I attempted to include it in a React-app by adding import "bootstrap/dist/css/bootstrap.css";. Unfortunately, this resulted in an error being thrown. Failed to compile. ./src/index.js Modu ...

Triggering click events for multiple items in a loop using jQuery

I'm struggling to make a jQuery click function work for all items generated by a PHP while loop. Currently, it only works on the first iteration. I want to trigger a popup window to show a message to the user when they activate the click function. P ...

Switch out a static image for a dynamic YouTube video using CSS styling

I've been working on customizing a template website, and I want to switch out the current image for a YouTube video. I have the code from YouTube ready to go, but I'm not sure what changes need to be made in the template. Take a look at the CSS ...

Adjusting the width of images using jQuery Mobile or jqTouch

I am looking to incorporate a static footer image with 5 navigation buttons into my mobile website. You can view the image here. The default color for the icons should be blue, but I want the yellow icon to appear only when hovered over or in an active sta ...

Embracing the balance of a gradient backdrop

I'm looking to create a gradient background for my page that transitions from light blue in the center to dark blue on the sides in a symmetrical way. I've tried using a linear gradient that goes from left to right, but it doesn't achieve th ...

Can a string be utilized as an HTML id in HTML5 without breaking any rules?

Would it be considered acceptable to use a string as an id? For instance, id="Nick Drake". I have seen that in html4 it is not recommended. Nevertheless, it serves my purpose effectively. I am just curious about the correctness of this approach. Thank you ...

What steps do I need to take in order to load the static folder from a CSS file?

Is there a way to load the static folder from a CSS file? I attempted to import using URL(), however, it was unsuccessful. I am looking to utilize the static path in order to apply a background to an element. .input_card:checked+.check-box { backgroun ...

Override the CSS property in question

I have tackled numerous lines of pure CSS code, and I always knew where each property was coming from. However, the inline CSS works fine, but not the class/id. The doctype is correctly typed. Despite spending a significant amount of time researching, noth ...

Which programming language is best suited for this task?

Seeking assistance with changing an image inside a div to another image with a simple mouse click. Wondering if this can be achieved through CSS or if jQuery would be the better option. Here is the code snippet: <div id="slideshow"> <img ...

The collapsing menu is malfunctioning due to duplicate selectors

Although I have been learning HTML, CSS and jQuery, one area that always trips me up is selectors. Currently struggling with redundant selectors. I am attempting to customize the ul and li elements after collapsing the li, however, the selector doesn&apos ...

Is it possible to refresh the page without using a hashtag and stay on the same page in AngularJS

Is it possible to refresh my view from the navigation bar without displaying the server folder? I have the html5Mode activated: if(window.history && window.history.pushState) { $locationProvider.html5Mode(true); } ...

Hyperlinks are being loaded

I recently noticed that my website was loading very slowly because I had quoted Wikipedia and included a direct link to the article. The link code looked like this: <a href="<?php echo $data['link']; ?>" target="_blank"><?php echo ...

Simplifying HTML/CSS tasks with effective tools

Looking to create a user-friendly web interface for company employees without spending too much time on design and markup. The end result doesn't have to be fancy, just clean HTML/CSS. Any suggestions for tools or techniques to streamline this proces ...

How can I expand and collapse all the Bootstrap panels simultaneously?

Is it possible to make all three panels collapse and expand simultaneously with just one button? <button data-toggle="collapse" data-target="#collapseOne-1,#collapseTwo-1,#collapseThree-1" ...></button> <div id="#collapseOne-1" class="coll ...

What is the most effective way to extract data from a .dpbox table using selectorgadget in R (rvest)?

Recently, I've been experimenting with web scraping data from different websites using selectorgadget in R. One successful example was when I extracted information from . My usual approach involves utilizing the selectorgadget Chrome extension to choo ...