Position the div at the bottom of the screen rather than at the bottom of its parent element

One of my HTML structures imitates an app layout with pages.

To understand the issue, please take a look at the JsFiddle link first.

.screen-emulator {
  width: 300px;
  height: 400px;
  background-color: red;
}

.head {
  width: 100%;
  height: 70px;
  background-color: blue;
}

.body {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}

.page {
  position: relative;
  width: 100%;
  height: 500px;
  background-color: black;
}

.page-test {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: violet;
}
<div class="screen-emulator">
  <div class="head">Head</div>
  <div class="body">
    <div class="page">
      <div class="page-test">page-test</div>
    </div>
  </div>
</div>

I am wondering if there is a CSS way to extend .page-test all the way to the end of the screen rather than its parent element (.page)?

The issue lies in the height: 100%; property of .page-test, causing it to reach the bottom of .page. I want to highlight that I cannot alter the HTML structure (even though it may fix the problem) and I do not know the height of .head in advance.

Answer №1

Using flexbox could be the solution to the problem, if I have correctly understood the question at hand.

To start, ensure that you explicitly set the margin and padding of all elements to zero, a practice sometimes referred to as bootstrapping. This will override any default styles applied by the browser.

In order to achieve the desired layout, set the height of .screen-emulator to 100vh and designate it as the flex container. To maintain the unknown height of .head, use flex: none;. The .body should be set as the sole flex item occupying the remaining available height with flex: 1;. https://css-tricks.com/boxes-fill-height-dont-squish/

body,
.screen-emulator,
.head,
.body,
.page,
.page-test {
  margin: 0;
  padding: 0;
}

.screen-emulator {
  width: 300px;
  height: 100vh;
  background-color: red;
  display: flex;
  flex-direction: column;
}

.head {
  width: 100%;
  background-color: blue;
  flex: none;
}

.body {
  width: 100%;
  flex: 1;
  overflow-y: auto;
}

.page {
  position: relative;
  width: 100%;
  min-height: 100%;
  background-color: black;
}

.page-test {
  position: absolute;
  width: 100%;
  min-height: 100%;
  background-color: violet;
}
<div class="screen-emulator">

  <div class="head">
    <h1>Head</h1>
    <p>Random content</p>
  </div>
  <div class="body">
    <div class="page">

      <div class="page-test">
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Senectus et netus et malesuada fames ac turpis egestas. Massa tincidunt dui ut ornare lectus sit amet. Posuere ac ut consequat
          semper viverra nam libero justo laoreet. Sit amet commodo nulla facilisi nullam vehicula ipsum a arcu. Phasellus vestibulum lorem sed risus. Congue eu consequat ac felis donec et odio pellentesque diam. Consectetur lorem donec massa sapien faucibus

        </p>
        <p>
          Nisl suscipit adipiscing bibendum est ultricies integer quis. Pellentesque diam volutpat commodo sed egestas egestas fringilla phasellus faucibus. Netus et malesuada fames ac turpis egestas. Ultricies mi quis hendrerit dolor magna eget. Tellus mauris
          a diam maecenas sed enim. Turpis massa tincidunt dui ut ornare lectus sit. Dolor magna eget est lorem ipsum. Sit amet consectetur adipiscing elit ut. Id donec ultrices tincidunt arcu non sodales. Interdum consectetur libero id faucibus nisl

        </p>

      </div>

    </div>
  </div>


</div>

Answer №2

.page-test {position: absolute;bottom: 0;width: 100%; height: 50%;background-color:violet;}

Does this meet your requirements? I adjusted the height for optimal visibility on JsFiddle

Answer №3

Are you referring to this concept?

You can make calculations based on the viewport height using vh. Please keep in mind that older browsers do not support the calc function.

Upgraded to latest specifications

Given the requirement is to adapt to the dynamic height of the .head element, utilizing JavaScript is necessary. CSS does not facilitate retrieving values from the DOM and applying them, which is where JavaScript comes in handy.

document.addEventListener('DOMContentLoaded', function() {
  resizePage();
}, false);

document.addEventListener("resize", resizePage);

function resizePage() {
  var head = document.querySelector(".head");
  var pageTest = document.querySelector(".page-test");

  pageTest.style.height = "calc(100vh - " + head.offsetHeight + "px)";
}
.screen-emulator {
  width: 300px;
  height: 400px;
  background-color: red;
}

.head {
  width: 100%;
  height: 70px;
  background-color: blue;
}

.body {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}

.page {
  position: relative;
  width: 100%;
  height: 500px;
  background-color: black;
}

.page-test {
  position: absolute;
  width: 100%;
  height: 100vh;
  background-color: violet;
}
<div class="screen-emulator">
  <div class="head">Head</div>
  <div class="body">
    <div class="page">
      <div class="page-test">page-test</div>
    </div>
  </div>
</div>

Answer №4

To ensure that the height of the .page-test class matches that of the screen, you can set either the min-height or height to 100vh. This approach disregards the parent div's height and instead takes up the entire screen space (while potentially causing overflows).

Remember to make the following adjustments in your css:

.page-test {
  position: absolute;
  width: 100%;
  /* min-height: 100vh; */
  height: 100vh;
  background-color: violet;
}

Answer №5

If I understand correctly, you are looking to rearrange the order of your div elements. Recently, I used Bootstrap to achieve a similar layout adjustment for mobile view:

@media only screen and (max-width: 960px) {
  .col-xs-12 {
    display: flex;
    flex-direction: column-reverse;
  }
}

