我使用这个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与主要内容紧密相邻,无论屏幕分辨率如何,如第一张图片所示。有什么问题,我该如何解决?
答案 0 :(得分:3)
你在寻找这样的东西:
<强> 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;
}
答案 1 :(得分:0)
将.special_event
元素放入主要内容中,然后说出#main-content
并应用此CSS:
#main-content {
position:relative;
....
}
.special_event {
position:absolute;
width:52px;
left: -52px;
...
}
jQuery的:
$(window).scroll(function() {
var fixedTop = 100; // set to whatever you want
$('.special-event').css({'top':fixedTop + 'px'});
});
答案 2 :(得分:0)
您可以将position:fixed
与浮动使用。
对特殊事件div使用float:right
,在右侧使用主容器。然后为position:fixed
设置.special_event
。同时为right
设置.special_event
属性的适当值,使其触及主容器的左边框。
答案 3 :(得分:-1)
您可以使用jquery将特殊事件dom与主要内容对齐。使用jquery,你可以得到主要内容的offset.top,并且你可以设置你的special_event dom的顶部。
// + you main content top
function setTop(){
var offset = $('main_content_selector').offset();
var event_special_top = offset.top + 100; // here you can alter the 100 as per your need, it is used to center the special event dom relative to main content
$('.special_event').css('top', event_special_top);
}
setTop();
// event you can use the setTop with browser resize