Adding a block between two other blocks on a mobile device using Bootstrap 4

I am trying to achieve a specific layout on both desktop and mobile devices. On desktop, I want the layout to be structured as follows:

[1][2]
[3][2]

The requirement is for column 2 to be the same height as columns 1 and 3 on desktop screens.

For the mobile layout, I want the structure to change to:

[1]
[2]
[3]

I have attempted two different options to achieve this layout:

Option 1:

        <div class="row">
            <div class="col-md-7">
                <span>1</span>
            </div>

            <div class="col-md-5">
                <span>2</span>
            </div>

            <div class="col-md-7">
                <span>3</span>
            </div>
        </div>

The issue with this approach is that block 2 ends up being the height of block 1 only.

Option 2:

I considered using column ordering to place block 2 between blocks 1 and 3:

        <div class="row">
            <div class="col-md-7">
                <div>
                    <span>1</span>
                </div>

                <div>
                    <span>3</span>
                </div>
            </div>

            <div class="col-md-5">
                <span>2</span>
            </div>
        </div>

However, I believe this approach may not be possible.

Are there any alternative methods that I have overlooked? Or is it feasible to achieve this layout using one of the methods mentioned above?

Thank you.

Answer №1

Here's an example you can try out:

#flex_box1 div
{
  width:100%;
  height:150px;
  border: 2px solid #555;
  margin-bottom:15px;  
}
#flex_box1 div:last-child{ margin:0; }
#flex_box2 div
{
  width:100%;
  height:100%;
  background-color: #555; 
}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col-4 d-flex flex-column" id="flex_box1">
          <div></div>
          <div></div>
        </div>
        <div class="col-8 " id="flex_box2">
          <div></div>
        </div>
      </div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

Answer №2

If you're looking for a simple and efficient solution, I recommend using the Masonry library. It's easy to implement and provides great results for both desktop and mobile versions. You can find more information and the link here.

For a demo of how Masonry works, you can check out this link: https://codepen.io/Bibeva/pen/qBExWjj

To implement Masonry, you can use the following HTML:

<!-- Include Bootstrap 4 CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
  integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="row grid">
  <div class="col-md-6 grid-item">
    <span>1</span>
  </div>

  <div class="col-md-6 grid-item grid-item-box">
    <span>2</span>
  </div>

  <div class="col-md-6 grid-item">
    <span>3</span>
  </div>
</div>

<!-- Include jQuery CDN -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<!-- Include Masonry JS CDN -->
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.js"></script>
<!-- Custom JS -->
<script>
$('.grid').masonry({
  itemSelector: '.grid-item'
});
</script>

And don't forget the CSS:

.grid-item { 
  background: #000;
  color: #fff;
  height: 100px;
}

.grid-item-box { 
  height: 200px;
  background: #333;
}

Answer №3

.container1{
    display:inline-flex;
    align-items: flex-start;
    justify-content: flex-start;
    flex-direction: row;
    flex-wrap: wrap;
    position: relative;
 }
.container1 div {
    height: 50vh;
    width: 200px;
    margin: 0;
    border: 1px solid black;
    color: #fff;
    text-align: center;
    align-items: center;
    display: inline-flex;
    justify-content: center;
    font-size: 50px;
    font-weight: bold;
}
.div1 {
    background: #f60;
}
.div2 {
    background: #f55;
    height: 100vh!important;
}
.div3 {
    position: absolute;
    bottom: 0;
    left:0;
    background: #0ae4dd;
}
<div class="container1">
  <div class="div1">1</div>
  <div class="div2">2</div>
  <div class="div3">3</div>
</div>

<pre>
**For Mobile View** 
@media(max-width: 992px){
  .container1{
    flex-wrap: wrap;
    width: 100%;
    max-width: 100%;
  }
  .container1 div {
    width:100%;
    height: auto;
  }
  .div2 {
    height: auto!important;
  } 
  .div3{
    position:inherit;
  }
}
</pre>

Answer №4

In the event that someone requires a solution in the future, I ultimately decided to utilize flex-direction: column and set a height on the container:

HTML:

        <div id="flex-container" class="row flex-lg-column">
            <div class="col-12 col-lg-6 order-1">
                <span>1</span>
            </div>

            <div class="col-12 col-lg-6 order-3">
                <span>2</span>
            </div>

            <div class="col-12 col-lg-6 order-2">
                <span>3</span>
            </div>
        </div>

CSS:

@media (min-width: 992px) {

    #flex-container { 
        max-height: 40rem; 
    }
    #flex-container .order-1, 
    #flex-container .order-2, 
    #flex-container .order-3 { order: initial; }
}

Furthermore, due to the fact that Bootstrap 4 .row has flex-wrap: wrap, the third block will wrap to the right side of the other two blocks on large screens.

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

Error in JavaScript: Uncaught TypeError - Unable to access the "left" property of an undefined object

