What is the best way to create a layout containing a <div> split into three columns, with the middle column having rows?

I'm currently working on a <div> layout that I need to split into rows. The challenge is creating a design that is fluid and can adapt to various screen sizes, including mobile devices. While using an HTML table might be a quick fix, it's not the ideal solution for responsiveness.

In the "What I want" section of the image, my goal is for columns ONE and SEVEN to automatically adjust their height to match the combined height of rows THREE through SIX. Columns ONE and SEVEN will remain empty, while rows THREE, FIVE, and SIX will contain text of different lengths, and row FOUR will feature an image with dimensions set to width: 1020px and height: 1024px.

The total width occupied by rows Three through Six should be 80% of the screen width, whereas columns ONE and SEVEN should each occupy 10% of the screen width.

I am currently utilizing bootstrap 3.3.6. Here is the code snippet along with the corresponding CSS code for the "what I'm getting" part of the image:

https://i.stack.imgur.com/iSbfC.jpg

.wrapper {
    width: 100%;
    height: 100%;
}
.one {
    display: inline-block;
    width: 10%;
    height: 100%;
    background: blue;
}
.two {
    display: inline-block;
    width: 80%;
    height: auto;
}
.three {
    width: 80%;
    height: auto;
    background: orange;
}
.four {
    width: 80%;
    height: auto;
    background: purple;
}
.five {
    width: 80%;
    height: auto;
    background: yellow;
}
.six {
    width: 80%;
    height: auto;
    background: green;
}
.seven {
    display: inline;
    width: 10%;
    height: 100%;
    background: red;
}
<div class="wrapper container-fluid">
    <div class="one">ONE</div>
    <div class="two">
        <div class="three">THREE</div>
        <div class="four">FOUR</div>
        <div class="five">FIVE</div>
        <div class="six">SIX</div>
    </div>
    <div class="seven">SEVEN</div>
</div>

Answer №1

For those utilizing bootstrap and with the container-fluid class, it is simple to create a responsive layout using Bootstrap's predefined classes such as .row and col-*-*, along with setting display:flex in the .row.

