如何使用css将图像移动到div的中心?

时间:2017-10-02 05:07:41

标签: html css

我尝试将margin-left : 25%;添加到课程.four但不起作用。我该怎么办?

.one{
  width: 100%;
}
.two{
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    float: left;
    position: relative;
    width: 100%;
    margin-bottom: 15px;

}
.three{
    float: left;
    list-style: none;
    color: #333;
    font-size: 19px;
    width: 100%;
    background: #fff;
    border: 1px solid #ccc;
    padding: 10px 0px;
}
.four{
    margin-left: 10px;
    width: 246px;
    height: 138px;
    margin-right: 16px;
    display: inline-block;
    position: relative;
    float: left;
}
.five{
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    height: 100%;
    margin-left: auto;
    margin-right: auto;
    overflow: hidden;
    display: block;
    cursor: pointer;
}
.six{
    transform: translateY(-50%);
    top: 50%;
    left: 0;
    background-color: transparent;
    display: block;
    position: absolute;
}
.seven{
    width: 246px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    max-width: unset;
}
<div class="one">
    <ul class="two">
        <li class="three">   
            <div class="four">  
                <a class="five" href="">
                    <div class="six">
				        <img class="seven" src="http://kawamono.com/img/cms/Hobby/Coin%20Bank/x100828-sanrio-hellokitty-mini-bank-ylw-SET.jpg.pagespeed.ic.iSCJA9hZBG.jpg">
                    </div>
                </a>
            </div> 
        </li>
    </ul>
</div>

2 个答案:

答案 0 :(得分:0)

.four的css更改为:

.four {
  width: 246px;
  height: 138px;
  margin: 0 auto;
  position: relative;
}

.one {
  width: 100%;
}

.two {
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  float: left;
  position: relative;
  width: 100%;
  margin-bottom: 15px;
}

.three {
  float: left;
  list-style: none;
  color: #333;
  font-size: 19px;
  width: 100%;
  background: #fff;
  border: 1px solid #ccc;
  padding: 10px 0px;
}

.four {
  width: 246px;
  height: 138px;
  margin: 0 auto;
  position: relative;
}

.five {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: 100%;
  margin-left: auto;
  margin-right: auto;
  overflow: hidden;
  display: block;
  cursor: pointer;
}

.six {
  transform: translateY(-50%);
  top: 50%;
  left: 0;
  background-color: transparent;
  display: block;
  position: absolute;
}

.seven {
  width: 246px;
  display: block;
  margin: 0 auto;
  max-width: unset;
}
<div class="one">
  <ul class="two">
    <li class="three">
      <div class="four">
        <a class="five" href="">
          <div class="six">
            <img class="seven" src="http://kawamono.com/img/cms/Hobby/Coin%20Bank/x100828-sanrio-hellokitty-mini-bank-ylw-SET.jpg.pagespeed.ic.iSCJA9hZBG.jpg">
          </div>
        </a>
      </div>
    </li>
  </ul>
</div>

我注意到您想要使用float:left .four来对图像居中,这与您期望的结果相冲突。我将您的边距更改为margin:0 auto;以正确地将其水平居中

答案 1 :(得分:0)

您可以添加:

width: 100%;

为.four和.six设置样式,这会使图像侧向居中。那些div没有搭配,所以图像不会居中。

希望这会有所帮助 Oel Kristian Ek Hornnes

相关问题