Use CSS to transform a rounded corner into a perfect circle by specifying the radius with a percentage

Looking to give my images rounded corners, but struggling with Bootstrap 4 responsive images (With the class img-fluid). Using border-radius=15px isn't ideal as it distorts the corners on small viewports and large images.

Tried border-radius=15%;, but non-square images end up with elliptical corners. Adjusting with border-radius=15%/10%; helps, but requires custom ratios for each image's aspect ratio.

Want to achieve consistently circular rounded corners for all images, regardless of aspect ratio in Bootstrap 4. Prefer a CSS-only solution over Javascript for simplicity given limited number of aspect ratios.

Answer №1

For a quick fix, consider leveraging CSS3 vw or exploring viewport width to achieve a similar outcome. By utilizing border-radius:5vw;, you can adjust the border radius percentage based on the overall viewport width.

The actual size of the image is irrelevant; focus on the viewport instead. Despite its roughness, this method can yield satisfactory results.

div{
  background-color:red;
  width:50%;
  height:100px;
  border-radius:5vw;
}
<div></div>

Consider using JavaScript for a more robust solution.

Answer №2

Unique approach using Javascript

If you're looking to add a 15% border radius to your images, one effective way is to utilize JavaScript (particularly jQuery). By implementing the following code snippet, you can dynamically adjust the border-radius based on the image's width:

$(window).resize( function() {
    $('img').each(function(){
       $(this).css('border-radius',($(this).width()*0.15)+'px');
    });
});
$(document).ready( function() {
  $(window).resize();
});

Explore this functionality further by checking out this CodePen example.

For optimal results, it's recommended to set a 'standard' border-radius for the image in CSS before the script executes.


An alternative solution without relying on Javascript

If you prefer avoiding Javascript, another approach involves wrapping the image within a container and utilizing pseudo-elements to create rounded corners. This method requires square images with solid backgrounds, preferably in SVG format. While functional, this technique introduces additional DOM elements and fixed background colors, making it less elegant compared to the straightforward Javascript solution.

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

Angular 13 encounters issues loading Bootstrap and JS files whenever the route button is clicked

As a beginner in Angular, I am currently working on converting HTML templates to Angular for the purpose of learning. However, I have encountered an issue when navigating to the signup page from the home's router link. The bootstrap and JS files are n ...

Styling the TinyMCE Toolbar within a Div container

While working on creating a unified TinyMCE toolbar that is fixed at the top of multiple containers within a div wrapper, I encountered an issue where the CSS styling of the toolbar was lost after wrapping. The toolbar only displayed hyperlink URLs inste ...

What causes the image to not appear at the center bottom of the page when using IE for loading?

Why does the loading image not appear at the center bottom of the page in IE? This function loads content when the page is loaded and also loads content when scrolled to the bottom. When you load the page index.php, you will see the loading image at the ...

Arranging Checkboxes Vertically in HTML Without Using Inline Styling

My checkboxes are currently displayed in a column format, but I would like them to be in a single row. To better illustrate the issue, here is a photo: https://i.sstatic.net/PxRzB.jpg I am seeking assistance on how to align the checkboxes horizontally in ...

Ensuring the input field and button are positioned in a horizontal line

Currently, I am developing a to-do list application using React and Chakra UI. I am trying to align the input field and the button on the same line for a more organized layout. This is the current layout: image I aim to achieve a design similar to this: ...

Two divisions placed side by side

The current structure I am working with looks like this: <div id="container"> <div id="msg"><p>Lorem ipsum dolor sit amet.</p></div> <div id="img"><div id="content"></div></div> </div> # ...

Scrolling horizontally using the WinJS list view

In my project, I incorporated a WinJS list view with the display style of ms-flexbox. However, I am facing an issue where the list is scrolling horizontally beyond its width even though I have set overflow to hidden. Is there a way for me to eliminate th ...

What is the appropriate syntax for the second division?

<style> .panel{ border:1px solid #000; } </style> <div class="body"> <div class="panel"></div> <div class="panel" style="border-top:0px;"></div> </div> In order to apply the style border-top:0px; to ...

Text in anchor vertically centered across browsers

Take a look at my JsFiddle project here: http://jsfiddle.net/JxXHE/ I want to ensure that the text in the menu items is perfectly centered vertically, with an 8px margin from the top and bottom in all browsers that are supported. The list of supported bro ...

Using `overflow: hidden` on a table will result in the bottom and right borders disappearing

I am facing an issue where the border of my table is being cut off when using overflow: hidden. Below is an example code snippet illustrating this problem. <style> table { border-collapse: collapse; } table { ...

The Bootstrap toast feature does not seem to be functioning properly within the context of

I am facing an issue with my datatable table where the Bootstrap toast component is not working correctly when placed above the table due to the backdrop-filter property. The toast functions normally when placed within any other element except for the tab ...

The vertical writing mode is causing boxes to stack in an unexpected direction

I am encountering an issue with three div elements that have a writing-mode of vertical-rl. Despite setting the writing mode to vertical, the block stacking context is not being displayed from left to right as expected, but rather stacking inline. CSS .v ...

Creating a minimalist metro-inspired menu using HTML and CSS

After researching online for a few days, I've just started learning HTML/CSS and my skills are currently at about a 6 out of 100. I have this code that functions as a blogger HTML widget, but I want to customize it further. Here is the code snippet: ...

Add a focus style to the submit button input

Having an issue with my CSS code. I can't get the style to apply to my submit button, but it works on the search field when focused. Please review my code snippet and screenshot below. What could be causing this problem? CSS .search-form .search-f ...

display the inner div adjacent to the outer div

I am trying to create a vertical menu with submenus, and I need the submenu to appear next to its parent div. Has anyone encountered this issue before? I searched on Google but could only find solutions for placing two divs next to each other. I specifica ...

Preventing right-aligned images from overflowing inside a div on Google Chrome

I am currently utilizing the jQuery cycle plugin to display a slideshow of images that floats to the right within an outer div element. The CSS I am using is as follows: .slideshow { float:right; margin-left: 20px; } The image alignment works per ...

Navigational footer designed by Bootstrap

I am hoping to create a footer menu similar to this using bootstrap classes. Here are the basic bootstrap classes: <footer class="footer"> <div class="container"> <p class="text-muted">Place sticky footer content here.</p> ...

CSS: creating a subtle fade-in animation for an address element and an image

Can you help me out with some advice? I'm working on a navigation bar that has a dropdown button. Each item in the dropdown menu has a small image with an opacity animation applied to it. When you hover over the image, it becomes visible and vice vers ...

Tips for controlling HTML elements using JavaScript

I'm currently working on implementing a mouse-over scale effect for an HTML image. I chose to use JavaScript for this task because I need the ability to manipulate multiple elements in different ways simply by hovering over one element. Below is the J ...

Issues with Laravel and Bootstrap 4 Integration

I'm currently utilizing bootstrap 4 for a laravel project and encountering issues with its functionality. Specifically, I am attempting to structure a post in the following manner: Having one column dedicated to the profile picture with the class col- ...