html - 如何在 div 中居中伪元素 ::after 边框?
问题描述
我要问你。我正在尝试使用伪元素(之后)创建边框。我想在我创建的 div 框的中间放置一个边框,而不设置(顶部,底部)和(左,右)。中间可以自动化吗?
<div class="box">
</div>
.box{
height:500px;
width:300px;
background-color:red;
position:relative;
&::after{
content:'';
position:absolute;
border:2px solid #fff;
height:90%;
width:90%;
}
}
解决方案
我已经使用百分比top
和left
而不是更正定位,transform: translate(-50%, -50%);
所以它在中间
.box{
height:500px;
width:300px;
background-color:red;
position:relative;
}
.box::after{
content:'';
position:absolute;
border:2px solid #fff;
height:90%;
width:90%;
left: 50%;
transform: translate(-50%, -50%);
top: 50%;
}
<div class="box"></div>
推荐阅读
- python-3.x - 在 Tkinter Python3 的列表中使用多个 StringVar() 变量
- python - sklearn Logistic Regression - 使用来自外部测试数据的自定义输入进行预测
- javascript - 可能无法导出受污染的“OffscreenCanvas”
- excel - Excel中TextFrame中的行距
- wordpress - 如何删除wordpress中的评论栏
- javascript - 进度条在达到 100% 后未完全占据
- javascript - 来自另一个网站 VLC 的实时视频流
- python-3.x - 找到总和等于 m 的第 k 个最小数
- c# - 价格范围不工作的存储过程
- javascript - Node.js: Variable scope behaves differently in Electron and non-Electron script