How to Ensure Uniform Visual Border-Radius Across Buttons of Varying Sizes with CSS calc() Function

I’m currently tackling a project that involves the use of two buttons with different sizes. Despite both buttons having the same specified border-radius of 6px, they seem to display differently in the browser. The smaller button appears to have a larger border-radius than the larger button, even though they are set to the same pixel value.

My objective is to make the smaller button look like a scaled-down version of the larger button, maintaining a consistent visual border-radius. This would ensure that both buttons appear identical in terms of their border-radius, regardless of their individual sizes.

Is there a method to achieve this using CSS calc()? I am seeking a solution involving calc() since I am restricted from altering the 6px value as it is designated as a variable in my project.

.small {
    padding: 0.1rem 0.6rem;
    font-size: 0.475rem;
    color: white;
    border-radius: 6px;
    background-color: red;
    border: none;
}

.large {
    padding: .5rem 3rem;
    font-size: 1rem;
    color: white;
    border-radius: 6px;
    background-color: red;
    border: none;
}
<button class="small">Test</button>
<button class="large">Test</button>

.small {
    padding: 0.1rem 0.6rem;
    font-size: 0.475rem;
    color: white;
    border-radius: calc(6px / 2); // This is my attempt to address the issue
    background-color: red;
    border: none;
}

.large {
    padding: .5rem 3rem;
    font-size: 1rem;
    color: white;
    border-radius: 6px;
    background-color: red;
    border: none;
}
<button class="small">Test</button>
<button class="large">Test</button>

Answer №1

To maintain a consistent border-radius across buttons of varying sizes, CSS's calc() function can be utilized to adjust the border-radius in proportion to the button size.

This approach ensures that the smaller button appears appropriately scaled with the border radius.

:root {
    --base-border-radius: 6px;
    --base-font-size: 1rem; /* Ensure this matches the font-size of the larger button */
}

.small {
    padding: 0.1rem 0.6rem;
    font-size: 0.475rem;
    color: white;
    border-radius: calc(var(--base-border-radius) * (0.475 / 1)); /* Adjust based on font-size ratio */
    background-color: red;
    border: none;
}

.large {
    padding: .5rem 3rem;
    font-size: 1rem;
    color: white;
    border-radius: var(--base-border-radius);
    background-color: red;
    border: none;
}
<button class="small">Test</button>
<button class="large">Test</button>

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

I'm having trouble with fixing the TypeError that says "Cannot read properties of undefined (reading 'style')." How should I address this issue

I'm having trouble with a slideshow carousel on my website. When the site loads, only the first image is shown and you have to either click the corresponding circle or cycle through all the images using the arrows for the image to display. I'm al ...

An error occurs when trying to modify the classList, resulting in an Uncaught TypeError for setting an indexed property

I am attempting to modify the classes of multiple sibling elements when a click event occurs. Some of these elements may have multiple classes, but I always want to change the first class. Below is the code that I created: let classList = event.currentTa ...

Implementing a background image layered over a background color using CSS

Currently, I am in the process of converting a webpage from PSD format. The main content of the page is designed with a solid background color, but there is also a separate layer that features a glow effect on top of the background. However, when implement ...

CSS: elements that are only visible when positioned above specific elements

Can we create an element that is only visible above specific other elements? For instance, in the following code snippet, I want the .reflection element to be visible only above the .reflective elements (located at the top and bottom) and not visible on t ...

The Safari browser restricts interaction with password inputs but allows interaction with other types of input fields

My password input field is styled like this: <input class="genericButton" id="login-password" type="password" name ="password" placeholder="Password"> While everything functions correctly in Chrome, I encounter an issue with Safari. When I try to i ...

The style of the button label does not persist when onChange occurs

Encountered an interesting issue here. There is a button designed for selection purposes, similar to a select item. Here's the code snippet: <button class="btn btn-primary dropdown-toggle" style="width: 166%;" type="button" id="dropdownMe ...

Appear and disappear gradually when hovering

When hovering over #one, the class .hidden is applied to #first. I have added a script to make .hidden fade out using transition: ease-in 0.3s;, but I am having trouble getting the fade in effect to work. I attempted adding transition: ease-in 0.3s; to #fi ...

Platform designed to simplify integration of high-definition imagery and scalable vector illustrations on websites

In the ever-evolving world of technology, some clients support advanced features like svg while others do not. With the rise of high resolution devices such as iPhone 4+ and iPad 3rd gen., it has become crucial to deliver graphics that can meet these stand ...

Achieving centered text alignment in a span using Bootstrap 5

I am facing an issue with a span in an input group that has a minimum width of 6rem. This is causing the text to not be centered, as spans typically adjust to the width of the text itself. .input-group-text { min-width: 6rem; } <link rel="stylesheet ...

Comparison between Bootstrap 4 and Firefox Preload Error

Hello fellow front end developers who are experiencing an issue with CSS preload in Firefox versions above 74.0. I have encountered a problem with the following code snippet: <link rel='preload' as='style' onload="this.onload=null; ...

Utilizing jQuery Methods within Node.js

Recently delved into the world of node.js and created multiple pages (such as login, signup, blog, etc). If I desire an effect in which: 1. Hovering over the "News" button triggers a slider to appear downwards, To make adjustments to an image within ...

Using jQuery, encapsulate an image within a div element and transfer the floating style attribute from the image to the div

Can anyone assist me in wrapping an image within a div along with transferring the float setting? This is my current code: $('.photo-img').wrap('<div class="photo-wrap" />'); I would like the new div to inherit the float proper ...

What is the method for rotating a map using JavaScript?

My map built with Leaflet displays a route and a moving car marker. Now, I am looking to implement a feature where the map rotates based on the direction of the car. I have access to both the current coordinates of the car and the target coordinates. ...

Data input not populating in email

After filling out the form, Gmail pops up with no data entered. How can I ensure that the information I input in the form is transferred to the email? <form class="" action="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_em ...

How come the cubic bezier timing function works flawlessly just on the first try when staggered transitioning element opacity over time with CSS and a touch of JS?

I'm currently working on a menu overlay that expands to fill the screen when a user clicks a button in the corner. I'd like the menu items to fade in one by one, similar to the effect used on this website: Most of the functionality is there, but ...

What adjustments can be made to alter the height of the borders on the left and right side of the block quote, and link the border to a different div element

Block Quote Example I'm looking to create a blockquote that resembles the image provided. I have successfully implemented the top and bottom gradients, but am facing challenges with the left and right borders as well as rounding the quotations more. ...

Challenges with displaying css/bootstrap classes within angular2

Experiencing difficulties with CSS/Bootstrap integration when displayed in an angular2 component. When attempting to display the CSS and HTML content in the Index.html file (with proper CSS and JS references), everything functions correctly. However, once ...

Ways to modify the appearance of the Sign up page on Moodle

I'm a newcomer to the world of Moodle, currently using version 2.9. I've created a unique design for my Signup page using HTML5 and CSS. How do I go about integrating this custom page? While I understand how to change the default Login page by m ...

Align images of varying sizes vertically within div containers, even when the image is larger than the div itself

I'm facing a challenge when it comes to aligning images vertically inside divs. The problem arises due to the following conditions: The images will have varying and unknown sizes. These images are larger than the divs they are contained in, requiri ...

Utilizing data attributes over classes for styling in CSS

Programming Languages <span data-bar> ... </span> JavaScript span[data-bar]{ ... } Do you agree with this coding practice? Are there any potential pitfalls? I believe implementing the data- attribute is beneficial when dealing with a large ...