Two-sided vertical CSS menu

I'm struggling to design a layout with two vertical menus flanking the main content section. Despite experimenting with different combinations of inline, relative, and fixed positions, I can't seem to make it work.

Here is the Fiddle link for reference: https://jsfiddle.net/g4vbampm/3/

The red and blue menu blocks should be positioned directly beside the green central section (setting position:fixed placed them at the left edge of the screen). They should remain fixed at the top of the screen and not move. The height of the green central section will be dynamic and change based on JavaScript code.

.app {
  text-align: center;
}
.left {
  background: red;
  width: 150px;
  height: 300px;
  display: inline-block;
}
.center {
  background: green;
  width: 300px;
  height: auto;
  display: inline-block;
}
.right {
  background: blue;
  width: 150px;
  height: 300px;
  display: inline-block;
}
<div class="app">

  <div class="left">
    menu1
    <br/>menu2
    <br/>menu3
    <br/>
  </div>

  <div class="center">
    CONTENT
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a

    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>a
    <br/>
  </div>

  <div class="right">
    menu1
    <br/>menu2
    <br/>menu3
    <br/>
  </div>


</div>

Answer №1

The red and blue menu blocks should be positioned right next to the central green section with a fixed position that keeps them at the edge of the screen on the left side. Their placement should remain static at the top of the screen without any movement.

If you want the menus to stay in place, you should use position:fixed (unless you prefer to constantly adjust their position using JavaScript, which can negatively impact performance and appearance.)

Fixed positioning always refers to the viewport as the reference point. However, you can still make it work dynamically by calculating the position.

Since your central element is 300px wide, the other two elements should be positioned at 50% + 150px from the right and left sides respectively.

.left,
.right {
  position: fixed;
  top: 0;
}
.left {
  background: red;
  width: 150px;
  height: 300px;
  right: calc(50% + 150px);
}
.right {
  background: blue; 
  width: 150px;
  height: 300px;
  left: calc(50% + 150px);
}

https://jsfiddle.net/g4vbampm/4/

Calc has good browser support, http://caniuse.com/#search=calc

If you need it to work in browsers that don’t support calc, you can also use a negative margin to offset the elements from the middle,

.left {
  right: 50%;
  margin-right: 150px;
}

https://jsfiddle.net/g4vbampm/7/

Answer №2

Below is a solution that should meet your needs:

.application{
        text-align:center;
    }

    .left-side{
      position:fixed;
      background:red;
      width:150px;
      height:300px;
      left:0;
      float: left;
    }

    .center-content{
      position: relative;
      background:green;
      width:300px;
      height:auto;
      left: 140px;
      float: left;
    }

    .right-side{
      position: fixed;
      background:blue; 
      width:150px;
      height:300px;
      left: 445px;
      float: left;
    }

https://jsfiddle.net/9wnwL7rp/

Answer №3

For a fixed position setup for left and right divs that remain in place while scrolling, follow these steps.

* {
    margin: 0;
  }

.left,
.right {
  position: fixed;
  top: 0;
  width: 150px;
  height: 300px
}

.left {
  left: 0;
  background: red;
}

.right {
  right: 0;
  background: blue;
}

.center {
  width: calc(100% - 300px);
  margin: 0 auto;
  background: green;
}
<div class="app">
  <div class="left">
    content
  </div>

  <div class="center">
    content
    content
    contentcontent
    <br><br><br><br><br><br><br><br><br><br>
    contetn
  </div>

  <div class="right">
    content
  </div>
</div>

It is important to note that using *{ margin: 0 } is advisable only if you have not reset your HTML margins previously.

Answer №4

Give It a Shot

.app{
    text-align:center;
}

.left{
  background:red;
  width:150px;
  height:300px;
  position:fixed;
  float:left;
  left:0px;
}

.center{
  background:green;
  width:calc(100% - 300px);
  width:-webkit-calc(100% - 300px);
  width:-moz-calc(100% - 300px);
  height:auto;
  float:left;
  margin-left:150px;
 
}

.right{
  background:blue; 
  width:150px;
  height:300px;
  position:fixed;
 float:left;
 right:0px;
}
<div class="app">

<div class="left">
menu1<br/>
menu2<br/>
menu3<br/>
</div>

<div class="center">
CONTENT<br/>
a<br/>
a<br/>
a<br/>
a<br/>
a<br/>a<br/>
a<br/>
a<br/>
a<br/>
a<br/>

a<br/>
a<br/>
a<br/>
a<br/>
a<br/>a<br/>
a<br/>
a<br/>
a<br/>
a<br/>
a<br/>
a<br/>
a<br/>
</div>

<div class="right">
menu1<br/>
menu2<br/>
menu3<br/>
</div>


</div>

Answer №5

Consider incorporating the css property float to achieve the desired layout. http://www.w3schools.com/css/css_float.asp. It is essential to divide your main container into three columns. For example, assign the .left class a width of 33.3% and continue in a similar manner. If you are looking for a reliable HTML framework to establish a grid system, check out http://getbootstrap.com/.grid system

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

