简体   繁体   English

如何在一组元素周围显示倾斜的边框?

[英]How to display an angled border around a group of elements?

Not too sure if I can even do this with just CSS but here goes: 不太确定我是否可以仅使用CSS做到这一点,但是可以:

I want to use the border property (or similar) to display an angled border around a group of elements. 我想使用border属性(或类似属性)在一组元素周围显示倾斜的边框。 I suppose the best way to explain is just to show you: 我想最好的解释方式就是向您展示:

HTML: HTML:

<ul>
    <li>link</li>
    <li>link</li>
    <li>link</li>
</ul>

CSS: CSS:

li {
    border-top:1px solid black;
    border-bottom:1px solid black;
    border-right: /* ??? */ ;
}

Looks Like: 看起来像:

-----------------\
link              \
-------------------\
link                \
---------------------\
link                  \
-----------------------\

Preview: 预习: 在此处输入图片说明

As far as I know plain css can't do this. 据我所知,普通的CSS无法做到这一点。 What about canvas or jQuery? 画布或jQuery呢? Would there be something I could use there to help? 有什么可以帮助我的吗?

There's a pretty good tutorial to achieve something similar over at TutsPlus ( link ) that should do the trick, with a few tweaks (for example, adding classes and padding to make it appear as if it's moving further and further each time). 在TutsPlus( link )上有一个相当不错的教程来实现类似的目的,应该进行一些调整(例如,添加类和填充以使其看起来好像每次都在不断发展)。

Pretty sure this won't work reliably in all browsers though, especially IE. 可以肯定的是,尽管在所有浏览器中,特别是在IE中,它也无法可靠地运行。 If you want it to work cross-browser I'd recommend having the background image on the list item as the slanted line and then adding padding to each list item to make it appear to move outwards. 如果您希望它跨浏览器工作,我建议将列表项上的背景图像作为斜线,然后在每个列表项上添加填充以使其看起来向外移动。

You can try something like this with HTML5 canvas: 您可以使用HTML5 canvas尝试类似的方法:

<canvas id="myCanvas" width="578" height="200"></canvas>

Javascript: 使用Javascript:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.beginPath();
context.moveTo(10, 10);
context.lineTo(150, 50);
context.stroke();

http://jsfiddle.net/hescano/YxF9t/1/ http://jsfiddle.net/hescano/YxF9t/1/

More Info: http://www.html5canvastutorials.com/tutorials/html5-canvas-lines/ 更多信息: http : //www.html5canvastutorials.com/tutorials/html5-canvas-lines/

I suppose you could do something akin to this: http://jsfiddle.net/TKhLw/ 我想您可以做些类似的事情: http : //jsfiddle.net/TKhLw/

using: 使用:

ul {
    position: absolute;
    top: -130px;
    height: 500px;
    left: -100px;
    width: 350px;
    padding-top: 170px;
    overflow-x: hidden;
    -webkit-transform: rotate(-30deg);
}
ul > li {
    position: relative;
    width: 120%;
    padding-left: 160px;
    list-style: none;
    border: 1px solid #000000;
    -webkit-transform: rotate(30deg);
}

If you nested it deeper, you could get the links to line up as well. 如果您将其嵌套得更深,则也可以使链接对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM