简体   繁体   English

我该如何居中 <div> ?

[英]How can I center this <div>?

Here's what's happening: http://prntscr.com/g67d7p 这是正在发生的事情: http : //prntscr.com/g67d7p

在此处输入图片说明

As you can see, the "payout box" is not centering correctly. 如您所见,“付款箱”未正确居中。 I tried multiple things to fix this but they all failed. 我尝试了多种方法来解决此问题,但都失败了。

CSS: CSS:

div[id=rollover-button]{
    height: 100px;
    width: 160px;
    background-color: #E4ECF1;
    text-align: center;
    line-height: 100px;
    font-size: 40px;
    color: gray;
    font-weight: 300;
    display: inline-block;
    float: left;
}

div[id=payout-button]{
    height: 100px;
    width: 160px;
    background-color: #E4ECF1;
    text-align: center;
    line-height: 100px;
    font-size: 40px;
    color: gray;
    font-weight: 300;
    display: inline-block;
}

div[id=chance-button]{
    height: 100px;
    width: 160px;
    background-color: #E4ECF1;
    text-align: center;
    line-height: 100px;
    font-size: 40px;
    color: gray;
    font-weight: 300;
    display: inline-block;
    float: right;
}

HTML: HTML:

<div id="buttons">
            <div id="rollover-button">
                >50.00
            </div>
            <div id="payout-button">
                2.0000x
            </div>
            <div id="chance-button">
                50%
            </div>
        </div>

How can I fix this to make the payout box perfectly centered? 如何解决这个问题,以使付款箱完美居中?

You can use the flex property. 您可以使用flex属性。 I have removed all floats and display properties from elements and used flex on the parent. 我已经从元素中删除了所有floatsdisplay属性,并在父级上使用了flex Hope this helps you. 希望这对您有所帮助。

 #buttons { display: flex; justify-content: space-between; } div[id=rollover-button] { height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; } div[id=payout-button] { height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; } div[id=chance-button] { height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; } 
 <div id="buttons"> <div id="rollover-button"> >50.00 </div> <div id="payout-button"> 2.0000x </div> <div id="chance-button"> 50% </div> </div> 

Just add 只需添加

 #buttons > div {
  width: 33.33%;
}

instead of fixed width for each button class div. 而不是每个按钮类div的固定宽度。 (This will work with all browsers responsively) (这将适用于所有浏览器)

OR if you want some space between boxes then use 或者,如果您希望在框之间留一些空间,请使用

#buttons {
  display: flex;
  justify-content: space-between;
}

 #buttons > div { width: 33.33%; } div[id=rollover-button]{ height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; display: inline-block; float: left; } div[id=payout-button]{ height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; display: inline-block; } div[id=chance-button]{ height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; display: inline-block; float: right; } 
 <div id="buttons"> <div id="rollover-button"> >50.00 </div> <div id="payout-button"> 2.0000x </div> <div id="chance-button"> 50% </div> </div> 

Try adding the following. 尝试添加以下内容。

div[id=rollover-button]{
    height: 100px;
    width: 33%;
    background-color: #E4ECF1;
    text-align: center;
    line-height: 100px;
    font-size: 40px;
    color: gray;
    font-weight: 300;
    display: inline-block;
    float: left;
}

div[id=payout-button]{
    height: 100px;
    width: 33%;
    background-color: #E4ECF1;
    text-align: center;
    line-height: 100px;
    font-size: 40px;
    color: gray;
    font-weight: 300;
    display: inline-block;
}

div[id=chance-button]{
    height: 100px;
    width: 33%;
    background-color: #E4ECF1;
    text-align: center;
    line-height: 100px;
    font-size: 40px;
    color: gray;
    font-weight: 300;
    display: inline-block;
    float: left;
}

I would use a flexbox for the container and put space in between the flex items. 我将使用flexbox作为容器,并在flex项目之间放置空间。

 #buttons { display: flex; justify-content: space-between; } div[id=rollover-button] { height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; display: inline-block; } div[id=payout-button] { height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; display: inline-block; } div[id=chance-button] { height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; display: inline-block; } 
 <div id="buttons"> <div id="rollover-button"> >50.00 </div> <div id="payout-button"> 2.0000x </div> <div id="chance-button"> 50% </div> </div> 

Try This, Simple method to achieve this. 试试这个简单的方法来实现。

 div[id=buttons]{ margin: 0 auto; text-align: center; } div[id=rollover-button]{ height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; display: inline-block; float: left; } div[id=payout-button]{ height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; display: inline-block; } div[id=chance-button]{ height: 100px; width: 160px; background-color: #E4ECF1; text-align: center; line-height: 100px; font-size: 40px; color: gray; font-weight: 300; display: inline-block; float: right; } 
 <div id="buttons"> <div id="rollover-button"> >50.00 </div> <div id="payout-button"> 2.0000x </div> <div id="chance-button"> 50% </div> </div> 

div[id=buttons]{
  display:flex;
  justify-content: space-between;
}

div[id=rollover-button]{
    height: 100px;
    width: 160px;
    background-color: #E4ECF1;
    text-align: center;
    line-height: 100px;
    font-size: 40px;
    color: gray;
    font-weight: 300;
    display: inline-block;
}

div[id=payout-button]{
    height: 100px;
    width: 160px;
    background-color: #E4ECF1;
    text-align: center;
    line-height: 100px;
    font-size: 40px;
    color: gray;
    font-weight: 300;
    display: inline-block;
}

div[id=chance-button]{
    height: 100px;
    width: 160px;
    background-color: #E4ECF1;
    text-align: center;
    line-height: 100px;
    font-size: 40px;
    color: gray;
    font-weight: 300;
    display: inline-block;
}

And, I want to say One thing. 而且,我想说一件事。

I think you can reduce your CSS Code. 我认为您可以减少CSS代码。

You can use the tables that is the easy way. 您可以使用表,这是简单的方法。 If you want to use div itself "Go for bootstrap." 如果要使用div本身,请使用“ Go for bootstrap”。

That must help. 那一定有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM