Having trouble aligning a button at the bottom using CSS positioning - it keeps ending up at the top instead

I am facing a challenge understanding the layout of my div. My intention was to place a button at the bottom, so I adjusted the parent element's position to absolute and set my element to have a relative position. However, instead of appearing at the bottom as expected, it is positioned at the top. This behavior is perplexing, and I am struggling to comprehend why this is happening.

https://i.sstatic.net/rOTGc.png

Below is my HTML structure:

<!DOCTYPE html>
<html>
<head>
  <style>
    body{
      background-color: #f3f2f4;
    }
    .header{
        width: 105%;
        height: 117px;
        left: -11px;
        position: absolute;
        background-image: linear-gradient(#8bc2e0, #4f9bc6);
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
        border-bottom-left-radius: 100% 70%;
        border-bottom-right-radius: 100% 70%;
    }
    .container{
      width:450px;
      height:70vh;
      background-color:white;
      border-radius:6px;
      position:absolute;
      top:50%;
      overflow:hidden;
      left:50%;
      transform:translate(-50%,-50%);
    }

    .logo {
        width: 100px;
        margin: 0 auto;
        margin-top: 46px;
    }

    .button {
        width: 80%;
        margin: 0 auto;
        padding: 15px 20px;
        font-size: 17px;
        color: #fff;
        border-radius: 30px;
        background: #428cb5;
        display: flex;
        align-items: center;
        justify-content: center;
        font-family: Arial;
        cursor: pointer;
        transition: .2s;
        position: absolute;
        bottom: 0px;
    }

    .button:hover {
        background: #4895bf;
    }

    .body {
        position: relative;
    }
  </style>
</head>
<body>
    <div class="container">
        <div class="body">
            <div class="header">
                <div class="logo">

                </div>
            </div>  

            <div class="button">Submit button</div>
        </div>
    </div>
</body>
</html>

Check out the fiddle here: https://jsfiddle.net/n35e6Lzh/

Answer №1

Avoid putting any unnecessary div tags above the body section to keep your code clean and organized. Instead of using absolute positioning in the header and button elements, try using the following 3 properties within the container:

display: flex;
flex-direction: column;
justify-content: space-between;

https://jsfiddle.net/qmstzrpf/

Answer №2

Take out the position: relative declaration from .body, and insert the following rules to convert it into a flexbox:

.body {
    /*position: relative;*/
    display: flex;
    flex-flow: column;
    height: 100%;
}

Next, eliminate the position: absolute and left/top positioning from both .header and .button, then apply these rules to .button:

.button{
    margin-top:auto;
}

By placing it in a flexbox, this will move it to the bottom.

Note: I adjusted the heights of html, body, and the .container to be 100%, removed overflow:hidden from the .container, set a min-height for it, changed the background-color of .body to #fff, and made the width of .header 100%. These changes were aimed at simplifying and organizing the styling.

html,body{height:100%}

body {
  background-color: #f3f2f4;
}

.header {
  width: 100%;
  height: 117px;
  /*left: -11px;
    position: absolute;*/
  background-image: linear-gradient(#8bc2e0, #4f9bc6);
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  border-bottom-left-radius: 100% 70%;
  border-bottom-right-radius: 100% 70%;
}

.container {
  width: 450px;
  height: 100%;
  background-color: white;
  border-radius: 6px;
  position: absolute;
  top: 50%;
  /*overflow: hidden;*/
  left: 50%;
  transform: translate(-50%, -50%);
}

.logo {
  width: 100px;
  margin: 0 auto;
  margin-top: 46px;
}

.button {
  width: 80%;
  margin: 0 auto;
  padding: 15px 20px;
  font-size: 17px;
  color: #fff;
  border-radius: 30px;
  background: #428cb5;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: Arial;
  cursor: pointer;
  transition: .2s;
  /*position: absolute;
    bottom: 0px;*/
  margin-top: auto;
}

.button:hover {
  background: #4895bf;
}

.body {
  /*position: relative;*/
  background-color: #fff;
  display: flex;
  flex-flow: column;
  height: 100%;
  min-height: 300px;
}
<div class="container">
  <div class="body">
    <div class="header">
      <div class="logo">

      </div>
    </div>

    <div class="button">Submit button</div>
  </div>
</div>

Answer №3

Here is the solution: https://codepen.io/DohaHelmy/pen/LYErOOL

If you want to see your card with proper height, you can also use full page view after running the snippet.

body {
  background-color: #f3f2f4;
}

.header {
  width: 105%;
  height: 117px;
  left: -11px;
  background-image: linear-gradient(#8bc2e0, #4f9bc6);
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  border-bottom-left-radius: 100% 70%;
  border-bottom-right-radius: 100% 70%;
}

.container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin: 50px auto;
  width: 450px;
  height: 70vh;
  background-color: white;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 2px 2px 2px #ddd;
}

.logo {
  width: 100px;
  margin: 0 auto;
}

.button {
  width: 80%;
  margin: 0 auto;
  padding: 15px 20px;
  font-size: 17px;
  text-align: center;
  color: #fff;
  border-radius: 30px;
  background: #428cb5;
  font-family: Arial;
  cursor: pointer;
  transition: 0.2s;
  position: relative;
  bottom: 20px;
}

.button:hover {
  background: #4895bf;
}
<div class="container">
  <div class="body">
    <div class="header">
      <div class="logo"></div>
    </div>
  </div>
  <div class="button">Submit button</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

Hover effects in CSS using z-index are causing the displacement of divs

Whenever I hover over the first div to reveal the entire text, the second div's position shifts to go behind it. My goal is to have the second div stay in place, allowing the text from the first div to overlap it. Check out the demo here: https://cod ...

What is the best way to include 2 or more radio buttons in every row of data within a table?

Is it possible to have multiple radio buttons for each row of data in a table? The current code snippet is as follows: echo '<table> <tr> <td>Home</td> <td>Not Home</td> <td>Address</td> <td>Su ...

Accessing a JavaScript function from a file stored on the SD card using Android Studio

I'm currently developing an Android application that writes an HTML file to the SD card and then loads it in a web view. The app is able to load the HTML file, but the JavaScript functions within the file are not functioning correctly. Here is the cod ...

What is the best way to declare a variable in order to apply a custom background color to a

I am having trouble getting the conditional background color to apply to the textbox when I set the variable columnstyle. Here is the snippet of HTML code: var columnstyle = "text-align:center;vertical-align:central;background-color:#FFBF00"; ...

Element A hovers with a CSS effect, while Element B is styled with CSS effects

There is a class B element that has the following CSS applied: .B{ display:none; } Then, there is a class A element with the following CSS effect when hovered over: A:hover .B{ display:block; } The desired outcome is to make the class B element app ...

The background image spanning across the entire screen, not just confined to the center

view image details here Can anyone explain why my background image for the main game is displaying like this? I want to set it for the entire screen as I have different backgrounds planned for the menu of the game and the game itself. Any suggestions to ma ...

Load plotly-latest.min.js file from your local directory

My code works fine when reading the "plotly-latest.min.js" file from online and saving it as ".js" in the same directory as my ".py" file. However, I am unsuccessful in reading it locally without an internet connection. import sys from PyQt5.QtWidgets ...

Trouble with CSS styling in Asp.Net MVC dropdown list

I have encountered an issue while trying to apply a CSS class to an ASP.NET MVC dropdown list. The CSS class works fine for the EditorFor case, but not for the DropDownListFor case. I am wondering what could be missing from my code? Here is the working CS ...

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 ...

The header and footer elements must be included along with a main div to house the content on the webpage

I am currently working on building a website using HTML and CSS. I would like to create an index.php file that calls all the different parts of the page. For example, I want to include the header and footer, and also have the ability to replace content wit ...

How can I retrieve the row index in an Angular Mat-Table that has expandable content?

Having a mat-table with expandable content on a page, I am seeking a solution to record the row number of the table when clicked. While I successfully achieved this with a regular table in the HTML file using: <tr mat-row *matRowDef="let row; columns: ...

Creating a clickable navigation menu item with a clickable caret located on the same line

I've been developing a solution using Twitter Bootstrap to create a main navigation menu that expands upon hover on desktop, while the menu expands when clicking the caret on mobile (caret hidden on desktop). Additionally, I aimed to make the top-leve ...

JavaScript failing to retrieve JSON information from PHP

I seem to be encountering an issue when trying to read JSON data that I create in PHP and send back to my JavaScript. The problem is puzzling me. Here's the PHP code: header("content-type: text/json"); curl_setopt($ch,CURLOPT_URL, $url); curl_setop ...

When I hover over my button, my dropdown unexpectedly vanishes

Does anyone know how to prevent the dropdown from disappearing when hovering on the button? I'm not using JavaScript for this dropdown menu and would appreciate any help. I'm still learning, so please bear with me as I try to resolve this issue. ...

Uploading Images Dynamically with Ajax and jQuery in PHP

[Resolved Previous Issue] Recently, I obtained an image upload script that utilizes Ajax and jQuery. My goal is to restrict the maximum number of uploads to just 8 images. Any suggestions or advice would be greatly appreciated. Source of Download: ...

Recommendation for a reliable and user-friendly chat platform without the need for third-party involvement

I am in need of a chat room for a specific group of users on my social network. Ideally, I want a chat feature that is simple and does not require a third party. The chat should automatically use the user's name based on their authorized id when they ...

Adjust Vue FilePond dimensions

I am currently working with a Vue Filepond and trying to ensure that it fills the height of its container. My Vue Filepond setup also involves Vuetify. Whenever I attempt to set values in the CSS, they are constantly overridden by 76px. Although fill-hei ...

Add data to the beginning of a textarea

This JQuery code has been created to prepend input from a textarea $(document).ready(function() { var data = <?php echo $data; ?>; $('#standard_response').on('change', function() { var sequence = $(this).val(); ...

Create a scrollable div within a template

After discovering a template that I want to use for my personal project, I noticed a missing element... There's a div where content can be added. That's fine, but what if I add more content than fits in the space provided? The whole website beco ...

Error found in the HTML tag data when viewing the page source for an issue

I am displaying some data from my express to ejs in HTML tag format. It appears correctly on the ejs template page and the web page. However, when I check the page source, the HTML tags are encoded and displayed as unescaped characters. Is there a solution ...