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

Unique CSS-created chronological schedule with alternating design

I am looking to customize my CSS timeline by removing the first line and changing the color of each individual circle. How can I achieve both of these modifications? I attempted to add a class called .not_complete to one of the timeline containers, but it ...

What is the best way to pass the ng-repeat value to the controller in AngularJS

On my webpage, I am trying to utilize ng-repeat with an array like the one below: var array = [{name: Bill, age: 12, number: 1}, {name: Tyrone, age: 11, number: 2}, {name: Sarah, age: 14, number: 3}]; I want to have a button that, when clicked, sends eit ...

Ways to expand the height of a child element within a flex container that has been

I am facing an issue with stretching the children of a column flexbox container. While I can stretch one of the flexbox child elements, I am unable to stretch its nested children. Below is an example where .flex-child-2-child and .flex-child-2-child-child ...

Credit for the Position swipejs

After integrating a swipeJS photo slideshow into my jQuery mobile application, I encountered an issue. I am trying to create points for counting the pictures similar to what is seen on this page: Although I have added the necessary HTML and CSS code to my ...

A comprehensive guide on transferring user input onto a php text file

I am trying to implement a feature in my PHP code where the contents entered in a text box are uploaded to a text file. However, I only want them to be written if the string contains a specific pattern. Here is the modified PHP code: <?php $data = $_ ...

What is the method for displaying Facebook comments alongside the WordPress comments count?

I am currently using a template theme that initially had WordPress comments enabled on the website. By implementing the Facebook Comments plugin, I have successfully replaced Wordpress comments with Facebook Comments. **However, there seems to be an issue ...

The current version of HTML5 Context Menus is now available

I'm in need of implementing the HTML5 Context Menu feature. Currently, only Firefox supports it. My main objective is to add some menu options without replacing the existing context menu. How can I achieve the same functionality now? I am aware of va ...

A unique way to present data: An HTML table featuring four fixed columns with horizontal scrolling

I've been having difficulty creating a table with fixed first four columns while allowing the rest to scroll horizontally. I came across this example first column fixed Although the example has the first column fixed, I'm struggling to extend it ...

Setting the height of columns in a Bootstrap panel to 100%

Is there a way to achieve 100% height for all three columns even without content? Check out this JSFiddle: <div class="row"> <div class="col-md-12"> <div class="shadow panel panel-default"> <div class="blue white-bord ...

Tips for refreshing a specific div element at set intervals using JQuery AJAX?

I need to make updates to a specific div element without refreshing the entire HTML page. The code below currently works, but it reloads the entire HTML page. Additionally, I am working with different layouts where I have separate files for the header, l ...

Setting a div's background using JavaScript works flawlessly on jsfiddle.net, but encounters issues when implemented in actual code

contains the files you need. For reference, here is the link to the code on I have attempted to remove all unnecessary elements from the actual project, but it has not made a difference. document.getElementById("image").style.backgroundImage = "url(" + d ...

Creating a clickable map for a PNG image: Step-by-step tutorial

My goal is to create a interactive map similar to this one. Click here for the image ...

Tips for altering the color of an image using CSS attributes

I am looking to create a male Head component in this design draft and modify the CSS according to the skin prop. I have attempted to use filter and mix-blend-mode properties, but have not found a solution yet. Any guidance on how to achieve this would be ...

Tips for dynamically styling a Styled Component with all the CSS housed in an external file

My goal is to dynamically render a Styled Component. In the past, it was simple because all styling was contained within the component itself. However, I now strive to maintain a separation of concerns by storing the CSS in an external file. While this app ...

Is it possible to simultaneously run multiple functions with event listeners on a canvas?

I'm attempting to create a canvas function that displays the real-time mouse cursor location within the canvas and, upon clicking, should draw a circle. I came across this code snippet that reveals the x and y coordinates of the mouse: document.addEve ...

HTML File Path for C Drive

I am a beginner with HTML and I'm facing an issue regarding displaying an image located at "C:\Users\Jas mine\folder\landscape.jpg" in HTML. I have tried various solutions posted by others but none of them seem to work for me. Can ...

How can I adjust the font size in a TextField when it is in focus?

As a novice in ReactJS, I am currently utilizing materia-ui to design a page. I am looking to make a custom change on a TextField where the font size adjusts when text is entered. However, adjusting the font size creates too much space between the label a ...

Is it possible to share deep links with just a hashtag?

I am currently developing a web application and I want to include social media buttons such as Facebook Like. I have tried using the HTML code/API provided by Facebook, AddThis, Shareaholic, ShareThis, but none of them seem to properly handle my deep link ...

The value entered in the HTML input is not being recognized by PHP

I'm facing an issue with my project (index.php), which is a HTML webpage. I am trying to use the 'include' function to bring in the header and footer (header.php, footer.php). Additionally, I want the current date to automatically populate i ...

Unable to deactivate account, experiencing technical issues

After running once, the code in deactivate_myaccount.php throws an error stating: "Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\includes\includes\header-inc.php on line 2". Furthermore, t ...