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

Ensuring Proper Tabulator Width Adjustment Across All Browser Zoom Levels

<div id="wormGearTabulatorTable" style="max-height: 100%; max-width: 100%; position: relative;" class="tabulator" role="grid" tabulator-layout="fitDataTable"><div class="tabulator-header" role="rowgroup"><div class="tabulator-header-co ...

Set the background color of a webpage depending on the data retrieved from the database when a specific radio button

I am facing a challenge with my radio buttons that are set to light up green when the page loads. However, I am working on an "order system" where I need a specific button or "locker" to appear red instead of green when the page loads. While I have succes ...

Looking to align a radio button in the middle of an inline-block?

Is it possible to center a radio button within an inline-block? I have provided a demo on JSFiddle that displays the current layout and how I envision it should appear. Check out the demo here Here is the code snippet in question: <span style="widt ...

Guide to verify the user selection within a select form

Here is a form with a select field: <form method="post" name="posting_txt" onSubmit="return blank_post_check();" id="post_txt"> <select style="background: transparent; border-bottom:5px;" name="subname" class="required"> ...

What techniques can I implement to optimize the speed of this feature in JavaScript?

I have developed a feature that highlights any text within a <p> tag in red based on a user-specified keyword. The current implementation works well, but it is slow when dealing with over 1000 lines of <p>. Is there a faster way to achieve this ...

HTML or JS/jQuery can create disorienting cursor behaviors

Is there a way to create a distorted or crooked mouse movement on a webpage, even though the user is moving the mouse normally? I'm exploring ways to simulate the experience of a Parkinson's or arthritic patient trying to navigate a web page wit ...

Issue with styled-components not being exported

Issue: ./src/card.js There was an import error: 'Bottom' is not exported from './styles/cards.style'. card.js import React from 'react' import { Bottom, Color, Text, Image } from "./styles/cards.style"; fu ...

Embedding a CSS file into a PHP class

I've encountered a little issue that I can't seem to solve on my own. I've put together a "webengine" using multiple classes. The primary class responsible for obtaining a theme's internals is called "Logic". This Logic class contain ...

The jQuery menu is malfunctioning in Internet Explorer due to the Document Mode being set to Quirks

I am encountering an issue where the below code is not functioning properly in Internet Explorer's document mode quirks. Each time I hover over the submenu, its length doubles unexpectedly. Can someone please provide assistance with this matter? < ...

What is the best way to establish a minimum width in pixels and a maximum width determined by the variable width of the browser window?

In this scenario: http://jsbin.com/anoyik/edit#preview http://jsbin.com/anoyik/edit#source Is there a way to make the red box's width a minimum of 200px and allow it to expand as the browser window's width increases horizontally? ...

Unable to sort the list items when utilizing the load() function

I have multiple li elements within a ul and I am using the following code to sort them in ascending order based on the data-percentage attribute: $(function() { $(".alll li").sort(sort_li).appendTo('.alll'); function sort_li(a, b) { re ...

Implementing jQuery during the navigation between Node routes

Below is a snippet of my jQuery code: $(function () { $('.mnav a').click(function () { el = $('.border'); el.addClass('blink'); el.one('webkitAnimationEnd oanimationend msAnimationEnd animatio ...

Center Button Horizontally and Vertically with CSS Code

I really need some assistance with getting this button I made to be perfectly centered on both the vertical and horizontal axes of the page. So far, I've managed to center it horizontally, but not vertically. Any advice or guidance would be greatly ap ...

What is the best way to implement validation in a textarea to restrict the word count to a minimum of 250 and a maximum

I want to implement jQuery validation for a textarea field, with a requirement of 250 minimum words and 1000 maximum words. Additionally, I need to display the word count in a span tag next to the text area. How can I ensure that this validation works even ...

WP How can I modify a particular thumbnail size with a custom CSS class?

I have been attempting to use add_image_size for my custom thumbnail resize, but it seems to be having trouble maintaining the thumbnail size at 220 x 220. Instead, it keeps changing the height to 167 pixels. As a workaround, I am exploring a CSS-based sol ...

Guide on transferring control from a successful jQuery event to an HTML form

I am currently using the following jQuery code to validate user details. $.ajax({ type: "POST", url: "Login", data:'uname='+encodeURIComponent(uname)+'&'+'pass='+encodeURIComponent(pass), ...

Creating a dynamic form in Angular based on user input

I am in the process of creating a dynamic web form where the user's input will determine the subsequent form inputs that are generated. For example: <mat-form-field> <mat-select placeholder="Type" [(ngModel)]="row.Type" (change)="Typ ...

Setting the position of a tooltip relative to an element using CSS/JS

I'm struggling to make something work and would appreciate your help. I have a nested list of items that includes simple hrefs as well as links that should trigger a copy-to-clipboard function and display a success message in a span element afterwards ...

Combine the content from multiple text areas and submit it to another text area

****UPDATE**** Thanks to @JasonB, I was able to resolve the previous issue mentioned. Now, I have three additional textareas on the same form that should only appear when their respective checkboxes are clicked. How can I integrate this into my current sc ...

Enhancing list item appearance by replacing bullets with Font Awesome icons

I need to create a list similar to this example Although I successfully replaced the bullet with a fontawesome icon, the result is not quite right as shown here You may notice a difference in spacing between the two. Here is my CSS code: .custom_list l ...