伪元素在父元素中居中_css使用伪元素实现自适应宽度的-分割线

322dfa6db8c87032533de396cf6950d0.png

第一种:使用伪元素+transform:translateX(-100%);

原理是设置文本居中,给定两个伪元素,分别绝对定位,那么此时伪元素也是跟随着水平居中的,设置的宽度,然后把左边的往左位移100%就可以了,父元素超出设置隐藏。

自由伸缩效果为:

08e2b27227fc301b4775f7271269f6d0.png
移动端

4506c5f486ec9d389e4ecb0e7b8751ae.png
平板

881b87ca6c0e8bc2adb6c9ff7413ff6e.png
pc端

具体实现如下html为

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>分割线</title>
		<style>
			.cutting_line{
			    position: relative;
			    text-align: center;
			    overflow: hidden;
			    font-size: 14px;
			    color: black;
			}
			.cutting_line::before{
				content: '';
				display: inline-block;
				width: 100%;
				height: 2px;
				position: absolute;
				background: black;
				top: 50%;
			}
			.cutting_line::after{
			    content: '';
			    display: inline-block;
			    width: 100%;
			    height: 2px;
			    position: absolute;
			    background: black;
			    top: 50%;
			}
			.cutting_line::before{
			    margin-left: -10px;
			    transform: translateX(-100%);
			}
			.cutting_line::after{
			    margin-left: 10px;
			}
		</style>
	</head>
	<body>
		<div class="cutting_line">分割线</div>
	</body>
</html>

第二种:伪元素+flex

设置display:flex,然后两个伪元素分别铺满剩下的空间

自由伸缩效果为:

08e2b27227fc301b4775f7271269f6d0.png
移动端

4506c5f486ec9d389e4ecb0e7b8751ae.png
平板

881b87ca6c0e8bc2adb6c9ff7413ff6e.png
pc端

具体实现如下html为

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>分割线</title>
		<style>
			.cutting_line{
			    display: flex;
			    align-items: center;
			    font-size: 14px;
			    color: balck;
			}
			.cutting_line::before,.cutting_line::after{
			    content: '';
			    flex: 1;
			    height: 2px;
			    background: black;
			}
			.cutting_line::before{
			    margin-right: 10px;
			}
			.cutting_line::after{
			    margin-left: 10px;
			}
			
		</style>
	</head>
	<body>
		<div class="cutting_line">分割线</div>
	</body>
</html>

第三种:伪元素+box-shadow/outline+clip-path

利用文本和伪元素居中,然后生成足够大的box-shadow或者outline,由于不支持单个方向,所以用clip-path或者clip裁剪掉

自由伸缩效果为:

08e2b27227fc301b4775f7271269f6d0.png
移动端

4506c5f486ec9d389e4ecb0e7b8751ae.png
平板

881b87ca6c0e8bc2adb6c9ff7413ff6e.png
pc端

具体实现如下html为

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>分割线</title>
		<style>
			.cutting_line{
			    text-align: center;
			    font-size: 14px;
			    color: black;
			    overflow: hidden;
			}
			.cutting_line::before,.cutting_line::after{
			    content: '';
			    display: inline-block;
			    width: 0;
			    height: 2px;
			    box-shadow: 0 0 0 9999px black;
			    vertical-align: middle;
			}
			.cutting_line::before{
			    margin-right: 10px;
			    clip-path: polygon(0 0, -9999px 0, -9999px 100%, 0 100%);
			}
			.cutting_line::after{
			    margin-left: 10px;
			    clip-path: polygon(0 0, 9999px 0, 9999px 100%, 0 100%);
			}
			
			
		</style>
	</head>
	<body>
		<div class="cutting_line">分割线</div>
	</body>
</html>

第四种:伪元素+right

伪元素+right:100%这个实现需要多一层标签,外部仍然是text-align: center,内部文本里添加两个伪元素绝对定位,其中左边的设置距离右边100%

自由伸缩效果为:

08e2b27227fc301b4775f7271269f6d0.png
移动端

4506c5f486ec9d389e4ecb0e7b8751ae.png
平板

881b87ca6c0e8bc2adb6c9ff7413ff6e.png
pc端

具体实现如下html为

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>分割线</title>
		<style>
			.cutting_line{
			    text-align: center;
			    font-size: 14px;
			    color: black;
			    overflow: hidden;
			}
			.inner{
			    position: relative;
			}
			.inner::before,.inner::after{
			    position: absolute;
			    content: '';
			    width: 9999px;
			    height: 2px;
			    background: black;
			    top: 50%;
			}
			.inner::before{
			    right: 100%;
			    margin-right: 10px;
			}
			.inner::after{
			    margin-left: 10px;
			}
			
		</style>
	</head>
	<body>
		<div class="cutting_line">
		    <span class="inner">分割线</span>
		</div>
	</body>
</html>

第五种:border+transform

这个可以不用到伪元素,给内部文本左右足够大的2px边框,此时需要设置line-height:2px,由于内部整体以及足够大了(超过父元素),可以使用绝对定位和transform: translateX(-50%)居中

自由伸缩效果为:

08e2b27227fc301b4775f7271269f6d0.png
移动端

4506c5f486ec9d389e4ecb0e7b8751ae.png
平板

881b87ca6c0e8bc2adb6c9ff7413ff6e.png
pc端

具体实现如下html为

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>分割线</title>
		<style>
			.cutting_line{
			    position: relative;
			    text-align: center;
			    font-size: 14px;
			    color: black;
			    overflow: hidden;
			    padding: .6em 0;/**把高度撑起来**/
			}
			.inner{
			    position: absolute;
			    left: 50%;
			    transform: translateX(-50%);
			    white-space: nowrap;
			    line-height: 2px;
			    border-left: 9999px solid black;
			    border-right: 9999px solid black;
			    padding: 0 10px;
			}
			
		</style>
	</head>
	<body>
		<div class="cutting_line">
			 <span class="inner">分割线</span>
		</div>
	</body>
</html>

第六种:fieldset和legend标签组合

自由伸缩效果为:

08e2b27227fc301b4775f7271269f6d0.png
移动端

4506c5f486ec9d389e4ecb0e7b8751ae.png
平板

881b87ca6c0e8bc2adb6c9ff7413ff6e.png
pc端

具体实现如下html为

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>分割线</title>
		<style>
			.cutting_line{
			    font-size: 14px;
			    color: black;
			    border: 0;
			    border-top: 2px solid black;
			    padding: 0;
			}
			.inner{
			    margin: 0 auto;;
			    padding: 0 10px;
			}
			
		</style>
	</head>
	<body>
		<fieldset class="cutting_line">
		    <legend class="inner">分割线</legend>
		</fieldset>
	</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值