Styling a div element in CSS with absolute positioning and the ability to

I am looking to set specific positions and dimensions for the div elements within my HTML document. Refer to this image for reference: https://i.stack.imgur.com/ANBVm.png

For each of these div elements, I want a vertical scrollbar to appear if the content exceeds the space available. Each div should have its own scrollbar, rather than one for the entire webpage.

The goal is to maintain fixed dimensions and positions for my div elements, with the ability to scroll through their contents when needed.

To achieve this, I have prepared the following CSS:

div {
    position: absolute;
}

div.header {
    background-color: red;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 10%;
}

div.menu {
    background-color: green;
    top: 10%;
    left: 0%;
    width: 20%;
    height: 80%;
}

div.main {
    background-color: blue;
    top: 10%;
    left: 20%;
    width: 80%;
    height: 80%;
}

div.footer {
    background-color: yellow;
    top: 90%;
    left: 0%;
    width: 100%;
    height: 10%;
}

Below is the HTML code that I have used:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Title</title>
        <link rel="stylesheet" href="index.css" />
    </head>
    <body>
        <div class="header"> [content goes here] </div>
        <div class="menu"> [content goes here] </div>
        <div class="main"> [content goes here] </div>
        <div class="footer"> [content goes here] </div>
    </body>
</html>

However, when there is a large amount of text, the expected result is not achieved. Here's how it looks: https://i.stack.imgur.com/KtjLO.png

As you can see, the individual div elements lack the necessary scrollbar for scrolling through the content, making it difficult to read everything present within them. How can I rectify this issue?

Your suggestions and help are highly appreciated. Thank you.

Best regards.


I've attempted implementing a solution provided by Cédric.

CSS:

body {
  height: 100vh;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
}

.container {
  overflow-y: scroll;
}

.content {
  height: 1000px;
}

.header {
  width: 100%;
  height: 10vh;
  background-color: red;
}

.menu {
  width: 20%;
  height: 80vh;
  background-color: green;
}

.main {  
  width: 80%;
  height: 80vh;
  background-color: blue;
}

.footer {
  width: 100%;
  height: 10vh;
  background-color: yellow;
}

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Title</title>
        <link rel="stylesheet" href="index.css" />
    </head>
    <body>
        <div class="container header">
            <div class="content"></div>
        </div>
        <div class="container menu">
            <div class="content"></div>
        </div>
        <div class="container main">
            <div class="content"></div>
        </div>
        <div class="container footer">
            <div class="content"></div>
        </div>
    </body>
</html>

Result:

https://i.stack.imgur.com/M5pec.png

In some cases, certain div elements still lack the scrollbar when the window size is reduced. This issue persists in smaller viewports.

Answer №1

Setting the width and height of elements using percentages or viewport-percentage units is a key aspect in web design.

I made sure to include overflow-y:scroll for the fourth container to enable vertical scrolling when necessary.

body{
  height:100vh;
  margin:0;
  display:flex;
  flex-wrap:wrap;
}
.container{
  overflow-y:scroll;
}
.content{
  height:1000px;
}
.header{
  width:100%;
  height:10vh;
  background-color:red;
}
.menu{
  width:20%;
  height:80vh;
  background-color:green;
}
.main{  
  width:80%;
  height:80vh;
  background-color:blue;
}
.footer{
  width:100%;
  height:10vh;
  background-color:yellow;
}
<div class="container header">
  <div class="content"></div>
</div>
<div class="container menu">
  <div class="content"></div>
</div>
<div class="container main">
  <div class="content"></div>
</div>
<div class="container footer">
  <div class="content"></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

Press one button to activate another button

I am looking to activate a button's class by clicking on another button. The button I have is: <button class="clear-cart banner-btn">clear cart</button> This button is used for clearing a cart. On the order confirmation page, I click on ...

Error encountered at /edit-menu/edit/: The 'Product' object does not contain the attribute 'is_valid'

I am attempting to extract all product names from the Product model, display them along with their prices on the screen, and enable users to modify the price of any item. Although I have managed to list them out and provide an input field for the price, I ...

Replace the content of the HTML tag in the index.html file with a different HTML file located at a specific URL in an Angular application

