如何在引导程序 4 中将图像居中? [英] How to center the image in bootstrap 4?
问题描述
我是 Bootstrap 4 的新手,在 Bootstrap 3 的早期我们使用类center-block",现在我无法在新版本中找到它.
居中内联元素实际上与 Bootstrap 无关.
使用 text-align
将图像居中图像是内联元素,可以使用 text-align
对齐.
文本将围绕图像流动,因为它是一个内联元素.
通常:
<img src="http://placehold.it/200x200";/>
Bootstrap 方式:
<img src="http://placehold.it/200x200";/>
使用 margin
将图像居中您可以将图像的显示更改为块元素并使用边距使块居中.
由于我们将显示更改为 block
,因此文本将被推入图像上方和下方.
<img src="http://placehold.it/200x200";样式=显示:块;边距:自动;"/>
Bootstrap 方式:
<img src="http://placehold.it/200x200";class =mx-auto d-block"/>
完整示例
.my-text-center {文本对齐:居中;}.my-block-center {显示:块;保证金:自动;}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="样式表"/><div class="my-text-center"><img src="http://placehold.it/200x200"/>
<div class="text-center"><img src="http://placehold.it/200x200"/>
<div><img src="http://placehold.it/200x200" class="my-block-center"/>
<div><img src="http://placehold.it/200x200" class="mx-auto d-block"/>