The mysterious behavior of HTML 5 causing images to mysteriously add a 3px margin at the

Whenever I update my website to

<!DOCTYPE HTML>

Each img element enclosed within a DIV mysteriously acquires a 3px bottom margin, even though no CSS rule defines it. To clarify, there are no style attributes responsible for this 3px bottom margin.

<div class="placeholder">
    <img alt="" src="/haha.jpg" />
</div>

Assuming haha.jpg is sized at 50x50 and .placeholder has display: table set, the observed height dimensions of .placeholder seem to be 50x53...

Has anyone else come across this oddity and found a solution?

UPDATE

Find the JS FIDDLE link below:

http://jsfiddle.net/fRpK6/

Answer №1

The issue at hand stems from the image acting like text, which results in extra space below it as if a character like "y" or "g" were hanging. This problem can be resolved by utilizing the vertical-align CSS property to indicate that no such additional space is required. Any value of vertical-align will suffice; personally, I prefer using middle.

img {
    vertical-align: middle;
}

View this code snippet on jsFiddle: http://jsfiddle.net/fRpK6/1/

Answer №2

One common solution I use is assigning the image element with either display:block or display:inline-block depending on the situation.

Answer №3

My issue has been successfully resolved.

line-height: 0;

Answer №4

In my opinion, applying

line-height: 1;

to the image may resolve this issue, especially when it is positioned as a standalone block.

Answer №5

To resolve the issue of images inside floated divs, ensure to add display: block to the image.

Answer №6

perhaps the issue lies in the white space causing this problem. here are some potential solutions to try:

img {
display: block;

} or

img {
vertical-align: top/middle/...;

}

or

.placeholder {
font-size: 0;

}

Answer №7

It took a mix of

font-size: 0px;
line-height: 0;

on the outer element to solve the issue.

Answer №8

The reason behind this occurrence is still unclear to me, but try setting the font size of your placeholder div to 0px.

.placeholder {
    font-size: 0px;
}

Answer №9

None of the solutions mentioned above were effective for my specific situation.

I encountered an issue with a wrapping div that had a display: flex; property applied to it.

To resolve the problem, I added align-items: flex-start; to both the wrapping div and all the images within it by applying display: block;.

Prior to explicitly setting the alignment, the layout was not displaying correctly. However, after specifying flex-start, everything fell into place perfectly.

Answer №10

Uncertain of the specific issue, but I have attempted two different options to solve it. The first is to apply a class on the img tag, which seems to work. The second option is to set the font size to 0px.

http://jsfiddle.net/fRpK6/4/

Answer №11

One common issue arises when dealing with a series of stacked divs, but this can be easily resolved by simply applying the float property as shown below:

<body>
  <div class="stackedDiv">Some text</div>
  <div class="stackedDiv">Some text</div>
  <div class="stackedDiv">Some text</div>
</body>

Problematic CSS:

.stackedDiv{
    width:100%;
    display:inline-block;   
 }

Solution:

.stackedDiv{
    width:100%;
    display:inline-block;
    float:left; 
 }

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

Conceal element when unchecked

There is a checkbox pre-selected labeled "Use profile address" with the address displayed below. If the customer unchecks the checkbox, the address that was shown before will disappear and a new input field for adding a different address will appear. I a ...

Form on the internet with a following button

Currently working on a basic form: <tr> <td> <label for="FirstName">First Name <span class="req">*</span> </label> <br /> <input type="text" name="FirstName" id="FirstName" class="cat_textbox" ...

WebDriverException: Error: {"errorMessage":"The object is undefined"

I encountered an error while running the code on this HTML page, specifically at /html/body/div[3]/div[1]/div[1]/div[1]/div/div[10]/a/div[1]/div[2]: WebDriverException: Message: {"errorMessage":"null is not an object (near '...ull).singleNodeVa ...

What is the best way to extract text directly from a span element without including the text of its child nodes?

I am dealing with a piece of HTML that is dynamically generated, and it looks like this: <span> 10/10/2012 <div class="calendar"> lots of text elements here </div> </span> Is there a way to specifically retrieve the tex ...

Provide users with the option to select a specific destination for saving their file

In the midst of my spring MVC project, I find myself in need of implementing a file path chooser for users. The goal is to allow users to select a specific location where they can save their files, such as C:\testlocation\sublocation... Despite r ...

Issues with JQuery .attr method not functioning as expected

I'm having trouble with the .attr() function in jQuery. It doesn't seem to be changing the background-color of the div with the id "outline" like I expected. Here's an example of my code: <div id="outline"></div> And here is t ...

Attempting to display divs at the bottom when hovering over menu options

I need help troubleshooting my HTML and CSS code. I want to be able to hover over a menu item and have the corresponding div appear at the bottom. Can you point out what's wrong with my code? <!DOCTYPE html> <html> <head> &l ...

Concealing and wrapping text using CSS in a table structure

My table is displayed below: <table style="width: 100%; white-space: nowrap; overflow: hidden;"> <tbody><tr> <th> Department </th> <th> Function </th> ...

Are you curious about the array of elements in React's carousel?

I'm currently in the process of constructing a website using React, and I have a specific challenge related to the "news" section. Within this section, I have a list of three components that represent different news items. These components are housed ...

The background image in the hovering textbox shifts and changes size

I've been trying to set a background image on a Kendo UI Textbox. It looks good until you hover over it. But when you do hover over it, this issue arises: How can I resolve this? The background image should remain in place even when I hover and cli ...

What is the best way to trigger a new css animation on an element that is already in the midst of

Let's talk about an element that already has an animation set to trigger at a specific time: .element { width: 100%; height: 87.5%; background: #DDD; position: absolute; top: 12.5%; left: 0%; -webkit-animation: load 0.5s ease-out 5s bac ...

I am experiencing difficulties with my bootstrap module not loading

Could you please investigate why my module isn't loading? The module is supposed to load under the specified conditions in the third last column, but when I test the code, it doesn't work. Can you assist me in identifying what might be causing th ...

Customized placement of form fields on an HTML grid determined by the user

My goal is to organize input elements on a grid based on user preferences. After researching, I stumbled upon CSS grids, which seem promising. I am considering creating a CSS grid with r rows and c columns, then using JavaScript to assign input elements t ...

Guide to making a dropdown menu that pulls information from a text box and displays the location within the text

I'm currently working on a unique dropdown feature that functions like a regular text field, displaying the text position. To achieve this, I've constructed a hidden dropdown menu positioned beneath the text field. My goal is to develop a jQuery ...

What is the best approach to execute the jquery script exclusively on mobile devices?

I want to modify this code so that it only functions on mobile devices and is disabled on computers. How can I achieve this? <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <ul id="pri ...

Define the position of a scrolled area within an HTML document to create a

In my current project, I have a scrollable area that highlights the selected div in gray. The HTML is written using Ember (hbs) and includes code to handle this functionality. Below is an example of how the div looks: Here is the corresponding HTML (hbs): ...

What is the best way to create a flexible Ul-LI menu?

Is it possible to create a flexible menu where clicking on an item does not cover other items in the menu? #demo { margin: 30px 0 50px 0; font-family: sans-serif; } #demo .wrapper { display: inline-block; width: 180px; height: 20px; position ...

jQuery selector unable to locate the specified class in the table row

After receiving an array of strings as a response from an SQL query, I am using JavaScript to dynamically add this data as rows to an HTML table: loadUsers: function() { . . . function displayUsersOnTable(response) { for(var i = ...

Issue: $injector:unpr Unrecognized Provider: itemslistProvider <-

I've spent several days debugging the code, but I can't seem to find a solution. I've gone through the AngularJS documentation and numerous Stack Overflow questions related to the error, yet I'm still unable to identify what's caus ...

Transforming data from PHP JSON format into HTML output

Struggling to convert JSON data into HTML format? Having trouble maintaining the correct order of child elements in your structure? Here's a solution: Take a look at this example JSON: { "tag": "html", "children": [ { "ta ...