Guide to making a circular shape with a gradient outline and see-through center using CSS

Looking to design a circle using CSS with a gradient border, and an inner area that is transparent to achieve this look:

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

While there are methods for creating a gradient border without transparency, such as the one shown in the code below, they typically involve overlaying a solid-colored div over the gradient.

#cont{
background: -webkit-linear-gradient(left top, crimson 0%, #f90 100%);
width: 150px;
height: 150px;
border-radius: 1000px;
padding: 5px;
}

#box{
background: #fff;
width: 150px;
height: 150px;
border-radius: 1000px;
}

#example {

background: url(http://blogs.taz.de/hausblog/files/2017/12/20171208_FB_reuters2.png);
}
<div id="example">
<div id="cont">
<div id="box"></div>
</div>
</div>

Check out the JSFiddle demo here

Answer №1

In my opinion, utilizing a linear gradient with SVG is the optimal approach. The concept involves generating a circle and filling its stroke with a gradient.

body {
  background: url(https://picsum.photos/id/1026/800/800);
}

text {
  font-size:8em
}
<svg viewBox='0 0 200 200' width=150 height=150>
  <!-- define the gradient -->
  <defs>
    <!-- x1,y1,x2,y2 are used to define the gradient direction -->
    <linearGradient id="linear" x1="0%" y1="0%" x2="100%" y2="60%">
      <!-- start color at 0%-->
      <stop offset="0%"   stop-color="crimson"/>
      <!-- end color at 100%-->
      <stop offset="100%" stop-color="#f90"/>
    </linearGradient>
  </defs>
  <!-- Create an svg circle at [100,100] with radius of 90
       make the border-width 6 (stroke-width) fill it with gradient (stroke)
       and make the content of circle transparent (fill)
  --->
  <circle cx="100" cy="100" r="90" stroke="url(#linear)" stroke-width="6" fill="transparent" />
  <!-- Create a text element at the postion [70,140] -->
  <text x="70" y="140" fill="white">7</text>
</svg>

You can also apply this as a background:

body {
  background: url(https://picsum.photos/id/1026/800/800);
}

#count {
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" width="150" height="150"><defs><linearGradient id="linear" x1="0%" y1="0%" x2="100%" y2="60%"><stop offset="0%"   stop-color="crimson"/><stop offset="100%" stop-color="%23f90"/></linearGradient></defs><circle cx="100" cy="100" r="90" stroke="url(%23linear)" stroke-width="6" fill="transparent" /></svg>') no-repeat;
  text-align: center;
  color: #fff;
  font-size: 8em;
  width: 150px;
  height: 150px;
}
<div id="count">
  7
</div>

UPDATE

To achieve this effect without relying on SVG, you can now utilize mask:

body {
  background: url(https://picsum.photos/id/1026/800/800);
}

#count{
  text-align: center;
  color: #fff;
  font-size: 8em;
  width: 150px;
  height: 150px;
  position:relative;
}

#count::before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  border-radius:50%;
  background:linear-gradient(crimson,#f90);
  -webkit-mask:radial-gradient(farthest-side, transparent calc(100% - 8px),#fff 0);
}
<div id="count">
  7
</div>

Related: Border Gradient with Border Radius

Answer №2

Check out this CodePen demonstration: (https://codepen.io/lazercaveman/pen/rNKbxdq) showcasing a solution (or something similar) using CSS to create a completely transparent loading spinner with an angular gradient.

.spinner-icon {
  &:before {
    background-image: conic-gradient(
      transparent 72deg,
      #fff 360deg
    );
    mask: radial-gradient(
      farthest-side,
      transparent calc(100% - 12px),
      #fff 0
    );
  }
}

.bg-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
            linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
            linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);
  border-radius: 12px;
  
}

Answer №3

Change the inner div to blend in with the background color and center the content both vertically and horizontally within the inner div. There are multiple ways to achieve this, but for now, I will use a simple method.

#container{
background: -webkit-linear-gradient(left top, teal 0%, #f90 100%);
width: 200px;
height: 200px;
border-radius: 100px;
padding: 10px;
}

#inner-box{
background: url(http://example.com/image.jpg);
width: 200px;
height: 200px;
border-radius: 100px;
text-align:center;
font-size:5em;
color:#FFF;
display:flex;
justify-content:center;
align-items:center;
font-family:Arial, sans-serif;
}

#sample {

background: url(http://example.com/image.jpg);
}
<div id="sample">
<div id="container">
<div id="inner-box">5</div>
</div>
</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

Tips for ensuring a custom list item remains at the top when the screen size decreases

My unordered list with custom bullets is displaying some unexpected behavior. When the screen size drops below 364px, the list items wrap to two lines. However, the custom bullet ends up on the second line instead of the first. How can I fix this for smal ...

After running assets:install and assetic:dump, Font Awesome fonts are no longer functioning properly

Setting up a site on shared hosting has been going smoothly, except for the FontAwesome icons which Symfony is having trouble finding in their designated locations. The steps I followed to move the site to production on shared hosting are: To overcome th ...

Easy Navigation Slider with CSS3 and jQuery

I'm currently working on implementing a push menu on my website. The objective is to have the menu slide out from the left and overlap the current page content. I've written the code below but it doesn't seem to be functioning as expected. C ...

What is the reason behind the modification in flex item size when align-items property is applied?

When creating the responsive design, I utilized flexbox. Surprisingly, without changing the size of each flex-item, applying align-items:center caused the size of the first flex item (PICTURE 2) to change when displayed in a vertical view. On the other han ...

Visitors may not immediately notice the CSS/JavaScript modifications in their browsers

Currently, I am working on a web application utilizing Spring MVC and Hibernate, deploying it on Apache Tomcat 8. Most of my users access the app using Google Chrome. However, I have encountered an issue where whenever I update the CSS or JavaScript files ...

Using the HTML video tag to apply different background colors based on the individual machines being

My web application includes a video that is set to play against the same background color as the webpage. However, I am experiencing an issue where the video background appears differently on various machines using the same browser. What I would like to ac ...

What is the best way to style only textboxes with CSS without affecting other input types such as checkboxes?

If attribute selectors were universally supported by all browsers, we could effortlessly implement the following styling: input[type='text'] { font:bold 0.8em 'courier new',courier,monospace; } input[type='radio'] { margin:0 ...

What dimensions are optimal for images on a Responsive web design?

In the process of making our website responsive, I have a question regarding image sizes. If I want images to fill the entire screen, excluding the navigation header, what specific dimensions should these images be in order to display correctly on all type ...

Tips for creating a button hover effect with dynamic color and opacity

On my webpage, the button color is determined by an admin using JavaScript on page load. Since I don't know what color will be used, I can't apply rgba(). While using opacity does change the button's color, it also affects the text, which is ...

Picking an art exhibit to visit

Can you provide recommendations for a gallery suitable for my web project? The project utilizes: jQuery Bootstrap An ideal gallery would be based on jQuery. Here are the requirements and preferences: Compact CSS and JavaScript files. Utilization of the ...

Position Bootstrap 5 modal footer elements to the left and right for an enhanced visual

Can anyone assist me with customizing the Bootstrap 5 modal footer? I am looking to align a text (either using span or label) to the left and a button to the right within the modal footer. I came across this solution and attempted to implement it by cha ...

Is it possible to incorporate a LINK within the CSS GRID layout?

Hey everyone, I've encountered an issue with my link placement in the grid. I'm looking to turn the entire box into a clickable link. * { padding: 0; margin: 0; } body { font-family: Hiragino Kaku Gothic Pro W3, Arial; } .title-conta ...

Positioning the dropdown menu on the right with Boostrap 5.2 Navbar

After grappling with this problem for the past 72 hours, I've exhausted all my options and still can't seem to resolve it. I am currently working with Bootstrap 5.2 and have implemented a Navbar with a dropdown menu. My objective is to position i ...

What is the best way to close the space between a box-shadow and a div background when incorporating semi-transparent rgba() values?

There seems to be an issue arising when applying partially transparent rgba() background-color and box-shadow color values to an element with a non-fixed (percent-based) border-radius. This combination results in a minuscule transparent gap appearing at th ...

Is there a way to reference a different CSS class within another class?

Currently, I am working on creating a navbar using React. In this navbar, I have three components for the menu bar: text, logo, and buttons. The background color of the navbar is set to transparent, while the components are black by default. However, whe ...

What steps can be taken to resolve the problem of CSS sprite causing blurry images on mobile devices when using background-position?

In the world of CSS, working with regular images is usually straightforward - setting the width to 50px will display them perfectly on both desktop and mobile devices without any issues as long as the image quality is good. However, when it comes to using ...

MVC - The HTML table is only showing the external border

I'm facing a challenge with my MVC view that has a table filled from a partial view. The table consists of one set of <thead><th></th></thead> and then a collection of <tr></tr>. In the full view, I have defined the ...

Set the color of the text in the Material UI pagination component to a subtle shade

I would like to customize the color of the text in Material UI's pagination component. Specifically, I want the action button to be white and the text portion to be grey, similar to the left action arrow in the image below. Is there a way for me to ac ...

Centering an item using Bootstrap

I am using bootstrap in reactjs and attempting to center a text in the middle of the screen, but it seems to not be working. Can someone please point out what I am doing wrong? <div className="h-100 d-flex align-self-center justify-content-center&q ...

Issues with implementing Dark mode in TailwindCSS with Nuxt.js

After spending a couple of days on this, I'm still struggling to get the dark mode working with Tailwind CSS in Nuxt.js. It seems like there might be an issue with the CSS setup rather than the TypeScript side, especially since I have a toggle that sw ...