繁体   English   中英

将div居中并将其放在页面顶部(仅CSS!)

[英]Center a div and place it at the top of the page (CSS only!)

我一直在寻找,但是找不到任何好的帮助。 这很简单-我想将div放在页面的绝对顶部,然后将其居中。 当我做position: fixed我也写了top:0pxleft:0px ,然后弄乱了位置。 救命?

您需要先设置宽度,然后设置margin: auto ,使其水平居中。

 .container { position: relative; background: #eee; } .positioned { position: fixed; top: 0; left: 0; right: 0; width: 50%; margin: auto; background: #ccc; text-align: center; } 
 <div class="container"> <p>Some text</p> <p>Some text</p> <p>Some text</p> <p>Some text</p> <div class="positioned">Content here</div> </div> 

使用position:fixedtop:0px将div放在顶部,将left:50%在div中间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM