css - 可以不使用绝对位置吗?
问题描述
我想使用以下代码向按钮添加动画https://codepen.io/chrisota/pen/bNdRaM 但对其进行了一些更改
a::before{
position: absolute;
top: 0;
bottom: 0;
margin: auto;
content:'';
border-radius: 50%;
display: block;
width: 30px*2;
height: 20px*2;
line-height: 30px*2;
left: -30px/2;
text-align: center;
transition: box-shadow 0.5s ease-out;
z-index: -1;
}
a:hover{
color: #fff;}
a:hover::before{
box-shadow:inset 0 0 0 $width rgba($button-color,1);
}
但是,据我所知绝对位置最好不要使用,但是当我删除它时,按钮消失了
<row>
<div class='col-5'>
a(href="http://www.dribbble.com/chrisota" title="Chris Ota Dribbble") <i class="fa fa-dribbble"></i> Dribbble
</div>
</row>
解决方案
position: absolute
正确使用时是一个很棒的工具。
它真正闪耀在伪元素中::before
。::after
当您将元素的位置设置为和relative
的位置时,您可以相对于元素本身定位伪元素。::before
::after
absolute
这使您可以做一些很酷的事情,例如向按钮添加图标。
只需确保设置display
和content
属性,如下所示。您可以添加top
、right
、bottom
和left
属性来移动元素。
.some-class {
position: relative;
}
.some-class::before {
position: absolute;
display: block;
content: '';
top: 10px; /* just an example */
left: 10px; /* just an example */
width: 32px; /* just an example */
height: 32px; /* just an example */
}
推荐阅读
- centos - Podman 图像未使用 podman 图像 ls 显示
- gremlin - Gremlin Java 用新属性替换旧的顶点属性
- android - Android WorkManager 检索所有请求
- mongodb - 如何计算加入文档的比例?
- php - 如何真正暂停条带订阅
- handbrake - 如何使手刹使用强度较小的cpu?
- asp.net-core-mvc - 被 CORS 政策阻止:对预检请求的响应
- sql - Postgresql regex_replace 逗号,单引号和双引号
- gtk - 在 TreeView 中获取指针的行/单元格
- javascript - javascript 创建输入时选择 iOS 键盘