html - 尝试将 3 个 div block 彼此并排居中并留有间隙,它们在水平方向上彼此相邻对齐

标签 html css

#block1 {
    background-color: blue;
    font-family:"Arial Black", Gadget, sans-serif;
    width: 25%;
    padding: 4px;
    border-radius: 1em;
    margin-left: auto;
    margin-right: auto;
    float: left;
    display: block;
}

#block1 p {
            color: white;
}

#block2 {
        background-color: blue;
        font-family:"Arial Black", Gadget, sans-serif;
        width: 25%;
        padding: 4px;
        border-radius: 1em;
        margin-left: auto;
        margin-right: auto;
        float: left;
        display: block;
}

#block2 p {
            color: white;
}

#block3 {
    background-color: blue;
    font-family:"Arial Black", Gadget, sans-serif;
    width: 25%;
    padding: 4px;
    border-radius: 1em;
    margin-left: auto;
    margin-right: auto;
    float: left;
    display: block;
}

#block3 p {
        color: white;
}

如您所见,我已将三个 block 设置为彼此相邻对齐,但我只是努力将它们居中,中间留有空格。 我已将其设置为所有 block 都并排 float ,但它们只是按照指定的 float 向左移动到左侧,有没有办法让它们居中?

最佳答案

不要使用 float ,使用 display:inline-block 并在父容器上添加 text-align:center

.container {
  text-align: center;
}
.block {
  background-color: blue;
  font-family: "Arial Black", Gadget, sans-serif;
  width: 25%;
  padding: 4px;
  border-radius: 1em;
  display: inline-block;
  height: 250px;
  margin: 0 10px;
}
<div class="container">
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
</div>

关于html - 尝试将 3 个 div block 彼此并排居中并留有间隙,它们在水平方向上彼此相邻对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27383699/

相关文章:

html - 如何用CSS绘制多边形背景?

ios - 使用IE9,我看不到Amazon S3中的音频播放器(HTML5音频标签)要通过iOS上传

html - Masonry Jquery 布局环绕框(如字母 L)

html - 创建一个具有两行文本的完美圆

css - Sass @imports 层次结构 : Bottom to top or top to bottom?

javascript - 如果没有附件,表格将无法提交

css - 跨浏览器 base64 网络字体支持

javascript - 设置商店小部件的样式,使其看起来完全不同

html - 两个 div 之间的垂直线间隔

javascript - 如何在我的 jquery 菜单中的 li 项下添加一行?