CSS-剪辑图像并添加边框以适合div [英] css - clip image and add border to fit in div
本文介绍了CSS-剪辑图像并添加边框以适合div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望从src图像中裁剪一个80px的圆圈,在所裁剪的部分周围添加一个红色边框(圆圈),然后将其放入一个div框.
I wish to clip a 80px circle from image src to add a red border (circle) around the clipped portion then fit to a div box.
下面是一个示例,但红色边框不起作用.我希望剪切后的部分仅适合div,但现在div的大小是原始图像的大小.
Below is a example but the red border not work. I would like the clipped portion to fit to the div only but now the div size is the original image size.
.my-clip {
position: absolute;
clip-path: circle(80px at 50% 25%);
border:1px solid red;//wish to get a circle border around the clipped portion
background-color: #bdbdbd;
box-sizing: border-box;
}
.square {
position: relative;
width: 300px;
height: 300px;
overflow: hidden;
border: 1px solid red;
}
<div class="square">
<img class="my-clip" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Tux.svg/300px-Tux.svg.png" alt="alternatetext">
</div>
更新:我尝试使用calc函数更改位置,但看起来数学不正确!
Update: I try to use calc function to change the position but looks like the math is not correct!
.my-clip {
position: absolute;
--x: 150px;
--y: 100px;
left: calc(80px + 5px - var(--x));
top: calc(80px + 5px - var(--y));
clip-path: circle(80px at var(--x) var(--y));
background-color: #bdbdbd;
}
.square {
position: relative;
width: 170px;
height: 170px;
overflow: hidden;
border: 5px solid lightgray;
border-radius:50%;
}
<div class="square">
<img class="my-clip" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Tux.svg/300px-Tux.svg.png" alt="alternatetext">
</div>
推荐答案
调整不同的值,您可以使用border-radius做到这一点:
Adjust the different values and you can do it with border-radius:
.my-clip {
position: absolute;
left: 50%;
top: -6%;
transform: translate(-50%);
/*clip-path: circle(80px at 50% 25%); no more needed*/
background-color: #bdbdbd;
}
.square {
position: relative;
width: 160px;
height: 160px;
overflow: hidden;
border: 1px solid red;
border-radius: 50%;
}
<div class="square">
<img class="my-clip" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Tux.svg/300px-Tux.svg.png" alt="alternatetext">
</div>
这篇关于CSS-剪辑图像并添加边框以适合div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文