Border Usage: Recommended
You can do this the same way as in the second fragment and use positioning as in the fragment below to avoid overlapping two div elements. Here the circles are created by pseudo-elements, and the overlapping part is cut out using overflow: hidden for their parents.
It should be noted here that any hover effect should be added to the pseudo-elements, and not to the parent elements. This is due to the fact that if :hover attached to the parent, then it will work even if it falls outside the circle (since the parent object is still a square).
Of all three solutions presented in this answer, this is the one that has the best browser support and will work even in IE8. Therefore, it is recommended.
.left, .right { position: relative; float: left; height: 200px; width: 200px; overflow: hidden; } .left:after, .right:after { position: absolute; content: ''; height: calc(100% - 12px); width: calc(100% - 12px); border-radius: 50%; border: 6px solid gray; } .left:after { right: -20px; } .right:after { left: -20px; }
<div class='left'></div> <div class='right'></div>
Using radial gradients:
If you donβt want to use pseudo-elements and overflow: hidden for the parent, you can also use radial-gradient background images to create a circle and arrange them so that they ultimately produce the desired effect. Below is an example of a fragment of this approach.
The disadvantage of this approach is the low browser support for radial-gradient . This will not work in IE9 and below. In addition, circles formed by radial gradients usually have jagged (rough edges), and when we change the stop position of the color to make it smoother, it is slightly blurred.
.left, .right { float: left; height: 200px; width: 200px; } .left { background: radial-gradient(circle at 70% 50%, transparent calc(50% - 4px), gray calc(50% - 2px), gray calc(50% + 2px), transparent calc(50% + 4px)); } .right { background: radial-gradient(circle at 30% 50%, transparent calc(50% - 4px), gray calc(50% - 2px), gray calc(50% + 2px), transparent calc(50% + 4px)); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script> <div class='left'></div> <div class='right'></div>
Using clips (CSS / SVG):
Another approach that can be used is to use clip-path . The advantage of this approach is that hover effects will only work when the cursor is within the circle (as can be seen from the fragment). This is due to the fact that unnecessary parts are cut off.
Downside again is poor browser support . The CSS version of clip-path supported only in Webkit, but not in Firefox, IE, while the SVG version (using the built-in SVG) is supported in Webkit, Firefox, but not in IE.
.left, .right { float: left; height: 200px; width: 200px; border-radius: 50%; border: 6px solid gray; } .left.css-clip { clip-path: polygon(0% 0%, 80% 0%, 80% 100%, 0% 100%); } .right.css-clip { margin-left: -86px; clip-path: polygon(20% 0%, 100% 0%, 100% 100%, 20% 100%); } .left.svg-clip { clip-path: url(#clipper-left); } .right.svg-clip { margin-left: -86px; clip-path: url(#clipper-right); } h3{ clear: both; } .left:hover, .right:hover{ background: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script> <h3>CSS Clip Path</h3> <div class='left css-clip'></div> <div class='right css-clip'></div> <h3>SVG Clip Path</h3> <div class='left svg-clip'></div> <div class='right svg-clip'></div> <svg width='0' height='0'> <defs> <clipPath id='clipper-left' clipPathUnits='objectBoundingBox'> <path d='M0,0 .8,0 .8,1 0,1z' /> </clipPath> <clipPath id='clipper-right' clipPathUnits='objectBoundingBox'> <path d='M.2,0 1,0 1,1 .2,1z' /> </clipPath> </defs> </svg>
Harry Nov 24 '15 at 8:31 2015-11-24 08:31
source share