Styling with CSS: The Art of Showcasing Initials or Images of Individuals

By following this elegant HTML and CSS example, I am able to showcase my initials over my photo.

While this is wonderful, I would like the initials to be displayed only if the image does not exist; if the image is present, the person's initials should not be shown.

In essence, the image should cover the initials when it exists (to hide the initials).

.profile-dot {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 3rem;
  width: 3rem;
  background-color: lightgray;
  border-radius: 50%;
  border: gray 2px solid;
  background-size: cover;
  background-position: center;
  background-repeat: none;
}

.profile-dot span {
  font-weight: 700;
  color: #fff;
  font-style: normal;
  font-size: 120%;
}
<i class="profile-dot" style="background-image: url(https://i.sstatic.net/u20P2.jpg)">
  <span>BM</span>
</i>

The actual initials are obtained from an Angular expression like:

  <span>{{ dataItem.personInitials }}</span>

I have a hint on using figure, but I haven't quite figured it out yet - i.e.

<figure>
<i class="profile-dot">
<img height="30" width="30" onerror="this.style.display='none'; this.className='' " src="{{ './assets/profiles/patients/' + dataItem.UID + '.jpg' }}"  >
<figcaption>
<span>{{ dataItem.patientInitials }}</span>
</figcaption>
</i>
</figure>

Answer №1

To handle the onerror event on an image, you can dynamically add a class to the image element. This added class can then be used in conjunction with CSS adjacent sibling selectors to show or hide a corresponding span element.

In your CSS file, make sure to initially hide the span element and only display it when the image has the specific class that was added during the error event.

.user-avatar img+span {
  display: none; /* Hide by default */
}

.user-avatar img.error-state+span {
  display: block; /* Show when image has error state class */
}
<figure>
  <div class="user-avatar">
    <img height="50" width="50" onerror="this.style.display='none'; this.className='error-state'" src="{{ './assets/avatars/' + user.ID + '.jpg' }}" />
    <span>{{ user.username }}</span>
  </div>
</figure>

Answer №2

If you happen to be working with angular, you can use a simple conditional if statement

<i class="profile-dot" style="background-image: url(https://i.sstatic.net/u20P2.jpg)">
  <span *ngIf="!dataItem.imageSrc">{{dataItem.personInitials}}</span>
</i>

To see this demo in action, click here - https://stackblitz.com/edit/angular-r3q4i6

Answer №3

Personally, my approach would look something like this.

The design includes a feature where a valid image is displayed, but if the link is broken, then the background color and text will be shown instead. Additionally, the text displays while the image is loading, which can be especially helpful for users with slower internet connections.

I hope you have an enjoyable weekend!

.profile-dot {
  position: relative;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 3rem;
  width: 3rem;
  background-color: lightgray;
  border-radius: 50%;
  border: gray 2px solid;
  overflow: hidden;
}

.profile-dot figure {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-size: cover;
  background-position: center;
  background-repeat: none;
  margin-block-start: 0;
  margin-block-end: 0;
  margin-inline-start: 0;
  margin-inline-end: 0;
}

.profile-dot figcaption {  
  font-weight: 700;
  color: #fff;
  font-style: normal;
  font-size: 120%;
}
<div class="profile-dot">
  <figure style="background-image: url(https://i.sstatic.net/u20P2.jpg)"></figure>
  <figcaption>BM</figcaption>
</div>

<br/><br/>

<div class="profile-dot">
  <figure style="background-image: url(BROKEN-LINK)"></figure>
  <figcaption>BM</figcaption>
</div>

Answer №4

To create a behind-the-image effect using CSS, you can utilize the z-index property to position the <span> element behind the background image. Further information on z-index can be found here. (It is necessary to remove the background color for this technique to function properly.)

.profile-dot {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 3rem;
  width: 3rem;
  border-radius: 50%;
  border: gray 2px solid;
  background-size: cover;
  background-position: center;
  background-repeat: none;
}

.profile-dot span {
  font-weight: 700;
  color: #fff;
  font-style: normal;
  font-size: 120%;
}
<i class="profile-dot" style="background-image: url(https://i.sstatic.net/u20P2.jpg);">
  <span style="z-index:-1;">BM</span>
</i>

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 is the best way to use jQuery to apply CSS only to elements with a specific attribute set?

Currently, I am attempting to utilize jQuery to apply specific CSS styles to elements, but only those that possess a particular attribute. The rationale behind not resorting solely to CSS is due to the immense amount of content on the site, making it impr ...

Sending a different name for the jQuery parameter

I'm looking to select CSS settings based on the presence of a data tag. What would be the correct approach for this? var datadirection = 'left'; //default direction if( $(this).attr('data-right') ){ datadirection = 'righ ...

