Tips for preserving a circular element shape while accommodating dynamic content

I'm attempting to generate a circular shape using the ::after pseudo element, which adjusts its size automatically based on the content inside.

.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>

This approach has yielded pleasing results thus far:

https://i.sstatic.net/SSeJu.png

Have a look at the JSFiddle here: https://jsfiddle.net/jsnbtmh3/

However, when the text length increases, the circle transforms into an oval shape:

https://i.sstatic.net/y0vns.png

Is there a way to make sure the circle dynamically adjusts its size based on the content?

Answer №1

Learn a neat CSS trick using radial-gradient! By utilizing circle closest-side, you can create a circular effect that starts from the center and expands to the nearest sides (left and right).

To simplify, here's the key part of the code:

.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>

Answer №2

Avoid placing real content within the pseudo-element especially given that this is meant for "styling" purposes rather than actual content, instead utilize the pseudo-element to generate a background circle by utilizing the padding/aspect ratio technique.

body {
  text-align: center;
}

.divider {
  margin: 3em;
  display: inline-block;
  position: relative;
  padding: 1em;
  font-weight: bold;
}

.divider:after {
  content: "";
  position: absolute;
  width: 100%;
  padding-top: 100%;
  background: lightblue;
  border-radius: 50%;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: -1;
}
<div class="divider">OR</div>

<div class="divider">LONG TEXT</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Randomly swap images in HTML when a button is clicked

In order to change images randomly on mouse click, I came across this solution: <SCRIPT LANGUAGE="Javascript"> function banner() { } ; b = new banner() ; n = 0 b[n++]= "<A HREF='index.html'><IMG SRC='images/pic1.jpg'> ...

Permit the use of HTML tags within a CSS-only tooltip

I have created a unique CSS tooltip. <span data-tooltip="The security code is a 3-digit number<br />on the back of your Visa or Mastercard debit or credit card, or a 4-digit number on the front of your American Express card.">Help</span> ...

The vertical-align property in CSS seems to be malfunctioning

Hi there, I'm struggling with the CSS code below: .parent { position : 'absolute'; top : '50px'; left : '50px'; width : '400px'; height : '160px'; padding : '10px'; ...

HTML and CSS link conflict

As a beginner in CSS, I am experimenting with creating a simple website using HTML and CSS. However, I have encountered an issue where the links on the left side of my page are being interfered with by a paragraph in the middle section. This causes some of ...

In JavaScript, you can ensure that only either :after or :before is executed, but not both

My html slider is causing me some trouble <div class="range-handle" style="left: 188.276px;">100,000</div> Upon loading the page, I noticed that in Firebug it appears like this https://i.sstatic.net/Rgqvo.png On the actual page, it looks li ...

Why is this element occupying an excessive amount of space horizontally?

I've been diligently working on a unique custom WordPress theme as part of a school project. Our task is to redesign the homepage of an association, in my case, a theater group. One of the challenges I encountered was customizing the WordPress menu t ...

Tips for switching the status of each item as I navigate the page with the tab key

I am facing an issue with my list of items that have hidden content appearing on hover. I want to achieve the same functionality while tabbing through my page, but currently, all hidden content appears at once. Check out a live example of my code here jQ ...

Building a Loading Bar with Two Images Using JavaScript and CSS

I am currently experimenting with creating a progress bar using two images: one in greyscale and the other colored. My goal is to place these two divs next to each other and then adjust their x-position and width dynamically. However, I'm having troub ...

Only CSS creates tooltips within text seamlessly without any line breaks

I have been exploring ways to incorporate multiple tooltips into text on my website. After experimenting with both span and div tags, I was able to create a tooltip using CSS only. However, I am facing challenges with positioning the tooltiptext correctly. ...

Issue encountered while trying to interpret Bootstrap 4 within Netbeans 8.2

Currently, I am working on an HTML5/JS application using Node.js in Netbeans 8.2. As I develop the welcome page, I intend to incorporate Bootstrap 4. However, when I attempt to add bootstrap.min.css to my stylesheets folder, I encounter an error as shown i ...

Achieving Vertical Alignment of Two Divs with CSS

I've come across several solutions to my issue, but none of them seem to work in my current situation. I have a banner at the top of my site with two floated columns, and suspect that the navigation menu in the right column may be causing the problem. ...

Opacity problem when hovering on Chrome

Work in progress: Hello everyone, I'm facing an issue with a hover effect that only seems to be occurring in Chrome. When hovering over a tile, the background color changes, the image opacity decreases, and an icon appears over the image. In Chro ...

Instructions on adding a solid border color to a circular div using CSS

Hey there! I'm curious, is it possible to style a straight border color on a rounded div using CSS? Here I have some basic Tailwind CSS code. <div class="rounded-2xl w-72 h-20 border-l-4 border-l-yellow-500"> ... </div> In the ...

Utilizing a Recycled jQuery UI Themeroller Overlay

I'm looking to optimize css-loading bandwidth by reusing the overlay from jQuery UI for a specific section of my page. However, I'm encountering an issue where the overlay only covers a semi-transparent blackout of the entire page instead of just ...

Images in assets folder are not showing up in Vue Bootstrap

I'm attempting to showcase an image similar to the example provided in this guide Below is the code I am using: <template> <b-container fluid class="p-4 bg-dark"> <b-row> <b-col> <b-img rounded="ci ...

Next.js production mode prevents CSS from loading properly

Issue Upon building and launching a production build of our application, the CSS fails to load. Inspecting the devtools reveals a multitude of errors and warnings: https://i.sstatic.net/R07q3.png Possible Causes The problems do not occur when running th ...

Modifying Div Size with Jquery

I am working on populating the div container with square boxes, but I'm having trouble adjusting the size of my #gridSquare div. Despite trying to change its height and width using jQuery, it doesn't seem to work as expected. $('#gridSquare ...

Reorganizing the order of Bootstrap columns in a single row for mobile devices

I am struggling with changing the order of Bootstrap columns on smaller devices. I need them to remain in a single row on all screen sizes, but I want them to display in a different order on phones. Specifically, I want the layout to look like this on desk ...

The poker table designed with CSS does not resize effectively when displayed in smaller dimensions

I have put together a CSS poker table design that I am quite happy with at the moment: https://jsfiddle.net/78fcs01m/3/ CSS: .poker-container { display: grid; width: 100vw; height: 100vh; .poker-table { justify-self: cent ...

Adjust the height of a DIV element using Jquery Resizable to a minimum height of 1px, smaller than its default value

Having an issue with the Jquery UI Resizable functionality. I've implemented Jquery resizable to adjust a div's width and height dynamically. It's been working well, but I'm encountering a problem when attempting to decrease the height ...