Position the button at the bottom of a div

I am currently utilizing Bootstrap 4.5, however, the following is just a brief demonstration of my desired outcome.

Within the dimensions of a fixed-width and height card, I aim to have the content occupy all remaining space while ensuring the button stays at the bottom consistently. How can I make this happen?

<html>
<body>

<div style="width:400px; height:704px; background-color:gray;">

<div class="container" style="width:100%; height:100%;">

    Some content
    <button>Some Button</button>
</div>


</div>

</body>
</html>

Answer №1

Consider trying this approach:

position:absolute can be used to position an element within a parent div.

.bttn {
        position: absolute;
        right:    50%;
        bottom:   0;
}

.cont {
        position: relative;
}
<html>
<body>

<div style="width:400px; height:154px; background-color:gray;">

    <div class="container cont" style="width:100%; height:100%;">
        Some content
        <button class="bttn">Some Button</button>
    </div>

</div>

</body>
</html>

Answer №2

Here is a suggestion for handling the inner div:

"Since the inner div is absolutely positioned, there may be concerns about potential overlap with other content in the outer div (requiring fixed heights).

If possible, it's advisable to place the inner div as the last DOM element within the outer div and set it to 'clear: both'."

   <div style="position: relative; 
                width: 200px; 
                height: 150px; 
                border: 1px solid black;">
    
        <div style="position: absolute; 
                    bottom: 0; 
                    width: 100%; 
                    height: 50px; 
                    border: 1px solid red;">
        </div>
    </div>

Reference: How can I send an inner <div> to the bottom of its parent <div>?

You could implement it like this:

<div class="container" style="width:100%; height:100%; position: relative;">

    Some content
    <div class="button" style="position: absolute; bottom: 0;"><button>Some Button</button></div>
</div>

Answer №3

If you're looking for a simple way to arrange your content using CSS Flexbox, here's a handy solution. Start by setting the parent container's display property to flex. Then, adjust the direction of the layout to column (by default, flex-direction is set to row). Finally, align the items using "space-between," ensuring that child elements are positioned at the beginning and end of the container. This will keep your content consistently at the top with the button fixed at the bottom.

<html>
<body>

<div style="width:400px; height:704px; background-color:gray;">

<div class="container" style="display: flex; flex-direction: column; justify-content: space-between; width:100%; height:100%;">

Some content
<button style="width: 100px">Some Button</button>
</div>


</div>

</body>
</html>

Answer №4

To achieve this layout in Bootstrap, you can simply use the following bootstrap classes -

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<body>
  <div class="card text-center" style="width:400px; height:704px; background-color:gray;">
    <div class="card-header">Header</div>
    <div class="card-body" style="width:100%; height:100%;">Content</div>
    <!-- The below button always stays at the bottom-->
    <div class="card-footer"><button class="btn btn-success">Some Button</button></div>
  </div>

</body>

If you want to achieve the same result using simple HTML and CSS, here's a way to do it -

<div class="card" style="width:400px; height:704px; background-color:gray; text-align:center;">
  <div>Header</div>
  <!-- Here you need to leave some height for the button -->
  <div style="width:100%; height:90%;">Content</div>
  <div><button>Some Button</button></div>
</div>

Answer №5

Typically, the size of your webpage is determined by its content, regardless of the size of the browser window. However, you can change this with some CSS:

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

If you want to make one element take up all the remaining space on the page, there are two main options: table layout or flex layout. Here's an example using table layout:

body {
   display: table;
}
.container {
    display: table-row;
    height: 100%;
}

(This example uses pure CSS without Bootstrap.)

View the example on jsFiddle here: https://jsfiddle.net/89rxtfuw/

Answer №6

If you place your content inside a separate div and utilize CSS flexbox, you can achieve this effect

HTML:

<div style="width:400px; height:704px; background-color:gray;">

<div class="container my-container" style="width:100%; height:100%;">
    <div class="content">Some content</div>
    <button>Some Button</button>
</div>

CSS:

.my-container {
  display: flex; /* add flex to container */
  flex-flow: column nowrap; /* set flex layout to be vertical */
}
.content {
  background: white; /* set white background on content div */
  flex-grow: 1; /* force content div to take over all the remaining space */
}

Answer №7

If you're looking for a straightforward way to achieve this, consider utilizing Flexbox which has extensive browser support. https://caniuse.com/#feat=flexbox.

Specifically, we'll delve into the concept of flex-grow. This property allows us to evenly distribute available space between elements on our webpage.

An Approach Using Bootstrap

(Update: The method demonstrated by Abishek with Bootstrap cards mentioned earlier is the optimal bootstrap solution tailored for cards: )

To gather more insights on using bootstrap and flex-grow efficiently, check out https://getbootstrap.com/docs/4.1/utilities/flex/#grow-and-shrink

