Create circular shapes using the border-radius property

I've been experimenting with converting some divs using border radius and I've almost got it, but sometimes they end up looking like 'eggs' haha. Here is the CSS code I'm using:

#div{   /*central div*/
    position:absolute;
    top:50%;
    margin-top: -20%;
    left:50%;
    margin-left: -20%;
    background: #00A8D9;
    width: 40%;
    height: 40%;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
    border-radius: 50%;
    border:3px solid #000;
}
#divSupIzq,#divSupDer,#divInfIzq,#divInfDer{    /*Individual divs: top-left , top-right, bottom-left, bottom-right*/
    background: #ddd;
    width: 20%;
    height: 20%;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
    border-radius: 50%;
    border:3px solid #fff;
    position:absolute;
}
#divSupIzq{  /*Top left Div*/
    top:15%;
    left:10%;
}
#divSupDer{ /*Top right Div*/
    top:15%;
    right:10%;
}
#divInfIzq{ /*Bottom left Div*/
    bottom:15%;
    left:10%;
}
#divInfDer{ /*Bottom right Div*/
    bottom:15%;
    right:10%;
}

In my HTML, I'm using JavaScript/jQuery to change the content of each div (text in top-left, top-right, bottom-left, bottom-right divs; and a number in the central div) based on the size of each div:

$('#div').resize(function(height){
                    var font = $(this).height()/2;
                    var margin = (font / 2)-5;
                    $('.content').css('font-size',font+'px');
                    $('.content').css('margin-top',margin+'px');  
                });

This is how it appears in Ripple extension for Chrome:

Can anyone help me ensure that the divs always maintain a circular shape, and not appear as eggs? Thanks in advance, Daniel

Answer №1

Creating a circular shape:

Using HTML

<div id="circle"></div>

Applying CSS

#circle {
    width: 100px;
    height: 100px;
    background: red;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 50px;
}

Click here to see the demonstration.

Example with fixed width and height: http://jsfiddle.net/eC3jq/1/

Achieving proper % width and height by containing circle within a div: http://jsfiddle.net/eC3jq/2/

Resource: CSS-Tricks

Answer №2

JQuery

Here is a useful piece of code that you can copy to your website for it to work. Alternatively, you can check out a demo:

https://jsfiddle.net/whLctrp4/

This is the HTML code with a JQuery function call:

<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
        <link rel="stylesheet" type="text/css" href="style.css">

    </head>
    <body>

    <div class="pies">
    </div>   

Include JQuery

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

The drawPie function takes parameters such as id/class HTML attribute, size (in pixels), percent of filling, and color of the pie.

    <script>

    function drawPie (id, size, percent, color) {                               
        var sizeString = "" + size + "px";                      
        var grad = 360/100*percent+90;      
        console.log(grad);
        var pie = $("<span></span>");

        pie.css({"width": sizeString, 
            "height": sizeString,               
            "display": "block",
            "border-radius": "50%",
            "background-color": color,                          
            "float": "left",
            "margin": "5px"             
        });         

        if(percent <= 50){
            pie.css({"background-image": "linear-gradient("+ grad + "deg, transparent 50%, white 50%), linear-gradient(90deg, white 50%, transparent 50%)"});
        } else if (percent >= 100) {
            pie.css({"background-image": "none"});
        } else {                                
            pie.css({"background-image": "linear-gradient("+ (grad+180) + "deg, transparent 50%, "+color+" 50%), linear-gradient(+90deg, white 50%, transparent 50%)"});                
        }

        $(id).append(pie);
    }

A for loop to show how drawPie function works:

    for(i=0; i<=100; i+=1){
        drawPie(".pies", 100, i, "orange");
    }

    </script>       

    </body>

</html>

Answer №3

To see the demo in action, click on this link: http://jsfiddle.net/AqSvP/4/

This particular demonstration functions by utilizing the resize event of the window object instead of the div itself. With each resize event, the div and its border radius are adjusted to appear as a perfect circle (ensuring width equals height and radius equals half of the width).

Here is the code snippet:

$(window).resize(function(height) {
    var div = $('#div');
    var width = $('body').width() * 0.4;
    var radius = width/2;
    width += 'px';
    radius += 'px';
    div.css({
        width: width, height: width,
        '-moz-border-radius' : radius,
        '-webkit-border-radius' : radius,
        'border-radius' : radius
    });

    // additional code for adjusting font-size etc..
});

$(window).resize();

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

How to Incorporate an Anchor Link into a Div as well as its Pseudo Element using CSS, Javascript, or jQuery

How can I make both the menu item and its icon clickable as a link? When either is clicked, the link should work. Here is a CodePen example: http://codepen.io/emilychews/pen/wJrqaR The red square serves as the container for the icon that will be used. ...

Internet Explorer will automatically apply a blue border to a div element when a CSS gradient is utilized

