页脚在中心对齐图像

时间:2017-05-16 08:12:26

标签: html css html5 css3

我在该页脚中有图像,我需要将图像对齐在中心,需要从页脚顶部调整,并且必须调整到所有移动设备。我给 margintop 图像是保持在同一个地方,但页脚高度降低以下是我的代码

<footer data-role="footer">
    <center>
 <img src="images/image1.png" style="width:40px;height:40px;margin-top:20px"/>
      </center>
      </footer>

2 个答案:

答案 0 :(得分:1)

使用flex来实现你的目标,你可以将页脚高度调整到你喜欢的任何值,但图像仍保持在中间

footer {
  position: relative;
  height: 100px;
  background-color: violet;
}

img {
  position: absolute;
  left: 50%;
  bottom: -20px;
  width: 40px;
  height: 40px;
  margin-left: -20px;
}
<footer data-role="footer">
    <img src="https://www.w3schools.com/css/trolltunga.jpg" />
      </footer>

答案 1 :(得分:0)

以flexbox为中心

body {  /* ignore, for demo */
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}
footer {
  display: flex;
  justify-content: center; /* horizontal align */
  width: 100%;
  background-color: black; /* ignore, for demo */
}
img {
  height: 40px; width: 40px;  /* ignore, for demo */
  transform: translateY(50%);
}
<footer data-role="footer">
 <img src="images/image1.png">
</footer>

以text-align为中心

body {  /* ignore, for demo */
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}
footer {
  width: 100%;
  text-align: center;
  background-color: black; /* ignore, for demo */
}
img {
  display: inline-block; /* centered by text-align on parent */
  height: 40px; width: 40px;  /* ignore, for demo */
  transform: translateY(50%); /* half below footer */
  /* overflow: hidden; turn this on if hiding the bottom half */
}
<footer data-role="footer">
 <img src="images/image1.png">
</footer>

以left为中心:和transform:translate

body {  /* ignore, for demo */
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}
footer {
  width: 100%;
  background-color: black; /* ignore, for demo */
}
img {
  position: relative;
  left: 50%; transform: translate(-50%); /* centered */
  bottom: -20px; /* half below bottom, based on img height */
  height: 40px; width: 40px;
}
<footer data-role="footer">
 <img src="images/image1.png">
</footer>