Creating CSS to wrap two columns for the main content and menu bar

I'm looking to create a menu bar that aligns on the right side of the page and allows the main content to flow underneath it. While using float:right; in CSS accomplishes this for desktop viewing, on mobile devices the menu bar appears above the content rather than below it. I've experimented with various methods like flex tables and reorder without success. Any guidance on how to resolve this would be highly appreciated.

.content {
    display: block;
    background: #f5f5f5;
    width: 100%;
}

.information {
    background: #ddd;
    padding: 10px;
    width: 100;
    display: block;
}

.menu {
    width: 100%;
    display: block;
}

.side-info {
    display: block;
    text-align: center;
    font-size: 13px;
    padding: 10px 20px;
}

.colour-1 {
    background: pink;
}

.colour-2 {
    background: lightblue;
}

@media all and (min-width: 992px) {
    .content {
        max-width: 1200px;
        background: #ccc;
        margin: 0 auto;
    }

    .information {
        width: auto;
    }

    .menu {
        width: 250px !important;
        display: block;
        float: right;
    }
}
<section class="content">

    <aside class="menu">
        <div class="side-info colour-1">Menu Item </div>
        <div class="side-info colour-2">Menu Item </div>
        <div class="side-info colour-1">Menu Item </div>
        <div class="side-info colour-2">Menu Item </div>
        <div class="side-info colour-1">Menu Item </div>
        <div class="side-info colour-2">Menu Item </div>
        <div class="side-info colour-1">Menu Item </div>
        <div class="side-info colour-2">Menu Item </div>
    </aside>

    <div class="information">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sagittis tristique enim, ut laoreet est maximus non. Duis cursus, ligula quis porta scelerisque, nunc nunc gravida neque, faucibus varius erat est non ipsum. Nunc vitae risus maximus, luctus turpis nec, facilisis justo. Pellentesque fermentum, nulla nec accumsan luctus, elit dolor tincidunt dui, vel imperdiet tortor massa et magna. Duis volutpat suscipit luctus. Sed sit amet nulla mollis, volutpat diam eu, lacinia velit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec lobortis urna ante, imperdiet ornare diam aliquet in. Vestibulum quis purus ultricies, pretium nulla id, maximus risus. Nunc volutpat, leo vitae imperdiet consectetur, elit odio ornare ipsum, sed fringilla diam lectus vel purus. Aenean quis diam sem. Donec quis tincidunt neque, quis eleifend felis. In hac habitasse platea dictumst. Praesent scelerisque, sapien vitae condimentum vestibulum, neque tellus tempus erat, et interdum arcu velit vel ipsum.</p>
        <p>In blandit sed augue nec hendrerit. Curabitur sed finibus mi. Donec in laoreet sapien, ac blandit odio. In hac habitasse platea dictumst. Donec vel lorem vitae lacus placerat facilisis. Etiam tortor dui, consequat sit amet lorem eu, lacinia eleifend tellus. Sed tristique tincidunt iaculis. Suspendisse laoreet augue leo, eu hendrerit neque pellentesque sed.</p>
        <p>Aliquam a suscipit ante. Sed fermentum eros a libero pellentesque imperdiet. Maecenas bibendum lobortis scelerisque. Sed nisi arcu, tristique sit amet lectus sit amet, suscipit gravida ex. Ut ut tortor cursus nulla lobortis posuere non vitae libero. Aliquam vitae neque ornare, tincidunt nibh ac, accumsan magna. In ut malesuada sem. Fusce non porttitor ante.</p>
        <p>Nulla at leo eget eros congue aliquet. Vivamus tincidunt erat ligula, sed tincidunt purus ullamcorper sit amet. Vestibulum suscipit molestie lectus in sagittis. Ut ipsum urna, tempor eget libero sagittis, gravida fringilla orci. Sed efficitur porta enim, eu auctor neque dignissim non. Suspendisse vitae purus at purus tristique pulvinar. Ut sed eros commodo, luctus diam sit amet, pulvinar tellus. </p>
    </div>

