Position the divs at the top and ensure they are left-aligned as well

1) I have two divs placed inside a larger div (container) to create small cards with information. However, I am facing an issue where if one card is taller than the other, the second card aligns itself to the bottom of the first card instead of staying at the top. Here is the code and fiddle:


    <div class="main-cards">
        <div class="card" style="width: 65%;">
            <h1>CARD 1</h1>
            <p>small</p>
            <p>small</p>
            <p>small</p>
        </div>
        <div class="card" style="width: 25%;">
            <h1>CARD 2</h1>
            <p>small</p>
        </div>
    </div>


    .main-cards{
        position: relative;
        height: auto;
        width: 80%;
        margin-left: auto;
        margin-right: auto;
        top: 150px;
        text-align:center;
        background-color: #6ab5dd;
    }
    .card{
        display: inline-block;
        background-color: white;
        height: auto;
        margin: 10px;
        margin-top: 40px;
        padding: 8px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
    }

FIDDLE: https://jsfiddle.net/4px7kc4v/2/

Desired Alignment: https://i.sstatic.net/kyXmm.png

2) How can I center three cards vertically in a large container while maintaining the layout shown in the image?

https://i.sstatic.net/DluZr.png

Currently facing this issue: https://jsfiddle.net/sx7ryv70/

Thank you for your time! Have a great day!

Answer №1

1) Include the following code in the CSS for the cards:

      vertical-align: top;

2) When using divs as inline-blocks, they are positioned on the same line like large text characters. Consider grouping smaller cards together in a separate container.

Answer №2

Incorporate vertical-align: top; into the styling of .card element

Answer №3

Check out your answers:

  • [here](https://jsfiddle.net/Atula/4px7kc4v/3/)

  • [click here](https://jsfiddle.net/Atula/sx7ryv70/1/)

Answer №4

Check out this JSfiddle

HTML Code Snippet

<div class="main-cards">
        <div class="card" style="width: 55%; height: 300px; float:left;"></div>
        <div class="card" style="width: 55%; height: 300px; margin-top: 50px; float: left;"></div>
        <div class="card" style="width: 25%; height: 650px;"></div>

CSS Styling

    .main-cards {

      position: relative;

      height: auto;

      width: 80%;

      margin-left: auto;

      margin-right: auto;

      top: 50px;

      text-align: center;

      background-color: #6ab5dd;

      padding: 30px;

    }

    .card {

      display: inline-block;

      background-color: white;

      height: auto;

      margin: 10px;

      margin-top: 40px;

      padding: 8px;

      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);

    }

The key concept to remember here is the use of floating elements.

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

`Unable to get SCSS media queries to properly set aspect ratio`

Just getting started with SCSS and trying to maintain high quality cover images on my website. The CSS seems to be working fine, but the SCSS is giving me some trouble. SCSS #page-login-index { @media screen and (min-aspect-ratio: 5/4) { background-c ...

Setting up Angular Toolkit via npm installation

After successfully installing the UIKit npm package within an Angular 2 CLI project, what steps should I take to utilize it? I have also installed typings for UIKit (@types/uikit), but I am unsure of how to properly import the package into a controller i ...

How to solve the issue of the loading graphic moving from left to center on the page in the Infinite Scroll jQuery Plugin?

While using the Infinite Scroll jQuery Plugin by Paul Irish, I have observed that every time new content is loaded onto the page, a loading graphic in the form of HTML markup is appended into the DOM: <div id="infscr-loading" style="display: none; widt ...

Exploring ways to increase the width of rows in Bootstrap 4

Is there a way to make a row expand to 100% of its containing div in Bootstrap 4? I've tried using w-100 and d-inline-block but neither seems to work. Any help would be appreciated. CODEPEN .container { width: 90%; border: 1px solid black; h ...

Elusive Appearance of the Flask Flash Message

I have developed a web application using Flask that allows users to upload files to an S3 storage. However, I would like to enhance the user experience by showing them the progress of their file uploads in real-time. To achieve this, I am utilizing Flash t ...

Fade In jQuery Effect at the Top of an Object

I'm currently working with a code snippet that fades in a div once it's scrolled into view. The only issue is that the div only starts fading in when the entire element is within the viewport. Is there a method to trigger the fade effect as soon ...

Using jQuery to Automatically Add Image Title Attribute from Alt Text

jQuery('img').each(function() { if(jQuery(this).attr('title') === '') { jQuery(this).attr('title', jQuery(this).attr('alt')); } }) Is there a way to tweak the code snippet above so that it ...

Angular PrimeNg carousel with personalized previous and next buttons

The information is provided in the image attached, with all CSS code available on stackblitz. I am looking to customize the Next and Prev buttons by having their borders match the inside boxes and be rounded. This will indicate to the user that there is a ...

Firefox not supporting position fixed within relative parent

Two columns are displayed here, with the left column showing a list and the right column displaying information based on the selected element from the list. Due to the large size of the list, I implemented a feature where the right column stays fixed at t ...

Displaying JSON data in an HTML table format

I am looking to display data from an API JSON in an HTML table. Here is the link: <html> <body> <table> <thead> <tr> <th>Currency</th> <th>Value</th> <th>Selling Rate&l ...

How do I ensure that a fieldset and a multiple selection box have equal heights using CSS?

Is there a way to make the multiple selection box the same height as the fieldset, without using JavaScript? I have a form with a fieldset and select box side by side, and any assistance would be appreciated. <!DOCTYPE html> <html lang="en> ...

Transfer the data from a post request through routes following the MVC pattern

Encountering an issue when attempting to submit a post form with username and password, as an error stating Cannot POST /validateUser is displayed. The structure of my form is as follows: <!DOCTYPE html> <html> <head> <title> ...

When an image is a child of the parent content and another element has opacity set, the CSS stack order will be altered

Recently, I came across an interesting issue while working with CSS, particularly with negative margins to stack elements on top of each other. I am aware of the natural stack order in which elements overlap, where the later element always appears on top ...

CSS - the styling properties of the class should not affect the child components

I am working on a Parent component that contains two child components. Each component utilizes accordion-groups for its functionality. I have created a specific class for styling purposes in my stylesheets, but I want this class to only apply to the parent ...

Steps to dynamically adjust the height of a div based on the height of the browser window

My question revolves around a fixed position div element whose height varies based on its content. In order to enable auto scrolling, I have applied the CSS property overflow-y: auto and assigned a fixed height. Is there a method to dynamically adjust the ...

Content within a Bootstrap 4 Jumbotron spills out of its container

I'm trying to create a Jumbotron component using the following code: <div className="jumbotron"> <div> <h6 className="display-6">Reset Password</h6> <p className="lead">We've just sent password reset instructions to ...

Expanding container for mobile devices

Hey everyone! I need some help with my website code: <html> <head> <!-- extra code here --> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <d ...

What could be causing my CSS to malfunction when I include two parameters in app.get?

While developing an express app, I encountered an issue where my CSS code stopped working whenever I added an ":ID" parameter to the URL. It seems like a filepath problem because bootstrap is still loading fine, but on the specific page with the ID paramet ...

Uploading files along with additional data fields

I am facing a dilemma with my HTML form that has enctype="multipart/form-data" attribute. I have a DTO class with all the necessary setters and getters. Since I am submitting the form as multipart, I cannot use getParameter() method directly. In my servlet ...

Using a combination of CSS, Ajax, and jQuery, dynamically insert new rows into a table generated within a div

Apologies for the awkward question. I am currently working on a solution to create a table using div tags, which is functioning properly. However, I am facing an issue with adding or removing a row using jQuery. The problem lies in the fact that when a new ...