Having trouble loading static CSS files in Django

Need help linking the main.css file in my index.html <link rel="stylesheet" href="{% static 'assets/css/main.css' %}"/> I have also included {%load static%} Here is my file structure: signup/static/assets/css/main.css In settings.py S ...

Checking boxes on Android for compatibility with Firefox, Chrome, and Internet Explorer 8

Could anyone assist me with finding Android checkboxes styled with CSS and/or jQuery for my project? I haven't been able to find any resources or plugins that could help. Any links you can provide would be greatly appreciated as I've come up empt ...

Steps to modify the Angular Universal entry file to app.js and relocate it to the root directory of the app

Currently, I have an Angular Universal SSR application running on node with the latest versions of Angular and Universal as of 21/01/2021. My hosting provider allows me to host my nodejs apps on a shared cPanel, but they only accept the entry file named a ...

The tab content refuses to show up in its designated fixed location

I've been working on creating a responsive tab system that functions as an accordion on smaller mobile devices, inspired by this example. I've made great progress and I'm almost where I want to be, but for some reason, the active tab content ...

Turn off the scrollbar without losing the ability to scroll

Struggling with disabling the HTML scrollbar while keeping the scrolling ability and preserving the scrollbar of a text area. Check out my code here I attempted to use this CSS: html {overflow:hidden;} Although it partially worked, I'm not complete ...

What is the best way to manage error handling in various locations within an Angular stream?

Currently, I am working on ensuring that errors are handled properly in a stream where the id of a group is retrieved first and then used to obtain profile information. I want to easily identify whether the error is occurring during the retrieval of the g ...

Switching button appearance when hovered over

Looking to create a page layout with 4 buttons, each occupying one quadrant of the page? These buttons should feature images as their background and change to different images when hovered over. Wondering how this can be achieved using CSS? ...

How about a fading effect for the select box?

I'm currently working on creating a select tag that opens its options slowly with a fade in, fade out effect when the user clicks on "Actions." I've attempted to use jQuery for this feature but haven't had any luck. Here's my code: &l ...

Navigate through the items in my subnavbar by scrolling the content

HTML Code <div id="subnavigation"> <?php $verbindung = mysql_connect("host", "user" , "pw") or die("Verbindung zur Datenbank konnte nicht hergestellt werden"); mysql_select_db("db") or die ("Datenbank konnte nicht ausgewählt w ...

Is it possible to make an image gradually appear and disappear?

Is there a way to create a continuous fade in and out effect for an image, transitioning between 40% and 100% opacity? I attempted using a CSS3 opacity property, but it only allows for 0% and 100%, causing the fade effect to be ineffective. Does anyone h ...

Steps for revealing all the information from a database when clicking on the 'Read More' button

I recently set up a database called Magazine, featuring a table named social_media. This table consists of 5 columns: ID, Headline, Author, Content Short, Content. The Content Short column contains condensed versions of the articles which are displayed on ...

ng2-toastr in conjunction with akveo/ng2-admin - Styles not being applied

I recently integrated ng2-toastr into my akveo/ng2-admin dashboard, utilizing the latest version with Angular 4. Following the provided installation documentation, I imported the necessary CSS in the index.html file and declared ToastModule in the app.modu ...

Utilize Angular 4 Router to intercept every router modification

I want to implement a Breadcrumb feature. More about Breadcrumbs on Wikipedia To achieve this, I am considering creating a Service to manage it. However, I need a way to monitor any router state changes automatically, without having to add an onActivate ...

design and construct a three-dimensional shelving unit

After much searching and failed attempts, I am determined to create a stationary cube-shaped bookcase using CSS. Can anyone offer some guidance on how I can achieve this? I have included an image of the design I want to replicate. Thank you .scene { ...

CSS- Strategically placing and centering images above specific keywords in (any) HTML content without disrupting the flow of text

My main objective involves dynamically inserting images above text on any given page using a content script. The challenge lies in maintaining the proper alignment of the text after adding the images. To achieve this, I surround the words where the image i ...

Live Server in VS Code not refreshing automatically as expected

My problem is with VS Code and Live Server for HTML CSS. I expected the page to automatically reload after editing my HTML, but that's not happening. Even though I have Auto-Save enabled, Live Server isn't updating or refreshing my page automati ...

Navigating through parent folder HTML files from child folder HTML files

Seeking guidance as I embark on a new project! Check out this link to see my skills in action: collegewebsite.zip Now for the query at hand. I am working on a project named Coffee Cafe for my tuition assignment. It involves utilizing CSS and HTML with J ...

Aligning Text to the Right Using CSS

HTML <body> <div class="menu_items"> <h1>My Heading</h1> <nav> <a>Item 1</a> <a>Item 2</a> <a>Item 3</a ...