CSS - 实现自适应分隔线的N种方法

1.伪元素+transform:translateX(-100%);
// html
我是分割线
// 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; }
2.伪元素+flex
// html
我是分割线
// css .title{ display: flex; align-items: center; font-size: 14px; color: #999; } .title::before,.title::after{ content: ''; flex: 1; height: 1px; background: #ccc; } .title::before{ margin-right: 10px; } .title::after{ margin-left: 10px; }
3.伪元素+box-shadow/outline+clip-path
// html
我是分割线
// css .title{ text-align: center; font-size: 14px; color: #999; overflow: hidden; } .title::before,.title::after{ content: ''; display: inline-block; width: 0; 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%); }
4.伪元素+right:100%
// html
我是分割线
// 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; }
5. border+transform
// html
我是分割线
// 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; }
6.伪元素+border+left/right
// html
我是分割线
// 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; }
7.伪元素+table-cell
// html
我是分割线
// 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; }
8.fieldset+legend
// html
我是分割线
// css .title{ font-size: 14px; color: #999; border: 0; border-top: 1px solid #ccc; padding: 0; } .inner{ margin: 0 auto;; padding: 0 10px; }

你可能感兴趣的:(CSS)