Attach the element to the bottom of the viewport without obstructing the rest of the page

My challenge is to create a button that sticks to the bottom of the viewport and is wider than its parent element. This image illustrates what I am trying to achieve:

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

The issue arises when the viewport height is shorter, as the button ends up covering the content. Here's an example of the problem:

https://i.stack.imgur.com/5dU4V.png

I want the button to remain at the bottom of the content when the viewport is less than the combined width of the button and the content. If I remove the bottom:0 property from the button, it resolves the issue but then the button doesn't stick to the bottom of the viewport for larger viewports:

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

To ensure the button spans the entire width of both the viewport and its parent element, I have positioned the button absolutely and left the parent element unpositioned. This also prevents me from using flexbox to solve the problem.

Is there a CSS-only solution to achieving this layout, or will I need to resort to using JavaScript?


body {
    box-sizing: border-box;
    height: 100%;
    margin: 0;
    padding: 0;
}
.child {
    background: lightblue;
    text-align: center;
}
.parent {
    overflow: scroll;
    border: 1px solid red;
    height: 100%;
    margin: 0 30px;
}
button {
    border: 1px solid green;
    background: blue;
    color: white;
    width: 100%;
    height: 50px;
    position: absolute; 
    left: 0;
    bottom: 0;
}

Here is the structure of the parent and child elements in the HTML:


<div class="parent">
    <div class="child">
        <!-- Content here -->
    </div>
    <button>button</button>
</div>

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

Answer №1

To address your issue effectively, it seems that utilizing JavaScript is necessary to accurately assess the viewport height versus the child height, enabling you to affix the button at the bottom correctly.

Here is a potential solution:

const windowHeight = window.innerHeight;
const child = document.querySelector('.child');
const button = document.querySelector('button');

function calculateHeight() {
  const childHeight = child && child.offsetHeight;
  // check if total of child's height + button height is less than the viewport height, then fix the button to the bottom
  button.classList.toggle('stick-to-bottom', (childHeight + button.offsetHeight) <= windowHeight);
}


// This section below is for demonstration purposes only, not directly related to the solution
let isOn = true;
button.addEventListener('click', function() {
  let content = 'bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />';
  if (isOn) {
     content = 'bla<br />bla<br />bla<br />bla<br />bla';
  }
  
  child.innerHTML = content;
  isOn = !isOn;
  
  calculateHeight();
});
body {
        box-sizing: border-box;
        height: 100%;
        margin: 0;
        padding: 0;
      }
      .child {
        background: lightblue;
        text-align: center;
      }
      .parent {
        overflow: scroll;
        border: 1px solid red;
        height: 100%;
        margin: 0 30px;
      }
      button {
        border: 1px solid green;
        background: blue;
        color: white;
        width: 100%;
        height: 50px;
        left: 0;
        position: absolute;
      }
      button.stick-to-bottom {
        position: fixed;
        bottom: 0;        
      }
<div class="parent">
      <div class="child">
        bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />
        bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />bla<br />
      </div>
      <button>toggle content</button>
    </div>

Answer №2

If you want to utilize the flex layout mode, there are a few adjustments you can make.

body {
  box-sizing: border-box;
  height: 100%;
  margin: 0;
  padding: 0;
}

.child {
  background: lightblue;
  text-align: center;
  margin: 0 30px;
  border: 1px solid red;
  flex: 1;
}

.parent {
  overflow: scroll;
  height: 100%;
  display: flex;
  flex-flow: column nowrap;
}

button {
  border: 1px solid green;
  background: blue;
  color: white;
  width: 100%;
  height: 50px;
}
<div class="parent">
  <div class="child>
    Updated content goes here...
  </div>
  <button>button</button>
</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 include all visible content, even when scrolling, within the full page height?

