如何在:: after伪元素中自动调整圆圈的大小? [英] How to auto resize a circle in an ::after pseudo element?

查看:265
本文介绍了如何在:: after伪元素中自动调整圆圈的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个圆形作为::after伪元素,该伪元素会根据其内容自动调整大小.

I'm trying to create a circle as an ::after pseudo element, which resizes automatically depending on its content.

.container {
    display: flex;
    flex-direction: row;
}

#dividerHost2 #left {
    flex: 1 1 auto;
    background-color: yellowgreen;
    height: 200px;
}

#dividerHost2 #right {
    flex: 1 1 auto;
    background-color: dodgerblue;
}

#dividerHost2 .divider {
    background-color: white;
    margin: 0px;
    width: 6px;
    font-weight: 800;
}

.divider.vertical {
    --divider-color: transparent;
    display: inline-flex;

    width: 1px;
    border-left: 1px solid rgb(var(--divider-color));
    margin: 0px 2px 0px 2px;
    overflow: show;
}

.divider.vertical.title::after {
    flex: 0 0 auto;
    align-self: center;
    border-radius: 50%;
    content: "OR";
    padding: 9px 8px 11px 8px;
    background-color: white;
    color: black;

    transform: translateX(-44%);
    z-index: 10;
}

<div id="dividerHost2" class="container">
  <div id="left" class="container" style="flex-direction: row;"></div>
  <div id="divider3" class="divider vertical title"></div>
  <div id="right" class="container" style="flex-direction: row;"></div>
</div>

到目前为止,这给出了一个非常不错的结果:

That gives a pretty nice result so far:

JSFiddle https://jsfiddle.net/jsnbtmh3/

JSFiddle https://jsfiddle.net/jsnbtmh3/

但是,如果文本较长,则圆圈变成椭圆形:

However, with longer text the circle turns into an oval:

如何根据其内容自动调整圆的大小?

How to make the circle auto resize depending on its content?

推荐答案

以下是使用径向渐变的技巧.想法是保持元素全高并使用circle closest-side对其进行着色,这将始终创建一个从中心开始并扩展到最近边(左右两边)的圆

Here is a trick using radial-gradient. The idea is to keep the element full height and color it using circle closest-side which will always create a circle that will start from the center and expand to the closest sides (left and right one)

我简化了代码,仅保留相关部分:

I simplified the code to keep only the relevant part:

.container {
  display: flex;
  flex-direction: row;
  margin:10px;
}

.left {
  flex: 1 1 auto;
  background-color: yellowgreen;
  height: 200px;
}

.right {
  flex: 1 1 auto;
  background-color: dodgerblue;
}

.divider {
  background-color: white;
  width: 6px;
  font-weight: 800;
  display: flex;
  justify-content: center;
}

.divider::after {
  display: flex;
  align-items: center;
  flex: 0 0 auto;
  content: attr(data-text);
  padding: 0 8px;
  background: radial-gradient(circle closest-side, white 98%, transparent 100%);
  z-index: 10;
}

<div  class="container">
  <div class="left "></div>
  <div class="divider" data-text="OR"></div>
  <div class="right "></div>
</div>

<div class="container">
  <div class="left "></div>
  <div class="divider" data-text="longer"></div>
  <div class="right "></div>
</div>

<div class="container">
  <div class="left "></div>
  <div class="divider" data-text="even longer"></div>
  <div class="right "></div>
</div>

这篇关于如何在:: after伪元素中自动调整圆圈的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