What is the best way to achieve a perfect rounded div using Bootstrap 4?

While browsing through the Bootstrap documentation and searching on stackoverflow, I came across a solution to create circular images using border-radius set at 50%. However, when I tried implementing it with my slightly oversized image, it ended up looking deformed. I then attempted to use Bootstrap as well, but encountered the same issue.

Below is an example of what I attempted:

import React, { Component } from 'react';
import yo from '../../assets/yo.jpg';
import './Card.css';

class Card extends Component {
    render() {
        return (
            <div className="container">
                <div className="row vertical-center">
                    <div className="">
                        <img src={yo} className="rounded-circle w-25" />
                    </div>
                </div>
            </div>
        )
    }
}

export default Card

; This component belongs to a React project. Here's the corresponding CSS code:

.vertical-center {
    min-height: 80vh;
    display: flex;
    align-items: center;
}

The intention behind this CSS snippet is to vertically center the content within the component.

However, the resulting output was not a circular image as intended:

https://i.sstatic.net/KI3j0.png

This issue persists despite various attempts. How can I rectify this problem using Bootstrap?

Answer №1

Here is the solution:

.avatar-big {
  height:100px;
  width:100px;
}

.avatar-small {
  height:50px;
  width:50px;
}

.avatar-med {
  height:75px;
  width:75px;
}


.avatar-img {
width: 100%;
height: 100%;
-o-object-fit: cover;
object-fit: cover;
}

.rounded-circle {
border-radius: 50%!important;
}
<div class="avatar-big">
  <img class="avatar-img rounded-circle" src="https://i.insider.com/5c007f32499ade0d21195b72?width=1100&format=jpeg&auto=webp" />
</div>

<div class="avatar-med">
  <img class="avatar-img rounded-circle" src="https://i.insider.com/5c007f32499ade0d21195b72?width=1100&format=jpeg&auto=webp" />
</div>

<div class="avatar-small">
  <img class="avatar-img rounded-circle" src="https://i.insider.com/5c007f32499ade0d21195b72?width=1100&format=jpeg&auto=webp" />
</div>

Answer №2

If you want a more balanced image, consider removing the min-height property and ensuring the width matches the height for a well-rounded picture.

Answer №3

The issue lies in the non-square aspect ratio of your image, causing your element to not appear square as well due to missing CSS rules enforcing it.

To resolve this, set both the width and height of the element to a fixed size, ensuring it is square in shape.

Next, apply the object-fit: cover property to properly position the image inside the element, preventing any distortion by filling the box without displaying any excess parts.

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

Instructions on removing rows by using buttons within a JavaScript-generated table

