How about creating a responsive layout using only CSS3 for dynamic width adjustments?

I'm struggling to solve a design challenge involving three columns: a navbar (dark gray), main content (dark red), and sidebar (dark green). The navbar should be expandable and collapsible, while the sidebar should slide in and out. My goal is to make this layout responsive by adjusting the size of the main content area when either or both the navbar and sidebar are expanded. My current approach involves using calc(100% - navbar width - sidebar width) to determine the width of the main content, but this solution feels verbose when checking if the sidebar, navbar, or both are expanded.

Here's an illustration showing how the main content shrinks:

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

I've considered using flexbox for this layout, but I haven't been able to figure it out yet.

Here's an example markup:

<nav> </nav>
<main> </main>
<aside> </aside>

Note: The nav and aside elements need to be 100% height of the page and fixed in place.

Answer №1

If you want to achieve this layout, consider using the flex-box approach. Here is a basic example: http://codepen.io/anon/pen/pgVVJb

Feel free to experiment with changing the classes to see how it impacts the layout. Note that I am utilizing classes to adjust column widths, but you could also accomplish this using JavaScript or static CSS in a similar manner.

Here's the code snippet:

<div class="container">
  <div class="small">Nav</div>
  <div>Content</div>
  <div class="medium">Sidebar</div>
</div>

html, body, div {
  height: 100%;  
  margin: 0;
  padding: 0;
}

.container {
  display: flex;
}

.container div {
  flex: 1;
  border: 1px solid #ccc;
  background: gray;
}

.small {
  max-width: 50px;
}

.medium {
  max-width: 150px;
}

Answer №2

To address this issue effectively, a commonly used method involves enclosing all the elements within a wrapper with position: relative or alternatively setting the body's position to relative and positioning all inner elements as absolute. The styling for each element can then be set as follows:

.navbar {
    position: absolute;
    top: 0px;
    left: 0px;
    bottom: 0px;
    width: 50px;
}

.main-content {
    position: absolute;
    top: 0px;
    left: 50px;
    bottom: 0px;
    right: 150px;
 }

.sidebar {
    position: absolute;
    top: 0px;
    right: 0px;
    bottom: 0px;
    width: 150px;
 }

It is important to note that the container element must have a specified height for this layout technique to function properly.

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

Developing a progress bar with jQuery and Cascading Style Sheets (

Below is the code I'm currently using: <progress id="amount" value="0" max="100"></progress> Here is the JavaScript snippet I have implemented: <script> for (var i = 0; i < 240; i++) { setTimeout(function () { // this repre ...

A div housing a jQuery progress bar within an unordered list (ul)

Having an issue incorporating a jQuery progress bar into a ul li tag. <ul> <li> <a href="test">test</a> <div id="progressbar"></div> </li> </ul> I have set display: block for the anchor tag and ...

Update the CSS for InputLabel

I have a drop-down list that I want to customize. The issue is illustrated below: https://i.sstatic.net/hzVtl.png I'm looking to center the text "choose format" within the field and adjust the font size. return ( <FormControl sx={{ m: 1 ...

Menu disappears below the screen

I am experiencing an issue with my dropdown menu. I am having trouble figuring out how to adjust the position of the dropdown that appears when hovering over the "support" button. I would like to move it slightly to the left as half of it is extending off ...

What is the best way to implement the Gotham-Light font using @font-face?

Looking to display Gotham-Light font on a website using CSS but lacking coding skills? I have the fonts Gotham-Light.eot and Gotham-Light.ttf. How do I go about using them in a browser? Thank you for your assistance! ...

Consider implementing a fixed position to the navigation bar

Currently, I am facing an issue with my menu layout as shown in this demo The problem arises when the menu goes outside of its container, similar to the image below: https://i.sstatic.net/nQA2K.png This issue occurs because I have applied position: fixe ...

The proper functioning of border-collapse and empty-cells together is not achieved

Refer to the link provided: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Tables To conceal empty cells, use the rule empty-cells: hide;. This allows a cell's parent element background to shine through the cell if it is emp ...

Creating a dynamic star rating system using CSS

A unique code pen project showcasing a pure css star rating interface can be found at: https://codepen.io/yaworek/pen/JJpEaZ Below is the custom css part: /*** * Custom Pure CSS Star Rating Widget for Bootstrap 4 * * www.TheMastercut.co * ***/ ...

scrollbar not appearing in Safari OSX

I have a side menu that slides in and out (utilizing Zurb Foundation). The overflow is set to auto so users can scroll through the menu if it extends beyond the screen. This functionality is currently supported on these browsers: Chrome Firefox Internet ...

Bootstrap - Progress Bar Display

I have a mobile application that is built using Bootstrap 3. I am currently working on creating a responsive progress indicator for the app. This indicator will consist of three to five steps, each showing an icon, step name, and status. On mobile devices, ...

Integrating video content directly into HTML code is a powerful

I've been attempting to solve this issue for ages now and I just can't seem to find a solution. Embedding a video using QuickTime has been causing major glitches with the latest release. Is there an alternative method to embed a video besides rel ...

Chrome page loading with smooth color transitions

Setting global colors on links and transitions on their color: a { color: blue; transition: color 300ms linear; } Later in the code, there's more specific styling for links in the navigation: nav a { color: red; } nav a:hover { color: black; } ...

Exploring ways to distribute the height of parent div among child divs

I am looking to dynamically adjust the height of child divs within a fixed-height parent div. The parent div's height is fixed, but the number of child divs can vary. I want each child div to have a height equal to the parent div's height divided ...

What steps do I need to follow to create a 3D shooting game using HTML5 Canvas?

I'm eager to create a 3D shooter game with HTML5 Canvas, focusing solely on shooting mechanics without any movement. Can anyone provide guidance on how to accomplish this? I've looked for tutorials online, but haven't come across any that m ...

Python Selenium: Strategies for Iteratively Waiting for a Clickable Element

My task involves downloading images from website pages using Python Selenium. I need to select the web elements of these images and click on them one by one: ... THUMBNAILS = browser.find_elements_by_xpath("//div[contains(@class, 'lg-thumb-item&a ...

Creating a custom dynamic favicon and title in NextJS

Hello there! I am in the process of creating a web constructor. Currently, my application functions as follows: I verify the URL that the user is visiting (such as localhost:3000) I identify their project name within my web constructor (localhost:3000 -&g ...

Styling the elements that are next to each other using CSS

Hey there, what if I have HTML elements structured like this: <div> <div>Name</div> <div><input type="text" class="check"> <div>Age</div> <div><input type="number" class="check"></div> ...

How can we add a class to a radio button using jQuery when it is checked, and remove the class when it

I'm looking for help with using jQuery to toggle between two radio buttons and display different divs based on the selection. jQuery(document).ready(function($) { if ($('#user_personal').is(':checked')) { $('.user_co ...

How can I showcase a different component within another *ngFor loop?

I am currently working on a table in HTML that displays product information. Here is the code snippet for it: <form [formGroup]="myform" (ngSubmit)="submit()" > <tbody> <tr class="group" *ngFor="let item of products;"&g ...

Ensure the child element is completely visible when it exceeds the size of its parent

I am facing an issue with a footer div that contains various other elements, one of which is an image that exceeds the height of the footer container. The image is not displaying completely as it is being cut off by the parent div (footer). My goal is to ...