How to add a circular edge to an element with a pseudo-element

I am trying to create an HTML element that needs to be responsive and cannot be an SVG. The element should resize when the text content inside breaks onto a new line. I have already skewed the element slightly, but now I need to add a rounded side on the right.

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

My attempts using the ::after pseudo-element have not been successful in creating the desired effect. I've tried various approaches, but I can't seem to get it quite right.

Below is one of my attempts at solving the issue:

body {
  width: 200px;
}

.container {
  background-color: red;
  transform: matrix(1, -0.03, 0.03, 1, 0, 0);
  padding: 0px 15px;
  position: relative;
}

.container::after {
  content: '';
  background: white;
  width: 10px;
  height: 100%;
  position: absolute;
  right: -5px;
  top: 0;
  z-index: 2;
}
<div class="container">
  <p>Long text Long text Long text Long text Long text</p>
</div>

<div class="container">
  <p>Short text</p>
</div>

Any assistance would be greatly appreciated.

Answer №1

Implementing a creative method involves utilizing a CSS radial-gradient() as the background image, with detailed comments to guide in the code:

/* establishing a shared CSS custom property: */
:root {
  --spacing: 0.5rem;
}

/* simplifying cross-browser sizing consistency
   for elements by adjusting width and padding,
   while removing default browser margins and padding: */
*,
::before,
::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  block-size: 100vh;
  font-family: system-ui;
  font-size: 16px;
  font-weight: 400;
  padding: var(--spacing);
}

main {
  border: 1px solid currentColor;
  inline-size: clamp(30rem, 80%, 1200px);
  margin-inline: auto;
  min-block-size: 100%;
  padding: var(--spacing);
}

.container {
  /* setting custom properties for .container elements
     and their descendants: */
  /* defining the desired background color */
  --backgroundColor: crimson;
  /* specifying the distance between gradient center and
     starting point of color: */
  --transparentRadius: 1rem;
  /* horizontal radius of ellipse */
  --rx: 0.5rem;
  /* vertical radius of ellipse: */
  --ry: 30%;
  background-image:
    radial-gradient(
      /* defining an ellipse (versus 'circle'), with
         double the horizontal diameter of the radius,
         along with determining vertical radius: */
      ellipse calc(2 * var(--rx)) calc(var(--ry) * 2)
        /* placing gradient's center horizontally at
           100% - 50% of the radius, and vertically at
           50% of element size on y-axis: */
        at calc(100% - (0.5 * var(--rx))) 50%,
        /* establishing initial transparent color at
           center towards a distance --transparentRadius
           from center: */
        transparent 0 var(--transparentRadius),
        /* setting second color - simulated 'background-color'
           of element, starting at --transparentRadius distance
           and filling the element: */
        var(--backgroundColor) var(--transparentRadius)
      );
  inline-size: 15rem;
  margin-block: 2rem;
  padding: var(--spacing);
  /* adopted from existing code: */
  transform: matrix(1, -0.03, 0.03, 1, 0, 0);
}

.container p {
  /* applying margin to end of <p> element inline,
     preventing text overflow into transparent area: */
  margin-inline-end: calc(var(--spacing) + var(--rx));
}
<main>
  <div class="container" style="--backgroundColor: lightpink">
    <p>Long text Long text Long text Long text Long text</p>
  </div>

  <div class="container">
    <p>Short text</p>
  </div>
</main>

View JS Fiddle demonstration.

References:

Bibliography:

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

Enhancing the SVG font size through custom CSS styling

Working with an SVG element and utilizing CSS to adjust the font-size of the text within the SVG. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720"> <rect class="vBoxRect" width="1280" height="720" fill="none" stroke="red"& ...

What is the best way to extract plain text with JQuery?

I found this Html code snippet: <div class="form-group row"> <label id="lb_size" class="col-lg-3 col-form-label"> <span class="must-have">****</span> Size </label> </div> My goal is to ext ...

Use CSS specifically for codeigniter viewpage

I am unsure if it is possible, but I would like to load CSS and JavaScript within a view page instead of on include.php. Currently, the CSS file is loading on include.php and it is working correctly. What I want to achieve now is to load it directly inside ...

The controller is failing to effectively showcase the ng-show functionality

