kitty sarvaj
kitty sarvaj

Reputation: 527

Footer align image in center

I have image inside of that footer,I need to align image in center and need to bring top from the footer and it must adjust to all the mobile device.IF i give margintop the image is stay in same place but footer height reduce below is my code

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

Upvotes: 0

Views: 4454

Answers (2)

Hastig Zusammenstellen
Hastig Zusammenstellen

Reputation: 4440

centered with 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>

centered with 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>

centered with left: and 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>

Upvotes: 0

hoangfin
hoangfin

Reputation: 721

Use flex to achieve your goal, you can adjust footer height to whatever value you like but the image stay still in middle

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>

Upvotes: 1

Related Questions