Align Bootstrap list items vertically

I'm facing a challenge with aligning the yellow list item blocks vertically in my design. The issue arises because the black dog item is not aligning properly due to its image being shorter than the other two blocks. I've been struggling to find a way to vertically align it so that it starts at the top like the other two blocks.

.gallery {
  width: 100%;
  margin: 0;
  padding: 0;
  vertical-align: top;
}

.grid {
  vertical-align: top;
}

.gallery.grid li {
  margin: 2px 5px;
}


.gallery.grid li {
  margin: 2px 5px;
  display:block;
}

.gallery.grid li:hover {
    background:#999;
}

.gallery.grid li {
  display: inline-block;
  border-top: 1px solid #eee;
  border-right: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  border-left: 1px solid #eee;
  padding: 6px;
  position: relative;
  -moz-box-sizing: border-box;
  border-radius: 3px 3px 3px 3px;
  width:200px;
  background:#000;
  color:#fff;
}

.gallery a {
    display: block;
    color:#fff;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<ul class='gallery grid'>

    <li>
        <h5>The Black Dog</h5>
        <div style='text-align:center; height:400px;'>
            <img class='img-fluid rounded img-thumbnail' src='https://i.scdn.co/image/01ffab20313cca9b5d95b70a97d02e85a79a03c7' width='200' height='200'>
        </div>
    </li>

    <li>
        <h5>Coldplay</h5>
        <div style='text-align:center; height:400px;'>
            <img class='img-fluid rounded img-thumbnail' src='https://i.scdn.co/image/6397b6a29c8d9081412e09feb53600f8c9a18313' width='200' height='200'>
        </div>
    </li>

    <li>
        <h5>The Corries</h5>
        <div style='text-align:center; height:400px;'>
                <img class='img-fluid rounded img-thumbnail' src='https://i.scdn.co/image/ab67616d00001e02a5a4803b3c9e3bd486505bc0' width='200' height='200'>
        </div>
    </li>

</ul>

Would it be feasible to vertically align the first block, even if the image inside it is of different dimensions compared to the other 2 blocks?

Answer №1

Apply vertical-align: top to .gallery.grid li. Here's the code snippet:

.gallery.grid li {
    ...
    vertical-align: top;
}

This is important because this styling should be applied to specific elements for alignment, not on the parent element.

Check out the updated CSS below:

.gallery {
  width: 100%;
  margin: 0;
  padding: 0;
  /*vertical-align: top;*/ /*remove this it*/
}

.grid {
  /*vertical-align: top;*/ /*remove this it*/
}

.gallery.grid li {
  margin: 2px 5px;
}


.gallery.grid li {
  margin: 2px 5px;
  display:block;
}

.gallery.grid li:hover {
    background:#999;
}

.gallery.grid li {
  display: inline-block;
  border-top: 1px solid #eee;
  border-right: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  border-left: 1px solid #eee;
  padding: 6px;
  position: relative;
  -moz-box-sizing: border-box;
  border-radius: 3px 3px 3px 3px;
  width:200px;
  background:#000;
  color:#fff;
  vertical-align: top; /*add this it*/
}

.gallery a {
    display: block;
    color:#fff;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<ul class='gallery grid'>

    <li>
        <h5>The Black Dog</h5>
        <div style='text-align:center; height:400px;'>
            <img class='img-fluid rounded img-thumbnail' src='https://i.scdn.co/image/01ffab20313cca9b5d95b70a97d02e85a79a03c7' width='200' height='200'>
        </div>
    </li>

    <li>
        <h5>Coldplay</h5>
        <div style='text-align:center; height:400px;'>
            <img class='img-fluid rounded img-thumbnail' src='https://i.scdn.co/image/6397b6a29c8d9081412e09feb53600f8c9a18313' width='200' height='200'>
        </div>
    </li>

    <li>
        <h5>The Corries</h5>
        <div style='text-align:center; height:400px;'>
                <img class='img-fluid rounded img-thumbnail' src='https://i.scdn.co/image/ab67616d00001e02a5a4803b3c9e3bd486505bc0' width='200' height='200'>
        </div>
    </li>

</ul>

Answer №2

Make sure to assign a unique identifier to your image

<img class='img-fluid rounded img-thumbnail' id="uniqueID123" src='https://i.scdn.co/image/01ffab20313cca9b5d95b70a97d02e85a79a03c7' width='200' height='200'>

CSS:

#uniqueID123{
    margin-top: 30px;
    margin-bottom: 30px;
}

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

The method _react.default.useContext does not exist as a function

I am currently working with Material UI Stepper. After using it, I noticed that the functionality is not working properly as expected from their website. To troubleshoot, I added a console.log inside the VerticalLinearStepper method. Upon checking the cons ...

Customizing Your JQuery.awesomeCloud.plugin with a Tooltip Functionality using jQuery

Is it possible to integrate a jQuery Tooltip feature when hovering over specific words in a word cloud? I am currently using [jQuery.awesomeCloud.plugin]:https://github.com/indyarmy/jQuery.awesomeCloud.plugin If there is an alternative method to add toolt ...

The br tag in HTML cannot be utilized in conjunction with JavaScript

I am currently working on a project involving HTML and JavaScript. I have a "textarea" where data is inserted into the database upon pressing the "Enter key." However, I am encountering two issues: Currently unable to save data like "Lorem Ipsum" (the ...

The container's height is determined by the CSS top position of the last child element within the container, with a

I'm attempting to adjust the height of a container based on the position of its last child using Isotope, infinite scroll, and Packery. Below is a snippet of the code: <html> <div id="container" class="variable-sizes isotope" style="p ...

Ways to align content in the center using Vuetify3

I have a component positioned at the top of the page, but I would like it to be centered instead. https://i.sstatic.net/wSFBP.png Something like this: https://i.sstatic.net/rfqAB.png Here is my current code: <template> <v-container class=" ...

Vue.js and webpack are having trouble loading the images located in the "assets" folder into the Vue components

How can I import images from an assets folder into Vue components? Here is my "Card.vue" component: <template> <div class="container"> <div class="card" style="width: 18rem;"> <!-- Use this <img> tag to load the ima ...

Discovering the CSS code for customization

As a beginner, I apologize in advance for any silly questions ˆˆ I am currently working on a website using bootstrap. Everything is functioning correctly, but I am struggling to override some CSS styles. In the image provided, you can see how I used the ...

Incorporate a fresh button into the Tinymce toolbar using C# programming

I have utilized Tinymce text editor/textarea in my webpage. How can I incorporate an additional button onto the toolbar? For instance, upon clicking the added button, it should prompt a dialog with three textfields: title, age, and gender. Upon filling ou ...

Is there a way to replicate the Vista Command Link in HTML?

This particular dialog mimics the design of a Windows Vista Command Link: View image here: http://i.msdn.microsoft.com/Aa511455.commandlinks01(en-us,MSDN.10).png I'm curious if anyone has created HTML code that replicates the appearance and function ...

Shifting the div with a sliding animation!

My webpage features a video background with text overlay, and I am looking to add a button in the center of the page. When users click on this button, I want the current text div to slide up using a CSS transition, revealing another div with the same effec ...

My custom style sheet not taking precedence over Bootstrap styles

Using !important is something I try to avoid as much as possible. When I downloaded Bootstrap 3.0, I made sure to include the call to the .css file AFTER my core style sheet so that any changes I make to elements like .h1, h1{} take precedence over Bootstr ...

Is a Responsive Split Layout the Solution for Your Web Design Needs?

I'm looking to create a webpage layout where the left half of the viewport is for Login and the right half is for Signup. This layout should be split side by side on tablets, desktops, and large screens, but stacked vertically (Login on top, Signup on ...

Utilizing Binding Time in Angular for Enhanced Views

I am completely new to Angular JS. I have a simple question that I have been searching for answers on SO. My goal is to display the current time in real-time (refreshing every second as the clock ticks) on my view. Here's what I have tried so far: H ...

The CSS styles are not being completely applied to the PHP function

My current task involves dealing with multiple folders containing images. When a link is clicked on the previous page, a unique $_GET variable is sent and processed by an if statement, triggering a function to search for and display all pictures within the ...

How can I avoid C3.js legends from overlapping when hiding or showing a div?

Every time I visit a specific page, a simple chart is automatically generated: function displayOptions() { $("#div1").show(); chartRef.flush(); } function displayChoices() { $("#div1").show(); } $("#div1").hid ...

Steps for setting up a barcode scanner on a smartphone (both iPhone and Android) using zxing in an html app

After reviewing different answers to my questions, I decided to try out some solutions below. The app and camera function properly on Android devices, but not on iPhones. What steps should I take? <!DOCTYPE html> <HTML> <HEAD> <script ...

background with alternating stripes to decorate the border of a div

Is it possible to give a striped effect to a div element's border using jQuery? Consider the following code snippet: <style type="text/css"> #panel { padding: 50px text-align: center; background-color: #e5eecc; bord ...

Parent div overflowing due to vertical flex container

In my current project, I am attempting to design a layout with a fixed header and footer along with a dynamic content area that utilizes the remaining vertical space. The content-wrapper contains a lot of data, which may require a scrollbar to navigate. H ...

What exactly is the CSS element grouping operator?

I am investigating the concept of element grouping in CSS. Consider the following code snippet: .user-form .group-area .test, .user-form .group-area .test2{} Is it possible to group .test and .test2 elements together like this: .user-form .group-area (. ...

Optimal method for streaming and viewing an mkv video captured with ffmpeg in a web browser

Is there a way to stream a video recorded with ffmpeg in a web browser using the HTML5 video tag? I currently have this code on my page, with the URL pointing to a video file (example.mkv) being recorded by ffmpeg. <video class="video-player" controls ...