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
563 views
in Technique[技术] by (71.8m points)

css - 如何在div中将绝对定位的元素居中?(How to center absolutely positioned element in div?)

I need to place a div (with position:absolute; ) element in the center of my window.

(我需要在窗口中心放置一个div (带有position:absolute; )元素。)

But I am having problems doing so, because the width is unknown .

(但是我遇到了问题,因为宽度未知 。)

I tried this.

(我试过了)

But it needs to be adjusted as the width is responsive.

(但是,由于宽度是响应性的,因此需要对其进行调整。)

.center {
  left: 50%;
  bottom:5px;
}

Any ideas?

(有任何想法吗?)

  ask by Ish translate from so

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

1 Reply

0 votes
by (71.8m points)

This works for me:

(这对我有用:)

 #content { position: absolute; left: 0; right: 0; margin-left: auto; margin-right: auto; width: 100px; /* Need a specific value to work */ } 
 <body> <div> <div id="content"> I'm the content </div> </div> </body> 


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

1.4m articles

1.4m replys

5 comments

57.0k users

...