开发者

Centre elements positioned using CSS fixed positioning

Not able to centre elements positioned using CSS "top and le开发者_StackOverflow社区ft and postion:fixed attributes".


Position fixed places the element in reference to the browser, not parent elements. See this excerpt regarding position:fixed -

Generates an absolutely positioned element, positioned relative to the browser window. The element's position is specified with the "left", "top", "right", and "bottom" properties

From here

You likely want to use position: relative, but without source it's hard to know for sure.


Add the class centee to the thing you want to center. Wrap it around a container with class centerer. The container will not be visible.

HTML

<div class="centerer">
 <div class="centee"></div>
</div>

CSS

.centerer {
 position: fixed;
 left: 50%;
 height: 50%;
 width: 1px;
 height: 1px; 
}

 .centee {
  position: absolute;
  width: 555px;
  height: 333px;
  margin-left: -50%;
  margin-top: -50%;
  background: blue;
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