Using an array of images with CSS and displaying them in HTML

Is there a way to use one class for 3 buttons, each with its own unique background image? I wanted to create an array of background images in CSS and then retrieve them individually for each button. However, after some research, it seems that CSS does not support array declaration. Is this correct? If so, what alternative methods can I use to assign different images to each button?

HTML

...
...
<button class="access_item">
        <span class="access_icon">
        </span>
         Button_1
</button>

<button class="access_item">
        <span class="access_icon">
        </span>
         Button_2
</button>

<button class="access_item">
        <span class="access_icon">
        </span>
         Button_3
</button>
...

CSS

.access_item {
  display: inline-block;
  background: none;
  border: 0;
  cursor: pointer;
  appearance: none; 
}

.access_icon {
  display: block;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  /* Each button will have its distinct background image */
  background-image: url("https://picsum.photos/id/237/200/300"); //Image 1
  background-image: url("https://picsum.photos/seed/picsum/200/300"); //Image 2
  background-image: url("https://picsum.photos/200/300/?blur"); //Image 3
  margin-bottom: 20px;

}

Answer №1

To implement this design, utilize the :nth-child() selector in your CSS. Modify your code to reflect the following:

.access_item {
  display: inline-block;
  background: none;
  border: 0;
  cursor: pointer;
  appearance: none; 
}

.access_icon {
  display: block;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-bottom: 20px;

}

.access_item:nth-child(1) > .access_icon {
  background-image: url("https://picsum.photos/id/237/200/300"); //Image 1
}

.access_item:nth-child(2) > .access_icon {
  background-image: url("https://picsum.photos/seed/picsum/200/300"); //Image 2
}

.access_item:nth-child(3) > .access_icon {
  background-image: url("https://picsum.photos/200/300/?blur"); //Image 3
}

Answer №2

Perhaps there may be differing opinions on this matter, but I personally lean towards utilizing CSS-IDs or additional CSS classes for a solution.

This approach allows for more flexibility as you are not confined to the nesting and order of the buttons.

Version 1 Using CSS-IDs in HTML:

...
<button class="access_item" id="button_1" >
        <span class="access_icon"></span>Button_1
</button>

<button class="access_item" id="button_2" >
        <span class="access_icon"></span>Button_2
</button>

<button class="access_item" id="button_3" >
        <span class="access_icon"></span>Button_3
</button>
...

Version 1 Using CSS-IDs in CSS:

.access_item {
    display: inline-block;
    background: none;
    border: 0;
    cursor: pointer;
    appearance: none; 
}
.access_icon {
    display: block;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    margin-bottom: 20px;
}
#button_1 {
    background-image: url("https://picsum.photos/id/237/200/300"); //Image 1
}
#button_2 {
    background-image: url("https://picsum.photos/seed/picsum/200/300"); //Image 2
}
#button_3 {
    background-image: url("https://picsum.photos/200/300/?blur"); //Image 3
}

Version 2 Using Additional CSS Classes in HTML:

...
<button class="access_item button_1" >
        <span class="access_icon"></span>Button_1
</button>

<button class="access_item button_2" >
        <span class="access_icon"></span>Button_2
</button>

<button class="access_item button_3" >
        <span class="access_icon"></span>Button_3
</button>
...

Version 2 Using Additional CSS Classes in CSS:

.access_item {
    display: inline-block;
    background: none;
    border: 0;
    cursor: pointer;
    appearance: none; 
}
.access_icon {
    display: block;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    margin-bottom: 20px;
}
.button_1 {
    background-image: url("https://picsum.photos/id/237/200/300"); //Image 1
}
.button_2 {
    background-image: url("https://picsum.photos/seed/picsum/200/300"); //Image 2
}
.button_3 {
    background-image: url("https://picsum.photos/200/300/?blur"); //Image 3
}

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

When HTML/JS code is utilized, the submit button or image should function to open the same page as

In addition to the left and right mouse buttons, you also have a middle mouse button. If you click on an image or hyperlink with this middle mouse button while browsing any website, it will open a new window in the background. Now, I want to achieve a sim ...

What could be causing the empty space on the right side of my container?

Currently, I'm tackling a project using only HTML, CSS, and Bootstrap. Things were going smoothly until I stumbled upon an unexpected gap on the right side of my container. I'm puzzled as to why this is happening and how I can go about rectifying ...

When flex items are stacked in a column, the bottom portion may be truncated

(Edit) Check out the codepen example here. I am working on creating a stack of cards using Vue.js and flexbox to showcase different apps on my website. Each card is represented by a component and I utilize Vue's `for` function to display cards with t ...

