Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to center the photo vertically by css

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to center photos vertically". In the operation of practical cases, many people will encounter such a dilemma, so 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!

Method: 1, add relative positioning style to the parent element of the photo element, add absolute positioning style to the photo element; 2, use the top attribute and "margin-top" attribute to set the photo vertical center, just add the "top:50%;margin-top: upper margin value;" style to the photo element.

The operating environment of this tutorial: windows7 system, CSS3&&HTML5 version, Dell G3 computer.

How does css center the photo vertically?

Using absolute positioning to realize vertical centering

1. Add a relative positioning attribute (position: relative) to the parent element of the img, and an absolute positioning attribute (position: absolute) to the child element, that is, the picture img element.

2. Set the top attribute of the picture element to 50%.

3. Now we need to set a negative margin-top value for the img element, which is half the height of the element you want to center vertically. * if you are not sure the height of the element, you can use the transform:translateY (- 50%) attribute instead of margin-top.

Remember: if you want to center horizontally at the same time, you can do it with the same technique as vertical centering.

Examples are as follows:

.posdiv {width: 300px; height: 250px; position: relative; border:1px solid red;}. Posdiv img {width: 100px; position: absolute; top: 50%; margin-top:-50px;}

Output result:

That's all for "how to center photos vertically with css". Thank you for 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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report