This snippet displays JS code to create a quiz index table and HTML code to display the index. function load(){ var data = [ { "id": "qc1111", "quizName": "Quiz1", "course": "111", "dueDate": "1/ ...

How can I ensure my JSX content is uniquely styled without repeating style properties?

I'm currently working on styling content in React using inline styles. Here's a snippet of the code I've been using: var textStyles = { emphasis: { fontSize: 38, margin: 0, padding: 0, }, smallEmphasis: { margin: 0, ...

Angular's inline style manipulation fails to function in IE browser

I am facing an issue with setting the position of a div based on the return value of a function in an angular controller Although it works fine in FireFox and Chrome, Internet Explorer is interpreting {{position($index)}}% as a literal string value, resul ...

Error encountered in React Native with getInitialState() due to unexpected token

I'm having trouble understanding why this code is throwing a syntax/unexpected token error on line 14. Any assistance would be highly appreciated. I believe that getInitialState() is properly configured, so I'm confused as to why it's causi ...

The webpage is missing a rendered React component even though it should be displayed

I am facing an issue where a React component is not appearing on the webpage despite being rendered. I have provided the code and screenshots of the components below for reference. Below is the snippet from the "App.jsx" file: function createCard ...

How can we utilize CSS floats to achieve maximum widths?

I currently have 5 divs that I need to structure in a specific way: Each div must have a minimum size of 100px The parent container should display as many divs as possible on the first row, with any remaining divs wrapping to new rows if necessary If the ...

Update the useMemo state value after the initial function block has been executed

Currently, I have a list of characters from Dragon Ball Z that users can filter based on gender, race, or both using useMemo. let dbzCharacters = useMemo<CharacterObj[]>(() => { if (filterGenderValue && filterRaceValue) { retur ...

Is it possible to load the surrounding markup in Angular before the guards are resolved upon the initial load of the router-outlet?

Within our Angular 12 application, we have implemented guards that conduct checks through API calls to the backend in order to validate the user's permissions for accessing the requested route. The default behavior of these guards follows an all-or-no ...

Transform the appearance of a button when focused by utilizing CSS exclusively or altering it dynamically

As a newcomer to coding, I am currently working on a project that involves changing the color of buttons when they are clicked. Specifically, I want it so that when one button is clicked, its color changes, and when another button next to it is clicked, th ...

Running a method at any given time within an ngFor loop in Angular 5

On my angular page, I am facing a challenge with updating a variable and displaying it in the HTML within an *ngFor loop. Here is an example of what I need: HTML: <table *ngFor="let data of Dataset"> somehowRunThis(data) <div>{{meth ...

React, React Router, and Flux are all tangled up in a confusing web of "dispatch in the middle of a dispatch" whenever I attempt to clear the messages

Utilizing React, React Router, and Flux, I have a MessageStore that maintains messages triggered by components using MessagesAction.addError(), MessagesAction.addWarning(), and MessagesAction.addSuccess(). Upon dispatching the message, the MessageStore is ...

What is the best way to incorporate styled components and interpolations using emotion theming?

I've been building a React web application with a dynamic theme feature using the emotion-theming library. This allows users to switch between different environments, each with its own unique theme. To achieve this, I created my own CustomThemeProvide ...

Tips for eliminating or deactivating the chosen css style of MUI v5 BottomNavigation

Looking to implement a bottom navigation for mobile devices but facing an issue with the selected CSS style when an icon is clicked/pressed. Currently, clicking on an icon enlarges both the text and icon due to default styles. I want them to remain the sa ...

Unable to create a polygon on the Google Maps API using GPS coordinates inputted from a text field

I am currently developing an interactive map using the Google Maps API. The map includes a floating panel with two input fields where I can enter GPS coordinates. My goal is to create markers and connect them to form a polygon. While I can easily draw lin ...

Using a modal within a map function: Tips and tricks

I've been working on a Gallery application using React.JS and Reactstrap. The application uses the map() function to display each piece of art in a Card. Each card has a button that triggers a modal to show more data from the map function. However, I& ...

Angular 4: preventing button click until form validation is successful

I am facing a situation where I have a form with required fields and 2 buttons: one button is for submitting the form, and the other button is for downloading a file. When the form is not valid, the submit button is disabled until the form becomes valid: ...

Bidirectional binding of attributes in an Angular directive

I have developed a custom Angular Directive known as evoEventMirror. Its main purpose is to attach a jQuery event to an inserted element and apply a specified CSS style to the "root" element. Refer to the example below: <div id="#mydiv" evo-event-mirro ...

How can I efficiently add multiple items to an array and store them in async storage using React Native?

I am trying to store multiple elements in local storage using React Native. I found some helpful documentation on how to do this here. Could someone guide me on the correct way to achieve this? Here's a snippet of my code: My current approach const ...

What is the best way to extract parameters from MERN routing?

Recently, I've been facing difficulties while trying to retrieve data from MongoDB using dynamic routing. The URL structure is as follows: http://localhost:3000/paprogram/:_id` http://localhost:3000/paprogram/60bfbf12f8d33aef9ae4ebb9` I am attempting ...

Distinguish between datalist selection and text input in BootstrapVue Autocomplete

When looking at the code snippet provided below, I'm trying to figure out the appropriate event/method to determine whether the value entered in the input field was manually typed or selected from the <datalist>. SAMPLE CODE: <div> &l ...