.row {
  display: flex
}
.one {
  background: blue;
}
.three {
  background: orange;
}
.four {
  background: purple;
}
.five {
  background: yellow;
}
.six {
  background: green;
}
.seven {
  background: red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="wrapper container-fluid">
  <div class="row">
    <div class="col-xs-2 one">ONE</div>
    <div class="col-xs-8 two">
      <div class="three">THREE</div>
      <div class="four">FOUR</div>
      <div class="five">FIVE</div>
      <div class="six">SIX</div>
    </div>
    <div class="col-xs-2 seven">SEVEN</div>
  </div>
</div>

Answer №2

For this layout, you have the option to utilize either flex or table for the display property: (breakpoints added around 600-720px for flex & 660px for table version)

To observe the behavior, run the snippet in full-page mode.

.wrapper {
   width: 100%;
   height: 100%;
 }
 .one {
   background: blue;
 }
 .two {
   width: 80%;
 }
 .three {
   background: orange;
 }
 .four {
   background: purple;
 }
 .five {
   background: yellow;
 }
 .six {
   background: green;
 }
 .seven {
   background: red;
 }

.flex  {
   display: flex;
   flex-wrap: wrap;
 }
.flex > div {flex: 1 1 10%}
.flex .two {
  min-width: 600px;
  max-width: 100%;
  flex: 1 1 80%
}

.table {
   display: table;
   table-layout: fixed;
 }
.table > div{
   display: table-cell;
 }
@media (max-width: 660px) {
  .table > div {
    display: block;
    width: 100%;
  }
}
<div class="wrapper container-fluid flex">
    <div class="one">ONE</div>

    <div class="two">
      <div class="three">THREE</div>
      <div class="four">FOUR</div>
      <div class="five">FIVE</div>
      <div class="six">SIX</div>
    </div>

    <div class="seven">SEVEN</div>
  </div>
 
<hr/>


  <div class="wrapper container-fluid table">
    <div class="one">ONE</div>

    <div class="two">
      <div class="three">THREE</div>
      <div class="four">FOUR</div>
      <div class="five">FIVE</div>
      <div class="six">SIX</div>
    </div>

    <div class="seven">SEVEN</div>
  </div>

Answer №3

Using media queries has been a key part of my design strategy for quite some time.

.container {
    margin: 10%;

    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    display: -webkit-flex;
    -webkit-flex-wrap: wrap;
    -webkit-justify-content: center;
}
@media (max-width: 900px) {
    .container {
        width: calc(100% - 12px);
    }
}
.column {
    width: calc(45% - 6px);
    height: auto;
    float: left;
}
@media (max-width: 600px) {
    .column {
        width: calc(100% - 6px);
    }
}

Here is the example in HTML:

<div class="container">
    <div class="column"></div>
    <div class="column"></div>
</div>

You can achieve a similar layout with divs (3 columns) by adjusting the calc equation in .column to ((100% / 3) - 6px). For every additional column, modify the division accordingly. Keep in mind that columns will get smaller as you add more.


Credit goes to @Adam Buchanan Smith

Fluidity with Cards of Different Lengths

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

Efficiently loading Google Visualizations for core charts by utilizing AJAX within jQueryUI tabs

Loading Google Visualizations via AJAX in jQueryUI tabs After encountering a similar issue, I discovered a solution provided in the link above that partially resolved my problem. I am trying to use Google Visualization core charts within jQueryUI tabs wit ...

The issue with setting width using % in React Native is causing trouble

While working on my project using expo react native, I encountered an issue with a horizontal scrollview for images. When I style the images using pixels like this: <Image code... style={{width: 350}}/>, everything works fine. However, if I try to ch ...

In JavaScript, when a div element contains an iframe, the click event will not trigger on the div

Check out this sample HTML markup: <div> <iframe></iframe> </div> The iframe is set to fill the outer div. I've added an event handler to the div click event: div.addEventListener("click", function () { console.log( ...

Is there a way to append the current path to a link that leads to another URL?

I am currently in the process of separating a website, with the English version being on the subdomain en. and the French version residing on the www. Before making this change, I have a drop-down menu that allows users to select their preferred language ...

Retrieve information from hyperlinks and organize it into a structured format in a chart

Is there a way to extract href data from a website page and input it into a table using jQuery? I often have clients who need me to transfer links from their old sites to my CMS. If we can put these links into a spreadsheet format, it would make importing ...

When the form is submitted, delete the file and its corresponding record

Currently, I have a form that deletes a record from my MySQL database. The database stores the image/file name. I am looking to enhance the statement so that it also removes the file in the website directory with the corresponding image/file name. If bot ...

Please refrain from including HTML code within the div element

When I input the following text inside a textarea: <strong>test</strong> and then display it within a div, the text appears in bold instead of showing the HTML code as entered. How can I prevent the HTML code from being rendered and instead d ...

Wagtail Django TableBlock not showing up properly on the webpage

Recently delving into the world of Django and programming, I decided to incorporate Wagtail into my website. However, I've encountered a roadblock with the 'TableBlock' functionality Despite setting up the model and block correctly, I&apos ...

Several HTML dropdown menus that do not expand

On my website, I have several select elements but the first one related to math is giving me trouble. It doesn't expand properly - sometimes it works on the first click but stops working afterwards. I've tested this issue on both Google Chrome a ...

Issues with checkboxes in HTML 5 showing inconsistent behavior

Utilizing: APS.NET MVC 4.0 Incorporating javascript/jquery to manage check boxes within a table, I have encountered an issue where the code functions correctly during the first and second passes. Initially, it checks all the checkboxes, while on the secon ...

Tips for inserting a footer at the bottom of every page during the conversion process from HTML to PDF

Could use some assistance with converting HTML into a PDF report. In particular, I want the footer to always appear at the bottom of the last page, even if there is minimal content on that page. Any suggestions? I've been utilizing the wkhtmltopdf to ...

Why isn't the right-clear property functioning correctly?

My understanding of `clear: left`, `clear: right`, and `clear: both` in CSS has always been a bit fuzzy. I know that `clear: both` means it prevents floating elements from appearing on both sides, but I recently did some testing here. I expected the layout ...

Problems with the design in the Opera browser

My website logo is displaying incorrectly in Opera, despite working fine in Firefox and Chromium. I've been trying to troubleshoot this issue for the past few hours with no success. The ID of the logo element is 'Logo'. You can view the sit ...

Implementing a class change to anchor elements when rearranging them

I have 4 divs and their corresponding anchor tags. The active class is dynamically added to anchors on scroll unless the order of the display elements changes. In that case, active classes are only applied to the sorted elements. I also sorted the nav anch ...

Is it possible to dynamically set the body id in React?

I'm currently working on implementing a dark mode feature in my app. The plan is to insert the following code into the root element: <div id="dark"> Afterwards, add the following CSS: #dark { background-color: #1A1A2E; } Subseq ...

Adjust the CSS of a nested div within the currently selected div upon page load

I am facing an issue with a page that users access by clicking through an ebook. Upon clicking a link, they are directed to a specific product on the page, and the URLs look like example.com/#product1 example.com/#product6 example.com/#product15, etc... ...

Enhance the spacing between the UL-LI navigation menu and the currently selected item

I'm struggling with adjusting the spacing between items in my navigation menu, specifically the li elements that are currently too close together. I've attempted to increase the margin-left within the .main_menu li CSS rule, but it doesn't s ...

Using jQuery to extract the value of an object from an <li> element and updating the class attribute of the <li> element

There is a div element: <div id="ddDistr" class="searchBoxSortDistrict" tabindex="1"> <ul id="ddd"> </ul> </div> I have inserted HTML from json into it: $("#ddd").html(" "), $.each(dis, function( index, value) { $("#dd ...

Guide on achieving horizontal scrolling in Ionic 3

Check out this image I have a list of 10 names in an ion-scroll, but they are appearing on separate lines like paragraphs. Below is my HTML code: <ion-scroll scrollX="true" style="width:100vw; height:50px" > <ion-row class="headerChip"& ...

Determine whether the element is visible in at least half of the viewport

I am working on a project that involves 4 cards with images. I want the image to animate in from the left when it comes into viewport. I have finalized the CSS for this effect, but the JavaScript code always returns false even when the element is visible o ...