How can I create a CSS border on a diagonal element

余生长醉 提交于 2019-12-10 17:47:36

问题


Here is an example. http://jsfiddle.net/52c7t/

Simply: I'm trying to get the div on the right side, to have a border like the div on the left. (I'd want the border to be on the left side of the right div)

I tried a million different combinations and haven't been able to do it. I was trying to avoid making an image and do this with css.

Thanks for your help!

UPDATE:

Image of what I mean. Sorry about my graphic design skills :P

http://i.imgur.com/pGSnL.png

HTML

<div id = "top_bar">
        <div  id="top_left_button" >border</div>
        <div  class = "trapezoid"> none </div>
</div>​

CSS

.trapezoid{
    vertical-align: middle;
    position:absolute;
    border-bottom: 60px solid blue;
    border-left: 45px solid transparent;
    border-top-left-radius:30px;
    *border-top-right-radius:15px;
    *border-bottom-right-radius:3px;
    height: 0;
    width: 50px;
    display: inline-block;
    right:1px;
}



#top_bar{
    background-color: #000;
    border-bottom: 1px solid #666;
    color: #222;
    position:fixed;
    left:0px;
    top: 0px;
    width:100%;
    overflow:hidden;
    height: 50%;
    font-weight: normal;
    white-space: nowrap;
    color: white;
    z-index:20; 
    line-height: 45px;
    min-width:320px;
    max-width: 320px;
    max-height:48px;
    border-radius: 5px;
    text-shadow: rgba(0,0,0,0.6) 0px -1px 0px; 
}

#top_bar:after {
    content: '';
    width: 10%;
    display: inline-block;
    font-size: 0;
    line-height: 0
}

#top_title, #top_left_button, #notifications, #top_right_button {
    color: white;
    height: 100%;
    overflow:hidden;
    display: inline-block;
    text-align: center;
    vertical-align: middle;
}
#top_left_button,#top_right_button{
    width: 20%;
    background: rgba( 100, 255, 255, .1 );
}


#top_left_button{
    border-right: 2px solid #666;

}​

EDIT: UPDATED LINK


回答1:


The simple solution is to create another div since your blue div is already made up using the border property.

That new div is essentially a clone of the blue div, but will be colored red and made a little larger using the CSS width property. This becomes a pseudo border for the blue div.

Example of new div:

.trapezoid-border{
    vertical-align: middle;
    position:absolute;
    border-bottom: 60px solid red;        /* Color Changed will be pseudo-border color */
    border-left: 45px solid transparent;
    border-top-left-radius:30px;
    *border-top-right-radius:15px;
    *border-bottom-right-radius:3px;
    height: 0;
    width: 53px;                       /* Extra 3 pix when compared to .trapezoid class width */
    display: inline-block;
    right:1px;
}

jsFiddle DEMO




回答2:


Frankly, I think you should be using an image for this, but if you really want or have to avoid that, a somewhat dirty (though I think very convincing looking) fix would be to create a fixed sized red <div>, that you position and rotate (using the transform property) just right to achieve the appropriate effect.

.redborder {
    background-color:red; 
    width:3px;
    height:70px;
    transform:rotate(37deg);
    -ms-transform:rotate(37deg);
    -moz-transform:rotate(37deg);
    -webkit-transform:rotate(37deg);
    -o-transform:rotate(37deg);
    position:absolute;
    right:70px;
    top:-10px;
}

On jsfiddle: http://jsfiddle.net/QBTpV/18/

(tested in Chrome and IE)



来源:https://stackoverflow.com/questions/13782432/how-can-i-create-a-css-border-on-a-diagonal-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!