What is the best way to position two horizontal divs beside a vertical one?

I need assistance aligning my content in the following format:

┌────┬─────────────────────────────────┐
│ No │  Some possible long content     │
│    ├─────────────────────────────────┤
│    │  Not so long stuff              │
└────┴─────────────────────────────────┘

I am considering using divs instead of tables to achieve this layout. It is not tabular data, just a way to present a counter, a title, and a subtitle:

<div id="container">
    <div class="div1">No</div>
    <div class="div2">Some possible long content</div>
    <div class="div3">Not so long stuff</div>
</div>

The width of 1 is fixed, while its height should be the combined height of 2 and 3. 3 will always be one line, but 2 can be multiple lines.

Any guidance on how to achieve this layout would be greatly appreciated.

Answer №1

If you adjust the HTML structure to encapsulate the divs on the right side, then flexbox can help with that.

.container {
  width: 50%;
  margin: auto;
  border: 1px solid grey;
  display: flex;
}
.div1,
.right {
  display: flex;
  flex-direction: column;
}
.div1,
.right > div {
  padding: 1em;
}
.div1 {
  background: orange;
}
.div2 {
  background: pink;
}
.div3 {
  background: lightblue;
}
<div class="container">
  <div class="div1">No</div>
  <div class="right">
    <div class="div2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eveniet explicabo omnis, suscipit laborum officiis, eius sint ipsam doloremque magnam reiciendis corporis repudiandae ducimus laboriosam ex! Porro dolores aut vitae. Assumenda ut laborum dolor
      fugiat ullam nihil enim sapiente ex vel praesentium quo dolores distinctio, aliquid perferendis non accusamus dolorum excepturi?</div>
    <div class="div3">Not so long stuff</div>
  </div>
</div>

Answer №2

To achieve a particular layout, you can utilize css's display:table and display:table-cell

For a live example, check out this link: http://jsfiddle.net/8qv9z762/

#container {
    display: table;
    background-color: #dda;`
    height: 1px; /*it might seem strange but it's crucial (forgot the reason)*/
}
.r {
    display: table-cell;
}
.div1 {
    height: 100%;
}
.div1, .div2, .div3 {
    border: solid 1px;
    padding: 5px 8px;
}

For further visual reference, visit: https://i.sstatic.net/OPYqy.png

This method provides better compatibility across various browsers as compared to flexbox which is still not universally supported.

The table-cell property is compatible all the way back to ie8, whereas flex is only supported from ie11 onwards (last two versions)

The drawback of using this technique is the inability to use margins directly. However, this can be overcome by creating another child container within the table-cell div to act as a wrapper with margins.

Answer №3

If you're looking to position elements precisely, consider using absolute positioning. Check out this example: https://jsfiddle.net/sueba6t2/1/

Here's the CSS based on a modified HTML structure with some class name changes:

.container {
    position: relative;
}
.d1 {
    height: 100%;
    position: absolute;
    width: 3em;
}
.d2, .d3 {
    margin-left: 3em;
}

Alternatively, if you prefer newer techniques, try utilizing flexbox: https://jsfiddle.net/k7e01o29/

HTML:

<div>
    <nav>No</nav>
    <article>
        <section>Long content</section>
        <footer>Not long</footer>
    </article>
</div>

CSS:

div {
    display: flex;
    flex-direction: row;
}
article {
    width: 100%;
}

Please note that flexbox may not be supported in all browsers. If compatibility is crucial, be prepared to use polyfills for older versions like IE10 or below. While flexbox offers more versatility, it also comes with limitations in terms of browser support.

For additional information on browser support, you can refer to http://caniuse.com/#search=flex.

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

What is the best way to incorporate a PHP variable into my Jquery image slider?

I've been experimenting with a Jquery Image slider and I'm interested in enhancing it further. My goal is to display two divs when the full-size image loads, one containing an image title and the other providing a description of the picture. How ...

Can you explain the differences between offsetHeight, clientHeight, and scrollHeight for me?

Have you ever wondered about the distinction between offsetHeight, clientHeight, and scrollHeight? What about offsetWidth, clientWidth, and scrollWidth? Understanding these differences is crucial for working effectively on the client side. Without this kn ...

What could be causing my CSS background image to not display properly?

For some reason, I can't figure out why it's just displaying a blank screen. This is what I'm seeing: https://i.sstatic.net/0gDIK.png Here is the structure of my files: https://i.sstatic.net/GDToF.png The CSS code looks like this: *{ ...

a practical guide on implementing overflow-x in the row class

