html - 即使自动应用边距,图像也不居中

标签 html css margin margins

尝试使用 CSS 将图片移动到页面的中心。我将这些值应用于图像,但没有成功。见下面的代码:

HTML:

<div class="list_of_reviews">
<ul style="list-style-type:none">
<li>What will happen to Brittish travelers when Brexit finaly happens</li>
</ul>
</div>
<img class="image_1" src="for left column.jpg" alt="What will happen to Brittish travelers when Brexit finaly happens"/>

CSS:

.list_of_reviews {
    margin:80px auto;
    font-family: "Times New Roman", Times, Serif;
    line-height: 1.5;
    font-size: 30px;
    width: 40%;     
    border:solid;
}


.image_1 {
    margin:auto;
    height:200px;
    width:350px;
    position:relative;
}

非常感谢直截了当的回答,请不要降级。

最佳答案

要使 margin: 0 auto 正常工作,您需要添加 display: block

.list_of_reviews {
  margin: 80px auto;
  font-family: "Times New Roman", Times, Serif;
  line-height: 1.5;
  font-size: 30px;
  width: 40%;
  border: solid;
}
.image_1 {
  display: block;
  margin: auto;
  height: 200px;
  width: 350px;
  position: relative;
}
<div class="list_of_reviews">
  <ul style="list-style-type:none">
    <li>What will happen to Brittish travelers when Brexit finaly happens</li>
  </ul>
</div>
<img class="image_1" src="http://placehold.it/350x200" alt="What will happen to Brittish travelers when Brexit finaly happens" />


编辑:

其实...

Don't change the default behavior of an element if you can avoid it. Keep elements in the natural document flow as much as you can.


由于默认情况下图像的行为类似于文本,因此最好这样做:

.list_of_reviews {
  margin: 80px auto;
  font-family: "Times New Roman", Times, Serif;
  line-height: 1.5;
  font-size: 30px;
  width: 40%;
  border: solid;
}
figure {
  text-align: center;
}
.image_1 {
  height: 200px;
  width: 350px;
  position: relative;
}
<div class="list_of_reviews">
  <ul style="list-style-type:none">
    <li>What will happen to Brittish travelers when Brexit finaly happens</li>
  </ul>
</div>
<figure>
  <img class="image_1" src="for left column.jpg" alt="What will happen to Brittish travelers when Brexit finaly happens" />
</figure>

关于html - 即使自动应用边距,图像也不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089363/

相关文章:

jquery - div底部的对齐按钮

javascript - PreventDefualts 不允许在 Invisible ReCaptcha 中提交表单

html - 我怎样才能让我的页面居中,但把它放在底部

jquery - GWT ClientBundle CSS 不使用相对路径来引用图像

javascript - 将元素的宽度设置为等于监视器大小而不改变缩放

html - 间隔不产生边距

css - 在 Bootstrap 中为元素添加顶部空间的方法

html - 图像有一个很大的 margin-left 没有设置

jquery - Bootstrap 使 Div 在列中彼此相邻 float

javascript - jQuery 鼠标悬停淡入淡出效果