9

I want to position my button on the bottom of a div or the bottom of the screen (but in a non-fixed position). My code structure looks like this:

  1. div-1
    1. div-2
      1. div-3
        1. button

I want to put the button at the bottom of div 1, which height is set using jQuery (The height is the height of the screen, so putting the button at the bottom of the screen may also be a solution)

What I've tried so far:

CSS

.button {
    position: fixed;
    bottom: 10px;
    left: 50%;
    margin-left: -104.5px; /*104.5px is half of the button's width*/
}

This centers the button (what I want) and it places it at the bottom of the screen, but the position is fixed, so if I scroll down the button goes down aswell. I've also tried setting the button's position to absolute and div-1's position to relative, this didn't work either.

Edit: The div's height is variable, so margins may not be such a good option

  • You cannot put the button on the bottom of div1 when you div 2 and 3 are smaller then div1 and are top aligned. Yes please share some code. That will help us understand the question better. If you just want the button at the bottom of the screen, then you can do so by css without the need to put them inside divs (but you can if you want to) – ravish.hacker Mar 30 '16 at 08:10

6 Answers6

10

just do the button position:absolute without putting the div relativ

.button {
    position: absolute;
    bottom: 10px;
    left: 50%;
    margin-left: -104.5px; /*104.5px is half of the button's width*/
}
.test{
  height:1000px;
  
}
<div class="test">
 <div>
    <div>
      <button class="button">
            test
      </button>
    </div>
  </div>
</div>
CodeWeis
  • 846
  • 6
  • 19
  • This worked for me, not sure why though, but thanks! –  Mar 30 '16 at 08:18
  • 1
    it is because if you do the div relative the button will be position absolut in the div it is because you did the nesting of the divs – CodeWeis Mar 30 '16 at 08:22
2

Try using VW instead of px.

HTML:

<button class="button">TEST</button>

CSS:

.button {
    position: fixed;
    bottom: 10px;
    left: 47vw;
    width: 6vw;
}

EDIT:

HTML:

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

CSS:

.div{
   position: relative;
   border: 1px solid black;
   width: 500px;
   height: 250px;
}
.button {
    position: absolute;
    bottom: 5px;
    left: 50%;
    width: 50px;
    margin-left: -25px;
}

I was looking the code instead of the question so i forget that the real question was add the button on the bottom of div or screen.

The parent div has to be position: relative; and the button position: absolute;

edisonmecaj
  • 1,062
  • 1
  • 9
  • 20
2

if width div 50% then left must 25% if width div 70% then left must 15%

.center{
  position:fixed;
  bottom:20px;
  left:20%;
  width:60%;
 
}

 .center .btn{
     background:red;
     width:100%;
     color:white;
     font-weight:bold;
     border-radius: 64px;
    padding:10px;
  }
<div class="center">
  <button class="btn">Login</button>
</div>
Revi Ana
  • 21
  • 2
1

I believe these Stack Overflow posts might be of help to you:

1) How do I get a div to float to the bottom of its container

2) HTML/CSS positioning float bottom

If this doesn't help can you please also provide your HTML code.

Community
  • 1
  • 1
Johnny
  • 751
  • 12
  • 24
  • I don't think the HTML code will help much since it are just some nested divs with Lorem Ipsum text in it. I'll take a look at the links you posted though. –  Mar 30 '16 at 08:09
  • Okidoki. Just wanted to double-check. – Johnny Mar 30 '16 at 08:10
  • Both of the answers given are basically the same. Sadly, I've tried these methods already (as stated in the last piece of text in my question) –  Mar 30 '16 at 08:11
0

You should use position: absolute on your button when parent element height and width is 100% (of document or page).

<div class="div-1">
  <div class="div-2">
    <div class="div-3">
      <button>
      Just a button
      </button>
    </div>
  </div>
</div>

and css with little reset:

* {
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
}

.div-1 {
  position: relative;
  height: 100%;
  width: 100%;
}

.div-2, .div-3{
  width: inherit;
  height: inherit;
}

button {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
}

Here is JSfiddle

oboshto
  • 3,478
  • 4
  • 25
  • 24
  • It centers the button, but it doesn't put it at the bottom of the div/screen though. If the position of the button was fixed it would work for some reason... –  Mar 30 '16 at 08:15
0

Here is the responsive width:

position: absolute;
bottom: 23px;
left: 10%;
width: 80%;

enter image description here

enter image description here

Parth Developer
  • 1,371
  • 1
  • 13
  • 30