0

How do I get the button id= button1 to match up with the button id=button2? I'm trying to get button1 horizontally in the middle of the page. I need this to work on chrome.

<div id='todoCnts' style="text-align:center">
    <span id='checkCnt' style="float:left">Item Count :</span>
    <button id='button1' >New TODO</button>
    <span id='uncheckCnt' style="float:right">Item Count for the win:</span>
    <br>
    <button id='button2' >New TODO</button>
 </div>
DCR
  • 14,737
  • 12
  • 52
  • 115
  • Have a look at [this answer](https://stackoverflow.com/a/33049198/4204026). Flex really is the way to go for these situations - floats are almost evil. – Drew Kennedy Oct 01 '18 at 23:30

3 Answers3

2

You can try flexbox like this. It will also be responsive on small screens.

#todoCnts {
  display:flex;
  flex-wrap:wrap;
}
#todoCnts > span {
  flex:1;
}
#uncheckCnt {
 text-align:right;
}
#todoCnts > div {
  flex-basis:100%;
  text-align:center;
}
<div id='todoCnts'>
    <span id='checkCnt' >Item Count :</span>
    <button id='button1' >New TODO</button>
    <span id='uncheckCnt' >Item Count for the win:</span>
    <div><button id='button2' >New TODO</button></div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • this is great. I notice though that you don't need: #todoCnts > div { flex-basis:100%; text-align:center; } – DCR Oct 02 '18 at 00:20
  • ahh, if you delete the second button or move it out of the upper div then you can remove the css as indicated – DCR Oct 02 '18 at 14:49
0

You need to take the centered button out of the flow, so that it ignores the floats.

Edit: For the benefit of Chrome, we need to change the markup slightly.

Or you can use Temani's "transform" solution. See the comments.

#button1wrapper {
  position:absolute;
  left:0;
  right:0;
  margin:0 auto;
}
<div id='todoCnts' style="text-align:center">
    <span id='checkCnt' style="float:left">Item Count :</span>
    <div id='button1wrapper'><button id='button1' >New TODO</button></div>
    <span id='uncheckCnt' style="float:right">Item Count for the win:</span>
    <br>
    <button id='button2' >New TODO</button>
 </div>
Alohci
  • 78,296
  • 16
  • 112
  • 156
-2

Try to use flexbox, it is made for exactly this! Set id="todoCnts" style to:

display: flex;
align-items: center;
justify-content: center;

And the children div to "margin: auto"