我通常使用包装容器元素并使用margin: 0 auto;
来获得两侧灵活的扩展边距。在目前的情况下,我想要一个元素伸展到页面的一个边界。我将它的大小设置得更大,宽度大约为999em,然后用overflow
剪切overflow-x:hidden
。
http://codepen.io/rpkoller/pen/omueg
问题是我已经了解到overflow
属性是一种错误,特别是在基于webkit的浏览器中并且完全被忽略。但是现在我试图完成任务,但是有点迷失和想法。我在codepen中创建了一个示例:
http://codepen.io/rpkoller/pen/fyLrn
我用于伪元素版本的代码是:
<section>
<div class="wrap">
<div class="box"></div>
</div>
</section>
css看起来像
.wrap {
background-color:red;
width: 50rem;
margin: 0 auto;
height: 40rem;
}
section {
padding-top:4rem;
width:100%;
background-color: green;
height:40rem;
}
.box {
height: 3.5rem;
line-height: 3.5rem;
position:relative;
text-align: left;
top: 0;
left:17.75rem;
padding-left: 1rem;
background-color:grey;
width:15rem;
&:after {
content: "";
position:absolute;
background-color:blue;
top:0;
bottom:0;
width: 35rem;
left:100%;
}
}
但我也可以完全删除:after
伪元素,并在.box
类上设置更大的宽度。但无论哪种方式,它都无助于将元素跨越到右边界。是否有可能将灰色框(没有伪元素)或蓝色框(带有伪元素)一直拉伸到页面的右边界?
答案 0 :(得分:0)
如果我理解你想要这样的东西:http://jsfiddle.net/xckL79he/4/?
我使用第二个div来做那个,因为主要的问题是.box有一个定义的宽度所以伪元素在宽度上会有相同的:100%; 第一个解决方案是伪,第二个是背景渐变(我认为不太安全)。
这是小提琴的代码
HTML
<section>
<div class="wrap">
<div class="boxContainer">
<div class="box">Plop</div>
</div>
<br /><br />
<div class="boxContainerGradient">
<div class="box">Plop</div>
</div>
</div>
</section>
CSS
section {
padding-top:4rem;
width:100%;
background-color: green;
height:40rem;
}
.wrap {
background-color:red;
width: 50rem;
margin: 0 auto;
height: 40rem;
}
.boxContainer{
position:relative;
}
.boxContainer::before {
content: "";
position:absolute;
background-color:blue;
top:0;
right:0;
width: 50%;
height:100%;
}
.box{
height: 3.5rem;
line-height: 3.5rem;
position:relative;
text-align: left;
margin:auto;
width:15rem;
}
.boxContainerGradient{
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIxMDAlIiB5Mj0iMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzAwMDBmZiIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjQ5JSIgc3RvcC1jb2xvcj0iIzAwMDBmZiIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjUwJSIgc3RvcC1jb2xvcj0iIzAwMDBmZiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiMwMDAwZmYiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(left, rgba(0,0,255,0) 0%, rgba(0,0,255,0) 49%, rgba(0,0,255,1) 50%, rgba(0,0,255,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(0,0,255,0)), color-stop(49%,rgba(0,0,255,0)), color-stop(50%,rgba(0,0,255,1)), color-stop(100%,rgba(0,0,255,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left, rgba(0,0,255,0) 0%,rgba(0,0,255,0) 49%,rgba(0,0,255,1) 50%,rgba(0,0,255,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left, rgba(0,0,255,0) 0%,rgba(0,0,255,0) 49%,rgba(0,0,255,1) 50%,rgba(0,0,255,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left, rgba(0,0,255,0) 0%,rgba(0,0,255,0) 49%,rgba(0,0,255,1) 50%,rgba(0,0,255,1) 100%); /* IE10+ */
background: linear-gradient(to right, rgba(0,0,255,0) 0%,rgba(0,0,255,0) 49%,rgba(0,0,255,1) 50%,rgba(0,0,255,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000ff', endColorstr='#0000ff',GradientType=1 ); /* IE6-8 */
}