Center inline-block elements inside a container with vertical alignment and justification

Although this question has been raised multiple times, I have not been able to find a solution tailored to my specific situation.

Here is an example of what I am dealing with: DEMO

Essentially, it involves a list of rows:

<ul>
    <li>
        <label>
            <span>element1</span>
            <span>element2</span>
            <span>element3</span>
            <span>element4</span>
        </label>
    </li>
    <li>
        <label>
            <span>element1</span>
            <span>element2</span>
            <span>element3</span>
            <span>element4</span>
        </label>
    </li>
</ul>

The span elements are all set as inline-blocks, and the label has a style of text-align: justify.

Everything seems to be functioning correctly except for one issue - the vertical alignment. All items appear to be aligned at the top, which is not the desired outcome. Initially, I suspected the common inline-block problem causing an extra space, but even after attempting solutions like setting font-size to 0 or eliminating white space, the alignment remains unchanged.

Answer №1

It appears that the issue is related to the line-height property, causing the pseudo element to occupy extra space. One way to minimize this effect is by setting line-height:0 for the label and the li, followed by a line-height:1 for .product-row-price. However, this solution still may not appear perfect as it leaves some space at the bottom and could result in text overlap.

An alternative approach would be to apply a negative margin-bottom to the label and set overflow to hidden:

.product-row-label {
    margin-bottom: -1.5em;
    overflow: hidden;
    /* additional styles here */
}

You can find an example of this implementation on this JSFiddle link.

Just a heads up, using div within a span is considered invalid HTML. It's recommended to replace those divs inside .product-row-price with spans and make them block-level elements.

The alignment issue may persist due to differing content widths in the first and third columns, causing a visual imbalance. A workaround could involve setting fixed width for the initial and final columns to ensure horizontal alignment, even if it results in slight spacing differences in the third column. You can explore this concept further on this JSFiddle link.

Answer №2

One potential solution is to apply the CSS property display: table; to <label> elements and display:table-cell; to your <span> elements. This will make the elements display as a <table> HTML element. Make sure to adjust padding and margin for proper display.

Check out the DEMO here

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

datepicker in bootstrap 4 obscured by the div

I am currently utilizing Bootstrap datepicker v4. However, I am facing an issue where the datepicker view is getting hidden behind the 'map' div. I have attempted to solve this problem by adding overflow: visible, but unfortunately, it does not s ...

Shifting Objects within Bootstrap 5 Navigation Menu

Hey there! I'm having some trouble aligning the items in a Bootstrap 5 navigation bar to the side, just like in this screenshot: https://i.sstatic.net/nxh80.png I tried adjusting the alignment but nothing changed on the site (I was using Live Server ...

Utilizing JSON for HTML conversion in Codeigniter

public function getCrew(){ $search = $this->input->post('name'); if($this->input->post('ajax') && (!empty($search))){ $result = $this->model->getNames($search); foreach($result as $r){ ...

Considering a background image for your email form?

I'm trying to create an email form with a background image for the entire form. I've attempted using CSS background-image and HTML to set the background image, but so far no luck. Most tutorials only cover how to add an image inside a text box ( ...

Tips for customizing the color of pagination in bootstrap

I'm currently in the process of designing a website and I've chosen to implement Bootstrap's pagination for navigating between pages. Due to time constraints, I was wondering if anyone knows how I can utilize classes like "bg-dark", "bg-prim ...

The 1st DIV element nudges the content within the 2nd DIV Element, causing it to stretch beyond the screen with 100%

I have gone through several other questions, but I am struggling to integrate the solutions into my problem. My main content DIV is set to 100% height and works perfectly fine until I add another element outside of the DIV towards the top of the browser. ...

After my data rows are filled, I add jQuery and CSS striping animations to enhance their appearance

Before any click, they have this appearance: Clicking on Sales in the business section reveals a drop-down select box. If I click on "Sales" and modify the text area, it will transition to grey. While the grey rows change accordingly, the black rows shoul ...

Generate a fresh div and add pre-existing components

I currently have the following structure: <body> <div class="one"></div> <div class="two"></div> <div class="three"></div> </body> Using jQuery, I am trying to create a new <div class="fo ...

Is it possible to call a REST API in Javascript by passing the username and password as

I have been attempting to use Javascript to call the AwaazDe REST API with a specific username and password, but I'm encountering issues. Below is my code: function authenticateUser(user, password) { var token = user + ":" + password; ...

Steps to design a text div that extends beyond the bottom boundaries

Can anyone help me troubleshoot this design code using Bootstrap 3.x? Specifically, I am facing an issue with the following div. Any suggestions? Click here for more information. img{max-width:100%;} .mydiv{margin-bottom:20px;} .mytext{ positio ...

Troubleshooting issue with ellipsis functionality in Bootstrap using flex layout

I have been struggling to include ellipsis after displaying 2 lines of icons. I have tried different approaches and modifications, but nothing seems to work. Seeking assistance from experts to tackle this issue, your feedback is highly appreciated. .elli ...

What is the best way to add an overlay to an image using canvas?

I'm trying to create hotspots on an image using JavaScript by drawing shapes like polygons, rectangles, and circles. I would like to achieve something similar to the examples provided in the links below. Also, I am curious about how to overlay a canva ...

Javascript - Conceal a Dynamic Div Depending on its Text

I have a unique table that generates dynamic Divs with ID's that count as they are created in the following format: <div id="dgpCheckDiv10_0"> <input Delete </div> <div id="dgpCheckDiv10_1"> text2 </div> Some of t ...

The scrolling speed of my news div is currently slow, and I am looking to increase its

This is the news div with bottom to top scrolling, but it is slow to start scrolling. I want to increase the speed. The current behavior is the div appears from the y-axis of the system, but I want it to start exactly where I define. The scrolling div is ...

Employing PHP conditions alongside JavaScript to conceal the div elements

I have been struggling with a problem for the past few hours and still haven't found a solution. I am new to php. What I am trying to achieve is to display and hide a div based on a PHP condition. Here is the scenario: Please note: By default, the DI ...

Steps for updating a text section beneath a carousel

I'm looking to enhance my webpage with a bootstrap carousel, which is pretty standard. However, I want the content under each slide to change dynamically, but I'm struggling to make it work. As a newbie, I could use some guidance. I've atte ...

Struggling to make the grunt.js task for postcss with autoprefixer function properly

I am currently facing issues with using postcss in conjunction with autoprefixer-core. Even though the css is being generated correctly, autoprefixer doesn't seem to be applying any prefixes. I have already installed postcss and autoprefixer via NPM ...

Steps to set up datetimepicker with varying date ranges for several input fields

I am having trouble getting a new date range for each text box in my form. It seems to only return the date range from the last textbox, even though I have made the textbox IDs dynamic. I have both start and end dates for each textbox and I have calculated ...

Animate a div to sense whether it has reached the top or bottom position

Is it possible for a div to animate when it reaches almost halfway while scrolling? I'm looking to achieve this effect on a sticky sidebar, similar to how it works on this website: sample This is the code I have: $(function(){ // document ready ...

I only notice certain text after carefully inspecting the elements in Google Chrome

While creating an application on Sitefinity (ASP.NET), I encountered an issue where certain elements, such as button text and labels, were not loading correctly when running the application. However, upon inspecting the element, they appeared to be working ...