居中图像的 Bootstrap 5

huangapple go评论45阅读模式
英文:

Bootstrap 5 Center image

问题

我尝试通过给父级div添加类justify-content-centertext-center来水平居中一张图片。

这对文本有效,但是图片仍然保持在左侧,而不是居中。我该如何解决这个问题?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
.thumbnail {
    cursor: pointer;
    height: 10em;
    width: 10em;
    -webkit-user-select: none;
    -ms-user-select: none;
    user-select: none;
    box-sizing: content-box;
}

.thumbnail > img {
    max-height: 10em;
    max-width: 10em;
}

<!-- language: lang-html -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<div class="row justify-content-md-center">
    <div class="col col-md-4 d-flex justify-content-center text-center">
        <a href="/characters/pine">
            <div class="thumbnail align-text-bottom text-center">
                <img class="center-block" src="https://media.gettyimages.com/id/157615990/photo/lasagna.jpg?s=612x612&amp;w=gi&amp;k=20&amp;c=ue-VP7TbzbfT-KUHdhUz5T9CPBGteCiTESjiqA8CVHw=">
            </div>
            <div>Pineapple Lasagna Coleslaw</div>
        </a>
    </div>
</div>

<!-- end snippet -->
英文:

I tried centering an image horizontally my giving the parent div the class justify-content-center as well as text-center.

This worked for any text, however the image remains to the left, rather than centered. How do I fix this?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.thumbnail {
	cursor: pointer;
	height: 10em;
	width: 10em;
	-webkit-user-select: none;
	-ms-user-select: none;
	user-select: none;
	box-sizing: content-box;
}



.thumbnail &gt; img {
	max-height: 10em;
	max-width: 10em;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC&quot; crossorigin=&quot;anonymous&quot;&gt;


&lt;div class=&quot;row justify-content-md-center&quot;&gt;
  &lt;div class=&quot;col col-md-4 d-flex justify-content-center text-center&quot;&gt;
    &lt;a href=&quot;/characters/pine&quot;&gt;
      &lt;div class=&quot;thumbnail align-text-bottom text-center&quot;&gt;
        &lt;img class=&quot;center-block&quot; src=&quot;https://media.gettyimages.com/id/157615990/photo/lasagna.jpg?s=612x612&amp;w=gi&amp;k=20&amp;c=ue-VP7TbzbfT-KUHdhUz5T9CPBGteCiTESjiqA8CVHw=&quot;&gt;
      &lt;/div&gt;
      &lt;div&gt;Pineapple Lasagna Coleslaw&lt;/div&gt;
    &lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

图片已经正确定位,但具有类名.thumbnail<div>定位在父锚点的左侧。只需将mx-auto添加到这个<div>以使其居中。

<div class="thumbnail align-text-bottom text-center mx-auto"><!-- 在这里添加 mx-auto -->
    <img class="center-block" src="https://media.gettyimages.com/id/157615990/photo/lasagna.jpg?s=612x612&amp;w=gi&amp;k=20&amp;c=ue-VP7TbzbfT-KUHdhUz5T9CPBGteCiTESjiqA8CVHw=">
</div>

请注意,我只翻译了您提供的内容,并添加了注释。

英文:

The image is positioned correctly but the div with the class .thumbnail is positioned to the left of the parent anchor. Just add mx-auto to this div to center it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.thumbnail {
	cursor: pointer;
	height: 10em;
	width: 10em;
	-webkit-user-select: none;
	-ms-user-select: none;
	user-select: none;
	box-sizing: content-box;
}



.thumbnail &gt; img {
	max-height: 10em;
	max-width: 10em;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC&quot; crossorigin=&quot;anonymous&quot;&gt;


&lt;div class=&quot;row justify-content-md-center&quot;&gt;
  &lt;div class=&quot;col col-md-4 d-flex justify-content-center text-center&quot;&gt;
    &lt;a href=&quot;/characters/pine&quot;&gt;
      &lt;div class=&quot;thumbnail align-text-bottom text-center mx-auto&quot;&gt;&lt;!-- added mx-auto to this --&gt;
        &lt;img class=&quot;center-block&quot; src=&quot;https://media.gettyimages.com/id/157615990/photo/lasagna.jpg?s=612x612&amp;w=gi&amp;k=20&amp;c=ue-VP7TbzbfT-KUHdhUz5T9CPBGteCiTESjiqA8CVHw=&quot;&gt;
      &lt;/div&gt;
      &lt;div&gt;Pineapple Lasagna Coleslaw&lt;/div&gt;
    &lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

这是因为缩略图的宽度本身与图像的精确宽度相同,所以它是居中的,但不如您希望的宽。将缩略图的宽度更改为100%,以继承其父元素的宽度,图像将正确居中。

英文:

That's because the thumbnail width itself is the exact width of the image, so it's centered but not as wide as you want it to be. Change the width of the thumbnail to 100% so it inherits the width of its parents and the image will center properly.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.thumbnail {
    cursor: pointer;
    height: 10em;
    width: 100%;
    -webkit-user-select: none;
    -ms-user-select: none;
    user-select: none;
    box-sizing: content-box;
    outline: 1px solid red;
}



.thumbnail &gt; img {
    max-height: 10em;
    max-width: 10em;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC&quot; crossorigin=&quot;anonymous&quot;&gt;


&lt;div class=&quot;row justify-content-md-center&quot;&gt;
  &lt;div class=&quot;col col-md-4 d-flex justify-content-center text-center&quot;&gt;
    &lt;a href=&quot;/characters/pine&quot;&gt;
      &lt;div class=&quot;thumbnail align-text-bottom text-center&quot;&gt;
        &lt;img class=&quot;center-block&quot; src=&quot;https://media.gettyimages.com/id/157615990/photo/lasagna.jpg?s=612x612&amp;w=gi&amp;k=20&amp;c=ue-VP7TbzbfT-KUHdhUz5T9CPBGteCiTESjiqA8CVHw=&quot;&gt;
      &lt;/div&gt;
      &lt;div&gt;Pineapple Lasagna Coleslaw&lt;/div&gt;
    &lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月13日 00:54:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定