Creating a space of 4 pixels below floated <li> elements with embedded images

My current HTML setup is as follows:

<div class="container">
    <ul>
        <li><img src="30x30.gif" /></li>
        <li><img src="30x30.gif" /></li>
        <li><img src="30x30.gif" /></li>
    </ul>
    <div style="clear: both" />
</div>

To ensure these images appear aligned horizontally, I have applied the following CSS:

div, p, ul, li {
    margin: 0;
    padding: 0;
    list-style: none;
}

div.container > ul li {
    float: left;
    background-color: yellow;
}

Despite successful implementation, there is an unexpected 4px gap at the bottom of each <li> element. This issue is visible through a slight indication of the yellow background at the bottom edge.

An illustration of this occurrence can be viewed here: http://jsfiddle.net/DBcPw/

NOTE: The problem exclusively arises when <li> elements consist of images. On the other hand, using <p> elements does not trigger the same anomaly.

In light of this situation, two fundamental questions arise:

  1. What is the cause of this discrepancy?
  2. How can this issue be rectified?

Answer №1

Check out the latest changes to your jsfiddle

New additions include:

img{display:block; float:left;}

This update eliminates any extra padding and margin that browsers usually add to images for spacing purposes that may not be desired. To remove this spacing, set them as display :block, or if you want them side by side, use float:left.

Answer №2

The <img> tag's spacing is determined by the font currently in use, which means it can be affected by white space. To resolve this, you can set font-size: 0; on the <li> elements and it should solve the issue.

li {
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 0;
}

Check out this link for more information.

Answer №3

Ensure that the height of the li elements matches that of the img elements.

div.container > ul li {
    float: left;
    background-color: yellow;
    height: 30px;
}

To see a working example, visit: http://jsfiddle.net/DBcPw/6/

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 there a way for me to choose multiple checkboxes simultaneously?

I have a dynamically created HTML table with checkboxes added for each row. $.each(data, function(id, value) { rows.push('<tr><td><input type="checkbox" autocomplete="off" class="chkbox" name="chkbox" value="'+value.site+' ...

Unexpected Results from Div Positioning

Here is the PHP code snippet I am working on: <?php include 'header-adminpanel.php'; ?> <div class="container"> <div class="body-content"> <div class="side-left"><?php include 'adminproduct_sidebar.ph ...

Developing a Chessboard Using JavaScript

Seeking help with a Javascript chessboard project. I have successfully created the board itself, but facing difficulty assigning appropriate classes (black or white) to each square. Managed to assign classes for the first row, struggling with the remainin ...

Angular getElementById is a useful method for accessing and manipulating elements

I am using asp.net and angular 7 to retrieve data saved by a form with a specific ID. The data is successfully stored in local storage but is not being displayed on the front-end for the user. No error messages are being shown. Can anyone assist me with th ...

Guide on activating full screen mode in Firefox without user interaction

Is there a way to make a webpage automatically go into fullscreen mode upon loading? I attempted the following code: <html> <head> <script type="text/javascript> webkitEnterFullscreen(); </script> </head> <body onload="mo ...

Show specific modal or image depending on various criteria

Seeking guidance on how to achieve the following tasks in a simple manner as my understanding of HTML, Bootstrap, and CSS is basic: 'When radio button #1, radio button #4, and button #2 are clicked, upon submission display modal 1 with an image at a ...

Retrieving information from 3 interconnected tables by utilizing inner joins

I have a table in my database called files which has columns for id, name, process, and action. When a user enters an action, the value is stored in another table called update_table which has columns for id, reason, and update date. If the user does not ...

When bsg_head(); is inserted into the <head> tag, the content mysteriously vanishes

While working on a Wordpress project, I encountered an issue where adding the code <?php bsg_head(); ?> to the header resulted in all the content disappearing from the page. Surprisingly, the background color change from the stylesheet was still b ...

Using HTML in radiobutton choiceNames within R Shiny applications

I am currently struggling to showcase Radiobutton choiceNames that were dynamically created from a text input. These choiceNames consist of a Greek letter (beta) and two subscripts - one numeric and the other, an alphabet 'j'. My attempt involve ...

Is it possible to conceal JavaScript comments on a page before it is displayed?

Curiosity has led me to ponder a question that may seem trivial. Consider this scenario: in my HTML or ASPX page, I include a comment for some JavaScript code. When the page loads, will the comments be rendered along with the rest of the page's conten ...

Changing the primary color dynamically in Ionic 5

In my Ionic Angular app, I am exploring ways to dynamically change the color scheme based on user interactions. With a large number of components needing to be responsive to these color changes, I am seeking a method where elements can reference a color va ...

Mobile devices have a fixed distance and center alignment on the horizontal timeline

I am attempting to create a horizontal timeline similar to this example: https://codepen.io/ritz078/pen/LGRWjE The issue I am facing is that I do not have specific dates (DD/MM/YYYY) but only ranges such as 1998-2002 or single years like 2009. This has ca ...

Text exceeding boundaries within a horizontal flexbox (accordion)

Need help with a flexbox (accordion) issue. The demo below showcases multiple groups of content, each contained within a cell in a horizontal flexbox. However, when the content within a group is extensive, it overflows the flexbox causing display problems. ...

The JQuery TextNTags plugin eliminates tag formatting once the trigger syntax has been modified

I have incorporated the JQuery TextNTags plugin into my web application. Here is the original code snippet: $.browser = { webkit: true }; $(function () { $('textarea.tagged_text').textntags({ triggers: {'!': { ...

HTML Email Not Displaying Properly in Outlook

My email is displaying correctly on webapps and browsers, but the layout is off on Outlook client version 2102 (build 13801.21106). I've tried adjusting cellspacing, cellpadding, and width, but nothing seems to work. https://i.sstatic.net/HrRlE.png h ...

Include a hyperlink that is activated when hovering over an image

UPDATE: I am amazed by the quick responses, this is my first time using this platform. Thank you to everyone who helped out. The person who provided the answer understood exactly what I was looking for. To others who responded, thank you as well. I need h ...

Utilizing the HTML5 <video> element to enable pausing the video when navigating to the next slide in BXSLIDER

Is there a way to easily stop an HTML 5 video from playing when you click on a next button? I've tried searching on Google but couldn't find a solution that works. I have implemented bxslider for a client on the about page . If you scroll down, ...

Issue with navigator.geolocation.getCurrentPosition not functioning again on Firefox

I've encountered a peculiar issue with JQuery that has me seeking assistance. On a certain page, I have the following block of javascript code. Upon initial page load, everything works smoothly. However, if the user navigates away and returns to the ...

Missing 'id' property in ngFor loop for object type

I'm currently learning Angular and I've been following a code tutorial. However, despite matching the instructor's code, it doesn't seem to be working for me. When I try to compile the following code, I receive an error message stating ...

Issues with CSS positioning are not being resolved in Chrome

I set up a Wordpress website and I'm trying to display two vertical banners outside of the main container. My CSS code seems to work fine on Firefox and IE, but on Chrome only the left banner is showing up. Any suggestions on what might be causing thi ...