My webpage contains content that exceeds the height of the window. I am looking for a way to retrieve the full height of my page using either jQuery or pure Javascript. Can anyone provide a solution? I have considered the following approach: $('body ...

Having trouble accessing the value of an object within a nested array object

Looking for a way to extract the object value from a nested array object using JavaScript? If the sourcecountry matches the country in the object, it should return the corresponding payment service. Here is what I have attempted: function getValue(source ...

Using Rails and Haml to Implement Smooth Scrolling with Jquery and CoffeeScript

Looking to accomplish a straightforward task using CoffeeScript, Rails, and Haml. While I don't have much experience with CoffeeScript, I'm eager to experiment. The goal is to have the view scroll to a specific div id when a button is pressed. A ...

Create a Jest unit test for a specific function

I started my first unit test in react with jest this afternoon. The initial 5 tests I need to do involve testing the return functions, which isn't too difficult. However, I'm struggling to understand how to perform unit testing on my login funct ...

Flexbox Resizing Notification(?)

Recently, I've been utilizing flexbox in my layout design and it has been a game-changer. However, I am facing an issue that might be linked to my heavy use of AngularJS, particularly the ng-include feature. The specific problem arises when I incorpo ...

Tabulator and its convenient scrollable column feature allows for easy navigation

In case my tabulator column is exceeding its length, is there a way to enable scroll functionality for that specific column? Although the resizable rows feature permits users to resize and view all the content, can a scrollbar be implemented exclusively ...

Php Error 404 occurs upon attempting to redirect from the main landing page

I have been encountering an issue while trying to add links to different pages of my website. Whenever I click on the link, it displays a "404 Page Not Found" error message. Objective: My main goal is to create links from my home page, index.php, to other ...

Does the modularization of code impact the performance of the react-app?

The client provided me with a React app that includes an index.jsx file where 90% of the coding has been completed. To prevent getting stuck in Scroll Limbo, I have started breaking down the code into modules for each specific requirement. These include ...

Regular expression for matching a CSS definition that includes a specific name and value

Looking to match all CSS definitions with the name "test" and color set to gray? Here are a few examples: a.test { color: grey; } or .test { color: grey; }, as well as a.test:after, a.test { font-size: 10px; color: gray; } It's important to consider ...

The error message "element is not defined" is indicating an issue related to the cordova-plugin-google

In my current project using Ionic 3, I decided to implement map-related features by incorporating the Google Maps plugin recommended by the Ionic Team. This specific plugin serves as a wrapper around cordova-plugin-googlemaps. Following the steps outlined ...

Using bluebird library for revoking promises

Recently, I've been diving into the bluebird promises library. To practice using it, I set up a basic express app with just one file and one route - a GET request to /test. The scenario I'm working on involves a promise with an interval that res ...

Is there a similar effect in jQuery? This is achieved using prototype

Simply click on the comments link. Observe how the comments smoothly appear, as if sliding down. Also, try clicking on 'hide' and see what happens. ...

Is there a way to access the badge hidden behind the collapsible menu in bootstrap 4?

After moving from bootstrap 3 to bootstrap 4, my items no longer align properly. I've scoured the entire Internet for a solution, but I've run out of options (and patience.. haha) This is how it currently looks: I want the badge to be positione ...

What is the best method for combining numerous tiles within a level in Kaboom JS?

Creating a level in kaboomJS with a extensive tile map collisions can sometimes result in slower performance. I'm exploring options to optimize this process, such as potentially merging multiple tiles together so that a whole row of blocks could funct ...

What are some creative ways to visually distinguish a TextField that is in readOnly mode?

I'm currently working on creating a form using the Material-UI library. I'm having difficulty figuring out how to distinguish my TextField when they are in readOnly mode versus edit mode. At the moment, they appear identical and I would like the ...

Unlocking the potential of the :not selector when combined with the :nth-of-type selector

My current project involves styling a table. I am looking to apply a specific background color to each odd row, excluding the first row which contains headers. I attempted the following code without success: .new-items-table tr:nth-of-type(odd):not(.new- ...

What is the best way to incorporate server-side rendered content from a CMS to hydrate Vue.js?

Consider this scenario: content is managed through a traditional CMS such as Joomla or Drupal content is provided by the CMS as fully rendered HTML and CSS In the Frontend React.js should be utilized for all UI interactions. Despite going over most of R ...

Conquering Bootstrap 3's Image Gallery Customizations

I am currently working with Bootstrap 3 RC1 and struggling to set up the image gallery properly. Issue 1 - The ul is adding unnecessary padding-left or margin-left. What do I need to do to eliminate this? Issue 2 - Each li is extending across the contain ...

Can you tell me the JavaScript alternative to Jquery's .load() shortcut method?

Exploring the modernized version of jquery, specifically the .load() ajax shorthand method that is not deprecated. One example to consider is finding the javascript equivalent for the traditional jquery ajax shorthand method mentioned below: $("#targetdi ...

What is the best way to align a series of divs vertically within columns?

Struggling to find the right words to ask this question has made it difficult for me to locate relevant search results. However, I believe a picture is worth a thousand words so...https://i.stack.imgur.com/LfPMO.png I am looking to create multiple columns ...