Make the image tag a precise circle shape

Is it possible to create a perfect circle image regardless of its original dimensions, such as not being a perfect square like 100px x 100px?

.circle {
    border-radius: 50%;
    height: 100px;
    width: 100px;
}

Using the above code, if the image size is 200x150, the img tag would be in an oval shape. Is there a way to always achieve a perfect circle no matter what the image size?

<img class="circle" src="/link/to/img.jpg" height="80" width="80" />

Answer №1

To achieve the desired effect, it is important to enclose the image within a div element, apply rounded corners to that container, and ensure overflow is hidden.

In addition, I have utilized flexbox to center the image although this step is not mandatory for achieving the desired outcome.

.circle {
 border-radius: 50%;
 height: 100px;
 width: 100px;
 overflow: hidden;
 display: flex;
 justify-content: center;
 align-items: center;
}
<div class="circle">
  <img src="http://www.fillmurray.com/460/300" alt="">
</div>

<h2> Actual Image</h2>

  <img src="http://www.fillmurray.com/460/300" alt="">

Answer №2

Although this question may seem outdated, there is a clever workaround to accomplish the desired outcome without relying on a wrapper div:

.round {
 border-radius: 50%;
 object-fit: contain;
}

<img class="round" src="/path/to/image.jpg" height="100" width="100" />

Answer №3

To create a perfect circle layout for images, utilize the object-fit property in CSS. Regardless of whether the image is wide or long, this property ensures it will always be displayed as a circle shape.

.circle {
 border-radius: 50%;
 height: 100px;
 width: 100px;
 object-fit: cover;
 object-position: center;
<img class="circle" src="/link/to/img.jpg" height="80" width="80" />

For a demonstration with various image sizes, refer to this example: https://codepen.io/rockycorp/pen/ZEKbzzp

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

What steps can I take to bring this idea to life in my gallery?

Currently, I am facing a roadblock while transitioning my design concept into actual code. My main concern lies with creating this art piece. Although the gallery is up and running with all the images in place, I'm encountering difficulties with the s ...

Running a Python script through a Javascript event

I'm looking to enhance my webpage by implementing a feature where users can generate a personalized QR code with the click of a button. My current setup involves a Python script that creates the QR code image and saves it as a .png file. I want to int ...

Steps for dynamically loading the content of a Bootstrap 4 Modal

I am currently in the process of developing a website that will showcase a wide range of images. The design is set up as a landing page with all content contained within the main HTML page - there is only an index.html file for now. This website will serv ...

Issue with Element's Response to JQuery Click Event

After pulling elements from the database in PHP, I utilized Jquery to add Elements to the page. Each button has two classes; one for controlling the GUI and another for handling the click event of that specific button. Here is the code snippet: echo " ...

The issue with ng-bind-html causing the disappearance of paragraph spaces

Is there a way to preserve paragraph breaks when using ng-bind-html to display text on the screen? I am having an issue where all the text appears as one big chunk if there are paragraph breaks in the data pulled from the database. Not sure what is causing ...

Issues with the Bootstrap date time picker functionality

Even though I have added all the necessary libraries for the date time picker, it still isn't working properly. You can check out this fiddle to see the code. <div class='input-group date' id='from_time'> <input type ...

Steps for incorporating a scrollbar into the context menu of CKEditor

I am working on adding items to a context menu. However, when too many items are added, they overflow onto the ckeditor area. To solve this issue, I want to implement a scroll bar for the context menu. editor.addMenuItem(suggestionBoxItem, ...

multiple elements sharing identical CSS styling

For each component, I made separate CSS files and imported them accordingly. However, despite the individual styling efforts, all components seem to have the same appearance. I specifically worked on styling an image differently for two components, but w ...

JavaScript Popup Box Failing to Trigger

Snippet of Code: <form method="post" id="cp_ind_form"> // several input fields here... <input type="submit" name="update_submit" value="Update" /> <input type="submit" name="delete_submit" value="Delete" onclick="deleteConfi ...

What's preventing vh from functioning in Chrome, yet it's fully operational in Internet Explorer?

Currently, I am utilizing vh for my project, which as far as I know represents 1 percent of the view height of the page. This measurement should function similar to percentages, especially with borders and potentially other elements. However, when I tested ...

Chosen Dropdown selection - Retrieve/Receive information

One of the challenges I'm facing involves a dropdown list on my web form that contains various dates. My main query is: How can I retrieve data for the selected date from a MySQL database and present it to the user? Additionally, if the chosen date do ...

Choose does not showcase the updated value

My form contains a form control for currency selection Each currency object has the properties {id: string; symbol: string}; Upon initialization, the currency select component loops through an array of currencies; After meeting a specific condition, I need ...

Having trouble getting flex-wrap to function properly on my div element

For the 928px breakpoint, my aim is to: Have my right column (.vd-right-col) positioned at the bottom of the page. (I attempted to achieve this using flex-wrap:wrap; but it did not work.) Arrange the two images in this column side by side. Unfortuna ...

Activate the input autofocus feature when displaying a dialog in Vue.js

When opening a dialog with input text using v-menu upon clicking a button, how can I focus on the input text field? I have attempted to use $ref but it does not seem to work. newFolderClick(){ this.$refs["input_new_folder"].focus(); //it still appea ...

Jquery loader for loading html and php files

My html and php pages contain a significant amount of data which causes them to take a few extra seconds to load from the server. I was wondering if there is a way to implement a "loader animation" for these pages using jquery. The idea is for the loader a ...

Having trouble with a jQuery selector that is supposed to target an element with both an id and

A scenario where a specific event-handling function is in place: jQuery(document).on('click', '#button .submitb', function(e){alert();}); Despite having jQuery included in the HTML document, clicking on <div id="button" class="subm ...

Automatically insert content into a div following the execution of an AJAX delete function using jQuery

I've been working on a feature to display an auto-populated message in the results div when a user deletes the last item from their favorites list, indicating that it is empty. However, I've hit a roadblock and can't seem to make it work. H ...

Is there a way to style the nav-item element specifically on the current page I'm viewing?

I'm just starting out with Vue.js and Nuxt and I need some guidance on how to apply a background image (blue line at the top of items) only to my active page (such as the contact page). https://i.sstatic.net/maDzc.png This is the HTML code I am usin ...

Add a file to the input field using JavaScript and the input type="file"

I am facing the challenge of sending a file (up to a few MB) generated by JavaScript to a server using POST (not ajax POST). I am wondering how I can add a file to an input type="file" using JavaScript. After checking Pre-Populate HTML form file input, it ...

Is it possible to delete an element from both local storage and HTML by referencing its ID?

Experience a simple flashcard game where you enter a question and answer to create a new flash card stored as an object within the cards array. The newly created flash card is also displayed by appending a new element to the flash cards section on the webp ...