【问题标题】:3 images centered in a row3 张图像连续居中
【发布时间】:2018-11-21 00:26:35
【问题描述】:

我试图让三张图像连续居中,然后在页面上居中。我把它们都排成一排,但我不能让它们居中。关于让我的小组居中的任何建议?我在包含类和社会类上尝试了 0 auto。很近!!

我的 HTML:首先是 div class=contain 来包装整个内容,但由于某种原因,如果我尝试在 HTML 中包含类 contains,它会在 Stack Overflow 上消失,请原谅。

.contain {
  max-width: 960px;
  text-align: center;
}

.social {
  position: relative;
  display: inline-block;
  float: left;
  padding: 10px;
}
<div class="contain">
  <div align="center;" class="social">
    <img src="http://theinvicto.com/wp-
content/uploads/2017/12/facebook.png" alt="" width="75" height="75" />
  </div>
  <div align="center;" class="social">
    <img src="http://theinvicto.com/wp-content/uploads/2017/12/twitter.png" alt="" width="75" height="75" />
  </div>
  <div align="center;" class="social">
    <img src="http://theinvicto.com/wp-
content/uploads/2017/12/instagram.png" alt="" width="75" height="75" />
  </div>
</div>

【问题讨论】:

  • 注意:align 已过时。不要用那个。此外,&lt;img&gt; 标签不使用或不需要右斜杠。

标签: html css image inline


【解决方案1】:

我建议使用 flexbox 容器作为元素。

使用 flexbox,您只需要三种不同的样式来水平和垂直集中元素:

请注意,您还需要在容器上设置 height,以便元素实际上可以填充垂直空间。

这可以在下面看到,添加了一个border来展示.container元素占据的区域:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  border: 1px solid black;
}

.social {
  position: relative;
  display: inline-block;
  float: left;
  padding: 10px;
}
<div class="container">
  <div align="center;" class="social">
    <img src="http://theinvicto.com/wp-
content/uploads/2017/12/facebook.png" alt="" width="75" height="75" />
  </div>
  <div align="center;" class="social">
    <img src="http://theinvicto.com/wp-content/uploads/2017/12/twitter.png" alt="" width="75" height="75" />
  </div>
  <div align="center;" class="social">
    <img src="http://theinvicto.com/wp-
content/uploads/2017/12/instagram.png" alt="" width="75" height="75" />
  </div>
</div>

希望这会有所帮助! :)

【讨论】:

  • 哇!其他答案很接近,但最终这一切都完美地居中并成功了!谢谢!
  • 嗯,答案已被接受,但 flexbox 只是让已经很冗长的 HTML 更加复杂,我永远不会这样做。 @Emigriff
【解决方案2】:

html

<div class="content">
    <div>
        <img src="facebook.png" alt="" width="75" height="75"/>
    </div>

    <div>
        <img src="twitter.png" alt="" width="75" height="75"/>
    </div>

    <div>
        <img src="instagram.png" alt="" width="75" height="75" />
    </div>
</div>

css

.content { 
    text-align:center; 
}

【讨论】:

  • 这应该是公认的答案。简单实用,永远适用于任何地方。
  • 我很抱歉直到现在才看到这个!
  • @Emigriff 我相信你可以改变这一点。
【解决方案3】:

也许你可以编辑css文件,删除float:left;

.contain {
     max-width:960px;
     text-align:center;
  }

 .social {
 position:relative;
 display: inline-block;
 padding: 10px;
 }
<div align="center">
<div align="center;" class="social">
<img src="http://theinvicto.com/wp-
content/uploads/2017/12/facebook.png" alt="" width="75" height="75" />
</div>
<div align="center;" class="social">
<img src="http://theinvicto.com/wp-content/uploads/2017/12/twitter.png" 
alt="" width="75" height="75" />
</div>
<div align="center;" class="social">
<img src="http://theinvicto.com/wp-
content/uploads/2017/12/instagram.png" alt="" width="75" height="75" />
</div>
</div>

【讨论】:

  • 这有帮助!然后最终下面的 flex 解决方案使一切都很好地居中。非常感谢!
【解决方案4】:

使用flex 是一个很好的解决方案,但这里有一个使用您已有的解决方案。通过从现有代码中删除float: left,我们可以获得所需的结果。

.contain {
  max-width: 960px;
  text-align: center;
}

.social {
  display: inline-block;
  padding: 10px;
}
<div class="contain">
  <div align="center;" class="social">
    <img src="http://theinvicto.com/wp-
content/uploads/2017/12/facebook.png" alt="" width="75" height="75" />
  </div>
  <div align="center;" class="social">
    <img src="http://theinvicto.com/wp-content/uploads/2017/12/twitter.png" alt="" width="75" height="75" />
  </div>
  <div align="center;" class="social">
    <img src="http://theinvicto.com/wp-
content/uploads/2017/12/instagram.png" alt="" width="75" height="75" />
  </div>
</div>

【讨论】:

    【解决方案5】:

    保留您当前的代码,只需删除 flex: left: (JSFiddle example):

    .contain {
      max-width: 960px;
      text-align: center;
    }
    
    .social {
      position: relative;
      display: inline-block;
      padding: 10px;
    }
    

    如果基于您的browser compatibility requirements,您可以负担得起使用display: flex; (MDN),那么这是最简单的方法(jsfiddle example):

    .contain {
      display: flex;
      justify-content: center;
    }
    
    .social {
      padding: 10px;
    }
    

    这里有一个很棒的 flexbox 教程:flexbox froggy。浮点数很奇怪,我个人觉得 flex 更直观。

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2013-04-04
      • 2013-09-04
      • 1970-01-01
      相关资源
      最近更新 更多