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

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

How can i do this?

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;
}
+4
source share
7 answers

Try the following: You need to add width: fit-content;a .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>
Run codeHide result
  1. Another way: display:tablein.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>
Run codeHide result
  1. The third way to use a property flex.

.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>
Run codeHide result
+2
source

margin: 0 auto; width required

https://jsfiddle.net/9e9rb6L5/1/

+1
source

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.

.one float:left; in .two , .

+1

https://jsfiddle.net/qoh1qLd7/

width:25% margin:auto 1

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

float:left

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

+1

: 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
source
  • To center the div : remove the float.
  • So that the width automatically matches 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>
Run codeHide result

Float takes items out of the page stream, so they cannot be positioned. Do not use a float unless you really want to float it!

+1
source

remove float: leave in 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>
Run codeHide result
+1
source

Source: https://habr.com/ru/post/1686456/


All Articles