Generate three separate inline blocks and position elements within them without any impact on the neighboring blocks

In my attempt to create three blocks with set height and width, aligned on top/bottom with a couple of elements inside each one, I encountered an issue. Whenever I add additional DIVs to one of the elements (resulting in a different number of DIVs inside each block), it causes that element to display one line lower than the rest. Below is an example of the code:

<div class="container">
    <div class="d-inline-block border border-primary" style="width:16%;height:15vh;">
        <div>thing1</div>
        <div>XXX</div>
    </div>
    <div class="d-inline-block border border-success" style="width:16%;height:15vh;">
        <div>thing2</div>
    </div>
    <div class="d-inline-block border border-primary" style="width:16%;height:15vh;">
        <div>thing3</div>
        <div>XXX</div>
    </div>
</div>

Using Bootstrap 5.0, the above code renders with the "thing2" box not aligning properly with the others, being pushed lower.

Do you have any suggestions for resolving this issue?

I've experimented with wrapping elements in additional divs to set max height, as well as tried using position-relative/absolute properties, but I still can't comprehend the effect of one box on another.

Answer №1

If you continue to use display: inline-block, make sure to include vertical-align: top

Check out this example snippet:

.d-inline-block {
    vertical-align: top;
}
<link href="https://cdn.mysite.com/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.mysite.com/js/bootstrap.bundle.min.js"></script>
<div class="container">
        <div class="d-inline-block border border-primary" style="width:16%;height:15vh;">
            <div>thing1</div>
            <div>XXX</div>
        </div>
        <div class="d-inline-block border border-success" style="width:16%;height:15vh;">
            <div>thing2</div>
        </div>
        <div class="d-inline-block border border-primary" style="width:16%;height:15vh;">
            <div>thing3</div>
            <div>XXX</div>
        </div>
    </div>

Answer №2

The issue is arising from the 'd-inline-block' property. To fix this, you can implement a grid system like the one shown below:

<div class="container">
  <div class="row">
    <div class="col-md-4">
      <div class="block border border-primary">
        <div>thing1</div>
        <div>XXX</div>
      </div>
    </div>
    <div class="col-md-4">
      <div class="block border border-success">
        <div>thing2</div>
      </div>
    </div>
    <div class="col-md-4">
      <div class="block border border-primary">
        <div>thing3</div>
        <div>XXX</div>
      </div>
    </div>
  </div>
</div>

<style>
  .block {
    height: 15vh;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
  }
</style>

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

Using the PMT function in PHP allows for easy calculation of

