"To ensure consistent alignment, position list items with varying heights at the bottom of the

Is there a way to align a series of li items to the bottom of their parent, the ul, without using any unconventional methods? The challenge lies in the fact that these li items need to have a float: left property and vary in height and width.

Below is my current code:

ul {
  width: 1000px;
  height: 400px;
  float: left;
  vertical-align: bottom; /* Although present, it doesn't influence the list items due to the float: left */
}

ul li {
  float: left;
  display: inline-block;
}

ul li figure {
  margin: 0; padding: 0;
  width: 150px;
  height: 150px;
  background: red;
}

ul li:nth-child(2n) figure {
  width: 300px;
  height: 300px;
}
<ul>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
</ul>

Ideally, I want to achieve the layout shown in the following image, maintaining variable widths for the ul and with no gaps between list items:

https://i.sstatic.net/74Trr.png

Answer №1

Take a look at the code snippet provided. The fixed width on the ul ensures an equal distribution of the li elements with table-cell. If you want to adjust the spacing between the li elements, you will need to make changes accordingly. Without specific instructions on how you want the layout to appear, I have left it as is.

To align the content at the bottom, this approach seems suitable:

ul {
  height: 400px;
  float: left;
  display: table;
  list-style: none;
}

ul li {
  display: table-cell;
  vertical-align: bottom;
  margin: 0;
  padding: 0;
}

ul li figure {
  /* determines the width & height of the list item */
  margin: 0; 
 padding: 0;
  width: 150px;
  height: 150px;
  background: red;
  display: inline-block;
}

ul li:nth-child(2n) figure {
  width: 300px;
  height: 300px;
}
<ul>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
</ul>

Answer №2

When it comes to vertical alignment, remember that float is not compatible with it.

  • inline-block & vertical-align solution:

ul {
  width: 1000px;
  height: 400px;
  float: left;
}

ul li {
  /*float: left;*/
  display: inline-block;
  vertical-align:bottom
}

ul li figure {
  margin: 0; padding: 0;
  width: 150px;
  height: 150px;
  background: red;
}

ul li:nth-child(2n) figure {
  width: 300px;
  height: 300px;
}
<ul>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
</ul>

  • display:flex & align-items method:

ul {
  width: 1000px;
  height: 400px;
  display:flex;
  align-items:flex-end;
  
}

ul li {
display:block;
float:right;
margin:1px;
}

ul li figure {
  margin: 0; padding: 0;
  width: 150px;
  height: 150px;
  background: red;
}

ul li:nth-child(2n) figure {
  width: 300px;
  height: 300px;
}
<ul>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
</ul>

  • display:grid approach

* {margin:0;padding:0;}
ul {
  width: 1000px;
  height: 400px;
  float: left;
  display:grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 0));
  grid-auto-flow: dense;
  grid-auto-rows: minmax(150px, 300px);
  grid-gap: 1px;
  align-items: end;
}

ul li {
  display:block;
}
ul li:nth-child(2n) {
  grid-column: span 2;}

ul li figure {
  margin: 0; padding: 0;
  width: 150px;
  height: 150px;
  background: red;
}

ul li:nth-child(2n) figure {
  width: 300px;
  height: 300px;
}
<ul>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
  <li>
    <figure>
      <img />
    </figure>
  </li>
</ul>

  • display:table option

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

Position a div in the center of the page horizontally

Can someone help me with aligning the #main div in the center of the page? I have this code snippet: <div id="main" style=" margin:0 auto; "> <div style="float: left"> <a style="display: block" href="#"><img src="test.gif" ...

What steps can I take to ensure a website designed for Firefox will work seamlessly on Safari and Chrome browsers as well?

As someone who is still learning about web development, I find myself struggling with browser compatibility issues. It's frustrating to see my website looking different in Chrome compared to Safari. While I know that browsers interpret code differentl ...

choose the <li> element with no <a> tag

Hello there, I am looking for a way to keep the padding consistent in <li> elements, but have it applied to the <a> tag instead when it's a link (to create a larger selection area). li a { padding: 1rem; } li:not(a) { padding: 1rem ...

Make the div block fill the entire body by setting its width to 100%

I am attempting to make the center block (which has a grey background) expand to the edges of the browser. By utilizing the code below, I have been successful in achieving this: #aboutBlock { background: #f2f2f1; position:absolute; left:0; ...

