What is the method for creating a curved line in CSS using span elements?

I am looking to create a half arc in CSS with various colors, similar to the image provided.

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

Here is the code I have used:

HTML

 <div class="gradient">
   <span class="one"></span>
   <span class="two"></span>
   <span class="three"></span>
   <span class="four"></span>
   <span class="five"></span>
 </div>

CSS

.gradient span{
height: 10px;
display: block;
width: 100px;
}

.gradient .one{
  background-color: red;
}

.gradient .two{
  background-color: green;
}

.gradient .three{
  background-color: yellow;
}

.gradient .four{
  background-color: black;
}

.gradient .five{
  background-color: orange;
}

I am actually trying to create a half gauge meter in CSS with rounded corners for each span element.

Answer №1

If you want to create something similar but without rounded corners on the colors, you can use a combination of conic-gradient and radial-gradient.

.round {
  --start: -90deg;
  --m: 1.8deg;
  position: relative;
  width: 200px;
  aspect-ratio: 1;
  border-radius: 50%;
  transform: rotate(var(--start));
  background-image: 
    radial-gradient(circle at 50% 50%, white 0%, white 66%, transparent 66%, transparent 100%),
    conic-gradient(#ff8b8b calc(0deg + var(--m)),
      #ff8b8b calc(36deg - var(--m)),
      transparent calc(36deg - var(--m)),
      transparent calc(36deg + var(--m)),
      #ffd68b calc(36deg + var(--m)),
      #ffd68b calc(72deg - var(--m)),
      transparent calc(72deg - var(--m)),
      transparent calc(72deg + var(--m)),
      #f7ff8b calc(72deg + var(--m)),
      #f7ff8b calc(108deg - var(--m)),
      transparent calc(108deg - var(--m)),
      transparent calc(108deg + var(--m)),
      #bfff8b calc(108deg + var(--m)),
      #bfff8b calc(144deg - var(--m)),
      transparent calc(144deg - var(--m)),
      transparent calc(144deg + var(--m)),
      #68ff68 calc(144deg + var(--m)),
      #68ff68 calc(180deg - var(--m)),
      transparent calc(180deg - var(--m)),
      transparent calc(360deg + var(--m))
  );
}
<div class="round"></div>

EDIT

To make it appear more rounded, you can add some small dots.

.round {
  --start: -90deg;
  --m: 3deg;
  --bw: 6px;
  position: relative;
  width: 200px;
  aspect-ratio: 1;
  border-radius: 50%;
  transform: rotate(var(--start));
  background-image:
    conic-gradient(transparent var(--m),
      #ff8b8b var(--m),
      #ff8b8b calc(36deg - var(--m)),
      transparent calc(36deg - var(--m)),
      transparent calc(36deg + var(--m)),
      #ffd68b calc(36deg + var(--m)),
      #ffd68b calc(72deg - var(--m)),
      transparent calc(72deg - var(--m)),
      transparent calc(72deg + var(--m)),
      #f7ff8b calc(72deg + var(--m)),
      #f7ff8b calc(108deg - var(--m)),
      transparent calc(108deg - var(--m)),
      transparent calc(108deg + var(--m)),
      #bfff8b calc(108deg + var(--m)),
      #bfff8b calc(144deg - var(--m)),
      transparent calc(144deg - var(--m)),
      transparent calc(144deg + var(--m)),
      #68ff68 calc(144deg + var(--m)),
      #68ff68 calc(180deg - var(--m)),
      transparent calc(180deg - var(--m)),
      transparent 360deg);
}

.round::before {
  content: '';
  position: absolute;
  border-radius: 50%;
  inset: var(--bw);
  background-color: white;
}

.round span {
  position: absolute;
  left: 50%;
  transform-origin: 50% 100%;
  transform: rotate(var(--d));
  display: block;
  width: 0.1px;
  height: 50%;
}

.round span::before {
  content: '';
  display: block;
  width: var(--bw);
  aspect-ratio: 1;
  border-radius: 50%;
  background-color: var(--clr);
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}
<div class="round">
  <span style="--d: calc(0deg + var(--m)); --clr: #ff8b8b;"></span>
  <span style="--d: calc(36deg - var(--m)); --clr: #ff8b8b;"></span>
  <span style="--d: calc(36deg + var(--m)); --clr: #ffd68b;"></span>
  <span style="--d: calc(72deg - var(--m)); --clr: #ffd68b;"></span>
  <span style="--d: calc(72deg + var(--m)); --clr: #f7ff8b;"></span>
  <span style="--d: calc(108deg - var(--m)); --clr: #f7ff8b;"></span>
  <span style="--d: calc(108deg + var(--m)); --clr: #bfff8b;"></span>
  <span style="--d: calc(144deg - var(--m)); --clr: #bfff8b;"></span>
  <span style="--d: calc(144deg + var(--m)); --clr: #68ff68;"></span>
  <span style="--d: calc(180deg - var(--m)); --clr: #68ff68;"></span>
</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

Angular pagination with enhanced animation effects