.container {
  background-color: #ccc;
}
.container button {
  background-color: #777;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container d-flex flex-column" style="width: 200px; height: 400px;">
  <div class="flex-grow-1">Hello World!</div>
  <button>Button</button>
</div>

A Simple Html/CSS Technique

.container {
  width: 200px;
  height: 400px;
  display: flex;
  flex-flow: column;
  background-color: #ccc;
}
.content {
  flex-grow: 1;
}
.container button {
  background-color: #777;
}
<div class="container">
  <div class="content">
    Hello World!
  </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

Unable to conduct this search with Python using Selenium on a website

I have written a script to search for books on a webpage using Selenium: from selenium import webdriver from selenium.webdriver.common.keys import Keys import time PATH = "C:\Program Files (x86)\chromedriver.exe" driver = webdriver.Chrome(PATH) ...

The locator for the span class cannot be found or interacted with in Selenium

Here is the HTML code snippet for a span element: <td class="header-logout-btn"> <a href="logout.htm" class="btn switch-btn"> <i class="fa fa-times"></i><span class="hidden-xs">Home</span> </a> </td> I ...

What are the steps to integrate <br> in a JavaScript code?

I have recently started learning about web development and I'm facing a challenge with this implementation. var obj = [[{ name: "John", age: 30, city: "New York"}, { name: "Ken", age: 35, city: "New Orleans"}]]; ...

A Guide to Retrieving ngCordova Camera Images Using JavaScript

Currently in the process of developing a photo-editing application using the Ionic Framework. Initially, I integrated an open-source JS Drag & Drop photo editor and made necessary modifications. However, my challenge lies in accessing the image created by ...

What steps can be taken to resolve this issue?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ut ...

Align image within box using Bootstrap

My project screenshot is displayed below. The red line separates the current state from the desired outcome. I am struggling to make it fullscreen, meaning it should extend all the way to the corners. Despite trying various methods with Bootstrap 4, I have ...

What is the best way to extract multiple children from a shared tag using BeautifulSoup?

My current project involves using BeautifulSoup to scrape thesaurus.com for quick access to synonyms of specific words. However, I've run into an issue where the synonyms are listed with different ids and classes for each word. My workaround is to tar ...

Is there a way to automatically send 404 errors to index.html and update the URL to the homepage URL?

I transformed a large and intricate website into a concise one-page site, necessitating users to be guided from 404 errors to the index.html page. To accomplish this, I added the following code snippet to my .htaccess file: ErrorDocument 404 /index.html ...

Selenium encountering difficulty extracting data via VBA with error message "NoSuchElementError"

Hello everyone, I'm new here on StackOverflow and seeking some assistance. I am attempting to extract data from a table on a website using Selenium for the first time. However, I encountered an issue while trying to use FindElementByClass as it return ...

Is the custom cursor feature malfunctioning in IE9?

In my form, I have a web browser control that uses a custom cursor in the CSS code. Everything appears to be working fine in IE8. Css Code cursor: url(AppDirectiry\Classes\QClasses\ClsDesignHtml\Cursors\arrow.ani); However, it s ...

Align images at the center of a division using the Bootstrap framework

I'm facing an issue with centering social network icons under a div in my login form while keeping it responsive. Can someone please assist me with this problem? Please help me!!. .row { background: #f8f9fa; margin-top: 20px; } .col { bor ...

What is the process for clearing the input value of a View from another View?

My situation seems simple and straightforward, yet I am struggling to achieve the desired output. In this scenario, I have two HTML pages named Index.htm and Main.htm, as well as a script page named mscript.js. Index.htm contains a button (Clear), and Mai ...

Conceal the scroll bar within a div set to 100% viewport

Having some difficulty with a scrollbar on my new portfolio. The layout consists of two columns, with content on the left. I need the ability to scroll without displaying the scrollbar. I attempted the code below but it did not work. Could the issue be rel ...

Sending JSON data to Python CGI script

I've successfully set up Apache2 and got Python running without any issues. However, I'm facing a problem with two pages - one is a Python Page and the other is an HTML page with JQuery. Could someone guide me on how to correctly make my ajax p ...

I am in need of a datepicker for my project that will display the END date as the current date and set the START date as 7 days before the END date

$(document).ready(function () { $("#end").datepicker({ dateFormat: "dd-M-yy", minDate: 0, onSelect: function () { var start = $('#start'); var startDate = $(this).datepicker('getDate') ...

Remove any unnecessary white space and new lines from the HTML code

Is there a way to remove double whitespaces and new lines from my HTML output without altering the ones within textarea and pre tags using PHP? ...

The bootstrap 4 modal is not displaying the button as expected

I am currently working on an Angular 4 project and I am trying to implement a pop-up using Bootstrap 4. <div> <span class="text-center" id="span1">{{title}}</span> <button type="button" class="btn primary-btn" data-toggle="modal" data ...

Executing a Javascript function post AJAX page loading

I'm not a coding expert, so I hope my question is still clear. Essentially, I have an index.php page with filters (by project, year, month) that send variables to filterData.php after clicking submit. These variables are then used in SQL statements t ...

Tips for inputting information without redundancy after selecting a specific designation

I'm experiencing a challenge with this code as it repeats when already chosen. My goal is to add data in the database once something has been selected. When I click on spin, the randomizer will choose a name and update their status to "done". Subsequ ...

Encountering a problem when transitioning Bootstrap 3 code to version 5

After finding this template on github, I noticed it was a bit outdated with the use of Bootstrap 3. I decided to update it to Bootstrap 5 and replaced the Bootstrap 3 CDN accordingly. However, upon doing so, I encountered some issues. Can anyone help me ...