简体   繁体   English

无法在div内居中放置图像?

[英]Cant center an image inside of a div?

So basically, I am trying to center a div with an image inside of a div with left and right divs and the middle div won't center. 所以基本上,我正在尝试将div的图像居中放置在具有左右div的div内,而中间div不会居中。 I have tried positioning absolute and setting left:0 and right:0 我尝试定位绝对并设置left:0right:0

here is the structure of the html logoLeft is float:left and logoRight is float:right . 这是html的结构logoLeft是float:left而logoRight是float:right

发布图片

Thank you. 谢谢。

First of all, I added an external div to align the items 首先,我添加了一个外部div来对齐项目

<div class="flex">
  <div class="logoLeft"></div>
  <div></div>
  <div class="logoRight"></div>
</div>

In css we do the magic 在CSS中,我们做魔术

.flex{
  display: flex;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  justify-content: space-between;
  -webkit-justify-content: space-between;
}

You could also use display: table with a container : 您还可以使用display: table带容器的display: table

<div class="table">
  <div class="logoLeft"></div>
  <div></div>
  <div class="logoRight"></div>
</div>

and the CSS : 和CSS:

.table {
  display: table;
}
.table > div {
  display: table-cell;
  text-align: center;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM