What is the impact of a floated element on vertical spacing?

I have come across an article discussing the usage of CSS, but I am puzzled as to why image number four is not positioned below image number one. Instead, it appears below image number three. Can someone please explain this to me? Here is the HTML code snippet:

<ul>
  <li><img src="http://placehold.it/100x100&text=1"></li>
  <li><img src="http://placehold.it/100x150&text=2"></li>
  <li><img src="http://placehold.it/100x100&text=3"></li>
  <li><img src="http://placehold.it/100x100&text=4"></li>
  <li><img src="http://placehold.it/100x100&text=5"></li>
  <li><img src="http://placehold.it/100x150&text=6"></li>
  <li><img src="http://placehold.it/100x100&text=7"></li> </ul>

Styling:

li {
 float: left;
 margin: 4px;
}

Answer №1

Referencing the W3C CSS 2.1 documentation, it explains how a floated box is positioned to the left or right until it touches the containing block edge or another float's outer edge. If there is a line box present, the top of the floated box aligns with the current line box's top.

In this scenario, utilizing display: inline-block; may be more effective. See example below:

li {
    display: inline-block;
    margin: 4px;
    vertical-align: top;
}
<ul>
  <li><img src="http://placehold.it/100x100&text=1"></li>
  <li><img src="http://placehold.it/100x150&text=2"></li>
  <li><img src="http://placehold.it/100x100&text=3"></li>
  <li><img src="http://placehold.it/100x100&text=4"></li>
  <li><img src="http://placehold.it/100x100&text=5"></li>
  <li><img src="http://placehold.it/100x150&text=6"></li>
  <li><img src="http://placehold.it/100x100&text=7"></li> </ul>

Answer №2

When the value of two is set to float left, any space or elements that can also float will align themselves accordingly.

To achieve the behavior you mentioned, with number 4 stacking below number 1, using inline-block might be more suitable.

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

Creating a jQuery plugin with configurable options but without attaching it to any specific HTML element can be

I am currently working on developing a unique plugin that does not require a DOM element but comes with default options pre-set. However, upon executing the code snippet below, I encounter the error message TypeError: $.myApp is not a function. What modi ...

In Wordpress, take advantage of the themes options page in the admin section to easily insert custom HTML into the header.php file when a

This is my first attempt at creating a custom theme options/settings page for my theme. I'm currently working on implementing a slider feature, but I want to provide the flexibility to disable it if needed. To achieve this, I am planning to include a ...

JQuery Falters in Responding to Button's Click Action

Forgive me for what may seem like a silly question, but as someone new to JQuery, I have been struggling to figure out why my function is not displaying an alert when the button is clicked. Despite thorough research on Google, I haven't been able to f ...

Difficulty in packaging JavaScript resources within Laravel Project

Having trouble setting JS functions as global in my project: In the 'resources\js"', I have: numerosALetras.js: /////////////////////////// function unidades_nal(n){ ... } function decenas_nal(n){ ... } function centenas_nal(n){ ...

Stable header that jumps to the top when scrolled

I have implemented the JavaScript code below to set the header to a fixed position when it reaches the top of the page so that it remains visible while the user scrolls. Everything appears to be functional, but the header movement is abrupt and not smooth. ...

Utilizing jQuery and ajax to invoke an MVC controller

Hi there, I am currently facing an issue with calling a method in my controller using ajax and jquery with parameters Controller: [HttpPost("{Id}")] public ActionResult PostComment(int Id, ShowViewModel model) { } View: I have a button named AddCommen ...

display a container and navigate to the specific link

Greetings! Could someone please help me make this code function properly? I am attempting to display the DIV (slidingDiv) and navigate to the selected ANCHOR (#anchor-01 + #anchor-02 + #anchor-03)... However, the code currently only allows me to go to the ...

The oval shape of a Canvas circle in HTML5 is due to its width and height properties

var qcanvas = $('#canvas'); var canvas = ctl_canvas[0]; var context = canvas.getContext('2d'); qcanvas.css('border', '1px solid black'); qcanvas.css('width', 400); qcanvas.css('height', 75); Afte ...

Positioning Input and Dropdown Elements within a Data Table Using CSS

I am currently working on setting up a data table for an application using Vue.js, and I am facing a challenge in relation to the arrangement of input elements. The specific requirement is that certain columns within the table should contain values that ar ...

Utilize dynamic Google Maps markers in Angular by incorporating HTTP requests

Struggling to find a solution for this issue, but it seems like it should be simple enough. Initially, I fetch my JSON data using the code snippet below: testApp.controller("cat0Controller", function($scope, $http){ var url = "../../../Data/JSONs/randomd ...

Finding the Closest Element with jQuery's .closest() Method

I'm facing an issue while trying to select an element that is positioned above another element. Specifically, I want to target the nearest occurrence of the .discount-dropdown class that appears above the .discount-type class. Can anyone help me figur ...

How can I deliver assets in React without the PUBLIC_URL showing up in the path?

I have set up a portfolio website that includes my resume. I am trying to make it so that when someone visits the route http://localhost:3000/resume.pdf, they can view my resume directly. The resume.pdf file is located in my public folder. However, instead ...

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 ...

Command line functionality to eliminate comments in HTML minifier

I am interested in utilizing html-minifier to compress my html documents. After executing the command npm install -g html-minifier, I have successfully installed it. However, the command sudo html-minifier rb.html --removeComments did not eliminate comme ...

Using AJAX to submit data and displaying the results in either an input field or a div

Having a PHP script to track button clicks to a text file <?php if (isset($_POST['clicks1'])) { incrementClickCount1(); } function getClickCount1() { return (int) file_get_contents("count_files/clickcount1.txt"); } function increme ...

What is the best way to determine which watchers are triggered in Vue.js?

Within my parent component, there are numerous nested child components. Whenever a click occurs on one of the child components, it triggers an update to the route. The parent component watches the route property and performs certain actions when it change ...

Methods for bypassing a constructor in programming

I am working on a code where I need to define a class called programmer that inherits from the employee class. The employee class constructor should have 4 parameters, and the programmer class constructor needs to have 5 parameters - 4 from the employee c ...

Is there a way to alter and send back this using a jQuery addon?

After consulting this thread on Stack Overflow about jQuery.unique on an array of strings, I was able to create a jQuery plugin with some guidance. Here is the code for my plugin: // Custom function $(function() { $.fn.removeDuplicates = function() { ...

react component not displaying image

I must admit, this might be a silly question but I'm just starting out in web development. My goal is to create a clickable image component that redirects to another page on the website. However, despite not encountering any errors, the image is not ...

How to Use Vanilla JavaScript to Fetch a JSON File, Convert the Data into an Array, and Iterate Through Each Object

Imagine having a JSON file called map.json: { "images":{ "background": ["images/mountains.png","images/sea.png"] } } The goal is for JavaScript to retrieve "images/mountains.png" from map.json and us ...