【问题标题】:Align image center both vertically and horizontally垂直和水平对齐图像中心
【发布时间】:2018-06-23 23:07:43
【问题描述】:

我在这里搜索过,但找不到适合我的解决方案。我试过以下 Align image to the center both vertically and horizontally Image center align vertically and horizontally 还有一些我发现的。

我有 2 列。第一个有一个图像 500px X 500px 第二个有文字

我需要垂直和水平对齐图像中心。当在移动视图上时,图像应该出现在文本上方/居中。

<section id="what">
  <div class="row">
    <div class="col-md-6 box-image"><img class="box-image" src="http://chalkgallerylewes.co.uk/wp-content/uploads/2013/08/Placeholder-500x500px.png"></div>
    <div class="col-md-6">
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
      </p>
      <ul>
        <li>Placeholder list </li>
        <li>Placeholder list </li>
        <li>Placeholder list </li>
        <li>Placeholder list </li>
        <li>Placeholder list </li>
      </ul>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
      </p>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
      </p>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
      </p>
    </div>
  </div>
</section>

我用我一直在尝试的一种方法制作了一个 JF Fiddle。

https://jsfiddle.net/davidstokes5000/rrdfLpyj/

【问题讨论】:

  • 我猜问题出在引导程序上。您在 col-md-6 中有相对位置
  • 这是您要找的布局吗? jsfiddle.net/sol_b/rrdfLpyj/3
  • 不,图片应该与桌面上的文字并排。
  • @DaveYme 它在我身边……你看到了什么?
  • @sol 它位于文本上方。即使我拉伸到全屏。

标签: html css twitter-bootstrap-3


【解决方案1】:

尝试使用 flex css。将display:flex 应用于row 类,对于子项的垂直对齐中心应用align-items:center

要将图像水平居中对齐,请将引导程序center-block 类应用于&lt;img&gt;

Reference for flex css

出于响应目的,使用媒体查询将display: block 设置为row 以实现您的结果。

您的代码中还有水平滚动,要删除它,请将您的 row 代码包装到具有 container-fluid 类的 div 中。

Updated Fiddle

#what {
  background-color: #fff;
}

#what .row {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

@media (max-width:768px) {
  #what .row {
    display: block;
  }
}

顺便说一句,@TemaniAfif 建议将您的 bootstrap3 升级到 bootstrap4。在 bootstrap4 中,您可以轻松解决此类问题。

如果您对使用 bootstrap4 有任何想法,请使用此参考

Bootstrap4 Documentation

【讨论】:

  • flex 很好,但在这种情况下我不同意......他正在使用 Bootstrap V3 并且通过更改某些引导类的属性,您可以创建一些冲突.. 可能会更改另一个类比row
  • @TemaniAfif 这就是为什么我为此使用了父 ID。我认为 flex 使用更少的代码而不是使用绝对位置会很好
  • 我同意这很好,但我认为最好让他升级到已经有 flex 的 V4 版本(或为此添加评论),他会发现处理 flex 更容易甚至没有添加 CSS 行 ;)
  • @TemaniAfif 是的,带领他升级到 V4 将是一个好主意(我会添加评论)。感谢您的建议
  • 谢谢。我也将对此进行测试。我确实在上面得到了一个可行的解决方案,但这看起来也不错。我会尝试升级到版本 4。但目前我需要 v3。
【解决方案2】:

#what{background-color: #fff;}
.box-image img{
display: flex;
  align-items: center;
  justify-content: center;
   
}
<section id="what">
	<div class="row">
		<div class="col-md-6 box-image"><img class="box-image" src="http://chalkgallerylewes.co.uk/wp-content/uploads/2013/08/Placeholder-500x500px.png"></div>
		<div class="col-md-6">
			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>

<ul>

<li>Placeholder list </li>
<li>Placeholder list </li>
<li>Placeholder list </li>
<li>Placeholder list </li>
<li>Placeholder list </li>
</ul>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>

		</div>
	</div>
</section>

绝对位置正在解决问题。

【讨论】:

  • 这不起作用,图像进入上半部分(不在此处的代码中)它不在它自己的 div 中。这是-250px
【解决方案3】:

您可以试试这个(无需设置img 的样式)。它应该使图像居中于文本上方。

.box-image {
    display: flex;
    justify-content: center;
    align-items: center;
}

【讨论】:

  • 尝试使用前缀支持旧浏览器
猜你喜欢
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
相关资源
最近更新 更多