I have been attempting to integrate the PMT function into my PHP code for a loan calculator. Unfortunately, the PHP code seems to be malfunctioning and I am unsure of the reason, especially since I am still at the novice level in programming. PHP(Get.php ...

I noticed a random gap between the elements with no discernible explanation

<div id="colorscheme"> </div> <div id="content"> <div id="display_saved"> TEXT TEXT TEXT </div> Here is the HTML structure related to a particular document issue. CSS: #colorscheme{ width:25%; display:inline-blo ...

Can this functionality be accomplished using only HTML and CSS, without relying on JavaScript?

Image for the Question Is it possible to create a zoom functionality using only HTML and CSS, without relying on JavaScript? I need this feature for a specific project that doesn't support JavaScript. ...

Unlimited horizontal slider control powered by HTML

In my quest to develop a slider control with infinite capabilities, I am looking for a way for users to slide left and right on a bar or panel, allowing them to navigate through past dates when sliding left and future dates when sliding right. While there ...

What is the best way to arrange card-columns in a horizontal order?

My smart-scrolling list of cards uses the card-columns style, but I find it frustrating that they are ordered top to bottom like this: 1 4 7 2 5 8 3 6 9 This vertical ordering becomes impractical when dealing with a large number of items. When I have 50 ...

The -moz-use-text-color property does not function properly in Chrome and IE versions 9 and above

I have added -moz-use-text-color for border-color in the following CSS code: .booksline{ border-image: none; margin: 0 auto 0px; width: 96%; height:220px; padding:20px 20px 20px 0; border-width: 1px 1px medium; border-style: solid solid none; border-colo ...

Guide on submitting a reply to Harvard's "Guess my Word" challenge using JSoup

I am currently working on developing a bot to enhance my gameplay in Harvard's intriguing "Guess my Word!" game. After some exploration, I found out about a post request feature available through Chrome's "inspect element" functionality when a pl ...

What is the process of compiling HTML code?

In controller, I bind the same data like $scope.name = "<div>geetha teko</div>", this name will now be bound to the HTML like {{name}}. When I view it in the browser, it prints as "<div>geetha tek0</div>". Is there a way to only di ...

Ways to emphasize the contrast between two texts using CSS

One method to highlight specific parts of text using CSS can be found in this script: span.highlight { background-color: #B4D5FF; } However, what if we want to highlight the differences between two strings? Take for example these two strings: this ...

How can we use JavaScript to retrieve an element with custom styling?

I've encountered a strange issue with my script where it seems to stack up borders and display a 2px border instead of the intended 1px border when switching elements on click. Here is the link code I am using: <li ><a href="plumbing.php"&g ...

"Troubleshooting: How to Fix Issues with document.getElementById on Dynamic Div

Struggling to incorporate div elements and generate graphs with Google charts? The issue arises in the draw function, where attempts to access a div element using document.getElementById() result in null values and an error message stating "container not ...

Utilizing Compass SCSS with Blueprint for Optimal Overflow Settings

I've been working with blueprint through Compass, but I'm facing an issue where longer pages don't have scroll bars. It seems like the default setting for overflow is 'hidden'. Since this appears to be the standard, I assume there ...

A data table created with Bootstrap is not displayed inline with the pagination feature

Customizing Bootstrap Data Table Code Instructions on how to customize this code: <div class="row"> <div class="col-sm-5"> <div class="dataTables_info" id="example_info" role="status" aria-live="polite"> Showing 1 to 10 of 2 ...

The text following a table is spilling into the table because it is too big to fit within the designated div

I've been searching for a solution to this issue (it's a common problem), but nothing I've attempted has resolved it. Currently, I am using jQuery Mobile. Below is the code snippet in question: <div style="width:100%; height:220px;"> ...

Steps to display text in a div upon clicking on an image

I am trying to create an image with two DIVs separated by a black line. The left DIV will contain 4 images, and I want the following functionality: When a user clicks on any of the buttons in the left DIV, a corresponding text should be revealed in the ri ...

Taking on The Notch: How Can I Improve?

I'm currently working on a website for my friend's bar, but I'm facing an issue with Chrome where the content can't push past the "notch". Interestingly, Safari displays it fine on mobile, while Chrome has this unsightly white space. I ...

Unusual issue in IE causing rendering problems in reporting services

Currently, I am creating a report using Reporting Services 2008, MVC 2, and Ajax as my development technologies. When I generate the report, everything displays perfectly if there is data present. However, if there is no data, the report body gets cut off ...

Creating and implementing a HTML template from scratch, devoid of any frameworks

Is there a way to create a quiz where all questions follow the same format and only one question is displayed at a time, without duplicating code? Perhaps using a template would be the ideal solution in this scenario. I appreciate your help. ...

Curved base of the main banner image

Struggling to achieve the perfect curve at the bottom of my hero image. Any suggestions on how to accomplish this would be greatly appreciated. I am aiming for something like this: https://i.stack.imgur.com/Emxfc.png body { margin: 0; background: lig ...

Tips for presenting PHP code in a Bootstrap 3 col-sm-4 layout for each set of 3 database entries in a single row

My PHP program retrieves 3 customer records from the database. I want to display the results in a Bootstrap grid view with 3 columns of col-sm-4 in one row. Currently, it displays all the results in a single vertical line. Another requirement is to show ...