Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the vertical center of the picture in div by css

Shulou Source: shulou.com Published: 2022-06-01 08:39:30 09月11日 Update

This article introduces the relevant knowledge of "how to achieve the vertical center of the picture in the div by css". In the operation of the actual case, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

HTML code

Method one

Using position and margin to realize

Child elements are positioned relative to the div by setting an absolute positioning attribute on the parent element

Relative retains its original position for positioning and is relative to its original location.

Absolute is located away from the original location, relative to the nearest parent with location; if there is no parent element to be located, it is located relative to the document

Note: to make the top and bottom of the child element 0, then setting margin:auto will automatically center vertically

The code is as follows

Div {

Width:200px

Height:200px

Border:1pxsolid#ccc

Position:relative;// parent element sets absolute positioning

}

Img {

Position:absolute;// relative positioning

Width:80px

Height:50px

Top:0

Left:0

Right:0

Bottom:0

Margin:auto;// centers it vertically

}

This is the end of the content of "how to achieve the vertical center of the picture in the div by css". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Positioning element location picture code content more knowledge practical up and down up and down learning next up and down dilemma actual attribute situation document article method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux Shulou Technology Shulou Tech Info Microsoft