简体   繁体   English

为什么我的图像没有集中在div上?

[英]Why isn't my image centered in the div?

Can anyone tell me why this doesn't work? 谁能告诉我为什么这行不通? I'm trying to center the image. 我正在尝试将图像居中。

CSS CSS

.img-center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

HTML HTML

<div class="container">
  <section id="whats-on">
    <h1>News</h1>
    <div class="img-center">
      <a href="#"><img src="assets/img/my-image.png"></a>
    </div>
  </section>
</div><!-- container -->

the img-center container has width 100% ( by default ) so it is already centered but covers the width of all the viewport . img-center容器的宽度为100%(默认情况下),因此它已经居中,但覆盖了所有视口的宽度。 this is because a div is a block element and as the DOCumentation says 这是因为div是一个block元素,正如DOCumentation所说

A block-level element always starts on a new line and takes up the full width available (stretches out to the left and right as far as it can). 块级元素总是从新行开始,并占据可用的全部宽度(尽可能向左和向右伸展)。 The <div> element is a block-level element. <div>元素是块级元素。

so you can remove the display:block 因此您可以删除display:block

option A. add text-align:center to .img-container 选项A.text-align:center添加到.img-container

for this option you can remove all other styles 对于此选项,您可以删除所有其他样式

 .img-center { text-align:Center; } 
 <div class="container"> <section id="whats-on"> <h1>News</h1> <div class="img-center"> <a href="#"><img src="http://placehold.it/350x150"></a> </div> </section> </div><!-- container --> 

option B. give the styles to the img not to it's container 选项B.将样式赋予img而不是容器

for this option you need to keep the display:block to the img because img is an inline element by default 对于此选项,您需要将display:block保留在img因为img默认情况下是inline元素

 .img-center img { display: block; margin-left: auto; margin-right: auto; } 
 <div class="container"> <section id="whats-on"> <h1>News</h1> <div class="img-center"> <a href="#"><img src="http://placehold.it/350x150"></a> </div> </section> </div><!-- container --> 

Your code centers the container of the image (which is already full width aka it doesnt do anything), not the image itself inside that container. 您的代码将图像容器居中(该容器已经是全宽,因此它什么也不做),而不是图像本身位于该容器内。 Just do this: 只要这样做:

img {
    display: block;
    margin-left: auto;
    margin-right: auto;
}

Fiddle 小提琴

Just Simply add text-align: center; 只需添加text-align: center; to .img-center 到.img-center

This has to do with your <div class="img-center"> not having a set width. 这与您的<div class="img-center">没有设置宽度有关。 Add width:100px; 添加width:100px; just to see. 只是看看。

See this : https://jsfiddle.net/s3x82hv4/ 看到这个: https : //jsfiddle.net/s3x82hv4/

为什么背景图片没有出现在我的内部<div> ?</div><div id="text_translate"><p> 我试图为我的 web 页面的这一部分放置一个背景图像,我将我的图像作为一个 id(水果),但它没有出现。 这是我的代码:</p><pre> &lt;div class="p-1 text-center bg-light" id="fruit"&gt; &lt;div class="col-md-6 mx-auto my-5"&gt; &lt;h1 class="display-4 font-weight-normal"&gt;TrendyEats&lt;/h1&gt; &lt;p class="lead font-weight-normal"&gt;The Latest Food Trends&lt;/p&gt; &lt;a class="btn btn-outline-secondary" href="#"&gt;Login&lt;/a&gt; &lt;p class="small pt-4"&gt;blablabla &lt;/p&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 水果 id 的 css 是:</p><pre> #fruit { background-image: url('/Pictures/fruits.jpg'); alt:"woops"; }</pre><p> “alt”也没有出现。</p><p> 任何帮助表示赞赏,谢谢!</p><p> 编辑:已修复,看来我必须写“Pictures/fruits.jpg”而不是“/Pictures/fruits.jpg”</p></div> - Why isn't the background image appearing inside my <div>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Div 中的图像未居中 - Image in Div isn't centered 我居中的div不太居中 - My centered div isn't quite centered 为什么我的提交按钮不居中? - Why isn't my submit button centered? 为什么我的动画元素没有居中? - Why isn't my animation element centered? 为什么我的图像不在我的DIV中居中? - Why is my image not centered in my DIV? 为什么这个div不在中心? 我听不懂 - Why it isn't this div centered? I can't understand CSS居中的div不在Firefox中居中 - CSS centered div isn't centered in Firefox 为什么我的无序列表没有放在居中图像旁边? - Why isn't my unordered lists not being positioned beside the centered image? 为什么背景图片没有出现在我的内部<div> ?</div><div id="text_translate"><p> 我试图为我的 web 页面的这一部分放置一个背景图像,我将我的图像作为一个 id(水果),但它没有出现。 这是我的代码:</p><pre> &lt;div class="p-1 text-center bg-light" id="fruit"&gt; &lt;div class="col-md-6 mx-auto my-5"&gt; &lt;h1 class="display-4 font-weight-normal"&gt;TrendyEats&lt;/h1&gt; &lt;p class="lead font-weight-normal"&gt;The Latest Food Trends&lt;/p&gt; &lt;a class="btn btn-outline-secondary" href="#"&gt;Login&lt;/a&gt; &lt;p class="small pt-4"&gt;blablabla &lt;/p&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 水果 id 的 css 是:</p><pre> #fruit { background-image: url('/Pictures/fruits.jpg'); alt:"woops"; }</pre><p> “alt”也没有出现。</p><p> 任何帮助表示赞赏,谢谢!</p><p> 编辑:已修复,看来我必须写“Pictures/fruits.jpg”而不是“/Pictures/fruits.jpg”</p></div> - Why isn't the background image appearing inside my <div>? 为什么我的背景CSS图像没有出现在DIV中? - Why isn't my background CSS image appearing in DIV?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM