css – 与他母亲的固定

我使用这个CSS在我的网页左侧显示一个固定的块.这是一个红色的方框,上面有白色的SPECIAL EVENT字样,由图像提供.

.special_event{
    display:block;
    position: fixed;
    width: 52px;
    height: 29px;
    background:url(../images/special_icon.png);
    left:10.5%;
}

但是当屏幕分辨率大于1200像素时,div会远离主要内容.

我希望special_event div保持紧邻主要内容,而不管屏幕分辨率如何,如第一张图片所示.有什么问题,我该如何解决?

最佳答案 你在寻找这样的东西:

position fixed is relative to any position that is not static, thus content & fixed element is always in center and relative to its parent

HTML

<div id="content_wrapper" class="border">
    <div id="special_event" class="border"></div>
    <div id="main_content" class="border">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
</div>

CSS

.border {
 border:1px solid red;   
}
#content_wrapper {
    width:800px;
    margin:0 auto;
    position:relative;
    left:50%;
    margin-left:-400px;
}
#special_event {
    display:block;
    position: fixed;
    width: 52px;
    height: 29px;
    top:0;
    margin-left:-52px;
    background:red;
}
点赞