My webpage consists of three elements: a search bar, a search result area, and a form. The use case scenario involves showing the search bar and form div by default, while hiding the search result div. When I enter keywords into the search bar, client data ...

Tips for ensuring that all children within a flex-container have equal heights

I seem to be facing an issue with this problem. My goal is to ensure that all the child divs within a flex container have the same height as the tallest child div, which in this case is 100px. Additionally, I want them to be aligned at the center. I&apos ...

How to retrieve nested menu items within the scope by utilizing JSON and AngularJS

I have recently started working with angular and am facing difficulty in accessing the submenu items within my angular application. While I can successfully access the top level menu items, I am struggling to retrieve the second level items. Here is a sni ...

Floating CSS3 animations within HTML elements

I'm struggling to understand why the animated arrow on my website refuses to go behind the other elements. The animation code I've used for the arrow in CSS3 is as follows: -webkit-animation-fill-mode:both; -moz-animation-fill-mode:both; -ms-an ...

Switch the video source using JavaScript

I've been experimenting with creating a custom dropdown (ul li list) that allows users to select a different video to play, which is then loaded into the background of the page. Once the user clicks on the dropdown, it should switch to a different vi ...

Using JavaScript to invoke Applescript commands

Is it feasible to execute Applescript from JavaScript? I would be grateful if someone could offer a sample code or useful reference link. ...

How can I place a div using pixel positioning while still allowing it to occupy space as if it were absolutely positioned?

I successfully created an slds path element with multiple steps. I want to display additional subtext information below each current step, but there might not always be subtext for every step. To achieve this, I created an absolute positioned div containi ...

How to place text on top of a thumbnail in Bootstrap using HTML

I've come across similar questions here, but none of the suggested solutions have worked for me. (I attempted making the image a background as recommended on how to display text over an image in Bootstrap 3.1, but it didn't seem to be effective) ...

The CSS form appears to be too large for the page

On every single one of my pages, the content fits perfectly within the body: However, when I have forms on the page, they extend beyond the footer and the body doesn't resize accordingly: Where could the issue be coming from? ...

Having trouble figuring out how to play an mp3 using the <audio> tag. The console is showing an error message that says, "Unable to decode media resource XYZ."

I've been frustrated while trying to make the HTML5 <audio> element function properly. My webpage has two elements - one serving a file from my server and the other serving the same file from a different source server: <audio preload="metada ...

html table displaying incorrect data while using dynamic data in vue 3

example status 1 Hello there! I'm trying to create a table (check out example status 1 for guidance). Here's the code snippet I am using: <table> <tr> <th>Product</th> <th>Price</th> <th>Av ...

Select any menu option to return to the one-page layout and then scroll down to find the location

I'm wondering if there is a way to navigate back from an external page to a specific section on my one-page website with a fixed menu? On my one-pager website, I have a fixed menu that includes a "apply for a job" button. When clicked, it takes users ...

Convert text to bold when checkbox is selected and alter color upon selecting a radio button

Having just started learning HTML, CSS, and PHP, I find myself in need of assistance. Despite extensive research online, I have hit a dead end and cannot seem to find any productive solutions. Any guidance or suggestions would be greatly appreciated! I am ...

Obtain the numerical value of the vertical position of the mouse (

Currently, I am coding a JavaScript game and my objective is to designate a variable specifically for the Y axis of the mouse. I kindly request that the code be kept as simple and straightforward as possible, avoiding unnecessary complexity. That conclud ...

Using PHP and JQuery to disable a button after the letter "U" is typed

I am looking for a way to disable the button when the term "U" (defined as Unable) appears. How can I achieve this? Below is the button in question: <input type="submit" class="form-control btn-warning" name="search" value="Search Data"></input& ...

Adjusting print page size according to the class of a specific element using CSS

I am currently working on a single page application and I have the need to print the page with either landscape or portrait orientation based on the class of a specific div element. The challenge is that I want to achieve this using only CSS and without an ...

Tips for adapting CSS images on mobile devices without distorting the aspect ratio

Our website is not responsive, and one of the requirements is to ensure that images render properly on mobile devices to fit the screen. However, we are encountering an issue due to the varying sizes of images uploaded to the web page. Below is the CSS co ...