开发者

How to create a right border that's only 70% of its actual size?

So, I have a sidebar on my site. It can vary dynamically in height. I want to have a 1px wide border to the right, but I don't want it开发者_如何学编程 to be as tall as the container; it should only be 70% that height. Also, it should be vertically centered to the middle of the container.

How can I do this? Most of the ways I've seen require the border's height to be defined, but I am not able to do that. Is this possible with just CSS? If not, how can I use JavaScript to perform this? Thanks!


I've got an idea, it's supported by FF6 and IE9 + Chrome and Opera 11:

html

<div id="container">
    <div class="border_r"></div>
    contents
</div>

css

#container {
    height: 356px;
    background: #eee;
    position: relative;
}
.border_r {
    border-right: 2px solid red;
    height: 70%;
    position: absolute;
    right: 0px;
    top: 15%;
}

jsFiddle ... I have no idea if it will work anywhere else


You can create a "pseudo-border" using CSS Pseudo-elements. Though the browser support (particularly in the IE department) is not exactly stellar, it's the more semantic (and recommended, if you can drop <IE7 support) way to do it.

Instead, if you don't mind the extra non-semantic element, you can simply do it using an extra <div> inside of your sidebar:

HTML

<div id="example-sidebar">
    <div id="example-border"></div>
    <ul>
        <li>a</li>
        <li>b</li>
        <li>c</li>
    </ul>
</div>

CSS

#example-sidebar{
    background-color: yellow;
    color: black;
    height: 400px;
    margin-left: 1px;   /* Required for 70% height border */
    position: relative; /* Required for 70% height border */
    width: 150px;
}
#example-border{
    background-color: red;
    height: 70%;
    position: absolute;
        left: -1px;
        top: 15%;
    width: 1px;
}


Try this:

.box:before
{
    width: 1px;
    height: 70%;
    position: relative;
    float: right;
    background-color: black;
    display: block;
    content: '';
}

http://jsfiddle.net/uxNar/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