Welcome to the awe-inspiring universe of Typescript, where the harmonious combination of

I have a question that may seem basic, but I need some guidance. So I have this element in my HTML template: <a href=# data-bind="click: $parent.test">«</a> And in my Typescript file, I have the following code: public test() { alert( ...

Styling list items (li) on hover without using the li:

I have a menu where I am using the :after element. When I hover over a li in the menu, the hover effect also includes the :after element. HTML: <ul class="main-menu"> <li class="active"><a href="">Menu</a></li> <l ...

Having trouble resolving the signature of a class decorator when invoked as an expression with @Injectable in Angular

Error Message: Unable to resolve the signature of a class decorator when called as an expression. The argument type 'ClassDecoratorContext' is not compatible with the parameter type 'string | symbol | undefined'. After creating a book ...

In IOS 9.0, flex items do not wrap to new lines and result in overflow

While it functions properly in other browsers, the issue arises in Safari. I require the flex items to occupy the entire width on screens smaller than 768px. Currently, they are taking up the full width of their container but remaining in a single line, wh ...

Creating a smooth image transition effect using CSS

I have successfully implemented a code that allows for crossfading between images when the mouse hovers over them. Now, I am looking to modify the behavior so that the crossfade effect happens only once when the mouse passes over the image. In other words ...

The Mat-slide-toggle resembles a typical toggle switch, blending the functionalities of

I am facing an issue with a `mat-slide-toggle` on my angular page. Even though I have imported the necessary values in the module, the toggle is displayed as a normal checkbox once the page loads. HTML: <div style="width:100%;overflow:hidden"> < ...

The inclusion of the <div> tag disrupts the functionality of the Material UI Grid

While attempting to enclose my element within a <div> that is nested inside the <Grid>, I realized that this was causing the <Grid> layout to break! Surprisingly, when I tried the same approach with Bootstrap grid system, this issue did n ...

What is the alternative to using echo when outputting HTML?

I am working on a function... $post_text .= '<div style="font-size: 15px; color: blueviolet">'; $post_text .= "<br>"; $post_text .= 'Text number 1.'; $post_text .= "<br>"; $post_text .= 'Text number 2.'; $po ...

HTML: Attempting to extract the inner div from the outer div and relocate it outside of the outer div

I am attempting to extract an inner div from an outer div and relocate it outside of the outer div. https://i.stack.imgur.com/NSS7B.png <div class="vc_gitem-zone-mini"> <div class="vc_gitem_row.............."></div> <div> I Would ...

Ways to center a div with position:relative vertically on the page

What is the best way to center a position relative div vertically within its parent element? For example: <div class="parent"> <div style="position:relative;" class="child"> </div> </div> Any suggestions on how to vertic ...

JavaScript Function Not Executed in Bottom Section

I've implemented AngularJS includes in my project. Below is the code snippet from my index.html: <!DOCTYPE html> <html ng-app=""> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> ...

Is it possible to apply CSS styling to all placeholders in a Sencha Touch application?

Having trouble figuring out how to style all the placeholders in my application with CSS. I've attempted a few different methods: .customField input::-webkit-input-placeholder { color: #2e4bc5; } .x-text-field input::webkit-input-placeholder{ c ...

Is it possible to achieve a height transition using CSS instead of relying on JavaScript

I created these custom cards using a combination of HTML, CSS, and JavaScript. However, I've noticed that the height transition animation is not as smooth as I'd like it to be, especially on certain pages. Is there a way to achieve this animation ...

Guide: Positioning the Icon in the Top Right Corner of the Image

Objective: The goal is to ensure that the expand icon is always at the upper right corner of the image, regardless of its size or the responsive design in place. Challenge: I have attempted to address this issue but have unfortunately been unsuccessfu ...

Stretching Images on Circular Images - Utilizing Bootstrap and SCSS styling

Can anyone assist me with this problem? I am currently utilizing bootstrap cards to create a section on my website. However, I am encountering an issue with the circular images of the cards. I want to prevent my images from getting stretched when placed i ...

What are the drawbacks of using JavaScript to load a series of images?

Currently, I am facing an issue with the loading speed of a sequence of images on my website. The javascript code I have implemented to cycle through 50 images works perfectly fine locally but becomes slow and buggy when the site is live. Despite the image ...