Exploring the power of $j in JavaScript

Could someone please explain what the $J signifies in this snippet of javascript code? if ($J('.searchView.federatedView.hidden').attr('style') === 'display: block;' || $J('.searchView.federatedView.shown').length ...

Maintain the selected bootstrap tab even after the page is refreshed, even when the content is loaded dynamically

I am currently facing an issue with loading tabs using Angular. Whenever a tab is clicked, the id is saved to localStorage. Now, I want to programmatically click on the same tab using jQuery when the page refreshes. However, since the DOM element for the ...

The grid items fail to refresh even after I have made changes to the useState in my React Native application

I am currently working on a react native project that includes a grid component. My goal is to update an array in the state called selectedHomeTypes whenever a user clicks on an item within the grid. Initially, the array is set to contain only one element: ...

Using NodeJS to assign key-value pairs to a JSON object

I am currently working with a NodeJS script that receives data in the form of "key, value" pairs and I need to transform this data into a JSON object. The data is obtained using SNMP where the key corresponds to the OID. My goal is to efficiently map thes ...

Can an internal/private function call a public function?

Just wondering if I'm missing something here, as I tried the following: (function() { var thing = function() { var doIt = function() { console.log("just do it"); this.updateValue(5); }; return { ...

Having difficulty retaining the value of a variable following the retrieval of JSON data

For my current project, I am utilizing the highstocks charting library in combination with JQuery. My goal is to create a single chart divided into three sections, each displaying different data sets. To import the data, I have referenced the sample code p ...

Display the results of the constantly changing JSON response

I am expecting to receive a dynamic JSON response based on the provided input. Here is an example: { results: [ { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="087c6d7b7c487c6d7b7c266b6765">[email protec ...

Combining DataTables with multiple tables sourced from various JSON arrays

I am trying to display data from two different arrays within the same JSON source in two separate tables, but my code seems to be malfunctioning. JSON Information: { "Policies": [ { "name": "A", "id": "1", "score": "0" } ], ...

Display everything when an option is clicked

I have a filter that is working perfectly. When I select a specific category, it filters out only rows with that category. However, I am stuck on how to display all rows again after clicking on the first option. My objective is to show all rows when "Categ ...

sort the array based on its data type

Recently diving into typescript... I have an array that is a union of typeA[] | typeB[] but I am looking to filter based on the object's type interface TypeA { attribute1: string attribute2: string } interface TypeB { attribute3: string attri ...

variables lacking the intended values

Planning on creating a real-time weather application using jQuery, I have encountered a slight issue. Within my jQuery code, I have defined two variables named 'latitude' and 'longitude' to hold the current location coordinates. Upon po ...

Encountering a problem while submitting the form - there appears to be an unexpected "s" token in the

This is my first attempt at creating a contact form using Node.js with Nodemailer. The goal is to have the form submitted through the website and sent directly to your inbox. Here is a snippet of my app.js file in the public folder: const form = document. ...

What is the best way to determine the position of the currently chosen selection in relation to the top

Let's say we have a situation like this: <select id="my-select"> <option id="iun">Option 1</option> <option id="sdd">Option 2</option> <option id="ert" select="selected">Option 3</option> <option i ...

Align navigation items in the center of the navigation container

I've been grappling with aligning the content of a nav-item inside a nav container in my project. I'm unable to apply a margin on the ul>li items as it doesn't adjust properly on different resolutions. I've tried using percentages, b ...

There seems to be an issue with FastAPI not sending back cookies to the React

Why isn't FastAPI sending the cookie to my React frontend app? Take a look at my code snippet: @router.post("/login") def user_login(response: Response, username :str = Form(), password :str = Form(), db: Session = Depends(get_db)): use ...

Sharing State with a Secure Route in Vue Router (using the script setup method)

Hello everyone, I'm encountering an issue while trying to send a state to the protected routes in vue-router. The error that I faced mentioned "Discarded invalid param(s) "_id", "dish_name", "description", "img" ...

Is it possible for me to pass a reference to a specific object's instance function?

Can JavaScript allow you to pass a function reference to a specific object's function, similar to what can be done in Java? Let's take a look at this code snippet: _.every(aS, function (value) { return exp.test(value); }); What if we want ...

Looking to obtain rendered tree data upon page load?

Is there a way to retrieve all rendered tree metadata when the page loads using jstree? I'm looking for an output similar to this: [23,24] Please note that I would like to have each id stored in the metadata object displayed as [23,24] upon page loa ...

Organizing and recycling React styled-components and native elements

As a backend engineer transitioning to frontend development, I am currently learning React and exploring styled-components for styling components. However, I am facing challenges in using styled-components with native React components and updating them in ...

Discover Vue3's efficient event handling feature that allows you to easily listen to events from dynamically generated child components

I am dynamically creating a Vue component and need to listen to the event it emits. While I know that you can use @eventName in the markup, my component is being created using createApp. const div = document.createElement('div'); this.$refs.logi ...