我想要一个保持页面水平中心的组件(两列),我有一个子组件(右列),我希望它的位置是固定的,所以子组件的位置是固定的,但整个两列要居中。
#content {
width: 1200px;
height:auto !important;
height:100%;
min-height:100%;
padding-top: 42px;
padding-bottom: 100px;
margin-auto: 0 auto;
position: relative;
}
#left {
width: 700px;
float: left;
}
#right {
width: 500px;
position: fixed;
top: 0px;
}
发布于 2010-07-13 05:54:21
你不能用margin:auto
做到这一点,但你可以这样做:
#CSS-SELECTOR-YOU-ARE-USING{
background:#FF0000; // Just so you can see whats going on
position:fixed; // Position the element
right:50%; // Move it to the right for 50% of parent element
margin-right:-250px; // You need here to put 1/2 of what you have below in width
width:500px;
}
这样,您可以将元素向右移动50%,然后将其移回宽度的一半。这样,您就可以使用position:fixed
获得居中元素。
发布于 2014-08-19 19:01:45
如果设置了left
和right
,则可以将margin: 0 auto
与position: fixed
一起使用。
.wrapper {
position:fixed;
top: 0;
left: 0;
right: 0;
width: 500px;
margin: 0 auto;
}
这也适用于position: absolute;
和垂直方向。
发布于 2014-05-02 05:48:34
我喜欢使用包装器来解决这个问题:
.wrapper {
position: fixed;
width: 100%;
top: 0px;
}
.wrapper .right {
width: 500px;
margin: auto;
}
https://stackoverflow.com/questions/3232514
复制相似问题