How To Center An Image In The Middle Of A Div?
I need to center an image in a middle of a div. In the example below the image is centered, but not in the middle. https:/
Solution 1:
Simple and easy method to do this,
.test {
background-color: orange;
width: 700px;
height: 700px;
display:flex;
align-items:center;
justify-content:center;
}
<divclass="test"><imgsrc="http://via.placeholder.com/350x150"></div>
Solution 2:
To vertically center your div, you can use positioning. Just apply
position: relative;
top: 50%;
transform: translateY(-50%);
to your image, and it will be vertically centered.
.test {
background-color: orange;
width: 700px;
height: 700px;
text-align: center;
}
.test>img {
position: relative;
top: 50%;
transform: translateY(-50%);
}
<divclass="test"><imgsrc="http://via.placeholder.com/350x150"></div>
Solution 3:
You can use the simplest way -> display: table-cell; which allows you to use vertical-align attribute
.test {
background-color: orange;
width: 500px;
height: 300px;
text-align: center;
display: table-cell;
vertical-align: middle;
}
<divclass="test"><imgsrc="http://via.placeholder.com/350x150"></div>
Solution 4:
You can use display: flex;
.test {
display: flex;
justify-content: center;
background-color: orange;
width: 700px;
height: 700px;
}
.testimg {
align-self: center;
}
Solution 5:
Cleanest solution would be to make your div display:flex
and align/justify content to center.
.test {
background-color: orange;
width: 700px;
height: 700px;
display: flex;
align-items: center;
justify-content: center;
}
Your updated Fiddle: https://jsfiddle.net/y9j21ocr/1/
Post a Comment for "How To Center An Image In The Middle Of A Div?"