I'm interested if it's possible to create wrapped (or maybe better said twisted) border using CSS. Effect I wanted to achieve is in the picture.
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
Most easiest and neat solution would be to use
svg
to create the border.You could even spice it up with some curves using quadratic curves.
You could easily add a drop shadow effect.
Alternatively you could always use
:after
and:before
:pseudo-elements.The
width
andheight
of the:after
and:before
:pseudo-elements were calculated using some basic trigonometry.The
opposite side
is thewidth
andheight
of the:after
and:before
:pseudo-elements. The one on the left is given top and right borders and the one on the right is given top and left borders. Then, the one on the left has been rotated45deg
and the one on the right has been rotated-45deg
.Yes, you can do this purely in CSS by manipulating the
:before
and:after
psuedo elements.The key advantages are that you can keep your HTML 'as is', and it maintains a strict seperation of concerns between content (
html
) and styling (CSS
).This is a different approach using pure CSS alone to achieve this effect (using the method explained in this answer but reverse rotation of elements).
Basically it involves two pseudo elements that are rotated with a bit of perspective and positioned one below the other to achieve the shape.
This approach works like below:
:before
and:after
which are roughly about half the size (including borders) of the main.button
element. The height of each pseudo-element is 35.5px + 3px border on one side (top/bottom) and 1.5px on the other side (because the two overlap at half distance).:before
element whereas the bottom half is achieved using the:after
element.rotateX
with perspective to achieve the tilted effect and positioning to place the two elements such that they form the expected shape.As-is, this would degrade quite well in IE 8 and IE 9 into a square button with borders. However, due to the nullification of one
border
(border-bottom
for:before
andborder-top
for:after
) it would leave a white area (resembling a strike-through line) in the middle. This can be overcome by adding a couple of IE < 10 specific styles using conditional comments like in this demo.Output Screenshots from IE 9 and IE 8: (both normal and during hover)