How can I implement a row scroll overflow on the x-axis for columns 5, 6, and 7? <div class="container-fluid"> <div class="row"> <div class="col-4">col 1</div> <div class="col-4">col 2</div> ...

Which is the better approach for performance: querying on parent selectors or appending selectors to all children?

I currently have 2 mirror sections within my DOM, one for delivery and another for pickup. Both of these sections contain identical content structures. Here's an example: <div class="main-section"> <div class="description-content"> ...

Is it necessary to save the details of a specific position in local storage when sliding?

Currently, I am in the process of replicating a webpage design from . I have written the code for the functionality where images and phrases change on every slide. There are three different phrases and images that are being displayed, and my goal is to sto ...

Creating a Web Form in Outlook Using the POST Method

Seeking help in creating an HTML email featuring two interactive buttons - Approve and Reject. I have successfully generated the HTML email and sent it to Outlook. Snapshot: https://i.sstatic.net/NrGFM.png The HTML code within the email: <!DOCTYPE ...

Utilizing React JSX within HTML structures

Attempting to integrate React into a basic HTML file, I came across an official guide on this website. However, I am struggling to grasp how to render a JSX-encoded component and how to nest one component within another: class LikeButton extends React.Co ...

I encountered an error stating "loadmodel is not defined" while I was using PHP and a JavaScript file with the type=module

Recently, I ventured into the world of THREE.js and WebGL, trying to load car models. My goal was to trigger a custom function through an onclick event listener that would extract an address from a data- attribute and pass it on to another custom function ...

Stop Gmail from Removing Attached HTML Files

I successfully developed an HTML file with its own CSS styling and custom HEAD tags to ensure it displays well in mobile browsers. I managed to make it work with PHP as a file attachment. However, the issue arises when Gmail distorts the view of the file ...

Pictures fail to load on Safari browser for desktop and iPhone, yet display correctly on various other smartphones and Windows browsers

Having trouble with images not displaying on Safari browsers for Mac and iPhones, but they load fine on Windows and Android. Even after updating to the latest version of Bootstrap, the issue persists. Here are the current Bootstrap content delivery networ ...

Steps for aligning an image to the right using Bootstrap Vue

I'm currently learning Bootstrap Vue and I'm trying to align an image on the right using a b-card header. The challenge I'm facing is that when I add the image, it disrupts the perfect size of the template header, causing the image to show a ...

Tips for centering multiple images vertically within a div

Currently, I am using jQuery.carouFredSel for displaying banners. Everything is working fine except for one issue - the banners have variable heights which causes them to align at the top in the slider. I need a solution to align shorter banners in the mid ...

Automate website button clicks using Python and Selenium

I could really use some assistance: Currently, I have a Python/Selenium code snippet that carries out basic actions on a website to retrieve names, and it works perfectly fine. The objective: However, what I am aiming for is to automate these actions to ...

The arrow icon on the select control is not showing up as expected in a form that is using Bootstrap when viewed in the

On my form using Bootstrap with Firefox browser, the select control is missing its arrow icon on the right side: https://i.sstatic.net/HcFf6.png The HTML code I used for this element is as follows: <div class="container-fluid"> <form id="fo ...

Can elements be eliminated from a div based on their order?

https://i.sstatic.net/XIUnsMRc.png Hello everyone, I am currently attempting to replicate this design using ReactJS, but I am facing challenges in getting my code to function as desired. The issue lies in positioning the profile picture next to the searc ...

How do I retrieve the value of a class that is repeated multiple times?

In the HTML code I am working with, there are values structured like this: <div class="item" onClick="getcoordinates()"> <div class="coordinate"> 0.1, 0.3 </div> </div> <div class="item" onClick="getcoordinates() ...

Interacting with a Bootstrap menu

I recently incorporated an example from Bootstrap, found at http://getbootstrap.com/examples/starter-template/, into my project and it worked well. However, after adding a custom left menu that appears on left-click, I noticed that the color of the Boots ...

menu icon not being hidden in media query

Currently, I am in the process of developing a landing page using a combination of HTML/CSS and Javascript. Everything seemed to be functioning smoothly with the media query max-width: 768px. However, when attempting to implement the media query for min-wi ...

When I apply flex to the display of .ant-steps-item-icon, the connection between the steps vanishes

I recently utilized ANTD and React to develop a customized component step style, but ran into an issue. Here is the CSS code snippet I used: /* step connector */ .ant-steps-item-title:after { border: solid rgba(65, 64, 66, 0.1) !important; } /* step * ...