Aligning two divs vertically within another div without relying on the display:flex property

My goal is to align the content of two divs to the top within a parent div.

I am unable to use display: flexbox, and it needs to be implemented as inline CSS. Currently, the layout appears as intended when the description div has only one line. However, if the description spans multiple lines, the number div appears above the description div. This is my current code:

<!--Single line-->
<div style="margin:10px 0 10px 0;">
  <div style="display:inline-block; vertical-align:top"> 1.1</div>
  <div style="margin-left:50px; display: inline-block; vertical-align:top;">
    Lorem ipsum dolor sit amet, pertinax honestatis suscipiantur mea eu, populo invidunt nam ne, nulla similique usu et. His accusata definitionem in.
  </div>
</div>

<!--Multiple lines-->
<div style="margin:10px 0 10px 0;">
  <div style="display:inline-block; vertical-align:top">1.2</div>
  <div style="margin-left:50px; display: inline-block; vertical-align:top;">
    Id harum paulo bonorum vim, sit adhuc aliquid oportere in, ei quo dicta labitur salutatus. Debet doctus facilis ius ad, assum expetendis vix ex. Modus nostro concludaturque nec ad. No unum appellantur sea, assum oratio legere has et, est lorem concludaturque
    id. Ex pro audire nostrud, vel sale dissentias an, eos no zril vocibus principes. Noluisse praesent mei ea. Eam intellegat omittantur ea, sit tacimates quaestio definitiones ei. Et vix appareat reprehendunt, at cum alii aliquando, audire iuvaret instructior
    has eu. Sea id placerat ponderum, ea has deserunt assentior consectetuer. Nobis denique nominavi qui ut, cu prima idque perfecto eos. Quo soleat possit impedit eu. Id mucius graeci noluisse his. Sanctus appareat usu ne, per ea purto tractatos deterruisset.
  </div>
</div>

I would like each item to be displayed similar to 1.1: https://i.sstatic.net/OcFMS.jpg

Answer №1

Assuming that inline elements are in use, I see no reason why tables shouldn't be included 😄

<table>
  <tbody>
    <tr>
      <td style="width: 3rem; vertical-align: top">1.1</td>
      <td>
        <p style="margin-top: 0;">
          Lorem ipsum dolor sit amet, pertinax honestatis suscipiantur mea eu, populo invidunt nam ne, nulla similique usu et. His accusata definitionem in.
        </p>
      </td>
    </tr>
    <tr>
      <td style="width: 3rem; vertical-align: top">1.2</td>
      <td>
        <p style="margin-top: 0;">
          Id harum paulo bonorum vim, sit adhuc aliquid oportere in, ei quo dicta labitur salutatus. Debet doctus facilis ius ad, assum expetendis vix ex. Modus nostro concludaturque nec ad.

          No unum appellantur sea, assum oratio legere has et, est lorem concludaturque id. Ex pro audire nostrud, vel sale dissentias an, eos no zril vocibus principes. Noluisse praesent mei ea. Eam intellegat omittantur ea, sit tacimates quaestio definitiones ei. Et vix appareat reprehendunt, at cum alii aliquando, audire iuvaret instructior has eu. Sea id placerat ponderum, ea has deserunt assentior consectetuer.

          Nobis denique nominavi qui ut, cu prima idque perfecto eos. Quo soleat possit impedit eu. Id mucius graeci noluisse his. Sanctus appareat usu ne, per ea purto tractatos deterruisset.
        </p>
      </td>
    </tr>
  </tbody>
</table>

Answer №2

To solve your issue, you must remember to specify a width for your container.

.number {
  display: inline-block;
  vertical-align: top;
}

.text {
 display: inline-block;
 width: 80%;
}
//One line
       <div>
            <div class="number"> 1.1</div> <div class="text">
        Lorem ipsum dolor sit amet, pertinax honestatis suscipiantur mea eu, populo invidunt nam ne, nulla similique usu et. His accusata definitionem in.
    </div>
        </div>

