Position the right div to float to the right with a dynamic width ahead of the left div in HTML to achieve

This particular challenge has proven to be quite difficult for me, and I have a feeling that I am overlooking something simple.

My goal is to create a layout with a fixed-width left sidebar and a responsive variable-width content section on the right.

The tricky part is that I want the HTML code for the left sidebar to come after the content section so that I can easily reposition them for mobile screens without using floats. This way, the content will appear above the sidebar when the resolution is reduced, both sections taking up 100% of the width.

.header {
  width: 100%;
  background-color: green;
}
.content {
  background-color: red;
  height: 100px;
  float: right;
}
.sidebar {
  background: blue;
  width: 240px;
  height: 100px;
}
@media (max-width: 750px) {
  .content {
    float: none;
    width: 100%;
  }
  .sidebar {
    width: 100%;
  }
}
<div class="header">Top</div>
<div class="content">Content (right on widescreen, top on less than 750px</div>
<div class="sidebar">Sidebar (left on widescreen, bottom on less than 750px</div>

Explore the solution here!

Answer №1

If you want your content to be responsive and fill the remaining width, you can achieve this by using calc() in your CSS. Simply add the following line of code: width: calc(100% - 240px); to the .content class.

Check out an example here

.header {
    width: 100%;
    background-color: green;
}
.content {
    background-color: red;
    height: 100px;
    float: right;
    width: calc(100% - 240px);
}
.sidebar {
    background: blue;
    width: 240px;
    height: 100px;
}
@media (max-width: 750px) {
    .content {
        float: none;
        width: 100%;
    }
    .sidebar {
        width:100%;
    }
}

For more information on calc() function in CSS, visit this link

Answer №2

I believe I understand now!

Could you please verify:

@media (max-width: 750px) {
.content {
    float: none;
    width: 100%;
    position: relative;
}
.sidebar {
    width:100%;
    float: ;
    position: absolute;
    bottom: 0;
}

Kindly review the following link to confirm if this is what you need:

http://jsfiddle.net/a5LLhmo8/2/

I trust this information is beneficial.

Answer №3

Utilizing flexbox design is a great way to create a responsive layout for your webpage without relying on media queries. It offers a high level of flexibility, making it easier to adapt to different screen sizes.

Below is an example of how you can structure your HTML and CSS using flexbox:

<div class="header">Top</div>
<div class="container">
    <div class="sidebar">Sidebar</div>
    <div class="content">Content</div>
</div>

To style the elements with CSS:

.header {
    width: 100%;
    background-color: green;
}
.container {
    display: flex;
    flex-wrap: wrap-reverse;
}
.content {
    width: 300px; /* Set minimum content width */
    background-color: red;
    height: 100px;
    flex-grow: 1;
}
.sidebar {
    background: blue;
    width: 240px;
    height: 100px;
}

If you want to learn more about flexbox design, visit this resource: http://css-tricks.com/snippets/css/a-guide-to-flexbox/

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

creating unique styles for your Ionic app with Angular using JSON

Having trouble changing the item color. Does anyone know why my CSS isn't working, or if I'm missing something? <ion-view title="Add order to a table"> <ion-content class="has-header has-subheader"> <ion-list> ...

Switch the CSS class when clicking on a responsive table within a table

I am looking to update the background color of a row within a table when a tr is clicked. Below is the code for the table: <div class="table-responsive"> <table class="table table-hover"> <thead> ...

delete the initial background color assigned to the button

The "ADD TO CART" and "save design" buttons are currently displaying as shown in the image below. I would like to make the "SAVE DESIGN" button look similar to the "ADD TO CART" button, meaning I want to remove the background-color and dot symbol. Code ...

What is the recommended image size for Next.js websites?

According to documentation: The width of the image, in pixels. Must be an integer without a unit. The height of the image, in pixels. Must be an integer without a unit. https://nextjs.org/docs/api-reference/next/image Different devices come in various ...

Ways to Toggle div Visibility for Elements with Identical Class Names on an Individual Basis

After searching for solutions on stackoverflow, I attempted to implement some answers provided by other users, but I'm still not achieving the desired outcome. In my website's about section, there are four different items. When a specific item&a ...

Converting HTML content into a single string simplifies data manipulation and extraction

exampleHTML=" This is sample HTML code that needs to be converted into a string using JavaScript </p>" I am looking to transform it into a single string format like str="This is sample HTML code, that needs to be converted into a string ...

Modify the text of a button using JavaScript without referencing a specific div class

THE ISSUE I'm facing a challenge in changing the text of a button on my website. The <div> I need to target doesn't have a specific class, making it difficult for me to make this edit. While I have some basic understanding of JavaScript, ...

Enlarged text size disrupts alignment of ul menu items

After creating a fixed menu using unordered lists and extensive CSS, I added a custom class button that redirects back to the frontpage. However, I noticed an extra pixel of height in the menu causing alignment issues with the buttons. The big button stick ...

I am interested in using a loop in Angular to highlight my div element

Enhancing my comprehension regarding the mentioned images. If I don't select anything within the div property, the default style (css) should appear like this, at least when one div is selected. However, the issue arises when unable to select. This ...

What do you do when you need to hide a table column that has a colspan

I have been searching for a solution to my unique table structure, but I haven't found any that match my situation. When attempting to hide column A or B, the following code works perfectly: $('table td:nth-child(1),table th:nth-child(1)') ...

Material-UI LeftNav with a sticky header

I attempted to create a fixed header, specifically a Toolbar, inside a LeftNav so that when the LeftNav is scrolled, the Toolbar remains in place. However, for some reason, setting position: 'fixed' on the Toolbar does not work within the LeftNav ...

Background Image Division (Twitter Bootstrap)

I am facing a challenge while creating a module with a span8 width and varying height. The div contains an image that dictates its height, with text overlaid on the image. My issue lies in preventing part of the image from getting cropped when Bootstrap re ...

Issue with mouseMove function not aligning correctly with object-fit:contain CSS property

My current code allows users to select a color from an image when hovering over the pixel with the mouse. However, I am encountering an issue where the colors do not map correctly when using object-fit: contain for the image. The script seems to be treatin ...

Could it be a mistake causing Echo to fail in fetching information from advanced custom fields?

When setting up in-content advertising for my blog posts on WordPress, I wanted the ads to point towards other sections of my site. Everything was working fine with the shortcode [in-content] pulling the most recent post from the 'advertising' cu ...

Click to enlarge the text on button press

Recently, I've been working on a project involving a table that has a button at the end. My goal is to double the text size of the rows in the table when the button is clicked. As someone who is new to JQuery, this question may sound basic but any hel ...

The <input> element is not functioning as expected when attempting to use it as a button. I am struggling to find the solution to

My HTML file contains: <!Doctype HTML> <html> <head> <title>Orange’s Game</title> <meta charset="utf-8"> <script src="game.js”></script> </head> <body> <input type="button ...

Steps for creating checkboxes for individual table rows in HTML using embedded PHP and updating their values in a PostgreSQL database:1. Begin by iterating through each

I have already retrieved the data from the database. It is displayed on the HTML table, but it is currently in non-editable mode. I need to ensure that the data shown is accurate and update its Boolean value in the database. Otherwise, incorrect records sh ...

Extract information from page 1 to page 2 by utilizing ajax

Greetings, I am currently facing an issue with parsing data using the href get method to retrieve PHP by ajax on another page. Is this achievable? By the way, my framework of choice is CODEIGNITER. This is my HTML href code snippet: <a href="'.b ...

The Safari browser is plagued by a flickering issue with CSS perspective flip animations, even when using the back

I'm facing an issue with Safari (both desktop and iOS) related to a simple CSS flip animation. Despite searching through similar posts and answers, I haven't found a solution yet and am in need of a fresh perspective. Essentially, I am creating ...

When a link is clicked, submit a form and send it to several email addresses simultaneously

My form has been styled using the uniform jQuery plugin. Instead of using an input type submit for submitting the form, I have utilized a link and added some effects to it to prevent it from being formatted with uniform. <form> <ul> ...