How can I create rounded corners and circles with CSS?

CSS offers powerful features to create visually appealing elements with rounded corners and circles. By applying border-radius and some creative styling, you can easily transform regular elements into smooth, modern shapes. In this comprehensive guide, we will explore various techniques to create rounded corners and perfect circles with CSS. Whether you want to round the corners of a div, create circular images, or design elegant buttons, these techniques will help you achieve stunning visual effects that enhance your web design.

1. Rounded Corners

Rounded corners soften the appearance of elements, making them more visually appealing and friendly. The border-radius property is used to achieve rounded corners in CSS.

1.1. Rounded Corners on a Box

/* Rounded corners on a box */
.box {
  width: 200px;
  height: 100px;
  background-color: #007bff; /* Blue background */
  border-radius: 10px; /* Rounded corners */
}

In this example, the .box element will have rounded corners with a radius of 10 pixels.

1.2. Rounded Corners on Individual Corners

You can create unique shapes by applying different radii to individual corners.

/* Rounded corners on individual corners */
.custom-shape {
  width: 150px;
  height: 100px;
  background-color: #ff6347; /* Tomato background */
  border-radius: 10px 20px 30px 40px; /* Top-left, top-right, bottom-right, bottom-left */
}

In this example, the .custom-shape element will have rounded corners with different radii for each corner. The values are applied in the order of top-left, top-right, bottom-right, and bottom-left.

1.3. Creating Circular Images

To create circular images, set the border-radius property to half of the element’s width or height.

/* Circular image */
.circular-image {
  width: 100px;
  height: 100px;
  border-radius: 50%; /* Half of the width or height */
}

In this example, the .circular-image element will have a circular shape with a radius equal to half of its width or height.

2. Perfect Circles

Creating perfect circles can be achieved by using the same value for both the width and height of an element along with border-radius: 50%.

2.1. Circle with a Solid Background

/* Circle with a solid background */
.circle {
  width: 150px;
  height: 150px;
  background-color: #4caf50; /* Green background */
  border-radius: 50%; /* Perfect circle */
}

In this example, the .circle element will form a perfect circle with a radius equal to half of its width or height.

2.2. Circle with an Image Background

/* Circle with an image background */
.circle-image {
  width: 200px;
  height: 200px;
  background-image: url('circle-image.jpg'); /* Image background */
  background-size: cover;
  border-radius: 50%; /* Perfect circle */
}

In this example, the .circle-image element will display a perfect circle with an image background. The background-size: cover property ensures that the image covers the entire circle.

3. Creating Ellipses

You can also create ellipses with CSS by specifying different values for the border-radius property.

/* Ellipse */
.ellipse {
  width: 200px;
  height: 100px;
  background-color: #ff9800; /* Orange background */
  border-radius: 50% 25% 50% 25%; /* Horizontal and vertical radii for each corner */
}

In this example, the .ellipse element will form an ellipse with different horizontal and vertical radii for each corner. The values are applied in the order of top-left, top-right, bottom-right, and bottom-left.

4. Border and Background Combinations

You can combine rounded corners, circular elements, and ellipses with various border and background styles to create unique designs.

4.1. Circular Image with Border

/* Circular image with border */
.circular-image-border {
  width: 150px;
  height: 150px;
  background-image: url('avatar.jpg'); /* Image background */
  background-size: cover;
  border-radius: 50%; /* Perfect circle */
  border: 4px solid #333; /* Border style */
}

In this example, the .circular-image-border element will display a circular image with a border.

4.2. Rounded Button

/* Rounded button */
.button {
  padding: 12px 24px;
  background-color: #ff5722; /* Deep orange background */
  border-radius: 25px; /* Rounded corners */
  color: #fff; /* White text color */
  font-size: 16px;
  cursor: pointer;
}

In this example, the .button element will be a rounded button with a radius of 25 pixels.

5. Conclusion

CSS provides various techniques to create rounded corners, perfect circles, ellipses, and unique designs that enhance the visual appeal of your web elements. By utilising the border-radius property and combining it with creative styling, you can create eye-catching shapes and elements that perfectly fit your website’s design. Whether you want to add rounded corners to boxes, create circular images, or design elegant buttons, these techniques will enable you to achieve beautiful visual effects that elevate your web design to the next level. Experiment with different values, border styles, and background combinations to create the desired shapes that best match your design vision. With the techniques covered in this guide, you can create smooth, modern, and visually appealing elements that captivate your audience and enrich the overall user experience.

Scroll to Top