I trying make a hanoi tower.
I want to put the control part be centre as , so I checked Bootstrap and using class="center-block"
, but nothing work, like this:
(source: wuzhiwei.net)
<div class="container"> <div class="panel panel-default"> <div class="panel-heading">Hanoi Tower</div> <div class="panel-body"> <canvas id="stage" class="center-block" width="400" height="220" style="border:1px black solid "></canvas> <div id="ctrl" class="center-block"> <label for="numsel">Disc Number:</label> <select id="numsel"> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select> <button class="btn btn-default" id="start" value="start">Start</button> <label for="step">Step:</label> <span class="badge" id="step">0</span> </div> </div> </div> </div>
.panel { min-width:400px; } #stage { margin-bottom:15px; } #start { margin-left:20px; margin-right:20px; }
How can I make this div <div id="ctrl" class="center-block">
be centred?
One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.
Use class center-block to set an element to center.
You can't center divs with margin: 0 auto; if you have not set width to element. Add width to . top-center to make it work.
After setting the “display: block” property, we can make our image to block element. It can be centered using “margin: auto” property. Note: The body tag has been set to the “text-align: center” property. We know that it is not affecting the block elements.
You need to add the class text-center
to the div
with the id
panel-body
.
Change your HTML to:
<div class="container"> <div class="panel panel-default"> <div class="panel-heading">Hanoi Tower</div> <div class="panel-body text-center"> <canvas id="stage" class="center-block" width="400" height="220" style="border:1px black solid "></canvas> <div id="ctrl" class="center-block"> <label for="numsel">Disc Number:</label> <select id="numsel"> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select> <button class="btn btn-default" id="start" value="start">Start</button> <label for="step">Step:</label> <span class="badge" id="step">0</span> </div> </div> </div> </div>
Or change your CSS to add:
.panel-body{ text-align:center; }
All the class center-block
does is to tell the element to have a margin of 0 auto
, the auto
being the left/right margins. However, unless the class text-center
or css text-align:center;
is set on the parent, the element does not know the point to work out this auto
calculation from so will not center itself as anticipated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With