Unusual floating div phenomenon

I am encountering an issue while attempting to generate a UL list that includes images and descriptions. When there are 5 items in the list, everything looks good, but as soon as the 6th item is added, it floats to the right, and the 7th item falls down and then floats to the left. What could be the cause of this behavior? I have observed this problem in both IE 8 and Firefox 13.

https://i.sstatic.net/ihRXd.jpg

Here is the HTML code:

<ul class="subpagesList subpagesGallery" id="subList2"><li class="l1">
      <div class="photo">
        <a href="?galeria-1,15"><img src="files/150/_demo_01.jpg" alt="" /></a>
      </div>
      <div class="descriptionBox">
          <h2><a href="?galeria-1,15">Galeria 1</a></h2>
          <div class="description"><p>Galeria 1</p></div>
      </div>
    </li><li class="l2">
      <div class="photo">
        <a href="?galeria-2,16"><img src="files/150/_demo_02.jpg" alt="" /></a>
      </div>
      <div class="descriptionBox">
          <h2><a href="?galeria-2,16">Galeria 2</a></h2>
          <div class="description"><p>Galeria 2</p></div>
      </div>
    </li><li class="l3">
      <div class="photo">
        <a href="?galeria-3,17"><img src="files/150/_demo_03.jpg" alt="" /></a>
      </div>
      <div class="descriptionBox">
          <h2><a href="?galeria-3,17">Galeria 3</a></h2>
          <div class="description"><p>Galeria 3</p></div>
      </div>
    </li><li class="l4">
      <div class="photo">
        <a href="?galeria-4,18"><img src="files/150/_demo_04.jpg" alt="" /></a>
      </div>
      <div class="descriptionBox">
          <h2><a href="?galeria-4,18">Galeria 4</a></h2>
          <div class="description"><p>Galeria 4</p></div>
      </div>
    </li><li class="l5">
      <div class="photo">
        <a href="?galeria-5,19"><img src="files/150/_demo_05.jpg" alt="" /></a>
      </div>
      <div class="descriptionBox">
          <h2><a href="?galeria-5,19">Galeria 5</a></h2>
          <div class="description"><p>Galeria 5</p></div>
      </div>
    </li><li class="lL">
      <div class="photo">
        <a href="?galeria-6,20"><img src="files/150/_demo_01.jpg" alt="" /></a>
      </div>
      <div class="descriptionBox">
          <h2><a href="?galeria-6,20">Galeria 6</a></h2>
          <div class="description"><p>Galeria 6</p></div>
      </div>
    </li></ul>

And here is the corresponding CSS code:

/* STYLING FOR SUB PAGES LIST */
.subpagesList {
    float: left;
    width: 100%;
    margin: 7px 0;
    list-style: none;
}
.subpagesGallery {
    /*width: 100%;*/
    overflow: hidden;
    width: 800px;
}

.subpagesList li {
    clear: left;
    float: left;
    width: 96%;
    margin: 10px 0;
    padding: 13px 2%;
    border-left: 2px solid #e7eaee;
    background: url('img/items_shade.png') repeat-x left top;
}
.subpagesGallery li {
    clear: none;
    float: left;
    margin: auto auto;
    padding: 0 0;
    overflow: hidden;
    width: 150px;
}

* html .subpagesList li {
    width: 100%;
}

.subpagesList li .photo {
    float: left;
    margin: 0 18px 5px 0;
}
.subpagesGallery li .photo {
    clear:both;
    overflow: hidden;
}

.subpagesGallery li .descriptionBox{
    clear:both;
    width:180px;
    overflow: hidden;
}

.subpagesList li h2 {
    padding-bottom: 10px;
}

Answer №1

You inquired about the reason behind this occurrence, rather than how to rectify it. Below is an illustration that should shed light on what is at play.

The divs are positioned to float left; as a result, the 6th div drops below the top 5 while aligning itself to the left. However, due to div 5 being shorter than div 4, div 6 ends up adjacent to div 4 and cannot shift further left. Consequently, div 7 descends past div 6 and has the ability to transition all the way to the left.

Dealing with divs can certainly be tricky.

Answer №2

The issue was due to the absence of a 'height' attribute for the 'LI' elements in the code. Specifically, the 'Gallery 5' element appeared shorter than the others and the 'Gallery 6' element was not floating correctly, instead appearing below 'Gallery 5'. After adding the 'height' property with a value of 200px, the problem was resolved.

.subpagesGallery li {
    clear: none;
    float: left;
    margin: auto auto;
    padding: 0 0;
    overflow: hidden;
    width: 150px;
    height:200px; /*that helped*/
}

It seems that seeking help on platforms like SO can lead to new insights and solutions. Perhaps it does make you smarter after all...

Answer №3

To ensure each <li> element has the same height, you can utilize JQuery equal height method. Take a look at the code snippet provided below:

function equalHeight(group) {
   tallest = 0;
   group.each(function() {
       thisHeight = $(this).height();
       if(thisHeight > tallest) {
         tallest = thisHeight;
       }
    });
    group.height(tallest);
}

 $(document).ready(function() {
    equalHeight($(".subpagesList li"));
 });

Check out this tutorial for more information.

Answer №4

My recommendation would be to set a min-height for the elements. By doing this, you ensure that there is always a minimum height maintained regardless of the size of the image or content, allowing the content to expand as needed. It's important to consider the maximum height of the <li> element to avoid encountering this problem in the future.

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

