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

Tips for preventing a React component from scrolling beyond the top of the page

I am looking to achieve a specific behavior with one of my react components when a user scrolls down a page. I want the component to reach the top of the page and stay there without moving any further up. Here is an Imgur link to the 'intranet' ...

Triggering the JavaScript KeyUp() event for input values consisting of multiple digits

I'm currently working on a JavaScript project that involves displaying numbers from 1 to N based on user input. I am utilizing the keyup() event for this functionality. When the input field is cleared, it correctly displays nothing thanks to the empty ...

Is it possible to create a collapse and expand animation that does not involve transitioning the `height

After extensively researching, I have come across numerous articles advocating for the use of transform and opacity for achieving smooth CSS transitions. An example can be found here: The prevailing notion always revolves around: ...the optimization ...

Deactivating The Canvas Element

I am currently working on a project that involves using Three.js alongside a menu placed above the canvas element, which is essentially a regular div. However, I have encountered an issue where the canvas element still registers input when interacting with ...

An Overview of Implementing Ajax Calls for Partial Views on Form Submission

Check out my jQuery code below: <script type="text/javascript"> $("#submitfileform").submit(function () { $.ajax({ type: 'POST', contentType: 'application/html;charset=utf-8', dataT ...

The div that scrolls gracefully within its boundaries

Currently, I am working on a task that involves a div containing images that need to be scrolled left and right. I have successfully implemented the scrolling functionality using jQuery. However, I now face the challenge of ensuring that the content stays ...

CSS Flexbox with 3 columns and a grid layout of 4 by 4, with one large item included

I am currently exploring how to implement this layout using Flexbox. So far, I have only managed to come up with the following code snippet: Is there a way to achieve this purely through CSS without altering the HTML structure, considering it is generated ...

Building Dynamic Forms with React.js and Bootstrap for Easy Input Field Management

In the process of developing a web application using React.js and react-bootstrap, I encountered an interesting challenge. On one of the form pages, users should be able to input symptoms of an illness they are dealing with. The key functionality required ...

Is the homepage the only page rendering correctly?

After working on an MVC project for over 6 months, I consistently faced a problem with HTML and CSS. The homepage always rendered correctly, as shown here: http://prntscr.com/tucp1 However, all other pages had strange white spaces between words, like in t ...

Is there ever a situation when it's acceptable to forego graceful degradation?

Currently, I am constructing a video-sharing CMS that heavily relies on jQuery and ajax for various functionalities such as rich UI effects and data submission/retrieval from the database. Unfortunately, if JavaScript is disabled, about 90% of the function ...

Contrast between employing element selector for concealment and activation tasks

I can't figure out why $('#mdiv input')[1].hide(); isn't working while $('#mdiv input')[1].click(); works perfectly fine. Firstly, I'm curious to understand why. Secondly, how can I get it to work without knowing the id ...

Customizing the style of a dropdown menu in React Material UI using NativeSelect

In our previous projects, we have utilized the NativeSelect components. Now, the task at hand involves altering the appearance of the dropdown Menu within the NativeSelect. If we were to switch to using Select instead, employing the MenuProps property wo ...

Personalized information boxes for particular data points within highcharts

When hovering over specific points, I want to display unique warnings or explanations in the tooltip. I have included custom text like 'WARNING' and 'WARNING 2' within the series data but am struggling to retrieve that data for each too ...

Make the inner div within the td stretch to cover the entire height of the td

Inside the first tds, there is dynamic text content that automatically expands the trs and tds. However, the second tds have a div with a background image but no text inside. I want these divs with background images to expand or collapse along with the tab ...

VueJS component fails to remain anchored at the bottom of the page while scrolling

I am currently using a <md-progress-bar> component in my VueJS application, and I am trying to make it stay fixed at the bottom of the screen when I scroll. I have attempted to use the styles position: fixed;, absolute, and relative, but none of them ...

Trigger specific scripts again after loading jQuery AJAX

Is there a way to make specific scripts re-run after an AJAX load is completed? ...

What is the best way to incorporate a sliding effect into my cookie form?

I created a cookie consent form for my website and I'm looking to include a sliding effect when it first appears and when it disappears after the button is clicked. How can I implement this sliding effect into the form? Here's the HTML, CSS, and ...

Customizing Geonames JSON Ajax Request

Having found the code I needed from a sample website, I am now seeking help to customize it to only display results from the USA. This is the current code snippet: $(function() { function log( message ) { $( "<div>" ).text( message ).pr ...

Is it possible to utilize $(this) within the $.post() function?

I seem to be having trouble accessing $(this) from inside the $.post section. It works perfectly fine outside of it. Here is the JavaScript code: $('.idea').each(function(){ var title = $(this).html(); $.post("votes.php", { t ...

Issue with fixed header in Vuetify v-data-table not functional

While working with the Vuetify v-data-table component, I have enabled the fixed-header property. However, I have noticed that the table header is scrolling along with the table body. I am using the latest version of Chrome. Does anyone know how to addres ...