I found a helpful tutorial on creating an expandable list using Angular. It's working fine, but I wanted to add an animation when the div expands or collapses. I tried adding a class with a transition in the max-height property, but it didn't wor ...

Ensure tables are vertically centered when printing an HTML page

I need to export two tables, each measuring 7cm×15cm, to a PDF file with A4 portrait paper size using the browser's "Save to PDF" option in the print tool. My goal is to center these two tables vertically on the A4 paper. If that proves difficult, I ...

Customize the appearance and text in the navigation bar

Currently, I am in the process of creating my own personal website template. However, I have encountered an issue with styling the image and text within the navbar. I am looking to adjust the aspect ratio of the image in order to center it within the n ...

Header stabilization on scroll

On my webpage, I have a table header positioned in the middle of the page. However, due to the length of the page, I am looking for a way to make the header stay fixed at the top of the browser as the user scrolls down. My query is: Is there a method to k ...

Display method JSONized

Looking for guidance on improving code efficiency and best practices. In my current project, I am working with a JSON Array structured like this: [ { "Date": "2014-07-16", "DiscPoint": "Description 1", "DisBy": "Person 1" ...

Incorporate an animated loading GIF image within the ajaxStart function, dynamically appending it within a div tag

Is there a way to dynamically display a loading GIF image on all HTML web pages? var loading = $(' <div id="loading"> <img src="../img/loading%20(1).gif" id="loading-image" alt="Loading..." /> </div>'); $(document).ajaxStart( ...

Unable to locate image in Wordpress navigation bar

Currently, I am in the process of creating a website for a client using a pre-existing theme. However, I am facing difficulty in locating certain images that are supposed to appear on the side of the navbar as per the client's styling preferences. Des ...

The JavaScript script to retrieve the background color is malfunctioning

I am currently working on developing a highlighting feature for an HTML table that will dynamically change the row colors on mouseover. Below is the code snippet I have been using, but it seems to be experiencing some issues. Any assistance would be greatl ...

Unusual problem arises with image hover on Chrome browser

I'm currently working on an HTML/CSS website that incorporates an image magnification effect when hovering over images. Everything seems to be functioning correctly, except for one minor issue in Chrome. Upon page load, the images seem to jitter and r ...

retrieving the selected date from the calendar widget

I'm utilizing jQuery's datepicker to collect date inputs from users. The form is structured as below: <form id="myform" action="/graphs/get_builds" method="post"> Start: <input type="text" id="start" /> End: <input type="t ...

Trigger CSS3 animation only once upon the page loading

I'm in the process of creating a stylish landing page using CSS3. One element that really stands out is an <a> tag with a cool animation: @keyframes splash { from { opacity: 0; transform: scale(0, 0); } 50% { ...

Embed JavaScript locally within an iframe HTML

When attempting to add HTML code within an iframe to showcase data, everything works as expected. However, the issue arises when trying to include any JavaScript within the iframe, as it doesn't seem to be recognized locally. Below is the example cod ...

Is there a way to automatically trigger onClick events to repeat or make functions auto repeat?

The current situation is as follows: I currently have a button that, when clicked, sends its ID to a PHP page. The table is then cleared and a new table is printed in the column. The issue I am encountering now is that I want the table to be automaticall ...

Shadows on menu buttons transform when clicked using React and CSS

I'm currently working on customizing the styling of a menu using CSS in a project that involves the use of "react-horizontal-scrolling-menu". While I've been successful in styling the menu items with .menu-item:hover & .menu-item:active, I am ...

I am struggling to retrieve the value of my angular variable

I am working on an ajax request that builds an angular repeater, and I have an event html: <md-button ng-if="item[2].enderecoGlgMaps !== null" class="md-icon-button" ng-click="GeraMapa('{{item[2].nomeSlug}}');" aria-label="Localizacao-Butto ...

How can I modify the color of a jQuery Mobile Select menu?

Is there a way to dynamically modify the color (background and text) of a single select menu in jQuery Mobile, without impacting other elements with the same class? I've experimented with various approaches, such as: $('#select').css({color ...

Can you customize the background color for the entire section where the first child appears in a nested collapsible list with CSS?

In my current setup, I have a nested list within a larger container box. Each ul element has a border-top style applied, while each li element has a border-bottom and padding. The HTML code looks like this: <div class="menu"> <ul ...

Using Typescript in React to render font colors with specific styling

Attempting to utilize a variable to set the font color within a react component, encountering an error with my <span>: Type '{ style: "color:yellow"; }' is not assignable to type 'HTMLProps<HTMLSpanElement>' The use of yel ...

Tips for extracting information from a table containing constantly changing data values

It seems like a bit of a challenge as the website developers are working to prevent this. I'm attempting to create a basic app to access and display data from a table on a website. The table can be found here: However, the table data appears after t ...

Ways to present a pop-up dialog box featuring word corrections

I have developed a word correction extension that encloses the incorrect word in a span element. Upon hovering over the word, a drop-down menu with possible corrections should be displayed. However, my current code is not functioning properly. How can I en ...