Toggle Checkbox Group for Both Multiple and Single Selection Options

Need help with a function to enable only one checkbox for single selection or multiple checkboxes for multiple selection. Use MultipleCheckbox(0) for single selection or MultipleCheckbox(1) for multiple selection. function MultipleCheckbox(elem){ i ...

Turn off the debugger statement using your web browser

I have a piece of code with the debugger keyword and I want to style it. However, every time I refresh the page, the browser's debugging window (IE, FF, Opera) stops at the debugger line. Is there a way to toggle or disable the debugger keyword throu ...

Search the database using a single text field and then automatically fill in the remaining fields using PHP

Being a beginner in PHP and Ajax, I find myself quite lost in the process. I have set up a MySQL database and created several forms in PHP. One of the forms is supposed to display customer information with text fields, including a search field for phone nu ...

Having trouble getting overflow:auto to work with CSS3 transformed child elements? Looking for a recommended workaround?

Issue: Browsers (FF5, Chrome12, IE9) are not recognizing css3 transforms applied to a child element inside a div when calculating the scrollHeight and scrollWidth of the containing div's scrollbars with "overflow: auto;". <style type="text/css"> ...

issues with rounded corners not functioning properly within frameset

I'm trying to display rounded corners in IE frameset, is there a way to do this? Check out the example below: index.html: <html> <head><title></title></head> <frameset rows="*,64" style="margin:0; padding: 0;" frame ...

Bug in Sublime Text's Autocomplete Feature for HTML Tags

Recently, I've encountered a small bug with my Sublime Text 3. Every time I try to auto-complete certain HTML tags, an extra '<' is added before the first tag, forcing me to manually delete it each time. I'm currently using build 317 ...

Failure to correctly apply CSS to Angular custom components causes styling issues

I have been working with a custom component and I have incorporated several instances of it within the parent markup. When I apply styles directly within the custom component, everything works as intended. However, when I try to set styles in the parent co ...

Retrieve the source code of an HTML element

When you run this code, you will see the output as [object HTMLUListElement]: ul = document.createElement('ul'); $('textarea').val(ul) Is it possible to get the output as <ul></ul> directly? The following solution wo ...

Include some text before and after the bar element

I am attempting to insert text preceding and succeeding the bar, as depicted in the image below. <div class="container"> <div class="progress progress-info progress-striped"> <div id="storage-component" class="bar" style="wi ...

Prevent past date and time selection in "date" text-box mode using ASP.NET

Is there a way to prevent users from selecting past dates and times in a textbox with TextMode set to DateTime? I was able to achieve this using a calendar control, but my specific requirement is to implement it only within the textbox itself. protected ...

PHP causing redirection problems on the list page

Working on PHP and fetched all data from the database to display on a listing page. Trying to show dynamic data in the listing but facing difficulty in redirecting to the listing page. Need help with checking the code. Below is my HTML code: <div clas ...

Challenges with adjusting the value of HTML range inputs

Within my HTML, I have incorporated the following three elements: <button id="left" >Previous</button> <input id="animSlider" type="range" min="0" max="0" step="1" value="0" /> //THE MAX VALUE IS SET DYNAMICALLY UPON PAGE LOAD, NOT AS ZE ...

PHP updating data in a MySQL database

I've been using easyPHP and encountering issues with updating records in my database. Whenever I try to update, the HTML form displays <?php echo $btitle; ?> and <?php echo $bauthor; ?> instead of updating the data. However, it does print ...

aligning elements on various screens

Is there a way to achieve this layout on various screen sizes? The grey rectangles represent divs with content. Picture 1 (left) is for normal widescreen displays, picture 2 (middle) caters to smaller screens like netbooks or tablets, and picture 3 (righ ...

Does Blazorise support the Material .figure-is-16x16 class?

While Blazorise supports the .figure-is-16x16 class, I have noticed that when using it in Material, it does not work by default. I am curious if the Material version of Blazorise supports this class or if there is another related class available. Current ...

Is there a way for me to position my chat messages on the right side in the chat room?

I have a react chat application and I'm looking to customize the appearance of my messages. Currently, all entries with an orange vertical bar belong to me and are displayed on the left side of the chat room. I would like to move them to the right sid ...

Using Bootstrap 4 beta for Form Validation

I'm struggling to grasp the concept of building validation forms. The bootstrap documentation includes a JS code snippet (referred to as starter code) like the one below: <script> // Example starter JavaScript for disabling form submissions if ...

What is the best way to navigate to a particular div within an iframe?

In one of my div elements, I have embedded an iframe with a source URL like this: <div> <iframe ng-src="{{somesourceurl}}"></iframe> </div> The content loaded by the iframe contains HTML with uniquely identified div tags as sh ...

Tips for adjusting the layout of a section class using Tailwind CSS according to screen size?

I'm really struggling with implementing my latest idea and could really use some help: I've designed this article preview section that looks amazing on small screens like mobile devices: https://i.sstatic.net/nWENQ.png <section class="w ...

The datepicker in Jquery is hidden beneath a div

I've been using this datepicker in my code for a while now. Recently, I made some changes to the HTML and added AJAX calls. However, when I click on the input field, the datepicker doesn't show up. It seems like it's been added to the DOM co ...