1px 共享:将边框悬停在两个元素上
两个彼此相邻的 inline-block
元素。
.e{border:1px #ccc solid}
.e:hover{border-color:#555}
我想要的是将它们之间的 1px
+1px
边框减少为共享的 1px
边框。
举例说明。
---------
| | |
---------
选择第一个元素。
+++++-----
+ + |
+++++-----
选择第二个元素。
-----+++++
| + +
-----+++++
通过将 border-right
或 border-left
设置为 ,可以轻松地将
,但是当选择任一元素时如何保持 2px
边框减少到 1px
>01px
共享边框呢?
没有 JavaScript。
Two inline-block
elements next to each other.
.e{border:1px #ccc solid}
.e:hover{border-color:#555}
What I'd like is to reduce the 1px
+1px
border between them to a shared 1px
border.
To illustrate.
---------
| | |
---------
Select first element.
+++++-----
+ + |
+++++-----
Select second element.
-----+++++
| + +
-----+++++
It's simple to reduce the 2px
border to 1px
by setting either border-right
or border-left
to 0
, but how to keep the 1px
shared border when either element is selected?
Without JavaScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以给它们一个 -1px 的左边距,以使它们的边框重叠,然后在第一个上撤消该边距。然后在悬停时调整
z-index
(并且不要忘记position:relative
以使z-index
起作用)。像这样的东西:演示: http://jsfiddle.net/ambigously/XTzqx/
您可能需要根据 HTML 的结构,稍微调整一下
:first-child
;如果:first-child
或另一个伪类不起作用,还有其他几个选项:中,并使用
padding-left: 1px
围绕margin-left: -1px
进行拼凑。margin-left: 0
的类添加一个额外的类。You could give them a -1px left margin to get their borders overlapping and then undo that margin on the first one. Then adjust the
z-index
on hover (and don't forgetposition: relative
to make thez-index
work). Something like this:Demo: http://jsfiddle.net/ambiguous/XTzqx/
You might need to play with the
:first-child
a bit depending on how your HTML is structured; a couple other options if:first-child
or another pseudo-class won't work:<div>
withpadding-left: 1px
to kludge around themargin-left: -1px
.margin-left: 0
.使
:hover
状态具有 2px 边框,并在两侧赋予-1px margin
。假设您不必关心每个浏览器,则对:first-child
和last-child
进行例外处理……我正在看您 IE6/7Make the
:hover
state have a 2px border and give it-1px margin
on both sides. Make exceptions for:first-child
andlast-child
assuming you don't have to care about every browser out there… I'm looking at you IE6/7