Guide to dividing the screen into three horizontal sections

I am currently working on a website and trying to divide a page into three different sections:
one for the title and a button,
one for the content,
one for the text input.

.

The issue I'm facing is that the divs are not filling the height and width of the screen. Additionally, the second div requires a scrollbar due to its variable content length.

I am looking for a solution using CSS, but I am open to any suggestions.

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Server Messaging</title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
  <div style="width:100vh; height: 100vh;">//container for the 3 divs
    <div style="width:100%; height: 15%;">//div1
      <h1>Welcome User</h1>
      <a href = "logout.php"><button class=button>LOG OUT</button></a>
    </div>
    <hr>
    <div style="width:100%; height: 70%; overflow-y: scroll; overflow-x: hidden;">//div2
      //php content
    </div>
    <hr>
    <div style="width:100%; height: 15%;">//div3
      <textarea name="message" rows="3" cols="100"></textarea>
    </div>
  </div>
  </body>
</html>

Answer №1

If you want to adjust container heights based on the viewport height, you can utilize the vh unit in CSS.

body {
  margin: 0;
}
.vh-15 {
  min-height: 15vh;
}
.vh-70 {
  min-height: 70vh;
}

/* for illustration */
.bg-red   { background: red;   }
.bg-green { background: green; }
.bg-blue  { background: blue;  }
div       { color: white;      }
<div class='vh-15 bg-red'>   1: 15% </div>
<div class='vh-70 bg-green'> 2: 70% </div>
<div class='vh-15 bg-blue'>  3: 15% </div>

Answer №2

To tackle this issue, I'd suggest employing flexbox as the solution. The values assigned to the children are interrelated. I've configured mine based on a scale of 100, but keep in mind that these numbers are simply placeholders. For instance, instead of using 70 and 15, you have the option to swap them out for 700 and 150.

.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.top,
.bottom {
  flex: 15;
}

.middle {
  flex: 70;
}

/****************** 
  Presentational
******************/

.middle { background-color: green; }
.middle::after { content: '2: 70%'; }
.top { background-color: red; }
.top::after { content: '1: 15%'; }
.bottom { background-color: blue; }
.bottom::after { content: '3: 15%'; }
.container > div { position: relative; }
body { margin: 0; }
.container > div::after {
  position: absolute;
  top: 50%;
  left: 2rem;
  transform: translateY(-50%);
  color: white;
  display: block;
  font-family: sans-serif;
}
<div class="container">
  <div class="top"></div>
  <div class="middle"></div>
  <div class="bottom"></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

Is it possible to align images vertically with text in a multi-column Bootstrap 4 container?

Inquiring on how to achieve vertical center alignment of text and images for a website section similar to the one shown in this image: https://i.sstatic.net/s8gNh.jpg. The intention is to align the logos and the corresponding text of company names situated ...

Issue with scrolling up on a responsive table when using Bootstrap-select

When I have a bootstrap-select inside a td of a table-responsive table, I encounter an issue. If I click on the select-picker, the dropdown menu is not completely visible as the visibility is limited by the table margin. To address this, I tried adding d ...

Substitute the information in the table with new data

I am working with an HTML table that has 5 columns, one of which contains dates. I need to convert the date format only if the data is not empty. To achieve this, I am utilizing moment.js for date formatting. While the date format conversion works perfect ...

What is the best way to eliminate all borders from a select box?

Is there a way to completely remove all borders from the selectbox using either CSS or JQuery? The code snippet is as follows: <select id="doctor_ch"> <option value="1" selected>One</option> <option value="2">Two</option& ...

Trouble with CSS: Struggling to apply margins to a line of images in React JS

I've encountered an issue where I can't seem to add margin to the row of images. The margin appears fine without any CSS when viewed on a wide screen, but once I scale it down to mobile view, the margin disappears completely. Even after attemptin ...

Is there a way to modify the style value within AngularJS?

