Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

html - 如何在更大的div内使图像中心(垂直和水平)居中[重复](How to make an image center (vertically & horizontally) inside a bigger div [duplicate])

I have a div 200 x 200 px.

(我有一个div 200 x 200像素。)

I want to place a 50 x 50 px image right in the middle of the div.

(我想在div的中间放置一个50 x 50像素的图像。)

How can it be done?

(如何做呢?)

I am able to get it centered horizontally by using text-align: center for the div.

(我可以通过使用text-align: center div的text-align: center使其水平居中。)

But vertical alignment is the issue..

(但是垂直对齐是个问题。)

  ask by Shameem translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Working in old browsers (IE >= 8)

(在旧的浏览器中工作(IE> = 8))

Absolute position in combination with automatic margin permits to center an element horizontally and vertically.

(绝对位置与自动边距相结合,可以使元素水平和垂直居中。)

The element position could be based on a parent element position using relative positioning.

(元素位置可以基于使用相对定位的父元素位置。)

View Result

(查看结果)

img {
    position: absolute;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...