//Multiple lines
<div>
    <div class="number">1.2</div> <div class="text">
    Id harum paulo bonorum vim, sit adhuc aliquid oportere in, ei quo dicta labitur salutatus. Debet doctus facilis ius ad, assum expetendis vix ex. Modus nostro concludaturque nec ad.

    No unum appellantur sea, assum oratio legere has et, est lorem concludaturque id. Ex pro audire nostrud, vel sale dissentias an, eos no zril vocibus principes. Noluisse praesent mei ea. Eam intellegat omittantur ea, sit tacimates quaestio definitiones ei. Et vix appareat reprehendunt, at cum alii aliquando, audire iuvaret instructior has eu. Sea id placerat ponderum, ea has deserunt assentior consectetuer.

    Nobis denique nominavi qui ut, cu prima idque perfecto eos. Quo soleat possit impedit eu. Id mucius graeci noluisse his. Sanctus appareat usu ne, per ea purto tractatos deterruisset.
</div>
</div>

Using flexbox is the most effective way to achieve this.

.container {
  display: flex;  
}

.space {
 margin-right: 10px;
}
//Single line
       <div class="container">
            <div class="space">1.1</div>
            <div>
              Lorem ipsum dolor sit amet, pertinax honestatis suscipiantur mea eu, populo invidunt nam ne, nulla similique usu et. His accusata definitionem in.
            </div>
       </div>

//Multiple lines
<div class="container">
    <div class="space">1.2</div> <div>
    Id harum paulo bonorum vim, sit adhuc aliquid oportere in, ei quo dicta labitur salutatus. Debet doctus facilis ius ad, assum expetendis vix ex. Modus nostro concludaturque nec ad.

    No unum appellantur sea, assum oratio legere has et, est lorem concludaturque id. Ex pro audire nostrud, vel sale dissentias an, eos no zril vocibus principes. Noluisse praesent mei ea. Eam intellegat omittantur ea, sit tacimates quaestio definitiones ei. Et vix appareat reprehendunt, at cum alii aliquando, audire iuvaret instructior has eu. Sea id placerat ponderum, ea has deserunt assentior consectetuer.

    Nobis denique nominavi qui ut, cu prima idque perfecto eos. Quo soleat possit impedit eu. Id mucius graeci noluisse his. Sanctus appareat usu ne, per ea purto tractatos deterruisset.
</div>
</div>

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

The setLanguage function in jsPDF does not support rendering different language characters

I'm currently working with jsPDF in Angular 2 and I'm experiencing an issue where my HTML content is not converting successfully into a PDF when it's written in Hebrew. Other languages seem to work fine, but Hebrew is causing a problem. How ...

Modify anchor link color in a one-page website by using Jquery to detect scroll position changes when reaching different page sections

Each if statement contains specific numbers to change the text color of the visited/current/active anchor link. The height of the page is dependent on its width. How can this be resolved? Apologies if my explanation is confusing, English isn't my stro ...

Issue with Ionic3: When using ElementRef nativeElement.getElementsByClassName, the returned collection is inaccessible

I've been following a tutorial on Ionic and directives, and everything seems to be working fine except for one thing. When I try to retrieve the FAB element using ElementRef's nativeElement.getElementsByClassName method, it returns undefined. Str ...

I am looking for a way to locate all meta tags that begin with either "ABC_" or "XYZ_" by using jQuery

