Tips for placing a button on the bottom of a window where the content expands

Seeking a solution to ensure the submit button is always positioned at the bottom of the page. While this can be done using position: absolute, bottom, there's a requirement to address.

If the content height is less than the wrapping div height, the button should align with the bottom of the window. However, if the content exceeds the div height, then the button should remain at the bottom of the content, not the window.

An example demonstrating the second condition can be viewed on this JSFIDDLE link. Assistance is needed to achieve the first condition.

.container {
  height: 400px;
  width: 400px;
  background: red;
  overflow: scroll;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  height: 100px;
  background: blue;
  margin-bottom: 10px;
  color: white;
}

button {
  display: block;
  width: 100%;
  background: black;
  height: 40px;
  color: white;
}

Answer №1

This solution seems like the perfect fit for your needs: https://jsfiddle.net/7wsrv6z7/

Just to clarify, my .wrapper should be placed within your .container

UPDATE

Integrated into your demo: https://jsfiddle.net/Lu3am0Lo/5/

* {
  box-sizing: border-box;
}

html, body {
  height:100%;
  margin:0;
}

.wrapper {
  padding-bottom:100px;
  min-height:100%;
  background:#fff;
  position:relative;
}

p {
  background:#eee;
}

.wrapper button {
  position:absolute;
  bottom:20px;
  right:20px;
  border-radius:40px;
  background:purple;
  color:#fff;
  padding:10px;
  border:none;
  transition:.2s;
}

.wrapper button:hover {
  padding: 10px 20px;
}

<div class="wrapper">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde facilis deserunt voluptates. Ratione quis, a quod, non impedit, earum minima nam asperiores ut sequi rem. Ullam error quia numquam eveniet dicta earum soluta dolor esse ea vel! Ipsum, sapiente, totam.</p>

  <p>lorem300</p>

  <button>SEND</button>
</div>

Answer №2

If you're looking for a solution, one option is to place the button outside of the container div and set specific max-width and height values to prevent overflow.

You can view examples here: https://jsfiddle.net/Lu3am0Lo/2/ for overflow, and https://jsfiddle.net/Lu3am0Lo/3/ if there's no overflow in the list.

.container {
  max-height: 400px;
  max-width: 400px;
  background: red;
  overflow: scroll;
}

UPDATE: Check out the revised fiddle https://jsfiddle.net/Lu3am0Lo/4/

Answer №3

It seems like you're utilizing Bootstrap in your code. One option to keep the button at the bottom of the page is by implementing a 'sticky footer' concept, achieved through setting margins on the body.

Check out the source here: https://getbootstrap.com/examples/sticky-footer/

You can also create a custom CSS class for any div (not necessarily a footer) and style it accordingly, similar to how they've used the 'footer' class.

Answer №4

Ensure to check whether the content within the container exceeds its boundaries - if yes, apply a specific class accordingly:

JavaScript

$(document).ready(function(){
  if ($('ul').height() > 400 ){
    $('button').addClass('overflow');
  } else {
    $('button').addClass('no-overflow');
  }
});

CSS

.no-overflow {
  position: absolute;
  bottom: 0;
}

.overflow {
  position: relative!important;
  bottom: 0;
}

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

Struggles with Aligning CSS Layout

I'm encountering some challenges with the layout of my webpage, particularly with alignment. Here are the issues I'm facing: The login section doesn't fit well in the header and lacks proper alignment. I aim to replicate the design from ...

When a jQuery checkbox is checked, the addClass and toggleClass effects are applied to all DIVs, not just the enclosing DIV

I am working with Bootstrap Cards that have checkboxes inside them. When a checkbox is checked, I want to apply 2 specific classes to the Card DIVs: Change class="card border-secondary" to class="card border-success" Change class="card-header" to class ...

Learn the process of retrieving a file from server.js and showcasing it on an HTML page

I am currently developing a YouTube video downloader using express, HTML, and JavaScript, with plans to transition to Vue.js in the future. Here is a snippet of my code: Index.html <!DOCTYPE html> <html lang="en"> <head> & ...

Position text fields precisely above the canvas elements

My issue involves positioning two side-by-side canvases within a browser window that the user can resize. I want these canvases to remain centered and not wrap when the window is shrunk, but there's a twist - I also need to collect user information th ...

