Creating a personalized avatar display with Bootstrap 5

I'm trying to create a display that resembles a rounded avatar with text next to it. While I've successfully made the rounded avatar, an issue arises when I place text beside it where the avatar sometimes appears oval instead of round. What could be causing this?

This is the code I'm using: View Code on JSFiddle

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

.red-label {
    color: red;
}

.fs-small {
    font-size: 0.8em;
}
// More CSS classes and styling
// Code snippet continuation

Answer №1

This code snippet ensures that your avatar remains circular and doesn't turn into an oval shape. It also utilizes CSS Grid's place-items property to align the text next to the avatar.

.user-avatar {
    display: grid;
    place-items: center;
    flex-shrink: 0;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

#map {
    width: 100%;
    height: 100vh;
    background-color: #f0f0f0;
}

.autocomplete {
    position: relative;
    display: inline-block;
}

.autocomplete .suggestions {
    list-style: none;
    padding: 0;
    margin: 0;
    position: absolute;
    z-index: 3;
    width: 100%;
}

.autocomplete .suggestions li {
    padding: 8px;
    background-color: #f1f1f1;
    border: 1px solid #ddd;
    cursor: pointer;
}

.autocomplete .suggestions li:hover {
    background-color: #5db8fd;
}

.red-label {
    color: red;
}

/* Override Bootstrap styles for offcanvas backdrop */
.offcanvas {
    z-index: 99;
}
.offcanvas.offcanvas-start {
    width: 40% !important;
}

.offcanvas-backdrop {
    z-index: 90;
}

.navbar {
    z-index: 100;
    position: fixed;
    width: 100%;
}

.fs-small {
    font-size: 0.8em;
}

.fs-smaller {
    font-size: 0.5rem;
}

.search-container {
    position: absolute;
    top: 10px;
    background-color: #fff;
    padding: 10px;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    z-index: 1;
}

.search-container.center {
    left: 50%;
    transform: translateX(-50%);
}
.search-container.end {
    right: 10px;
}

.search-container > * {
    white-space: nowrap;
}

/* .search-contai...

</div>

Answer №2

To change the width: 48px;, you can use flex: 0 0 48px; as it represents:

flex-grow: 0;
flex-shrink: 0;
flex-basis: 48px;

This means that the element will neither grow nor shrink and will have a fixed size of 48px.

Alternatively, you could go with:

width: 48px;
flex-shrink: 0;

By default, a flex item has flex-shrink: 1, so specifying flex-shrink: 0 is necessary in this case to prevent shrinking.

Another approach would be using:

min-width: 48px;

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

.red-label {
    color: red;
}

.fs-small {
    font-size: 0.8em;
}

.fs-smaller {
    font-size: 0.5rem;
}

.user-avatar {
    background-repeat: no-repeat;
    background-size: cover;
    background-color: white;
    background-position: center;
    height: 48px;
    flex: 0 0 48px;
    background-image: url('https://placehold.co/48x48');
}

.ellipsis-2 {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2; /* number of lines to show */
    line-clamp: 2;
    -webkit-box-orient: vertical;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="74161b1b00070006150434415a475a47">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debcb1b1aaadaaacbfae9eebf0edf0ed">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<div class="accordion-body row g-3">
... (The rest of the content remains unchanged)
  <div class="px-3 pe-4 m-3 text-end">
    <a href="#">View All</a>
  </div>
</div>

Answer №3

It turns out that the solution was simpler than expected. I just had to switch the DOM element containing the image from

<div style="background-image:url('path/to/image')" class="rounded-circle">
to
<img src="path/to/image" class="rounded-circle">
because, for some reason, the .rounded-circle class only seems to work on an <img> tag.

I hope this information is useful for anyone facing a similar issue.

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

Change the class of the div when the first div is selected

My goal is to switch the class of the #klapp element (from .klapp to .klappe) whenever a click event happens inside the #label-it container, including when the #klapp element itself is clicked. The challenge is that I am not able to use unique IDs for each ...

What could be the reason that angularjs html template requests aren't appearing in the top websites when viewed in a

After referring to the link , I discovered a way to determine which programming tools different sites use for application development. However, when I debug HTTP requests using Firebug, I noticed that HTML requests are not explicitly shown in the log. Alt ...

Eliminating every single dynamic 'data attribute' from a specific Element

Within my div element, dynamic data- attributes are added to some tags. (The names of these data- attributes are generated dynamically by a script, so the exact name is unknown) <div data-1223="some data" data-209329="some data" data-dog="some value"&g ...

