Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

css - How to maintain a circlur shape of element with dynamic content?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...