What is the best way to transfer the value of a radio button, retrieved from a database, to a textbox?

Greetings, I am trying to figure out how to pass the value of a radio button to a textbox using jQuery and PHP. The radio buttons are generated dynamically based on rows from my database, so I set the row ID as the ID for each radio button. However, the co ...

The scope of JavaScript's dynamic object

Can created objects be utilized in a different scope? var iZ = 0; var pcs = {}; function Pc(_name, _os) //Constructor { this.name = _name; // Pc Name this.os = _os; //Pc Os this.ausgabe = function() { //Do something }; ...

Tips for implementing a function within a toggle button

I am looking to enhance the functionality of a toggle button when it is changed. Currently, there is no action associated with the toggle change event. I want to create a method that will be executed when the toggle is switched on or off. Additionally, I n ...

dropdown navigation menu with an image slider positioned ahead

I'm facing an issue with my image carousel using Flickity where the dropdown menu is getting hidden behind it. I have attempted to adjust the z-index property, but even with !important added, it doesn't seem to resolve the problem. I've been ...

Tips for creating a responsive image gallery

After completing the development of my interactive Video Gallery using only HTML and CSS, I encountered an issue with responsiveness. Whenever I resize my browser, the images go out of the frame. I am looking for a solution to make the image gallery adjust ...

The global CSS header element in Next.js is not being applied to parsed markdown files; instead, it is being replaced by Tailwind CSS

After attempting to convert Markdown into HTML, I encountered an issue. While the markdown parsed correctly, the rendered output did not match my expectations. Specifically, I expected the headers to automatically appear bold and with a larger font size, b ...

Guide on creating a donut visualization with canvas?

I am in need of assistance, as I am trying to create a donut chart using canvas and JavaScript. Here is the code I have so far, but I am unsure how to complete it: <body> <canvas id="chart" width="500" height="500" style="background-color:bl ...

What is the best way to implement form fields that have varying validation patterns based on different conditions?

Currently, my focus is on developing a form that prompts the user to choose between "USA" or "International" via radio buttons. The input field for telephone numbers should then adapt its requirements based on the selected country - either a 10-digit US nu ...

Creating a sequence of isometric foes in the right arrangement or tackling the task of handling the stained rectangle?

Can someone lend a hand in fixing my Z-index order? I've been staring at this code for hours and my eyes are starting to cross. Maybe leaving a question on Stack Overflow overnight will steer me in the right direction. I've been tinkering with h ...

What is the best way to adjust the size of a DIV containing a background

Recently, I've been delving into the world of CSS3 and have encountered an issue where the height of a DIV does not adjust as the background image expands. Ideally, the DIV should expand along with the background image. Following the DIV, there will b ...

Issue with md-stretch-tabs in Angular-Material causing a problem

I am in the process of developing an Angular-based application. ISSUE: I included md-stretch-tabs in my md-tabs element, but when I switch to tab#2, the tab retracts as if there is not enough space to flex. Is this problem related to dependencies or the c ...

What is the best way to implement CSS in a web application?

I've encountered an issue where the CSS styles are not applying when I add my web app to the home screen of an iPhone and launch the app using a shortcut icon. To illustrate, consider the following examples. First, opening the app with Safari. Below ...

Creating a Radial/Pie Menu using CSS3: A Step-by-Step Guide

I am interested in creating a radial menu using CSS3 transforms animations, similar to the example shown on this Wikipedia page or the flash version demonstrated on this website. Although there is a jQuery implementation available using canvas called Radme ...

Applying CSS transition delays to multiple images

I'm having trouble implementing a transition delay in my CSS. I've tried adding it in various places but it doesn't seem to be working as expected. This is my CSS: .imageBox { position: relative; float: left; transition ...

Remove underlining from a Custom Link when hovering over it

Is there a way to remove the blue underline from my custom donate button? I created it by going to Appearance -> Menus -> Custom Link. The issue is that this custom link (donate button) is inheriting the same CSS as the navigation menu items, which I ...

Swapping out a background video for a background image in cases where auto-play is restricted on the device

How can I replace an auto-play background image with a static image on devices that do not support auto-play? The <video> tag's POSTER attribute works, but it seems that on my iPhone (which does not allow auto-play), the video is still downloade ...