Display the menu upon activating the hover event icon using only CSS

Assistance Needed - Unable to activate hover and display the rest of the div I want to activate the hover effect where early levels are visible, and when hovering over the first level, the subsequent levels appear List item body { margin: 3%; } div.ti ...

incorporating HTML attributes without specified values in .NET

Is there a way to include the multiple attribute in a select element using .net? I attempted the following approach: If MultiSelect Then drpSelect.Attributes.Add("multiple", "true") End If However, technically speaking, the HTML should only require the ...

Tips for changing a div's visibility with radio buttons in jQuery

$(':radio').change(function(event) { var id = $(this).data('id'); $('#' + id).addClass('none').siblings().removeClass('none'); }); .none { display: none; } <script src="https://ajax.googleapis.com/ ...

Is it possible to reuse a variable within a single HTML tag when using Angular 2?

I encountered a strange issue with Angular 2 that may be a bug. I noticed that I couldn't print the same variable in a template twice within the same HTML tag. When I tried to use the following code, it resulted in error messages. <div class=" ...

Resolving Conflicting CSS Styles

It's frustrating to constantly have to ask questions. I've been struggling to implement code on my website, but every time I try to add something new, it just doesn't work. I even tried changing the CSS names, thinking that might solve the i ...

Angular HTML fails to update correctly after changes in input data occur

Within my angular application, there is an asset creator component designed for creating, displaying, and editing THREE.js 3D models. The goal was to implement a tree-view list to showcase the nested groups of meshes that constitute the selected model, alo ...

The jQuery code functions smoothly on computers, but experiences delays when running on an iPhone

I was working on my website and trying to add a div that sticks to the top of the browser when it scrolls out of view. I found a script that works well on desktop, but when testing it on iPhone, there is a slight delay before the div pops back up in the ri ...

"Stay informed with the latest updates on Wordpress through our

I am currently working on creating a static homepage with a newsfeed, but I only want to display the title and date posted. I considered using a widget, but I plan to integrate this news feed with a slideshow in the future and didn't want to limit my ...

blurring out of an input field and a division element

I am currently working on creating a dropdown suggestion box that hides when the input field and dropdown box are no longer in focus. My HTML code: <input type="text" id="user_address"> <div id="user_address_sg">SUGGESTION</div> <di ...

Code Snippet: Adjust Table Column Alignment using CSS

Apologies for the basic question, as I am new to CSS. Below is the UI that I am working with: https://i.stack.imgur.com/crAYE.png Here is the code that corresponds to the UI: <div class="row centered-form center-block"> <div cla ...

The orientation of Bootstrap flex - horizontal row or vertical column -

Bootstrap operates based on the class assigned to the parent element for flex row/column behavior. .bd-highlight { background-color: rgba(86, 61, 124, .15); border: 1px solid rgba(86, 61, 124, .15); } <link href="https://stackpath.bootstrapcdn.co ...

Button appears and disappears sporadically while browsing in Safari

I created a slider using SCSS, JavaScript, and HTML. You can view the demo at this link: https://jsfiddle.net/rr7g6a1b/ let mySlider = { initializeSlider: function (options) { let slider = options.container; let slides = slider.querySelectorAll( ...

Unable to switch the text option

[Fiddle] I'm currently working on a project where I want pairs of buttons to toggle text by matching data attributes. While I can successfully change the text from "Add" to "Remove" on click, I am facing an issue with toggling it back to "Add" on the ...

The Mystery of jQuery Isotope Plugin: Why Can't I Add "display:none" Inline?

I'm currently in the process of integrating Isotope into my latest Wordpress theme. However, I've encountered an issue where it's not appearing on the page due to some external factor adding an inline style (display:none) to the main isotope ...

Eliminate any hyperlink mentions from the Media Print Screen

I'm experiencing a small issue where I am unable to remove the link reference from the print view. Below is the HTML code snippet: <table style="width: 436px; height: 374px;" border="0" cellpadding="5"> <tbody> <tr style=" ...

Repeatedly view the identical file on HTML

I'm currently working with the following HTML code: <input type="file" style="display: none" #file(change)="processImage($event)" /> <button type="button" class="btn" (click)="file.click()" Browse </button> When I select image1 fr ...

Is it necessary to have col-xs-12 in Bootstrap 3?

In order to achieve the desired display for different screen sizes, we have implemented the following code: <div class="col-md-6">left</div> <div class="col-md-6">right</div> When viewed on smaller screens than md, this code succe ...