JavaScript implementation of a carousel slider with responsive design

I have successfully implemented a feature carousel slider using the jquery.featured.carousel.js file along with some CSS. Here is the jsfiddle link to my project: LINK. When running this code on localhost, I noticed that I need to refresh the page every ...

I need assistance with retrieving an image from a database using PHP

How can I display an image from the database on a new page using PHP? Whenever I click on the image, it always shows the same image on the new page. How can I make it so that the image displayed on the new page is the one I clicked on in the previous pag ...

Difficulty arises when trying to engage with the addition span within a span using jQuery

Looking for some assistance here! Currently in the process of developing a mail system and focusing on creating a list of receivers. I am working on adding user names to a span element, with the goal of being able to remove users by clicking their name. & ...

The default padding and margin in Bootstrap are making the images protrude beyond the edges of the container

I have an issue where my images are stretching into the padding or margin that bootstrap uses to separate columns. I want to maintain the column division but avoid having the images stretch. While I have managed to shrink the image using padding, the white ...

Converting Bootstrap rows into columns based on viewport size

My current layout displays like this on devices with large and small screens: https://i.sstatic.net/htk3e.png I'm looking to have the same divs arranged as rows on large screens and columns on small screens. Is there a way to achieve this without du ...

Showing a hidden div after an unsuccessful AJAX call

Encountering an issue with displaying a div notification using the code snippet below: $.ajax({ url: url, cache: false, async: false, success: function (data) { response = jQuery.parseJSON(data); }, error: functi ...

Vertical scroll bar positioned on the left side of a DIV

Can a vertical scroll bar be placed on the left side of a DIV using CSS? What about JavaScript? ...

What is the best way to toggle and slide two forms within a single page?

In this scenario, the goal is to display multiple forms on a single page, like a Login Form and Reset Password Form shown below. One form is initially hidden until a link is clicked, triggering an effect. The current setup includes a .toggle() effect that ...

Redirecting files from the file system to the emulator is not functioning properly

I am new to Phonegap and need help resolving this issue. In my application, I need to redirect to the List.html file when a button is clicked. Here is the code I have written: button1 function test() { window.location="/home/swift-03/phonegapexa ...

ASP Table extends into adjacent column

Having some trouble with the table layout here. In the screenshot, you'll notice a grid view within another gridview. There are currently 3 grid views - the main one and one in each table. I'm looking to move the description to the Price 2 column ...

I seem to be having trouble with this JavaScript code. Could it be a syntax error, or is it possibly another issue causing it

I am encountering an issue with this code, as it is not functioning as intended. Although, I am unsure about the root of the problem. Code: <body> var randNumForQuote = Math.floor((Math.random() * 11)); if (randNumForQuote == 0) { docum ...

How can I incorporate checkboxes and crosses for validation in AngularJS?

I've been searching through the documentation for angularjs and online resources, but I can't seem to find any information regarding a specific aspect of form validation. You know when you input something in a form field (like a name, phone numbe ...

Unable to display and conceal div elements

<ol class="novice"> <li> <p class="glava">HTML</p> <div class="vsebina"> <p>Hyper Text Markup Language (slovensko jezik za označevanje nadbesedila...</p> </div> </li> <li> ...

Undoing alterations to the user interface in Angular 2 after discontinuing bi-directional data binding

UPDATE: Including code for further clarification. client.component.ts import { Component } from "@angular/core"; import { ClientService } from "../services/client.service"; import { Client } from "../types/client"; @Component({ selector: "rpcs-client", ...

Issues persist with TCPDF when rendering HTML tables accurately

I'm struggling with creating HTML tables using TCPDF in PHP. Here's the PHP code snippet: $tbl = <<<EOD <TABLE width="100%" border="1" cellspacing="0" cellpadding="0"> <TR> <TD> <STRONG>F ...

Latest versions of Bootstrap are facing issues with the functionality of the Carousel feature

I'm struggling to create a basic carousel, but for some reason, it's not functioning properly. I attempted to use the sample code provided by Bootstrap's documentation, but it doesn't behave as expected (slides won't transition and ...

Unveiling the mystery of extracting information from a string post serialization

When working with a form, I am using this jQuery code to fetch all the field values: var adtitletoshow = $("#form_data").serialize(); After alerting adtitletoshow, it displays something like this - &fomdata1=textone&fomdata2=texttwo&fomdat ...