Adaptable, two-column design: Place one column between the other column

I have a website that adapts to different screen sizes, featuring a two-column layout in larger browser windows. Currently, the layout is achieved using the float property. However, I want the layout to switch to a single column on smaller screens. In this new configuration, the content from the second column should be displayed between the elements of the main column, as illustrated here:

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

<div class="two-columns">
  <div class="main-column">
    <div class="red-element"></div>
    <div class="yellow-element"></div>
  </div>
  <div class="sidebar-column">
    <div class="green-element"></div>
  </div>
</div>

Although I attempted a flex-box-based solution similar to the one described in this question, the flex-basis property appears to be unsupported in Safari when used with flex-direction: column. Ensuring proper support in Safari is crucial since it is the primary browser for my visitors.

Is there a CSS-only method to achieve this layout adjustment without duplicating the green element in the markup?

Answer №1

Here's a simple solution using a single flex container:

<div class="container">
    <div class="box"> ... </div><!-- red box -->
    <div class="box"> ... </div><!-- green box -->
    <div class="box"> ... </div><!-- yellow box -->
</div>

For smaller screens, start by stacking them vertically:

.container {
    display: flex;
    flex-direction: column;
}

.box {
    height: 100px;
    width: 100%;
}

To adjust the layout for larger screens:

@media (min-width: 800px) {

   .container {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .box {
        flex-basis: 45%;
    }
}

When the screen is wider than 800px, the items will be displayed in a row with wrapping enabled. Each box will have enough width specified by flex-basis to fit only two on each line.


Complete demonstration:

* {
    box-sizing: border-box;
}

.container {
    display: flex;
    flex-direction: column;
    padding: 5px 0;
    background-color: #f5f5f5;
    border: 1px solid #aaa;
}

.box1 { background-color: red; }
.box2 { background-color: lightgreen; }
.box3 { background-color: yellow; }

.box {
    height: 100px;  
    width: calc(100% - 20px);
    margin: 5px 10px;
    border: 1px solid #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2em;
}

@media (min-width: 800px) {
    .container {
        flex-direction: row;
        flex-wrap: wrap;
    }
    .box {
        flex-basis: 45%;
    }
}
<div class="container">
    <div class="box box1"><span>1</span></div>
    <div class="box box2"><span>2</span></div>
    <div class="box box3"><span>3</span></div>
</div>

View jsFiddle Example


Regarding your concern:

...but flex-basis still seems to be unsupported in Safari when flex-direction is column

It appears that flex-basis is supported in Safari. However, you can use width or height properties as alternatives to flex-basis. More information can be found here: Differences between flex-basis and width.

Answer №2

When utilizing Bootstrap,

<div class="row">
    <div class="col-md-8">
        <div class="red-element"></div>
    </div>

    <div class="col-md-4">
        <div class="green-element"></div>
    </div>

    <div class="col-md-8>
        <div class="yellow-element"></div>
    </div>
    <div class="clearfix"></div>
</div>

This approach utilizes float methods and is compatible with all browsers.

Answer №3

To enhance the visual display of your elements, utilize @media along with the margin-top property. When specific screen widths are met (using @media), adjust the margin-top of the green element to -200%, and change the margin-top of the yellow element to 100%. The result is a visually appealing position switch between the two elements. Check out this link for an example:

http://jsbin.com/xozeviseka/edit?html,output

Answer №4

To achieve the desired outcome, adjustments need to be made to the HTML structure.

*,*:after,*:before {
box-sizing:border-box;
}
.two-columns {
position:relative;
background:#EFEFEF;
border:1px solid #000;
}
.red-element,
.green-element,
.yellow-element {
margin-bottom:30px;
}
.red-element {
height:70px;
background:#FF0004;
}
.green-element {
height:70px;
background:#7ED321;
}
.yellow-element {
height:100px;
background:#F8E71C;
}
@media (min-width:767px) {
.main-column {
width:70%;
padding:10px;
}
.sidebar-column {
position:absolute;
right:0;
top:0;
width:30%;
padding:10px;
}
}
<div class="two-columns">
    <div class="main-column">
        <div class="red-element"></div>
            <div class="sidebar-column">
                <div class="green-element"></div>
            </div>
        <div class="yellow-element"></div>
    </div>
</div>

If you prefer not to alter the HTML structure, an alternative solution is to include a separate element that is only visible on mobile devices.

*,*:after,*:before {
box-sizing:border-box;
}
.two-columns {
    background: #EFEFEF;
    border: 1px solid #000;
    overflow: hidden;
}
.red-element,
.green-element,
.yellow-element {
margin-bottom:30px;
}
.red-element {
height:70px;
background:#FF0004;
}
.green-element {
height:70px;
background:#7ED321;
}
.yellow-element {
height:100px;
background:#F8E71C;
}
.hideMobile{
display:none;
}
@media (min-width:767px) {
.main-column {
width: 70%;
float: left;
padding: 10px;
}

.sidebar-column {
float: right;
width: 30%;
padding: 10px;
}
.showMobile {
display:none;
}
.hideMobile {
display:block;
}
}
<div class="two-columns">
  <div class="main-column">
    <div class="red-element"></div>
    <div class="green-element showMobile"></div><!--only for mobile-->
    <div class="yellow-element"></div>
  </div>
  <div class="sidebar-column hideMobile"><!--hide in mobile-->
    <div class="green-element"></div>
  </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

Mac users may experience lag when using Javascript parallax effects

I created a parallax page where the background image changes using JavaScript translateY(- ... px)' similar to what you see on the firewatch website. On Windows, the parallax effect works smoothly. However, on macOS it is smooth only in Safari. All ...

Show the back button only if the user is not currently on the homepage

Is there a way to display a back button in the header when a user transitions from the homepage to the login page? .config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider){ $ionicConfigProvider.tabs.position("standard"); $ioni ...

Using Python's BeautifulSoup library to pull information from an HTML table

I need help extracting a table from a webpage. Here's the HTML and Python code using beautifulsoup that I've been trying with. It usually works for me, but this time it's coming up blank. Any insights are appreciated! <table> <thea ...

The clash between mixitup and bootstrap's .active class causes confusion

I've encountered an issue while trying to implement a mixitup filter gallery on my bootstrap template. It seems to work fine when loaded under the first active tab-pane, but fails to work properly when placed under the second or third tab-pane. Could ...

The JavaScript function modifies the value stored in the local storage

I'm in the process of developing a website that requires updating the value of my local storage when certain JavaScript functions are executed. Currently, I have this code snippet: localStorage.setItem('colorvar', '#EBDBC2'); I&ap ...

Span inside button not responding to clicks

I'm encountering an issue with a simple button that contains a span with a bootstrap icon as its class. It seems like nothing serious, but the problem is that the click event is not registering when I click on the rendered button where the icon is pla ...

Capturing user input in HTML and passing it to JavaScript

I have a good grasp on how to implement input in HTML to execute JavaScript functions that are defined in the same directory as the HTML file. For example: <input type="button" value="Submit" onclick="someFunc(500)" > When the button is clicked, th ...

Disabling Request.Form validation for inherited button control

Is there a way to disable Request.Form validation for individual controls? The issue comes up when IE 6 and IE 7 automatically fill the content of the <button> tag into the name attribute, causing ASP.NET server errors due to HTML code in the attribu ...

Tips for aligning text in the middle of a customizable and adjustable button with the use of only Bootstrap 4

I am encountering an issue with button text alignment. After customizing the width and height of a button, I noticed that its text does not remain centered on certain screen sizes while in debugging mode. Html <!doctype html> <html lang="en> ...

Arrange list items to consume specific width dimensions

I think showing an example would be the best way to answer this question. Check out this JSFiddle. HTML: <ul> <li> <a href="#"> <img alt="placeholderImg" src="http:/ ...

jQuery datatables provide a powerful feature for column filtering using jQuery functions

Looking for advice on how to add a filter to my jQuery datatable column that contains checkboxes. I want the filter to be a drop-down menu with options for "checked" and "unchecked", so that when a user selects one of these options, only the rows contain ...

HTML and JavaScript - Facing issues rendering HTML content during the conversion process from Markdown format

In this particular scenario, my goal is to transform the content inside #fileDisplayArea into markdown format. However, I am encountering an issue where the HTML code within the div element is not being rendered. <div id="fileDisplayArea"># Title ...

How to align scrolling images with their scroll origin - a step by step guide

Curious to know the name of the effect where images scroll in a different orientation than the page, creating a 2D appearance. Take a look at the Google Nexus website and scroll down - do you see the effect? What is this effect called? Is it created usin ...

HTML: Choose from a list of options by selecting multiple items from

I want to implement a select field with the multiple attribute as a standard dropdown field with size=1: <select name="test[]" size="1" multiple> <option>123 <option>456 <option>789 </select> Any reason why the a ...

What is the best way to implement jQuery on my website?

Instead of copying all the example code here, you can check out the jQuery demo page: Demo Page I would like to integrate the Latest News example with scrolling text in red from the demo page into my website. The demo page also provides links to the sour ...

Trouble arises with AJAX due to DOM traversal errors

I am trying to set up a system for liking and disliking with a counter. However, I am facing issues with my AJAX call, specifically when attempting to change the HTML of selected elements in the view after sending values to the DB. The element in question ...

What is the best way to retrieve the file and directory tree structure from git using php?

I am looking to streamline the process of downloading zip files from my public git repository on Bitbucket. Rather than manually creating a download button for each zip file, I want to dynamically generate a PHP page that displays all available zip files a ...

What is the best way to monitor and record the height of a div element in a React application?

Exploring the Height of a Div I am interested in monitoring the height of a div element so that I can dynamically adjust distances based on varying screen widths. The animation should respond to changes in screen width, such as when text stacks and the he ...

Generate a new Div dynamically, positioned higher than the rest

Once a second, this script will utilize AJAX to open a new page and display the contents in a dynamically created div on this page. However, the issue is that the new divs are stacking under each other, and I would prefer them to be added at the top instea ...

Is it possible to customize the appearance of ordered list numbers using javascript?

Is it possible to change the color of a specific line number in an ordered list using JavaScript? I am familiar with CSS styling such as li::marker, but curious about altering individual list item markers dynamically through JavaScript. How can this be ach ...