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

Viewed 1527

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;
}
7 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>

This solution to center a dom:

1. set the wrapper `text-align:center;` 
2. and then set the dom `margin:0 auto;` 
3. At last need to set the width of the dom.

In your code you should set the width of the .one and the float:left; in .two effect the style, just remove it.

https://jsfiddle.net/qoh1qLd7/

add width:25% and margin:auto in class one

    .one{
    height: 66px;
    line-height: 36px;
    text-align: center;
    width:25%;
    margin:auto;
}

remove float:left from class two

.two{  
    background-color: #3498db;
    color: white;
    font-size: 24px;
    padding-top: 15px;
    padding-bottom: 15px;
    padding-left: 20px;
    padding-right: 20px;
}

this should do the work

Specify a width and remove the float: https://jsfiddle.net/ka1h5a6k/

.one{
    height: 66px;
    line-height: 36px;
    text-align: center;
    margin: 0 auto;
    width: 150px;
}
.two{
    background-color: #3498db;
    color: white;
    font-size: 24px;
    padding-top: 15px;
    padding-bottom: 15px;
    padding-left: 20px;
    padding-right: 20px;
}
  1. To centre the div: Remove the float.
  2. To make the width automatically fit the size of the text: display:inline-block;

No need to change anything in .one!

Run the snippet to see it in action.

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

<p>Width changes automatically:, e.g. </p>

<div class="one">
    <div class="two">Another div with longer text</div>    
</div>

Float takes elements out of the flow of the page, so they can't be positioned. Don't use float unless you actually want it to float!

remove float: left in your code

.one{
    height: 66px;
    line-height: 36px;
    text-align: center;
    margin: 0 auto;
}
.two{
    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>

Related