【发布时间】:2021-04-28 12:47:30
【问题描述】:
我一直在尝试使用 CSS 执行关闭按钮以关闭标签。 我使用此处描述的模式组合完成了此操作:Patterns for Closebuttons,以及用于创建 X Close Button using CSS 的纯 CSS 模式。
现在我的问题是: 我不明白为什么 X 不是完全居中的。 如果我将位置更改为我用于线条的任何宽度的 50%,它似乎很好,因此我得出的结论是它必须与线条的宽度有关。
我在这里写信是想看看是否有人可以向我解释一下。
'HTML
<button type="button" aria-label="Close">
<span aria-hidden="true" class="close"></span>
</button>
'CSS
button {
display: flex;
justify-content: center;
align-items: center;
font-weight: 700;
padding: 0px;
background-color: rgb(192,192,192);
border: none;
color: white;
text-decoration: none;
height: 150px;
width: 150px;
border-radius: 50%;
}
button:hover {
background-color: rgb(146, 146, 146);
cursor: pointer;
}
.close {
position: relative;
/* right: 10px; */
width: 60%;
height: 60%;
}
.close:before, .close:after {
position: absolute;
content: ' ';
height: 100%;
width: 20px;
background-color: rgb(255, 255, 255);
}
.close:before {
transform: rotate(45deg);
}
.close:after {
transform: rotate(-45deg);
}
为了简单起见,我创建了一个代码笔:Codepen link
【问题讨论】:
-
一切看起来都很好,但只需要正确定位线条。 i.stack.imgur.com/MxiYj.png
-
你只需要负补偿那些伪元素宽度的一半。