Ora

How to make an oval button in CSS?

Published in CSS Button Styling 6 mins read

To create an oval button in CSS, the most effective method is to apply the border-radius property to a rectangular HTML element, adjusting its values to curve the corners into an elliptical shape. This typically involves setting border-radius to 50% or half of the element's height.


The Core Method: border-radius Property

The border-radius CSS property is the key to transforming a basic rectangular button into an elegant oval. By manipulating this property, you can control the curvature of each corner, allowing for a perfectly rounded or oval appearance.

An oval shape is achieved when a rectangular element has its corners rounded significantly enough to form semi-circles on its shorter sides, while the longer sides remain relatively straight or form a wider, shallower curve.

Let's explore the common approaches:

1. Simple Oval Using border-radius: 50%;

This is often the easiest way to create an oval button. If your button has a width greater than its height, applying border-radius: 50% will automatically round the ends into perfect semi-circles, resulting in an oval shape.

HTML Structure for a Button:

<button class="oval-button">Click Me</button>

CSS for an Oval Button with Percentage border-radius:

.oval-button {
  background-color: #007bff; /* Example background color */
  color: white;
  padding: 10px 25px; /* Adjust padding to control width/height ratio */
  border: none;
  border-radius: 50%; /* This makes the magic happen */
  cursor: pointer;
  font-size: 16px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  transition: background-color 0.3s ease;
}

In this example, the padding: 10px 25px; makes the button rectangular (wider than it is tall). border-radius: 50%; then rounds its shorter sides completely, forming an oval.

2. Using Pixel Values for border-radius

You can also use explicit pixel values for border-radius. To achieve an oval, set the border-radius value to half of the button's height.

CSS for an Oval Button with Pixel border-radius:

.oval-button-pixels {
  background-color: #28a745;
  color: white;
  padding: 10px 25px; /* Provides internal spacing */
  height: 40px; /* Explicit height */
  border: none;
  border-radius: 20px; /* Half of the height (40px / 2 = 20px) */
  cursor: pointer;
  font-size: 16px;
  display: inline-flex; /* Use flexbox for vertical alignment if needed */
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s ease;
}

In this case, border-radius: 20px; (half of height: 40px;) ensures the vertical ends are perfectly rounded, creating an oval. A simple border-radius: 16px; would create a slightly less rounded, but still distinct, oval if the height was larger (e.g., 50px).

3. Controlling Individual Corners with Four Arguments

The border-radius property can also accept up to four values to control each corner independently (top-left, top-right, bottom-right, bottom-left). While less common for a uniform oval, understanding this flexibility is useful.

Syntax for Four Values:
border-radius: top-left-radius top-right-radius bottom-right-radius bottom-left-radius;

For instance, border-radius: 10px 20px 30px 40px; would give each corner a different curve.
To achieve an oval using this method, you would typically set all four values to be consistent, such as border-radius: 20px 20px 20px 20px; which is equivalent to border-radius: 20px;.

Understanding border-radius Values for Oval Shapes

The border-radius property offers versatile ways to define corner curvature:

  • Single Value (e.g., border-radius: 20px; or border-radius: 50%;): Applies the same radius to all four corners. For an oval button, 50% or a pixel value equal to half the button's height is most effective when the button's width is greater than its height.
  • Two Values (e.g., border-radius: 10px 30px;): The first value applies to the top-left and bottom-right corners, and the second value applies to the top-right and bottom-left corners. This is generally not used for standard oval shapes.
  • Four Values (e.g., border-radius: 10px 20px 30px 40px;): Allows you to specify a different radius for each corner in the order: top-left, top-right, bottom-right, bottom-left. While powerful, it's rarely used for simple oval buttons as consistent rounding is desired.
  • Slash Syntax for Elliptical Corners (e.g., border-radius: 50px / 20px;): This advanced syntax allows you to define different horizontal and vertical radii for the corners. For example, border-radius: 50% / 50%; is equivalent to border-radius: 50%;. However, you could use border-radius: 30px / 50px; to create a more squashed or stretched oval if desired, though 50% on a rectangular button is usually sufficient. Learn more about border-radius at MDN Web Docs.

Best Practices for Oval Buttons

  • HTML Structure: Always use semantic HTML elements like <button> for actions or <a> for navigation links.
  • Sizing: Instead of fixed width and height, consider using padding to allow the button to adapt to its content, making it more responsive.
  • Text Overflow: Ensure that the text within your button doesn't overflow, especially if you use fixed dimensions.
  • Interactivity: Add :hover and :active states to provide visual feedback to users.

Quick Reference: border-radius for Ovals

Scenario border-radius Value Effect
Rectangle (width > height) 50% Creates a perfectly rounded oval with semi-circular ends.
Rectangle with fixed height height / 2 (e.g., 20px for height: 40px;) Also creates a perfectly rounded oval.
Less rounded oval 16px (or any pixel value) Creates an oval shape with a less aggressive curve than 50% or height/2.
Custom elliptical shape value1 / value2 (e.g., 30px / 50px;) Defines distinct horizontal and vertical radii for elliptical corners.

HTML Structure for Your Button

It's good practice to wrap your button text within a semantic tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Oval Button Example</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <h2>Oval Button Examples</h2>

    <button class="oval-button">Submit Form</button>
    <a href="#" class="oval-link">Learn More</a>

</body>
</html>

CSS Styling for an Oval Button

Here's a comprehensive CSS example combining the techniques:

/* General button styling */
.oval-button, .oval-link {
  display: inline-flex; /* Allows content alignment and respects padding */
  align-items: center; /* Vertically centers text */
  justify-content: center; /* Horizontally centers text */
  padding: 12px 30px; /* Controls internal spacing and overall size */
  font-family: Arial, sans-serif;
  font-size: 1em;
  font-weight: bold;
  text-decoration: none; /* For links */
  border: none;
  cursor: pointer;
  outline: none; /* Remove default focus outline */
  transition: background-color 0.3s ease, transform 0.1s ease;
  min-width: 120px; /* Ensure a minimum width for consistent shape */
}

/* Specific styling for an oval button */
.oval-button {
  background-color: #007bff;
  color: white;
  border-radius: 50px; /* A large pixel value or 50% will create an oval */
  /* If padding is 12px vertical, the total height is approx 12*2 + font-size.
     A border-radius of 50px (or 50%) ensures fully rounded ends for most heights. */
}

/* Styling for an oval link button */
.oval-link {
  background-color: #6c757d;
  color: white;
  border-radius: 50%; /* Excellent for making links oval if width > height */
}

Enhancing User Experience (Hover Effects)

Adding hover effects makes your button more interactive and user-friendly.

.oval-button:hover {
  background-color: #0056b3; /* Darker blue on hover */
  transform: translateY(-1px); /* Slight lift effect */
}

.oval-link:hover {
  background-color: #5a6268; /* Darker grey on hover */
  transform: translateY(-1px);
}

.oval-button:active,
.oval-link:active {
  transform: translateY(0); /* Press down effect */
}

Accessibility Considerations

When creating buttons, always ensure they are accessible:

  • Keyboard Navigation: Use <button> or <a> elements for native keyboard support.
  • Focus States: Ensure :focus styles are clear (e.g., outline: 2px solid #0056b3; outline-offset: 2px;) so users can see where they are on the page.
  • Color Contrast: Maintain sufficient color contrast between the text and background for readability. You can check contrast with tools like WebAIM Contrast Checker.

By following these guidelines and utilizing the border-radius property effectively, you can easily create visually appealing and functional oval buttons in CSS.