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

Solving routing issues using object constructors in expressjs

Currently, I am in the early stages of developing an API REST using express and nodejs. As part of my routing process, I have decided to create separate "controllers" for each route and call these controllers within a router file. For example: ... router. ...

Refreshing a div using ajax technology

I am attempting to use Ajax to update an h3 element with the id data. The Ajax call is making a get request to fetch data from an API, but for some reason, the HTML content is not getting updated. This is how the JSON data looks: {ticker: "TEST", Price: 7 ...

Unable to retrieve the value of ng-model using $scope

Having some trouble getting the ng-model value when clicking a button that triggers a function to add each ng-model value to an object. Interestingly, when trying to get the value of $scope.shipNameFirst, it shows up as undefined in the second example. I& ...

Difficulties encountered while attempting to modify a class using Javascript

Recently, I've encountered an issue with my JavaScript where I am unable to keep a particular element's class changed. Despite attempting to change the class to "overlist", it only stays that way briefly before switching back to its original stat ...

"Entering a text message will prompt the appearance of the on-screen keyboard in capital

Our website is optimized for use on Android tablets. I have added the following CSS to the input's class: text-transform: uppercase; While this successfully displays typed letters in uppercase, it does not change the appearance of the on-screen keyb ...

Pass the ID and content of a textarea element by utilizing the AJAX post method and incorporating SweetAlert version 2 for a seamless and

function solveTheIssue(id) { promptUser({ title: "Share your complaint", input: "textarea", showCancelButton: true, confirmButtonColor: "#DD8B11", confirmButtonText: "Yes, submit it!", ...

Is it possible to alter the font size in Vuetify depending on the viewport size?

Looking to customize font sizes based on viewports? <v-card-text style="font-size:5em"> Some Heading Here </v-card-text> If you want to change the font size to 3em on XS view, without duplicating code, consider using CSS only. Avoid dupli ...

The importance of dependencies in functions and testing with Jasmine

In troubleshooting my AngularJS Service and Jasmine test, I encountered an issue. I am using service dependency within another service, and when attempting to perform a Unit Test, an error is thrown: TypeError: undefined is not an object (evaluating sso ...

The partnership between Selenium and WhitePages has created an innovative solution

I am currently attempting to register on . The registration process requires entering my first name, last name, email address, and password. For the first name field, I attempted to locate the element by CSS using the syntax "input#name_fname", however, I ...

Extracting data from websites using Python's Selenium module, focusing on dynamic links generated through Javascript

Currently, I am in the process of developing a webcrawler using Selenium and Python. However, I have encountered an issue that needs to be addressed. The crawler functions by identifying all links with ListlinkerHref = self.browser.find_elements_by_xpath( ...

Is there a way to verify the results of a Python script within a PHP webpage?

For my school project, I am creating a PHP website where I want to implement a Python code Quiz. I envision a scenario where users can input Python code in an on-page editor/IDE and the output is checked automatically using PHP If-function to determine cor ...

I am struggling to link my JS application with a PHP file as it is showing "PHP file not found

I am facing an issue with my JS application. I am unable to send data from jQuery to a PHP file, as I encountered this error message: POST https://magazyn.rob-tech.pl/savesettings.php 404 The app is running on PORT=8080 npm run start and the package.json ...

Ways to configure asp.net-mvc6 to deliver JSONP instead of JSON

I'm currently navigating the world of asp.net-mvc6 and encountering some obstacles. The documentation isn't quite cutting it for me, so I'm turning to the community for some assistance. I've created a small website with just one page, ...

Create a generic function that retrieves a specific property from an array of objects using the select method

Currently, I have implemented four functions that select entries from an array based on different properties. if ($scope.filters.filter1) $scope.filteredEntries = $scope.filteredEntries.filter(function (o) { return o.field1 === $scope.filt ...

What is the best method to reset the chosen option in a dynamic select dropdown using React?

I have a form set up with a Select dropdown that is populated dynamically from the server. The issue I'm facing is that after selecting an option from the dropdown and then saving or canceling the form, the selected value remains in the field when I ...

Changes in date format using jQuery datepicker

I am having trouble with the code below. Whenever I attempt to change the date, it switches from 15/5/2012 to 05/15/2012. <script> $(document).ready(function(){ $("#date").datepicker({ }); var myDate = new Date(); var month = myDa ...

Avoiding type errors in d3 v5 axis by using Typescript

I am new to TypeScript and I have some code that is functioning perfectly. I believe if I define a type somewhere, d3's generics will come into play? Within my code, I have an xAxis and a yAxis. Both are the same, but D3 seems to have an issue with t ...

Issue retrieving Select2 tag values from view to controller in ASP.NET MVC 5

I need assistance in transferring tag values from the view to the controller. Here is my approach: I have a model attribute for fetching Select2 values: In the model: public string[] TagList { get; set; } In the view, I have a select element like this: ...

In React Js, the state is being updated correctly when console logging, however, the user interface is not reflecting

Recently, I encountered an issue with updating the UI after clearing the input states in my object. Despite setting the input values to empty strings upon clicking the clear all button, the UI does not reflect these changes as expected. The initial state ...

Ineffective Implementation of While Loop for Div Visibility

Here's a simple code that generates divs using a while loop. However, it doesn't seem to be working as expected. It's actually just basic HTML, nothing too complex. But for some reason, it's not displaying any of the five divs. <? ...