CSS

绝对定位实现元素居中 与 img路径为空处理

笔记

Posted by Wason on January 5, 2020

绝对定位实现元素居中

绝对定位 position:absolute 设置该元素居中

position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;

上下左右依次设置为0,然后margin: auto; 可实现上下左右居中显示;
去掉 right: 0; 则是左侧居中显示;
去掉 left: 0; 则是右侧居中显示;
这种方式可以设置按钮竖直居中,也是设置文字居中

<div class="l-str">主持分享</div>

.l-str {
position: absolute;
top: 0;
left: 5px;
bottom: 0;
display: inline-block;
width: .5rem;
height: 1rem;
margin: auto;
white-space: normal;
text-align: center;
}

父元素高度固定

img 路径为空时,CSS去掉边框

当img的 src路径为空时,CSS去掉其边框

img[src=""],img:not([src]){opacity:0;}