I'm attempting to create a gradual fading gray line by using a div that is sized at 1x100px with a CSS gradient applied for the fade effect. The only issue I am encountering is in Internet Explorer where the div is displaying a blue border which I am ...

What are the best techniques for styling a SELECT box with HTML and CSS?

I've received a form from a talented designer which includes SELECT inputs designed in a unique manner. Despite my attempts to add paddings, adjust backgrounds, I'm unable to replicate the exact appearance of the select box shown in the image. ...

Is your sticky sidebar getting in the way of your footer?

I've developed a custom sticky sidebar for displaying ads, but I'm facing an issue. When I scroll to the bottom of the page, it overlaps with the footer. Can someone please take a look at this? - var stickySidebar = $('.sticky'); if ...

"Enhance your webpage with Flexslider and captivating stretched images

When working with Flexslider, I encountered a challenge where the image was being stretched to fit the width of the flexslider. I prefer the image to be displayed centered, keeping its original dimensions, and having a black background-color. Is there a ...

jQuery hover once the DOM has been updated

Here's my approach to utilizing jQuery hover: hover( handlerIn(eventObject), handlerOut(eventObject) ) $(".box").each(function() { var $this = $(this); $this.data('baseColor', $this.css('background-color')); $thi ...

What is the best way to customize the MenuProps of Material UI Select component using createTheme?

I'm trying to update the dropdown menu style of my Material UI Select component using createTheme, but I'm having trouble getting it to work. Here is the code I have so far. Can you spot what might be causing the issue? import { createTheme } fr ...

A guide on retrieving nested objects from my API and parsing the resulting data in each iteration for individual elements

Struggling to update my website with a new API, specifically with accessing nested objects in a loop. Still trying to wrap my head around ajax and JSON concepts. Take a look at the API output and JS code I've been working on for a better understanding ...

Using more than one submit button in an HTML form

I am attempting to include multiple buttons on a single form. I would like to perform different actions on the form depending on which submit button is clicked. <script type="text/javascript> $("#<portlet:namespace/>Form").submit(function( ...

The typewriter effect is configured to appear as a separate layout,

<div className= "HomePage-typewriter"> I do{'\u00A0'} <Typewriter options={{ strings: [ '<span> this </span>', '<span> that </span>', '<span&g ...

Invisibility of form data on new page - PHP and Javascript

I have a webpage with multiple hyperlinks. Each hyperlink should redirect the user to a new page with the URL sent as POST data. What I can do: 1. Open the new page successfully. The issue I'm facing: 1. On the new page, I cannot access the URL ...

What is the best way to display two radio buttons side by side in HTML?

Looking at my HTML form in this JSFiddle link, you'll see that when the PROCESS button is clicked, a form with two radio buttons appears. Currently, they are displayed vertically, with the female radio button appearing below the male radio button. I& ...

Guide to repairing a CSS ball animation

I am attempting to create a simulation where a ball moves in the following pattern: bottom right -> top center -> bottom (/) left. However, my animation seems to be moving from top right -> bottom center -> top left (/). #stage { height:300px; border: ...

Form submission button gets stuck after AJAX request is made

Hey there, I'm currently working on a form that utilizes Ajax for making a post in Rails 4. The JavaScript in the form includes some validation checks and is supposed to display an alert message upon successful posting. However, I'm facing an iss ...

Enhancing user experience by implementing dynamic text box functionality that triggers ajax load for dropdown options using a combination

Here is the task I need help with, along with the code snippet: I want to enable the author select box when both the start and end dates are filled out. The author names should be fetched dynamically from the database based on the selected start and end ...

Customize the progress bar color in Bootstrap by setting a unique color to it

Is it possible to apply a custom color to the progress bar in Bootstrap that is not one of the standard options like success, info, warning or danger? I attempted adding the following code snippet to my bootstrap-theme.css file: .progress-bar-custom { b ...

Using Google Fonts in a Typescript React application with JSS: A step-by-step guide

How can I add Google fonts to my JSS for use in styling? const styles = ({palette, typography}: Theme) => createStyles({ time: { flexBasis: '10%', flexShrink: 0, fontSize: typography.pxToRem(20) }, guestname: ...

What is the best way to eliminate all instances of the period symbol from an array?

How can I eliminate all instances of . from my array? var arr = ['...my name is apple', 'my girl .... friend is banana.......']; Below is the code snippet I am currently using. var arr = ['...my name is apple', 'my g ...

Is it possible to implement a feature in Angular and Bootstrap where the toggle menu can be closed by clicking anywhere on the page, rather than just the toggle button

I'm working on an Angular project where I've implemented a navbar component. The navbar is responsive and includes a toggle button that appears when the browser window is resized. This button allows users to hide or display the menus. One issue ...

Magnific Popup displaying only the initial item

As someone new to using jQuery and Magnific Popup, I am working on a grid of images. When an image is clicked, I want Magnific Popup to display a specific div containing information relevant to that particular image. <div class="grid"> <div c ...