Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set this button to center of x-axis using css?

Tags:

html

css

I tried to set this button to center of x-axis using css but it does not work.

How can i do that ?

https://jsfiddle.net/9e9rb6L5/

this is html code.

<div class="one">
    <div class="two">start now</div>    
</div>

and this is css.

.one{
    height: 66px;
    line-height: 36px;
    text-align: center;
    margin: 0 auto;
}
.two{
    float: left;
    background-color: #3498db;
    color: white;
    font-size: 24px;
    padding-top: 15px;
    padding-bottom: 15px;
    padding-left: 20px;
    padding-right: 20px;
}

1 Answers

Try this: You need to add width: fit-content; in .one class.

.one{
    height: 66px;
    line-height: 36px;
    display:block;
  margin:0 auto;
       width: fit-content;
  text-align:center;
}
.two{
    float: left;
    background-color: #3498db;
    color: white;
    font-size: 24px;
    padding-top: 15px;
    padding-bottom: 15px;
    padding-left: 20px;
    padding-right: 20px;
  
}
<div class="one">
    <div class="two">start now</div>    
</div>
  1. One more way: display:table in .one class

.one{
    height: 66px;
    line-height: 36px;
    display:table;
    margin:0 auto;
    text-align:center;
  
}
.two{
    float: left;
    background-color: #3498db;
    color: white;
    font-size: 24px;
    padding-top: 15px;
    padding-bottom: 15px;
    padding-left: 20px;
    padding-right: 20px;
  
}
<div class="one">
    <div class="two">start now</div>    
</div>
  1. 3rd way you can use flex property.

.one{
    height: 66px;
    line-height: 36px;
    text-align: center;
    margin: 0 auto;
    display:flex;
    justify-content:center;
}
.two{
    float: left;
    background-color: #3498db;
    color: white;
    font-size: 24px;
    padding-top: 15px;
    padding-bottom: 15px;
    padding-left: 20px;
    padding-right: 20px;
}
<div class="one">
    <div class="two">start now</div>    
</div>
like image 147
All about JS Avatar answered Feb 03 '23 04:02

All about JS