html - 建仓 :static (or :relative) element span the width of the page

标签 html css css-position

我想要一个用于广告的顶部“托盘”。它可以使用 javascript 脚本滑出页面边界。它下方的“分隔线”包含关闭图标,主要是装饰性的,因为当页面滚动超过一个点时它会固定。 (这是为了防止浏览器的主题搞砸页面设计)。

我的问题是在分隔符通过 javascript 获取 position:fixed 之前,它需要是 position:relativeposition:static,因为绝对定位会将其从页面流中移除。当我将它设置为 position:staticwidth:100% 时,有一个边距可以通过它显示背景图像。我希望它覆盖整个页面顶部。我已经搜索了解决方案,但找不到有关如何删除此边框的任何信息。使用 jsfiddle 更容易解释:顶部有红色边框的部分,当您向下滚动时,它正下方有一个分隔线固定到页面。我不知道如何让红色边框和分隔线(在它固定之前)跨越页面的宽度。

这是有问题的 HTML 片段:

    <div class = "adTray">

    <div class = "ad"></div>
    <div class = "ad"></div>
</div>

<div  class="topBorder"></div>

还有 CSS:

.adTray{
    border: 1px solid red;
    height:100px;
    width:100%;
}

#topBorderScrolled{
    position:fixed;
    z-index:100;
}
.topBorder{
    right:0;
    left:0;
    top:0;
    background: rgb(68,68,68); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(68,68,68,1) 0%, rgba(51,51,51,1) 50%, rgba(38,38,38,1) 50%, rgba(20,20,20,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(68,68,68,1)), color-stop(50%,rgba(51,51,51,1)), color-stop(50%,rgba(38,38,38,1)), color-stop(100%,rgba(20,20,20,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(68,68,68,1) 0%,rgba(51,51,51,1) 50%,rgba(38,38,38,1) 50%,rgba(20,20,20,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(68,68,68,1) 0%,rgba(51,51,51,1) 50%,rgba(38,38,38,1) 50%,rgba(20,20,20,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(68,68,68,1) 0%,rgba(51,51,51,1) 50%,rgba(38,38,38,1) 50%,rgba(20,20,20,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(68,68,68,1) 0%,rgba(51,51,51,1) 50%,rgba(38,38,38,1) 50%,rgba(20,20,20,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#444444', endColorstr='#141414',GradientType=0 ); /* IE6-9 */
    width:100%;
    height:15px;

    border:1px solid #111111;
}

我希望所有这些都有意义。

最佳答案

使用 reset.css 规范化所有样式,阅读此处可用的重置 CSS 之一:http://meyerweb.com/eric/tools/css/reset/ .

另一种快速的方法是将 body 上的边距设置为零,例如。 body {margin: 0} 但推荐的方法是使用 reset.css。

关于html - 建仓 :static (or :relative) element span the width of the page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23704684/

相关文章:

php - 以下邮件功能突然失效

html - 如何将 Jupyter Notebook 转换为适合 Wordpress 的 HTML

css - z-index和css Position :?有什么关系

javascript - 带有居中 Logo 的自定义引导导航栏

css - 绝对定位的播放按钮图标未在 Firefox 中显示

javascript - 使用 Space 值从 JSON 中过滤数组

css - 如何通过单选按钮的延迟使 CSS3 一个接一个地动画化?

css - 固定层滚动

css 困境(大背景)

javascript - 默认使用 xsl :when test? 返回所有元素