</section>

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

Answer №1

If you're looking to implement this using flexbox, simply utilize flex-wrap: wrap-reverse;

Check out the live example below:

.content {
    display: flex;
    background: #f5f5f5;
    width: 100%;
    flex-wrap: wrap-reverse;
}

.information {
    background: #ddd;
    padding: 10px;
    width: 100;
    display: block;
}

.menu {
    width: 100%;
    display: block;
}

.side-info {
    display: block;
    text-align: center;
    font-size: 13px;
    padding: 10px 20px;
}

.colour-1 {
    background: pink;
}

.colour-2 {
    background: lightblue;
}

@media all and (min-width: 992px) {
    .content {
        display: block;
        max-width: 1200px;
        background: #ccc;
        margin: 0 auto;
    }

    .information {
        width: auto;
    }

    .menu {
        width: 250px !important;
        float: right;
    }
}
<section class="content">

    <aside class="menu">
        <div class="side-info colour-1">Menu Item </div>
	...
</section>

Answer №2

Embrace the power of flex for easier styling.

  1. Avoid using display: block on <div> elements as it is already the default property.
  2. Reorder the blocks, swapping .information and <aside>.
<style>
    .content {
        display: flex;
        flex-wrap: nowrap;
        background: #ccc;       
        max-width: 1200px;
        margin: 0 auto;
    }
    .information {background: #ddd; padding: 10px; width: 100;}
    .menu {
        flex: 0 0 250px;
    }
    .side-info {display:block; text-align:center; font-size: 13px; padding: 10px 20px; }
    .colour-1 {background:pink; }
    .colour-2 {background: lightblue;}

    @media all and (max-width: 992px) {
        .content {width: 100%; background: #f5f5f5; flex-direction: column; }
        .menu {flex: 0 0 auto;}
    }
</style>

<section class="content">
    <div class="information">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sagittis tristique enim, ut laoreet est maximus non....</p>
    </div>

    <aside class="menu">
        <div class="side-info colour-1">Menu Item </div>
        <div class="side-info colour-2">Menu Item </div>
        <div class="side-info colour-1">Menu Item </div>
        <div class="side-info colour-2">Menu Item </div>
        <div class="side-info colour-1">Menu Item </div>
        <div class="side-info colour-2">Menu Item </div>
        <div class="side-info colour-1">Menu Item </div>
        <div class="side-info colour-2">Menu Item </div>
    </aside>
</section>

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

Hide in certain zones, contextual menu

I recently discovered how to create my own context menu with links based on the div clicked (check out here for more details). However, I am facing an issue now. I am struggling to prevent the context menu from appearing above the specific div boxes where ...

Having trouble displaying the cover image properly on mobile devices using Bootstrap/MDBootstrap, facing issues with the live server as well

My project includes a cover image in .jpg format that displays perfectly on desktop, but is invisible on mobile devices. When viewed through Live Server on Vscode, the image appears at all sizes including mobile, but when hosted on Netlify and accessed fro ...

Chrome's display of HTML5 form validation bubble is not aligned correctly

Can anyone shed some light on why the Form Validation Bubble is showing up with a big offset in Google Chrome when trying to validate a form inside a jquery UI dialog? It seems to work fine when the javascript call creating the dialog is removed. Just want ...

What is causing the pull-right class to not function correctly in Bootstrap version 4.1.0?

After upgrading from Bootstrap version 3+ to 4.1.0, I encountered an issue with aligning elements using pull-right and pull-left, as well as float-right and float-left. The problem persists despite my efforts. Here is an image (https://i.sstatic.net/Z6ba3. ...

Numerous sections of content

Struggling to align these two pieces of content side by side. While I've had success displaying content like this in the past, I'm hitting a roadblock this time around. Any assistance would be greatly appreciated. HTML <div class="block-one" ...

Transform the usual button into a jQuery button that requires a press and hold action

Within my code, there are two buttons in play. One button triggers the burger menu, while the second button adjusts the size of a div. I am looking to transform only the second button into a hold button that activates after a 3-second delay. code: $doc ...

Integrate Vue.js with Laravel

Hey there fellow developers, I'm a university student who is new to using frameworks. Recently, I completed a project for my internship using the Laravel framework for the back end. Now, I want to tackle the front end using VueJS. Can anyone guide me ...

Is there a way to incorporate ellipsis for text that overflows in the AntDesign Table component?

Issue: While working with AntDesign's Table component, I have successfully set the width of each cell. However, I am facing a challenge with text overflow as I would like the overflowing text to be truncated and displayed with an ellipsis. My ultimate ...

Overlay one image on top of another

Recently, I've been working on this code snippet. I am attempting to center the profile picture even when the screen width is reduced. The issue lies with using position:absolute, preventing margins:auto from functioning properly. As a workaround, I ...

Guide to incorporating HTML formatting and tags from JSON URL into Swift 3

Currently, I am in the process of developing an application that utilizes data retrieved from a JSON URL. The JSON data contains HTML formatting and tags. My main concern is how to preserve the formatting, such as bold text, when working with SWIFT 3. To ...

Synchronization Problem between Background-Color Transition and Before/After Opacity Transition

I'm currently experiencing difficulties trying to fade in a navigation item using before and after pseudo-elements. When I hover over the nav item, it should transition its background-color from white to blue. It's a simple effect, but it also n ...

Ways to detect events even after the object has been swapped out in JavaScript

I am currently developing a JavaScript-based Scrabble game and encountering a puzzling issue. The problem arises when tiles are generated within a tile rack div using a specific function, and an event listener is set up to respond to clicks on the tile div ...

Guide to enabling picklist editing in the specified component within an Angular application

Currently utilizing the p-picklist component and looking to customize the displayed data in the target list span. You can find the source code at: Is there a way to enable editing of the product name within the target list? <p-pickList [source]=&quo ...

Slider MIA - Where Did it Go?

I'm having an issue with my slider. When I load the page, it doesn't show the first image until I click the button to load it. How can I make it display the first image by default? Below is my JavaScript code: <script> var slideIndex = 1 ...

How come I am experiencing difficulty modifying the background color of ion-header using a CSS class selector within my Ionic Vue component?

Recently, I began my journey with ionic vue and started incorporating CSS with the components. I added styles in the same file like this: <style scoped> .abc { background-color: blue; } </style> I attempted to apply these style ...

How to trigger a hover effect on a div and its child simultaneously using HTML and jQuery

Here's my concept: I want the text to be contained within div elements with an integrated image, rather than just having fading in and out pictures. Here's my attempt: #first{ position: absolute; } #second{ position: absolute; -we ...

Issue with Bootstrap 4 navbar collapse: it collapses correctly but unable to expand

I am currently learning how to use Bootstrap and I am experimenting with the navbar collapse feature. Although I have successfully collapsed the navbar, I am facing an issue where it does not expand when clicked on. I have exhausted all possible solutions ...

Tabulator and its convenient scrollable column feature allows for easy navigation

In case my tabulator column is exceeding its length, is there a way to enable scroll functionality for that specific column? Although the resizable rows feature permits users to resize and view all the content, can a scrollbar be implemented exclusively ...

What is the process of retrieving user input from an HTML form and locating specific data within it?

Here is the structure of my form: <div id="employeeinfo" style="padding:40px" class="employee-body"> <form id="employeeform" title="" method="post"> <label class="title">First Name</label> < ...

Is it possible to apply a slanted CSS3 line-through effect on text?

Can CSS3 be used to create a slanted strike-through line over text, starting from the bottom-left and going to the top-right in a diagonal direction? ...