【转】CSS实现自适应分隔线的N种方法
1.伪元素+transform:translateX(-100%);
主要原理是设置文本居中text-align: center;
,然后给定两个伪元素,分别绝对定位,那么此时伪元素也是跟随着水平居中的,设置足够的宽度,然后把左边的往左位移100%
就可以了,父级记得超出隐藏。
具体实现如下
html
结构为
<div class="title">我是分割线</div>
css
样式为
.title{
position: relative;
text-align: center;
overflow: hidden;
font-size: 14px;
color: #999;
}
.title::before,.title::after{
content: '';
display: inline-block;
width: 100%;
height: 1px;
position: absolute;
background: #ccc;
top: 50%;
}
.title::before{
margin-left: -10px;
transform: translateX(-100%);
}
.title::after{
margin-left: 10px;
}
点击查看效果 CSS分隔线 (伪元素+transform)
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
2.伪元素+flex
这个比较好理解了,设置display:flex
,然后两个伪元素分别铺满剩余空间。
具体实现如下
html
结构为
<div class="title">我是分割线</div>
css
样式为
.title{
display: flex;
align-items: center;
font-size: 14px;
color: #999;
}
.title::before,.title::after{
content: '';
flex:;
height: 1px;
background: #ccc;
}
.title::before{
margin-right: 10px;
}
.title::after{
margin-left: 10px;
}
点击查看效果 CSS分隔线 (伪元素+flex)
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
3.伪元素+box-shadow/outline+clip-path
同样利用text-align: center
使文本和伪元素居中,然后生成足够大的box-shadow
或者outline
,由于不支持单个方向,所以用clip-path
或者clip
裁剪掉
具体实现如下
html
结构为
<div class="title">我是分割线</div>
css
样式为
.title{
text-align: center;
font-size: 14px;
color: #999;
overflow: hidden;
}
.title::before,.title::after{
content: '';
display: inline-block;
width:;
height: 1px;
box-shadow: 0 0 0 9999px #ccc;
vertical-align: middle;
}
.title::before{
margin-right: 10px;
clip-path: polygon(0 0, -9999px 0, -9999px 100%, 0 100%);
}
.title::after{
margin-left: 10px;
clip-path: polygon(0 0, 9999px 0, 9999px 100%, 0 100%);
}
点击查看效果 CSS分隔线 (伪元素+box-shadow/outline+clip-path)
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
4.伪元素+right:100%
这个实现需要多一层标签,外部仍然是text-align: center
,内部文本里添加两个伪元素绝对定位,其中左边的设置距离右边100%(相对于文本标签)即可
具体实现如下
html
结构为
<div class="title">
<span class="inner">我是分割线</span>
</div>
css
样式为
.title{
text-align: center;
font-size: 14px;
color: #999;
overflow: hidden;
}
.inner{
position: relative;
}
.inner::before,.inner::after{
position: absolute;
content: '';
width: 9999px;
height: 1px;
background: #ccc;
top: 50%;
}
.inner::before{
right: 100%;
margin-right: 10px;
}
.inner::after{
margin-left: 10px;
}
点击查看效果 CSS分隔线 (伪元素+right:100%)
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
5. border+transform
这个思路可以不用到伪元素,不过需要额外的标签,给内部文本左右足够大的1px
边框,此时需要设置line-height:1px
,由于内部整体以及足够大了(超过父级),可以使用绝对定位和transform: translateX(-50%)
居中
具体实现如下
html
结构为
<div class="title">
<span class="inner">我是分割线</span>
</div>
css
样式为
.title{
position: relative;
text-align: center;
font-size: 14px;
color: #999;
overflow: hidden;
padding: .6em 0;/**把高度撑起来**/
}
.inner{
position: absolute;
left: 50%;
transform: translateX(-50%);
white-space: nowrap;
line-height: 1px;
border-left: 9999px solid #ccc;
border-right: 9999px solid #ccc;
padding: 0 10px;
}
点击查看效果 CSS分隔线 (border+transform)
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
6.伪元素+border+left/right
这个思路只需要一个伪元素,在文本内部生成一个伪元素,利用足够大的border
和相同的负值(绝对定位+left/right)还原位置
具体实现如下
html
结构为
<div class="title">
<span class="inner">我是分割线</span>
</div>
css
样式为
.title{
text-align: center;
font-size: 14px;
color: #999;
overflow: hidden;
}
.inner{
position: relative;
padding: 0 10px;
}
.inner::before{
content: '';
position: absolute;
height: 1px;
top: 50%;
border-left: 9999px solid #ccc;
border-right: 9999px solid #ccc;
right: -9999px;
left: -9999px;
}
点击查看效果 CSS分隔线 (伪元素+border+left/right)
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
7.伪元素+table-cell
主要思路为父级设置display:table
,伪元素设置display:table-cell
,并设置足够大的宽度即可
具体实现如下
html
结构为
<div class="title">
<span class="inner">我是分割线</span>
</div>
css
样式为
.title{
display: table;
font-size: 14px;
color: #999;
}
.inner{
display: table-cell;
white-space: nowrap;
padding: 0 10px;
}
.title::before,.title::after{
content: '';
display: table-cell;
width: 9999px;
overflow: hidden;
background: linear-gradient(#ccc 0,#ccc) center no-repeat;/**这里用线性渐变生成的,也可以用其他方式**/
background-size: 100% 1px;
}
点击查看效果 CSS分隔线 (伪元素+table-cell)
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
8.fieldset+legend
利用fieldset
和legend
标签组合,可以天然实现分隔线效果,参考至张鑫旭的这篇文章
具体实现如下
html
结构为
<fieldset class="title">
<legend class="inner">我是分割线</legend>
</fieldset>
css
样式为
.title{
font-size: 14px;
color: #999;
border:;
border-top: 1px solid #ccc;
padding:;
}
.inner{
margin: 0 auto;;
padding: 0 10px;
}
点击查看效果 CSS分隔线 (fieldset+legend)
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
转自:https://www.cnblogs.com/zml1023/p/12083153.html
【转】CSS实现自适应分隔线的N种方法的更多相关文章
- CSS实现自适应分隔线的N种方法
分割线是网页中比较常见的一类设计了,比如说知乎的更多回答 这里的自适应是指两边的横线会随着文字的个数和父级的宽度自适应 偷偷的看了一下知乎的实现,很显然是用一块白色背景覆盖的,加一点背景就露馅了 心想 ...
- CSS巧妙实现分隔线的几种方法
单个标签实现分隔线: 点此查看实例展示 .demo_line_01{ padding: 0 20px 0; margin: 20px 0; line-height: 1px; border-left: ...
- 文字在线中间,CSS巧妙实现分隔线的几种方法
单个标签实现分隔线: .demo_line_01{ padding: 0 20px 0; margin: 20px 0; line-height: 1px; border-left: 200px so ...
- CSS实现导航条Tab的三种方法
前面的话 导航条Tab在页面中非常常见,本文说详细介绍CSS实现导航条Tab的三种方法 布局 根据上图所示,先规定几个定义,上图的模块整体叫做导航,由导航标题和导航内容组成.要实现上图所示的布 ...
- CSS + ul li 横向排列的两种方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- css巧妙实现分隔线
单个标签实现分隔线 .demo_line_01{ padding: 0 20px 0; margin: 20px 0; line-height: 1px; border-left: 200px sol ...
- css+div制作圆角矩形的四种方法
圆角矩形一向是设计师最倾心的一种设计,因为他们可以让整个网页生动起来,不那么死板,所以,作为一个优秀的网页设计师,学会一种或多种编辑圆角矩形的方法是必不可少的,而且圆角矩形应用范围极广,一个网页内的所 ...
- ios webview自适应实际内容高度4种方法
有的时候会碰见类似的苦逼需求, webview自适应实际内容高度 下面有四种方法供使用 方法1:获取webview中scrovllview的contentsize进行设置 1 2 3 4 5 6 ...
- CSS中设置DIV垂直居中的N种方法 兼容IE浏览器
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...
随机推荐
- java中二进制反码补码的理解
7句真言 1,二进制最高位是符号位 0正数 1负数 2,正数的原码,反码,补码都一样 3负数的原码反码 补码 (符号位不变,其他的位数取反 0->1 1->0) 4 0的反码补码都是0 5 ...
- Windows下使用nginx问题
1.下载完成后,解压缩,运行cmd,使用命令进行操作,不要直接双击nginx.exe,不要直接双击nginx.exe,不要直接双击nginx.exe 一定要在dos窗口启动,不要直接双击nginx.e ...
- 用JS写一个网站树形菜单
先上效果图: 主体内容就是侧边展示的一二三级菜单,树形结构的. 前端页面布局内容,页面内容简单用ul li 来完成所有的罗列项.用先后顺序来区分一级二级三级: <body> <b&g ...
- jenkins#安装jenkins之后的操作
1.全局安全配置 运行用户注册 任何用户可以做任何事情 2.全局工具配置 指定maven的settings文件位置 指定java信息 指定maven信息 指定git信息
- [U53204] 树上背包的优化
题目链接 本文旨在介绍树上背包的优化. 可见例题,例题中N,M∈[1,100000]N,M \in [1,100000]N,M∈[1,100000]的数据量让O(nm2)O(nm^2)O(nm2)的朴 ...
- 018.CI4框架CodeIgniter数据库操作之:Delete删除一条数据
01. 在Model中写数据库操作语句,代码如下: <?php namespace App\Models\System; use CodeIgniter\Model; class User_mo ...
- memortstream Base64编码和filestream base64编码不同
memorystream base64 function BaseImage(fn: string): string; var m1: TMemoryStream; m2: TStringSt ...
- NO7 利用三剑客awk-grep-sed-head-tail等7种方法实践
·seq sequence #序列·sed stream editor #(三剑客老二)流编辑器.实现对文件的增删改替换查. -n #取消默认输出.sed -n '20,30 ...
- P1037 在霍格沃茨找零钱
转跳点:
- 指定盘符获取u盘PID、VID、序列号等信息
最近学习scsi和DeviceIoControl,下载了微软WDK一些例子,以下代码精简自Windows-driver-samples-master\storage\tools\spti\src\sp ...