html - 使用 ::after 和 ::before 超出填充范围
问题描述
p {
background: rgba(80,150,220,1);
width: 100px;
height: 100px;
text-align:center;
}
p::before {
content="asdf";
background: rgba(220,150,80,1);
width = 50px;
height = 50px;
top = - 25px;
left = - 25px;
}
<!DOCTPE html>
<head>
</head>
<body>
<p>TEXT HERE</p>
</body>
不改变 HTML
这是我的尝试。如果您有更好的解决方案,请告诉我。
应该显示虚线,它仅代表元素大小。
我试图将填充和边距保持为 0。
谢谢您的帮助。
解决方案
您可以指定填充和边距,但通过考虑与负边距相同的填充量,使它们在视觉上为 0,然后依赖于一些背景颜色:
.box {
width: 100px;
height: 100px;
margin: -20px 0 0 -20px;
padding: 20px 0 0 20px;
background:
/* Top red line (width:80% height:20px) */
linear-gradient(red,red) 0 0/80% 20px,
/* left red line (width:20px height:80%)*/
linear-gradient(red,red) 0 0/20px 80%,
/* Main background; color only the content area (not the padding)*/
rgba(80,150,220,1) content-box;
background-repeat:no-repeat;
text-align:center;
}
body {
padding:50px;
}
<div class="box">TEXT HERE</div>
如果你想使用伪元素:
.box {
width: 100px;
height: 100px;
text-align: center;
background: rgba(80, 150, 220, 1);
position: relative;
z-index: 0;
}
.box:before {
content: "";
position: absolute;
z-index: -1;
top: -20px;
left: -20px;
width: 80%;
height: 80%;
border-top: 20px solid red;
border-left: 20px solid red;
}
body {
padding: 50px;
}
<div class="box">TEXT HERE</div>
推荐阅读
- java - Java 长字节长度
- java - java.lang.ClassCastException:实现 parcelable 时无法转换为 java.lang.String
- excel - COUNTIF 日期出现在同一周数和年份
- java - 检测动态 IP 地址变化
- swift - 迅速从阿拉伯语中删除变音符号
- sql - 在查询结果中只出现一次的行
- angular-material - 角反应形式 - 如何设置选择/下拉控件的默认值?
- ios - Git 说当我向项目中添加新文件时,我删除了 .pbxproj 文件中的一行
- c++ - stl 结构列表作为类成员
- python - 远程图像上传执行与开发服务器不同