<div class="meter"> <span style="width: 13%"></span> </div> I have the following HTML code snippet. I am looking to update the width value using AngularJS. Does anyone have a solution for this issue? ...

When hovering or mousing over, the text remains stationary and does not follow the scroll bar movement within the

A method I am using involves displaying additional data on hover for a shortened header, like this: Hover behavior However, the text shifts across the page when the scrollbar is used, as shown here: Scrollbar interaction This table showcases HTML, CSS, ...

Mastering the art of combining images and text harmoniously

I am having trouble aligning my lion image and h1 tag side by side. There seems to be an issue but I can't figure out what it is. h2 { width:50%; float:right; padding:30px 0px 0px 0px; margin:0 auto; } .lion { width:10%; float: left; paddin ...

Tips for automatically adjusting the height of the footer

Is there a way to dynamically adjust the height of the footer based on the content length inside the body? I've been exploring solutions like setting the position of the footer as "fixed," but it seems to stay at the bottom of the screen, which is no ...

Can an HTML DOM object be converted to a JSON string using JSON.stringify in JavaScript?

Trying to fetch an external HTML file and convert its body content into a string has been giving me unexpected results. Is there a way to achieve this successfully? var xhr = new XMLHttpRequest(); function loadFile(){ xhr.open("GET", 'index.html ...

Selenium is unable to interact with iframes

I'm attempting to extract the Job Description and Job Requirements from this specific link using selenium. Check out my code below: driver = webdriver.Firefox() url = "https://www.jobsbank.gov.sg/ICMSPortal/portlets/JobBankHandler/SearchDetail.do?id= ...

Encountering a NextJS error with head component

Trying to incorporate the head element into a NextJS page format has proven challenging. Despite consulting the official documentation, implementing next/head to add <head> has resulted in an error within the code block. Code: import Head from &apos ...

Ways to create a scrolling effect for a datalist dropdown menu

My HTML datalist shows various career paths, but the number of options is too long. I want to add a scrolling effect to it. I have looked for ways to do this, but I read that CSS cannot be applied to datalists. Is it possible to apply styles using jQuery i ...

Retrieve the content of a specific HTML tag using JavaScript

Is it possible to extract the content of a specific HTML tag as a string or XML format? <body> <script src="jquery.min.js"> //var test = document.getElementsByTagName("input"); //alert(test[0]); $(document).ready(function ( ...

The request to access /test.php has resulted in an error (404) indicating that the file cannot be found

Disclaimer: I am completely new to php. I recently followed a tutorial on creating a php script that captures input from a sign-up page (html) and saves it in a database (mysql/Wamp). However, when I attempted to test the functionality, I encountered the ...

When utilizing the Express framework, the object req.body is initially empty when collecting data from a basic

I'm encountering an issue where I receive an empty object in req.body when submitting my HTML form. This problem persists whether testing in Postman or directly submitting the form from localhost in the browser. Upon logging it in the node console, t ...

Issue with IntersectionObserver not detecting intersection when the root element is specified

I am encountering an issue with my IntersectionObserver that is observing an img. It works perfectly when the root is set to null (viewport). However, as soon as I change the root element to another img, the observer fails to detect the intersection betwee ...

Controller function failing to trigger

I'm new to asking questions, so if I've made a mistake, please let me know. I've searched for an answer here but couldn't find one. Any help would be greatly appreciated. I'm attempting to load a list of "Countries" using a contro ...

Tips for combining multiple fields into a single variable in PHP

When I click the submit button, the image source does not show any result but the image color is displayed. Any help would be greatly appreciated. <div class="form-group"> <input type="file" name="images[source][]" class="form-control input-l ...

button that takes you back to the top of a mobile website

I want to customize the Scroll To Top button so that it only appears once the user begins scrolling down, rather than always being visible even when at the top of the page. Just a heads up, I don't have much experience with JavaScript, so I might need ...