Currently utilizing angular universal. The primary URL being: Upon opening this URL, the function res.render('index') is triggered, leading to the rendering of the index.html file, as depicted in the following code snippet. app.get('*' ...

How can I ensure that the search form stays aligned with the other elements in the navbar using Bootstrap CSS?

I am new to CSS and I am facing an issue with my search form in the navbar. When I initially load the page, the search form appears on 2 lines but when I refresh the page, it aligns properly with the other elements. How can I ensure that the search form st ...

Can you explain the distinction between sockets and proxy passing in nginx compared to uwsgi?

My latest project setup involves flask, uwsgi, and nginx. The backend solely serves json data through an API, while the client side takes care of rendering. Typically, my Nginx configuration looks like this: My usual setup (with basic proxy passing): se ...

What is the best method to ensure that an image remains at the bottom of a div even as the div's height adjusts?

In my current project, I am facing a challenge with positioning an image in the bottom-right corner of a dynamically changing div. Initially, this is easily achieved by using CSS selectors: #div1 { position: relative; } #div1 img { position: abso ...

Add drop caps to every individual word within a hyperlink

Is there a way to recreate the menu effect similar to using CSS fonts and drop caps for each word? I'm aware of the CSS code: p.introduction:first-letter { font-size : 300%; } that enlarges the first character of the first word, but I want this ef ...

utilize dynamic variable within the template's views

Here is a snippet of my HTML code var marker; function initMap() { map = new google.maps.Map(document.getElementById("mymap"), myOptions); getMapMetadata([]); // setInterval(function(){ getMapMetadata([]); }, 3000); } function createMarke ...

Create Stylish Popups and Spacious Containers with CSS

A unique pure css popup was crafted by merging and . Hovering over "John Smith" triggers the appearance of the popup. The code for this can be seen at http://codepen.io/kikibres/pen/MwEgQX. However, there are some challenges being faced. The aim is to ke ...

Troubleshooting the non-functioning addEventListener in JavaScript

I am facing an issue where a function that should be triggered by a click event is not working and the console.log message does not appear <script src="e-com.js" async></script> This is how I included the javascript file in the head ...

Tips for repairing a side bar and header with CSS when using a JS & jQuery scroller

I am working on a layout design and my goal is to have the left sidebar, right sidebar, and header remain fixed in place. Here is the CSS code I am using : #container { padding-left: 200px; /* Left Sidebar fullwidth */ padding-ri ...

What is the best way to eliminate a CSS style from a div?

I have implemented jQuery Autosize to automatically adjust the height of textarea elements. It works perfectly when I focus on the textarea element. However, when I blur out of the textarea, I want to reset the height to its default value. I am unsure of ...

Modify the text inside a div based on the navigation of another div

Hey there, experts! I must confess that I am an absolute beginner when it comes to JavaScript, JQuery, AJAX, and all the technical jargon. Despite my best efforts, I'm struggling to grasp the information I've found so far. What I really need is ...

Guide to creating a hierarchical navigation system with HTML

Is there a way to create a menu tree using just HTML and some scripting, without downloading additional software? I tried searching on Google but all the results require downloads. Can anyone provide guidance or help with this task? Thank you in advance. ...

In the absence of a value

In my code, I've implemented a functionality that saves the user's input into local storage and displays it in a specific ID. However, I want to make sure that if the input field is left empty, the user is prompted to enter their name. function ...

What is the best way to use CSS to ensure that dynamic, data-driven characters can be properly displayed within a div

I'm in the process of updating a data-centric website that relies on information from an automated database engine. In the HTML, there's a fixed-size button containing text pulled from the database. I'm looking to incorporate some CSS styles ...

What are some effective strategies for incorporating multiple Themes into an AngularJS project?

Currently, I am in the process of working on a project that involves utilizing AngularJS, UI Bootstrap, and Sass. The next step is to incorporate different themes that can be selected by the user. While I have successfully implemented the ability to apply ...

Customizing Navigation Color on Laravel Websites for Staging and Live Servers

I am seeking a solution to change the color of the Laravel navigation bar depending on whether it is the live or staging server. Our current setup involves testing code on a staging server before pushing it to the live server in production via Bitbucket. ...

Expanding image in card to fit device width using Ionic

Currently utilizing the ionic framework and attempting to expand the image within the card to fit the device width. Referencing the example provided here, here is my current code progress. <div class="list card"> <div class="item item-avatar" ...

The website's content is displayed as a mobile-friendly menu

I've been working on creating a responsive menu using HTML and CSS, and everything is functioning well so far. However, I'm facing an issue with the mobile view of the menu. When the menu opens up, it obstructs the text below it, making it diffic ...