简体   繁体   English

水平对齐并居中3个相等的div

[英]Horizontally align & center 3 equal divs

So normally, when I need to align 3 divs, i'd go with this solution: 所以通常情况下,当我需要对齐3个div时,我会选择这个解决方案:

<div class="left"></div>
<div class="right"></div>
<div class="center"></div>

but in this case, I don't wanna do that. 但在这种情况下,我不想这样做。 Now I have 1 class, .medew which I need to show in a container three times. 现在我有一个类, .medew ,我需要在容器中显示三次。 These three should be aligned in the center. 这三个应该在中心对齐。 I've tried what I knew, but my knowledge is letting me down again. 我已经尝试过我所知道的,但我的知识让我再次失望。

This is the css I'm currently using: 这是我目前正在使用的CSS:

.medew {
    background-repeat: no-repeat;
    background-color: #D3D3D3;
    background-position: 5px 0px;
    color: #313131;
    margin: 5px 0 5px 8px;
    padding: 10px 10px 10px 40px;
    -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    box-shadow: 0px 3px 0px #B2B2B2;
    font-size: 12px;
    width: 150px;
    min-height: 45px;
    display: inline-block;
    margin: 0 auto;
}

Is there any way to have three of those divs that are horizontally aligned and centered in a parent container? 有没有办法让三个div水平对齐并居中在父容器中?

Simply center a parent container with margin: 0 auto and set all three inner containers to display: inline-block with a certain width! 只需将父容器置于margin: 0 auto并将所有三个内部容器设置为display: inline-block具有一定宽度的display: inline-block

CSS CSS

.parent {
margin:0 auto;
text-align:center;
}

.inner {
background-color: #D3D3D3;
width: 150px;
min-height: 45px;
display: inline-block;
}

HTML HTML

<div class="container">
   <div class="inner"></div>
   <div class="inner"></div>
   <div class="inner"></div>
</div>

Of course you can use all the other CSS style options along with this! 当然,您可以使用所有其他CSS样式选项!

Another way to do this might be using flexbox like this: Demo 另一种方法可能是使用这样的flexbox: Demo

<div class="container">
    <div class="medew"></div>
    <div class="medew"></div>
    <div class="medew"></div>
</div>

.medew {
    background-repeat: no-repeat;
    background-color: #D3D3D3;
    background-position: 5px 0px;
    color: #313131;
    margin: 5px 0 5px 8px;
    padding: 10px 10px 10px 40px;
    -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    box-shadow: 0px 3px 0px #B2B2B2;
    font-size: 12px;
    width: 150px;
    min-height: 45px;
    display: inline-block;
    margin: 0 auto;
}

.container {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}

Try like this: Demo 试试这样: 演示

.container {
    margin:0 auto;
    text-align:center;
    display:block;
}
.medew {
    background-repeat: no-repeat;
    background-color: #D3D3D3;       
    color: #313131;
    margin: 5px 0 5px 8px;
    padding: 10px 10px 10px 40px;
    -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    box-shadow: 0px 3px 0px #B2B2B2;
    font-size: 12px;
    width: 150px;
    min-height: 45px;
    display: inline-block;
    margin: 0 auto;
}

HTML: HTML:

<div class="container">
    <div class="medew"></div>
    <div class="medew"></div>
    <div class="medew"></div>
</div>

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

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