Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center absolute positioned div [duplicate]

I have a div, in it there is a button:

I let the button position to absolute, and its style code:

  .buy-btn {
    text-align: center;
    position: absolute;
    bottom: 10px;

  }

How can I align it to center? I tried add margin: 0 auto;, but it not work.

like image 672
user7693832 Avatar asked Mar 30 '18 03:03

user7693832


People also ask

How do you center an absolutely positioned div?

Answer: Use the CSS margin , left & right property You can align any absolutely or fixed positioned <div> element horizontally center using the CSS margin property in combination with the left and right position property.

How do you center an absolute position in CSS?

If you want to center something horizontally in CSS you can do it just by, using the text-align: center; (when working with inline elements) or margin: 0 auto; (when working with block element).

Why is Z Index not working?

You set z-index on a static element By default, every element has a position of static. z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.


3 Answers

If i understood your question then it will work as below.

You can do it in three ways.

No1 - Using position. Apply width 100% to button parent div and apply the button style as bellow.

.buy-btn{
    display: inline-block;       /* Display inline block */
    text-align: center;          /* For button text center */
    position: absolute;          /* Position absolute */
    left: 50%;                   /* Move 50% from left */
    bottom: 10px;                /* Move 10px from bottom */
    transform: translateX(-50%); /* Move button Center position  */
}

No2 - Using parent div, apply width 100% to your parent div and remove the postion absolute from button.

.parentDiv {
    width: 100%;            /* for full width */
    text-align: center;     /* for child element center */
 }

.buy-btn{
    display: inline-block;       /* Display inline block */
    text-align: center;          /* For button text center */
}

No3 - Using margin, apply width 100% to your parent div and remove the postion absolute from button.

.parentDiv {
    width: 100%;            /* for full width */
 }

.buy-btn{
    display: inline-block;       /* Display inline block */
    text-align: center;          /* For button text center */        
    margin: 0 auto;              /* For button center */
}
like image 175
devzakir Avatar answered Nov 08 '22 11:11

devzakir


/* Replace below css  and add position relative to its parent class*/

.buy-btn {
       text-align: center;
       position: absolute;
       bottom: 10px;
       left: 50%;
       display: inline-block;
       transform: translateX(-50%);
    }
like image 24
Trupti Avatar answered Nov 08 '22 11:11

Trupti


you may add these.

.buy-btn {
    /* ... */
    left:50%;
    transform: translateX(-50%);
}
like image 23
Jacob Goh Avatar answered Nov 08 '22 12:11

Jacob Goh