I am facing a challenge with multiple <meta> html tags. My task is to utilize jQuery to retrieve all the meta tags where the name begins with "ABC_" or "XYZ_", and then iterate through them. This is what I have accomplished so far: var tags = $( "m ...

Learn how to dynamically disable or hide the "Today" button in an angular-ui datepicker based on whether it is the weekend

What is the process to hide or disable the "Today" button based on a condition, for example, if today is a weekend? The goal is to conceal the "Today" button in such scenarios. <div class="form-group"> <div class="input-group datePicker"> ...

Customize the background color of a particular select element within an array of dynamically created select elements

I've been experimenting with jQuery to dynamically adjust the background color of a select element based on the chosen option. The select elements are being generated using the following code: while ($qrow = $qquery->fetch(PDO::FETCH_ASSOC)) { ...

Would it be beneficial to assign a single class name to all elements with matching styles within a CSS framework?

Currently, I am in the process of creating my own CSS library or framework. However, I have encountered an issue where all 10 li tags share the same classes, such as .pl{ padding-left:10px} .mr{ margin-right:10px}, along with other necessary attributes. Wh ...

Adjust the background color of the body content to reflect changing content

How can I change the background color to #97EFFC when a menu item is selected? I want the body content background to be transparent until a menu item is displayed. Site.css /* Responsive: Portrait tablets and up */ @media screen and (min-width: 768px) { ...

Tips for creating a versatile text with javascript

I am attempting to achieve a similar result: <?php $url = 'http://www.mydomain.com/img/picture.jpg'; ?> <img src='<?php echo $url ?>' /> However, I would like to achieve this using javascript. It would look something ...

Exploring MaterialUI withStyles to customize disabled switches with a CSS override

As I work on my project using React and MaterialUI, I've encountered a problem with customizing inputs. Despite trying various selectors, I can't seem to change the black color of this particular input. It would be helpful to have a clearer way o ...

JavaScript refuses to connect with HTML

Just starting out in the world of programming, I decided to dive into a Programming Foundations class on Lynda.com. However, after following along for fifteen minutes, I encountered an issue - I couldn't seem to connect my JavaScript file to my HTML f ...

Complete visibility of Bootstrap progress labels is compromised

Progress bar with a customized label inside: <div id="progress" class="progress mb-1" style="height: 20px;"> <div class="progress-bar" role="progressbar" style="width: 5.0%" aria-valu ...

Insert a span element before an HTML input using JavaScript

On my webpage, there is an html input inside a div. Here is what it looks like: <input type="text" class="form-control" placeholder="Barcode" role="barcode"> Using JavaScript only, I am trying to add the following code into the DOM above it: <s ...

Developing an interactive URL in an HTML page for a Flask web application

I'm attempting to create dynamic URLs in HTML and route all "orders/<key_id[0]>" to a Flask app. The hyperlink on the browser should display as a link to name, but it should actually route as "orders/<key_id[0]>". I've tried numerou ...

Updating pictures on Bootstrap's project showcase template

I attempted to modify the source of an image by clicking on smaller ones using bootstrap’s template found at . Despite my efforts, I have not been able to achieve the desired result. (Any help is greatly appreciated!) I included id="myImage" on the main ...

The jquery function is not operating as anticipated

I wrote the following code to toggle a class for a div. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <html> <head> </head> <body> <div class="members-section " id="a ...

Is there a way to align both JSX elements in a single row without having one overlap the other?

Is there a way to showcase both conditions side by side on the same line? If Condition 1 is met, display the images; if Condition 2 is met, show the video. import React, { useState } from "react"; import { Card } from "react-bootstrap" ...

display select items from a webpage

Despite researching many topics, I am still unable to get this to work. My goal is to hide certain elements on my webpage when the user tries to print it. I am currently using Bootstrap 5, which includes the following CSS: @media print{.d-print-none{disp ...

The anchor tag <a> is configured for inline display but is unexpectedly occupying the entire space

I am currently incorporating Bootstrap into my HTML, however, the <a> tag is behaving like a block display despite the fact that I have specified the CSS to be display:inline; <!doctype html> <html> <head> <link rel="styleshee ...

Setting a value in a numeric input field - a step-by-step guide

I need to assign specific values to each number in a number input field. For instance, number 1 should be equal to 0.125, number 2 should be 0.250, number 3 should be 0.375, and so forth. Here's the code snippet: <input type="number" min="0" max=" ...