Creating inner borders on a pie chart with HTML and CSS: A step-by-step guide

After putting my coding skills to the test, I successfully crafted a stunning pie chart using only HTML and CSS.

https://i.sstatic.net/5xCbl.png

Here's the code snippet I tinkered with:

HTML -

<div class="pie-chart"></div>

CSS -

.pie-chart {
        margin-left: 10px;
        --size: 8rem;
        --fg: #369;
        --bg: #def;
        width: var(--size);
        height: var(--size);
        border-radius: 50%;
        display: grid;
        place-items: center;
        background:
          radial-gradient(closest-side, white 40%, transparent 0 99.9%, white 0),
          conic-gradient(
            #301B84 0 45%,
            #715fa5 45% 65%,
            #aca6cd 65% 75%,
            #d6d0e4 75% 100%);
        color: var(--fg);
      }

I am striving for the eye-catching look of this pie chart, particularly focusing on creating that inner white border which adds an extra touch of elegance:

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

However, please keep in mind that I am strictly limiting myself to HTML and CSS alone, without incorporating jQuery or Javascript. Can anyone offer assistance in accomplishing this task?

Answer №1

Conic gradients operate in angular segments, but introducing white spaces between each segment would create a thinner center and wider edges, which may not be the desired effect.

Therefore, there are two possible solutions:

Solution 1: Utilize SVG, as it is well-suited for this purpose and still falls within the HTML and CSS requirements. There are numerous tools and guides available.

Solution 2: Incorporate divider elements into your chart. While somewhat of a workaround, it can effectively address the issue. Here's an example: https://codepen.io/davidleininger/pen/abKJOOo/699fb1595f469e69054e36b27280344a

In essence, update your HTML to include dividers with a CSS custom property, then use these properties to generate divider lines within your chart:

<div class="pie-chart">
  <div class="divider" style="--precent: 45;"></div>
  <div class="divider" style="--precent: 65;"></div>
  <div class="divider" style="--precent: 75;"></div>
  <div class="divider" style="--precent: 100;"></div>
</div>

Once dividers are added to your HTML, apply the following CSS code to achieve the divider lines:

.pie-chart .divider {
  background: white;
  height: 50%;
  left: 50%;
  margin-left: -1px;
  position: absolute;
  top: 0;
  transform-origin: 0 100%;
  transform: rotate(calc(1turn * (var(--precent) / 100)));
  width: 2px;
}

The key component here is the transform property, responsible for positioning and rotating all divider lines accordingly.

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

Encountered issue while initializing object from controller in AngularJS

Here is the demonstration on how the fiddle appears: var app = angular.module('testApp', []); app.controller = angular.('testAppCtrl', function ($scope) { $scope.vehicle = { type: 'car', color: 're ...

When viewed on a mobile device, the page defaults to displaying the NumPad instead of the Text Keyboard in Angular

The email field in my angular app is opening a Key Pad in mobile view and I'm not sure why. <form (ngSubmit)="onSubmit()" #emailForm="ngForm"> <div class="emailaddress" style="text-align:center;" *ngIf="!showEmail"& ...

Table does not have appropriate rowspan and colspan attributes

I've created a table using bootstrap, but it's not displaying as intended. I used row-span and col-span to format the table, and while most rows are correct, some are not appearing on the page. To better illustrate the issue, I have included an ...

How can I feature an image at the top of the page with large 3 and jQuery in FireFox?

I am interested in showcasing a single image at the forefront of the page when it is selected. While there are numerous plug-ins that offer this feature, many come with unnecessary extras like galleries, video support, and thumbnails which I do not requir ...

Accessing stored web pages / Browser as an interactive interface

I have a couple of questions I need help with. The first one is about how to open a saved HTML file in a browser without an internet connection. Next, I'm looking for advice on using the browser as a user interface for a desktop image viewing program ...

Uncertainty surrounding the application of class selector within CSS

Currently, I'm deep into my CSS learning journey on Progate.com. I've been cruising through the exercises, but I hit a roadblock when it comes to a specific class selector in the CSS code. The task at hand is simple: I need to tweak the CSS so th ...

In NextJs SSR, external components from a different package do not inherit styles when applied

I have incorporated an external react component into my Next.js app code. Here is a snippet of how the component appears: <AppBar className={clsx(classes.header)}> </AppBar> export const header = makeStyles((theme) => ({ header: { ...

What is the reason for the presence of white space surrounding the div element

In the td element, I have four nested div elements. However, there seems to be a small margin or space created between them that I cannot figure out how to remove. If this spacing is due to the behavior of the inline-block, then how can I override it? ...

What could be the reason for Internet Explorer pulling styles from the media=print CSS?

The HTML below incorporates a DHTML behavior into a CSS class. Unfortunately, Internet Explorer (specifically version 8 in compatibility mode) does not read the top style exclusively; instead, it also recognizes the @media print. <!--[if IE]> < ...

How can I execute a function when ng-click is triggered on an <a> tag, and ensure it opens in a new tab?

I am facing an issue with an element in my code, which is as follows: <a ng-click="vm.openPage(page)">{{page.pageId}}</a> Within the vm.openPage() function, there are validations that need to be performed before redirecting to the page refere ...

Using the .val method on an input element modifies the HTML content without affecting the value displayed

Currently, I am working on validating the range input type by utilizing JavaScript along with jQuery's val method. While it successfully displays the output in HTML, I encountered an issue where changes to the value are not being logged in the console ...

Is the XPath in Selenium executed against the Document Object Model (DOM) or the HTML file

Currently, I am in the process of developing a library for analyzing webpages. My approach involves using Selenium to access elements on a webpage through xpath. I have been contemplating replacing Selenium with an offline xpath tool. However, upon furthe ...

CSS Challenge: How to crop an image without using its parent container directly

I'm currently facing a complex CSS challenge that I can't seem to solve. I want to create an image controller (two-by-two layout on two lines) that can display: The top-left image in full size, The top-right with horizontal scrolling, The botto ...

What steps can I take to ensure my CSS component remains unaffected by the global CSS styles?

My navbar component is not displaying the styles correctly as intended. I have a Navbar.module.css file to style it, but after using next-auth for social login, only the buttons remain unstyled while everything else gets styled. The code snippet for impor ...

Steps to alter background image and adjust its height upon function activation

I am working on a search page with an advanced search option that only certain users can access. I need the height of my div to increase accordingly and also change the background image to a larger size when the advanced search is selected. How can I make ...

I require fixed headers that no longer stick once reaching a specific point

I have a setup where the names and occupations of participants are displayed next to their artworks, and I've made it sticky so that as we scroll through the images, the names stay on the left. Now, I want the names to scroll out once the images for e ...

What is the best way to save a JavaScript array in HTML5 local storage?

I am looking for a way to store an array of inputs using the local storage feature of HTML5. Currently, I am able to add weight inputs to the array, but they disappear once I refresh the page. Can anyone assist me with this? <!DOCTYPE html> <html ...

Unexpected symbols appearing in image tag after AJAX response

I'm currently grappling with an issue related to an AJAX request I am working on (I am quite new to the world of AJAX). I have set up an API and my goal is to fetch a PNG image using an Authorization header that requires a token supplied by me (which ...

Struggling to locate the src/site/globals/site.variables file to customize the font-family in Semantic UI React. Is there an alternative method to achieve this?

I've been attempting to modify the overall font-family for my application, but so far, I haven't had any luck. Since I'm working with Semantic-UI-React, I initially thought that could be the issue. However, when I tried to locate src/site/g ...

AngularJS 1: Dynamically updating input values in real-time

Hey everyone, I'm experimenting with Angular and have encountered a roadblock. I am trying to assign values to an input element using ng-repeat for a list of parameters. <input type="text" ng-model="parameters.inParameterName" class="form-control ...