<div class="row">
  <div class="col-xs-12">
    <div>This content is shown at the top in desktop view and at the bottom on mobile devices.</div>
    <div>This content appears at the bottom in desktop view and at the top on mobile devices.</div>
  </div>
</div>

You can adapt this code to fit your specific needs. Does this help address your question?

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

The ID becomes corrupted when there is duplicate information in MYSQL

Hey there! I've set up a database for a school project competition where people can join, and I've implemented a feature to prevent duplicate email addresses. However, even if there is a duplicate, the ID will still increment. For example, if som ...

I am unable to connect my jQuery

I've searched far and wide on numerous coding forums for solutions to this issue. It's usually just minor syntax mistakes like forgetting a closing tag or improper src attribute formatting. Despite double-checking, my html and js seem to be erro ...

Creating a Popup with a Hazy Background: A Step-by-Step Guide

I've been working on implementing a popup on my existing page that opens 4.5 seconds after the page loads. I want to add a blur effect to the background when the popup appears. I've tried various methods to add the blur effect to the main div, bu ...

Encountering issues when attempting to retrieve localized images within .vue files using a custom webpack configuration

Currently, I am deep into learning webpack and attempting to craft my own vue template. However, I have encountered an issue that does not arise in webpack-simple or other templates. Despite having a configuration for images that closely resembles what is ...

Problem with cloning I-text in FabricJS

After selecting an object in i-text and cloning it, when you double click on the cloned object to start editing, unselecting it may not work as expected. This issue can be observed in the kitchensink example provided at: To replicate this behavior, navig ...

Choose certain table cells that do not have an identifier or any classes assigned to them

My table has 3 rows and 3 data cells. To select the first row, first data cell, and all bold tags from it: tr + td b {} To select the second row, second data cell, and all bold tags from it: tr+tr > td+td b {} I need help selecting them without usi ...

JQuery and CSS can be used to create a div overlay without impacting the positions of other divs on the

Is there a way to make a div overlay other divs (like a dropdown list) without affecting the positions of other divs? I have a #hidden div that appears when you hover over a #HoverMe div, but it moves the position of other divs when hovered. How can I ...

Saving a user with a BLOB avatar in Angular 5: Tips and Tricks for Success

As a newcomer to Angular, I am trying to figure out how to properly save a new user with an avatar. Can someone help me understand how to pass the Blob value of the avatar to my user Model for successful saving? Below is the code snippet I am working with ...

What is the process for attaching an on-click event listener by utilizing an argument that is passed to a function in JavaScript?

These are the functions I have created: function type(message) { document.getElementById("body").innerHTML="<div id=\"textbox\" class=\"textbox\"></div><div id=\"oc\&q ...

When the window size is reduced (smaller than a page width), the layout of the page becomes distorted

Check out my cool header: <head> <link href="/css/header.css" rel="stylesheet" type="text/css"> </head> <body> <div id="background"><img src="/multi/background.jpg" width="100%" height="100%" alt=""&g ...

Tips for saving information from a textarea into an HTML file

I have a unique question regarding the usage of $fopen and $fwrite functions. My goal is to include a button named "save as HTML" below a textarea. Upon clicking this button, I want a popup box to appear mimicking the 'save as...' dialog window ...

PHP script for converting HTML lists to plain text

Looking to convert HTML ordered/unordered lists into plain text for export to a txt file? Here's an example: Original HTML: <ol><li>Item 1</li></li>Item 2</li><li>Item 3</li></ol> Desired format: 1. ...

Using static typing in Visual Studio for Angular HTML

Is there a tool that can validate HTML and provide intellisense similar to TypeScript? I'm looking for something that can detect errors when using non-existent directives or undeclared scope properties, similar to how TypeScript handles compilation er ...

Enhance the look of your application by customizing the caret color in JavaFX with

I'm looking to customize the caret color for all JavaFX text inputs (such as TextField, TextArea, ComboBox:editable, DatePicker, etc.). I came across a solution on Stackoverflow: How to change the caret color in JavaFX 2.0? There's also an examp ...

Learn the trick to make this floating icon descend gracefully and stick around!

I'm trying to create a scrolling effect for icons on my website where they stay fixed after scrolling down a certain number of pixels. I've managed to make the header fixed after scrolling, but I'm unsure how to achieve this specific effect. ...

Alter Elements in AngularJS Based on Their Relationship to Other Elements

Creating diagonal lines that connect squares in the corner using HTML/CSS is my current project. I am currently accomplishing this by transforming divs with a custom Angular directive, but I'm wondering if there's a simpler way to achieve this wi ...

The spinning icon in the button is activated when the list-group feature of Bootstrap 4.x is

My goal is to have an icon spin inside a button in the bootstrap list group item only when it is active. However, I am more confident with CSS and HTML than JavaScript. Adding "fa-spin" to the class makes the icon spin constantly, but my desire is for it t ...

Tips for incorporating image and text sections into a responsive layout

I have implemented the following CSS to organize 8 sections of text and images. The issue is with fixing the size of the boxes, as they currently adjust based on their content length. This means that a box with shorter content will appear smaller than a bo ...

bourbon neat quadrilateral layout

Attempting to revamp someone else's messy, yet effective, CSS3 template into a more organized format. The transition to bourbon/neat has been smooth, but I'm facing a roadblock when it comes to creating a responsive image grid. <section class ...

issue when Internet Explorer is formatting text following an image

My website functions perfectly in Chrome and Firefox, but it's not rendering correctly in IE. I've fixed most of the bugs, but one issue remains unresolved. Currently, the image is displayed with the text bunched up next to it, while the table ap ...