OneBite.Dev - Coding blog in a bite size

rounded corners css

Code snippet for how to rounded corners css with sample and detail explanation

Creating rounded corners for elements in CSS is a common design feature and can add a soft and appealing touch to your website. In this article, we will learn how to create rounded corners in CSS using the border-radius property.

Code snippet for Rounded Corners CSS

Let’s consider the CSS code snippet below:

div {
    border: 2px solid;
    border-radius: 20px;
}

Code Explanation for Rounded Corners CSS

The above CSS code features a div element. Let’s break it down to understand how rounded corners are created in CSS.

  • div: A CSS selector that selects all the div elements on the document.

  • border: 2px solid: This property is specifying that a border should be present around the div element. The border’s width is given a value of 2px here, and the type of border is defined as solid.

  • border-radius: 20px: The border-radius property is used to create rounded corners. The value assigned to the border-radius property defines the radius of the curve. Here it is set to 20px, meaning that the border will curve around the corner in a radius of 20 pixels, creating a rounded appearance.

This is quite a simple and powerful way of adding sophistication to your design elements using CSS. You can adjust the value of the border-radius as per your needs. Setting a larger pixel value will give a more pronounced roundness whereas a smaller value will yield subtly rounded corners. It is worth noting that if the pixel value exceeds half of the smallest side of the element, the element will become an ellipse instead of a rectangle with rounded corners. In short, you have complete control over the roundness of your elements.

css