This error is really frustrating and I've come across several inquiries regarding this console issue. It's not providing enough information in the chrome console for me to effectively troubleshoot. /** * Adjustment of dropdown menu positio ...

The Blockquote class is not displaying the lines properly in Bootstrap 4 Beta 3

I encountered an issue with two simple blockquote HTML elements. Despite adding the Bootstrap blackquote class, I expected a bigger font size and lines in the blockquotes. Unfortunately, while the fonts did become larger, the lines did not appear as antici ...

Can a stack method be utilized to create a HTML tag checker?

I've been developing a project to create an HTML validation tool based on stacks, but I've encountered some issues. The main objective is to verify if the HTML tags are correctly nested and closed. However, there seem to be discrepancies in the r ...

Utilizing the "row" and "column" attributes in Angular (Flexbox) results in significant spacing between input fields within the HTML code

I'm working on aligning 2 input fields side by side within the same line. To achieve this, I've been utilizing Flexbox. However, I've observed that when using Flex with both 'row' and 'column', it introduces extra spacing ...

Strategies for accessing elements within a #shadow-root

Having trouble changing the display style inside of the #shadow-root and targeting it. Can I do it through CSS or JS? #retirement-services-modal #rs-two-col::shadow(.wrapper) { display: flex; align-items: center; } ...

Dynamic content that adjusts based on a multi-row element

I'm currently designing a webpage layout with a calendar on one side and a series of information-divs on the other. In desktop view, I want the calendar to span three rows like so: CAL DIV1 CAL DIV2 CAL DIV3 Right now, I am achieving this using neste ...

Show the current temperature data retrieved from the weather API

I'm trying to figure out what's causing the issue with the temperature not displaying. Can anyone help me identify and fix the error? <script type="text/javascript"> $.ajax({ url: 'https://api.weather.gov/gridpoints/EWX ...

I'm struggling to retrieve and display data from my Flask application using console log. It seems to be a challenge when combining it with

I am encountering an issue while attempting to load data into my Flask page and log it in the console. The error I keep receiving is shown below: VM165:1 Uncaught ReferenceError: data is not defined at <anonymous>:1:13 Below are snippets of cod ...

How to perfectly center a dropdown menu using CSS across different web browsers

I am dealing with a dropdown menu that is auto-generated by WordPress, so altering the HTML structure is not an option. However, I have the flexibility to adjust the CSS styling to correct an alignment issue in my dropdown menu. Currently, I'm facing ...

Optimal methods for refreshing website lists

I've designed an ASP.NET MVC application with a list that displays all the current items from a database. There is also a button that triggers a popup-dialog to create a new entry. The goal is to automatically add the new item to the list once it is c ...

Creating PNG images in a scripting language for web applications

Imagine giving your website user the ability to specify a radius size, let's say 5px, and in return receiving a PNG file with a circle of that radius. This question can be divided into two sections: In what language and using which technologies can ...

React component allowing for the reuse of avatars

As a novice in React, I've encountered a challenge. My goal is to develop a simple reusable component using MUI. Specifically, I aim to create an avatar component that can be easily customized with different images when called upon. I want the flexibi ...

MacOS web browsers adjust content to fit the screen size

As I delve into React development, one thing that has caught my attention is how MacOS browsers try to fit all the page content in view rather than allowing users to scroll. Let me illustrate this with an example: function Example() { const Elements = ...

Tips for correcting the sentence that extends beyond the boundaries of the Material UI box

Utilizing Material UI for my project... I am trying to contain the text within a box but facing issues with responsiveness as the text is overflowing outside of the box. Note: I have attempted various solutions from Stack Overflow such as word-wrap, max- ...

Implementing a dual hover effect on a Div element

I am working with HTML code that includes an image and text <div class="subcontainer1"> <img src="a.png" alt="" class="imgcolumn"> <h3 class="header3">Hello</h3> </div> This setup places the content above the image. ...

Error in thread : TagNameUnexpectedException

I encountered an issue while trying to find a dropdown using Select: An error occurred: Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input" Even when attempting to use ...

Aligning Content in the Header

Currently, I am attempting to place a logo on my WordPress site right at the top of the header above the menus and in the center. Even though I've positioned it at the top, I'm struggling to align it horizontally in the center. I've experime ...

Create an XML document using the DOM structure, but exclude specific elements in the process

Having some HTML that needs to be converted into an XML document, I am struggling to skip certain elements and focus only on the divs. I wrote a simple DOM traversal function to achieve this, but it seems to be stuck in an infinite loop. (More details belo ...

I am attempting to center an image on a webpage while disregarding the margins set on the

Bar.html: <!DOCTYPE html> <head> <link rel="stylesheet" type="text/css" href="customStyle.css" /> </head> <body> The text formatting is spot on. Unfortunately, the following image should be centered, but it's off ...

The lines intersecting with the ethereal image in the background

I'm trying to achieve button links with an image overlay, but so far I've only been able to do it using CSS: section a:link, section a:visited { width: 150px; -moz-border-radius: 10px; -webkit-border-radius: 10px; border-radius: ...