search
HomeWeb Front-endCSS TutorialHow to implement adaptive dividers in CSS? Method introduction

How to implement adaptive dividers in CSS? Method introduction

How to implement adaptive dividers in CSS? The following CSS column will introduce to you N ways to implement adaptive dividers in CSS. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

(Recommended tutorial: CSS tutorial)

The dividing line is a common type of design in web pages. For example, Zhihu’s more Answer more

How to implement adaptive dividers in CSS? Method introduction

The adaptive here means that the horizontal lines on both sides will adapt according to the number of text and the width of the parent

Look secretly After looking at the implementation on Zhihu, it is obvious that it is covered with a white background. Adding a little background reveals the secret

How to implement adaptive dividers in CSS? Method introduction

I thought: Is Zhihu’s front-end not that good? Maybe other people’s focus is not on these.

The following are some better implementation methods, the kind that will not reveal the secret

1. Pseudo element transform:translateX(-100% );

The main principle is to set the text to be centered text-align: center;, and then given two pseudo-elements, each of which is absolutely positioned, then the pseudo-element will also follow Horizontally center , set enough width, and then move the left one 100% to the left. Remember to hide the parent.

The specific implementation is as follows

htmlThe structure is

<div class="title">我是分割线</div>

cssThe style is

.title{
    position: relative;
    text-align: center;
    overflow: hidden;
    font-size: 14px;
    color: #999;
}
.title::before,.title::after{
    content: &#39;&#39;;
    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 separator line (pseudo element transform)

2. Pseudo element flex

This is easier to understand, set display:flex, and then two pseudo elements Cover the remaining space separately.

The specific implementation is as follows

htmlThe structure is

<div class="title">我是分割线</div>

cssThe style is

.title{
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #999;
}
.title::before,.title::after{
    content: &#39;&#39;;
    flex: 1;
    height: 1px;
    background: #ccc;
}
.title::before{
    margin-right: 10px;
}
.title::after{
    margin-left: 10px;
}

CSS divider (pseudo element flex)

3. Pseudo element box-shadow/outline clip-path

Also use text-align: center to make the text Center it with the pseudo element, and then generate a large enough box-shadow or outline. Since a single direction is not supported, use clip-path or clip Crop out

The specific implementation is as follows

htmlThe structure is

<div class="title">我是分割线</div>

cssThe style is

.title{
    text-align: center;
    font-size: 14px;
    color: #999;
    overflow: hidden;
}
.title::before,.title::after{
    content: &#39;&#39;;
    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%);
}

CSS divider (pseudo element box-shadow/outline clip-path)

4. Pseudo element right:100%

This implementation requires one more layer of tags , the outside is still text-align: center, add two pseudo-elements to the internal text for absolute positioning, the left one is set to 100% from the right (relative to the text label)

Specific The implementation is as follows

htmlThe structure is

<div class="title">
   <span class="inner">我是分割线</span>
</div>

cssThe style is

.title{
    text-align: center;
    font-size: 14px;
    color: #999;
    overflow: hidden;
}
.inner{
    position: relative;
}
.inner::before,.inner::after{
    position: absolute;
    content: &#39;&#39;;
    width: 9999px;
    height: 1px;
    background: #ccc;
    top: 50%;
}
.inner::before{
    right: 100%;
    margin-right: 10px;
}
.inner::after{
    margin-left: 10px;
}

CSS separator (pseudo element right :100%)

5. border transform

This idea does not require pseudo elements, but it requires additional tags to give the internal text left and right large enough 1pxBorder, you need to set line-height:1px at this time. Since the internal whole is large enough (exceeding the parent), you can use absolute positioning and transform: translateX(-50%)center

The specific implementation is as follows

htmlThe structure is

<div class="title">
    <span class="inner">我是分割线</span>
</div>

cssThe style is

.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 divider line (border transform)

6. Pseudo element border left/right

This idea only requires one pseudo element, and generates a pseudo element inside the text, which is enough Large border and the same negative value (absolute positioning left/right) restore the position

The specific implementation is as follows

htmlThe structure is

<div class="title">
    <span class="inner">我是分割线</span>
</div>

cssThe style is

.title{
    text-align: center;
    font-size: 14px;
    color: #999;
    overflow: hidden;
}
.inner{
    position: relative;
    padding: 0 10px;
}
.inner::before{
    content: &#39;&#39;;
    position: absolute;
    height: 1px;
    top: 50%;
    border-left: 9999px solid #ccc;
    border-right: 9999px solid #ccc;
    right: -9999px;
    left: -9999px;
}

CSS divider (pseudo element border left/right)

7. Pseudo element table-cell

The main idea is to set the parent level display:table, set the pseudo element display:table-cell, and set a large enough width

The specific implementation is as follows

htmlThe structure is

<div class="title">
    <span class="inner">我是分割线</span>
</div>

cssThe style is

.title{
    display: table;
    font-size: 14px;
    color: #999;
}
.inner{
    display: table-cell;
    white-space: nowrap;
    padding: 0 10px;
}
.title::before,.title::after{
    content: &#39;&#39;;
    display: table-cell;
    width: 9999px;
    overflow: hidden;
    background: linear-gradient(#ccc 0,#ccc) center no-repeat;/**这里用线性渐变生成的,也可以用其他方式**/
    background-size: 100% 1px;
}

CSS separator (pseudo Element table-cell)

8.fieldset+legend

利用fieldsetlegend标签组合,可以天然实现分隔线效果,参考至张鑫旭的这篇文章

具体实现如下

html结构为

<fieldset class="title">
    <legend class="inner">我是分割线</legend>
</fieldset>

css样式为

.title{
    font-size: 14px;
    color: #999;
    border: 0;
    border-top: 1px solid #ccc;
    padding: 0;
}
.inner{
    margin: 0 auto;;
    padding: 0 10px;
}

CSS分隔线 (fieldset+legend)

小结

上面一共列举了8中方式来实现分隔线的效果,每种方法思路各不相同,重要的是可以发散自己的想象力,可能这才是CSS与其他语言所不同的吧~

这里整理了一下,整体效果如下,可访问这里查看,大家在实际项目中可自行选取所需要的方式

How to implement adaptive dividers in CSS? Method introduction

可能还有其他方式没有想到,欢迎大家集思广益,在下方留言讨论

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of How to implement adaptive dividers in CSS? Method introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:github. If there is any infringement, please contact admin@php.cn delete
The Lost CSS Tricks of Cohost.orgThe Lost CSS Tricks of Cohost.orgApr 25, 2025 am 09:51 AM

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor