CSS Rounded Corners
CSS Rounded Corners allow you to apply rounded edges to elements using the border-radius
property. This property is highly flexible and can be used to create shapes like circles, ellipses, or partially rounded elements.
Key Topics
Basic Usage of border-radius
The border-radius
property allows you to round the corners of an element. You can specify a single value to apply uniform rounding to all corners, or multiple values to control each corner individually.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic border-radius</title>
<style>
.rounded-box {
width: 200px;
height: 100px;
background-color: #007BFF;
color: white;
text-align: center;
line-height: 100px;
margin: 20px;
border-radius: 15px;
}
</style>
</head>
<body>
<div class="rounded-box">Rounded Corners</div>
</body>
</html>
Explanation: The border-radius: 15px;
property rounds all corners of the element by 15 pixels.
Creating Circles and Ellipses
You can use border-radius
to create perfect circles or ellipses by applying specific percentages.
<style>
.circle {
width: 100px;
height: 100px;
background-color: #28A745;
color: white;
text-align: center;
line-height: 100px;
border-radius: 50%;
}
.ellipse {
width: 150px;
height: 100px;
background-color: #FFC107;
color: black;
text-align: center;
line-height: 100px;
border-radius: 50% / 25%;
}
</style>
<div class="circle">Circle</div>
<div class="ellipse">Ellipse</div>
Explanation: The border-radius: 50%;
creates a circle, while 50% / 25%
creates an ellipse by applying different radii to the horizontal and vertical axes.
Partial Rounding
Partial rounding allows you to control individual corners using multiple values for border-radius
.
<style>
.partial-rounded {
width: 200px;
height: 100px;
background-color: #DC3545;
color: white;
text-align: center;
line-height: 100px;
border-radius: 15px 50px 0 0;
}
</style>
<div class="partial-rounded">Partial Rounding</div>
Explanation: The border-radius: 15px 50px 0 0;
applies specific rounding values to each corner, starting from the top-left corner and proceeding clockwise.
Complete Example
This example demonstrates the different usages of border-radius
in one layout:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Rounded Corners</title>
<style>
.rounded-box {
width: 200px;
height: 100px;
background-color: #007BFF;
color: white;
text-align: center;
line-height: 100px;
border-radius: 15px;
margin-bottom: 20px;
}
.circle {
width: 100px;
height: 100px;
background-color: #28A745;
color: white;
text-align: center;
line-height: 100px;
border-radius: 50%;
margin-bottom: 20px;
}
.ellipse {
width: 150px;
height: 100px;
background-color: #FFC107;
color: black;
text-align: center;
line-height: 100px;
border-radius: 50% / 25%;
margin-bottom: 20px;
}
.partial-rounded {
width: 200px;
height: 100px;
background-color: #DC3545;
color: white;
text-align: center;
line-height: 100px;
border-radius: 15px 50px 0 0;
}
</style>
</head>
<body>
<div class="rounded-box">Rounded Corners</div>
<div class="circle">Circle</div>
<div class="ellipse">Ellipse</div>
<div class="partial-rounded">Partial Rounding</div>
</body>
</html>
Explanation: This example combines multiple uses of border-radius
to demonstrate its flexibility and applications in web design.
Key Takeaways
- Basic Rounding: Use
border-radius
with a single value for uniform rounding. - Circles and Ellipses: Achieve these shapes with percentages like
50%
or50% / 25%
. - Partial Rounding: Specify values for individual corners for customized designs.
- Flexibility:
border-radius
enables creative and visually appealing designs.