当父元素居中时,如何将元素拉伸到页面的左边缘?

时间:2011-08-10 14:20:00

标签: html css xhtml

我需要定位一个50px高的颜色,但它从中间容器到页面的左端。

是否有一个位置左宽100%类型选择器?

这是一个例子,所以你知道我的意思......

enter image description here

该网站适合960px宽,但纯色需要在左侧拉伸。

2 个答案:

答案 0 :(得分:2)

请参阅: http://jsfiddle.net/thirtydot/HjrcT/

li, li:before {
    height: 25px;
}
li {
    position: relative;
    width: 200px;
    background: #777
}
li:before {
    content: ' ';
    display: block;
    position: absolute;
    width: 9999px;
    left: -9999px;
    top: 0;
    background: #f0f
}

IE7 does not support :before,因此如果您需要在该地狱浏览器中使用它,:before必须替换为<span>http://jsfiddle.net/thirtydot/HjrcT/1/

答案 1 :(得分:0)

这是一种稍微不同的方法,使用位于容器后面的50%宽度div。条形图的颜色与菜单div对齐,并产生一种相同的错觉。

http://jsfiddle.net/F74cT/1/