Create div elements that have equal heights, even when they are not consistently positioned on the same row

I've been experimenting a bit and facing some challenges.

There are 6 divs with headings and lists. My goal is to align them as needed, with a maximum of 3 on any given line, creating layouts of 3x2, 2x3, or 6x1.

The main issue I'm encountering is getting the divs to have consistent heights.

Some problems I've encountered:

  • I can't use display:table-cell because it creates fixed rows.
  • Trying fixed heights leads to strange offsets, as shown in the second image.

I have set up a jsfiddle with the code, which is also provided below.

How can I (preferably using CSS only) make these divs have the same heights and widths while aligning them vertically correctly?

CSS (generated from LESS, excluding ul and h2 styling):

div div {
        display:inline-block; 
        border:1px solid gray;
        padding:5px;
        margin:10px;
        width:20%;
        height:100%;
        border-radius: 8px;
}
h2 {
    margin:0px;padding:0px;
    font-size:12pt;
    text-align:center;
}

HTML:

<div id="homeFaq">

    <div>
        <h2>What is metadata?</h2>
        <ul>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
        </ul>
    </div>

    <div>
        <h2>What is [BRAND]?</h2>
        <ul>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
        </ul>
    </div>
    <div>
        <h2>How can I get data?</h2>
        <ul>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
        </ul>
    </div>
    <div>
        <h2>How can I add content?</h2>
        <ul>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
        </ul>
    </div>
    <div>
        <h2>Lorum Lorum?</h2>
        <ul>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
            <li>Ipsum ipsum</li>
        </ul>
    </div>
    <div>
        <h2>Lorum Lorum?</h2>
        <ul>
            <li>Ipsum</li>
            <li>Ipsum</li>
            <li>Ipsum</li>
        </ul>
    </div>
</div>

http://jsfiddle.net/VNcaH/

Answer №1

If you want to ensure a fixed height and align content at the top, you can follow this example:

div div {
        display:inline-block; 
        border:1px solid gray;
        padding:5px;
        margin:10px;
        width:20%;
        height:150px;
        border-radius: 8px;
    vertical-align:top;
};

Check out the Fiddle Demo for reference

Answer №2

Check out the updated javascript fiddle here: http://jsfiddle.net/nikhilvkd/VNcaH/4/

To ensure proper functionality, consider using the min-height property in your h2 and ul tags.

div div h2 {
  min-height: 30px;
}
div div ul {
  min-height:80px;
}

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

Trouble with PHP Online Form Delivery to Gmail

Having an issue with submitting this form and receiving an error message. Can anyone help me identify where the mistake is? HTML code: <div class="main"> <div class="info">Leave your feedback</div> <form action="mail.php" m ...

Is there a way to keep Angular from automatically re-sorting my list when I make edits to it?

I have designed a small application with Angular for managing Todolists. Each list contains multiple todos, where each todo has attributes such as name, value1, and value2. To automatically sort each list using Angular, I utilized ng-repeat="todo in selec ...

What is the best way to anchor the components to a specific location on the screen in ASP.NET?

Currently I am working on creating a registration page in asp.net. I have been using panels to group the components such as labels, dropdown lists, and text boxes. However, when I run the page, I noticed that the positions of these components keep changing ...

What is the proper way to retrieve the JSON data?

How can I retrieve data from another table to use in a detail view if the value returned by the getData method is null? <th data-field="id" data-detail-formatter="DetailFormatter">ID</th> <th data-field="prefix&quo ...

"Seamless responsiveness in design with jQuery, but encountering compatibility issues

My usage of jQuery is quite basic to ensure that elements are properly positioned when they are moved: function ipad() { var width = jQuery(window).width(); if (width < 1200 && width >= 768){ //if tablet jQuery('.mobbut').css(&apo ...

What is the best way to implement a CSS transition for styles that are dynamically created by React?

I have a situation where I am using a button component that is styled based on a theme provided by a context: The code in Button.js looks like: () => { const theme = useContext(themeContext); // { primaryColor: "blue" } return <button className ...

What is the best way to horizontally align paragraphs within different divs or spans?

Currently, I have 2 divs placed side by side that each contain about 5 paragraphs. My goal is to align these paragraphs horizontally so that paragraph 2 in the first div starts at the same line as paragraph 2 in the second div (and so on for all other para ...

Creating a dynamic navbar with Python (Django), HTML, CSS, and Javascript for a fully responsive

I am currently utilizing a guideline to develop a responsive navbar that adapts its layout based on the screen size. It will display elements in a dropdown list for smaller screens, as opposed to an inline layout used for larger screens. Here is a snippet ...

Trouble encountered in aligning two DIVs in a horizontal position

I have been through numerous discussions on the same problem, but none of the solutions seem to work in my case. I am currently working on a section of my website that has 3 small boxes, each containing an image on top and text below. The issue arises when ...

How can I stack one column beneath two columns in a unique format?

I am looking to create a layout similar to the one depicted below. https://i.sstatic.net/m1aCj.png The window is represented by the red box, and the black boxes represent the <div> elements. I have attempted to arrange the image into two columns, bu ...

Combining Array Elements to Create Overlapping Images in JavaScript

My goal is to create a planner where users can choose a wallpaper based on the season. Right now, I'm focusing on fall, and when you click on fall, you can see all the different wallpapers (each URL from my array is displayed). However, once the alert ...

Unlocking the Secrets: Adding Multiple Contents to Your Master Page in ASP

I need some clarification on a dilemma I'm facing. My master page has a content placeholder that is used by all subpages: <%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %> <!DOCTYPE html> ...

When the next button is clicked, the button content disappears

I am struggling with a problem involving two buttons that store tables. The issue is, I want the table to disappear when the second button is clicked and show the contents of the second button immediately after clicking it once, rather than having to click ...

Creating a Seamless Bond Between JavaScript and HTML

I've been struggling to find a helpful and straightforward solution to a simple problem. On my web page, I have five image placeholders that should be filled with one of nine random pictures. To achieve this, I wrote a JavaScript code that generates r ...

Display image once form has been submitted

The page I have consists of the following code: HTML CODE <table border="0" cellspacing="1" cellpadding="1" id="echipajucator" title="Echipa Jucator"> <tr> <th><div align="left"><span>Team</span></div> ...

Extracting data from HTML that dynamically updates with button click

I am working on extracting data from a webpage with a large table that displays 100 entries by default. At the bottom, there is a dropdown menu where you can select to show 200 entries or view all of them. Is there a way for me to automatically set the dr ...

Retrieving Data Value from a Website with Python Web Scraping

Introduction Greetings everyone! I am new to the world of Python and web scraping. Currently, I am using a Mac (Sierra) with Jupyter Notebook through Firefox (version 87.0). My goal is to extract various data points from a webpage similar to the one found ...

My custom font is not compatible with the browser on my asp.net site

I'm having trouble embedding my own font in my .aspx file. Google Chrome and Firefox don't seem to support the font, while IE does. Can someone please provide guidance on how to successfully embed the font in an asp.net page? Below is my code: ...

A web address shared in a post appears within the nested frame of an iframe

I'm encountering a somewhat confusing issue. I am attempting to submit a URL to an iframe located on the same page. Upon clicking submit, the iframe replicates the current page, but with the correct URL displayed in the duplicated iframe. It's ma ...

Difficulty with Horizontal Mousewheel Scrolling

Struggling to implement a horizontal scrolling feature (via mousewheel) for both containers in the code below. I want this feature to be easily applied to any future container creations as well. <body> <style> #container { display: flex ...