2

I want to use one class for 3 buttons but with different background-image such that each button will have its own image. I was looking to declare a background-image array in css and then retrieve the image. However as per my research, there is no array declaration in css. Is this right? If yes what alternative do I have to retrieve each image for 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%;
  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;

}
0

2 Answers 2

4

Use the :nth-child() selector. The CSS code will become:

.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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks, but the buttons are only taking the first nth-child. Do I have to declare in HTML as <span class="access_icon:nth-child(2)"> ?
I've modified it. It should work now. jsfiddle.net/r67puwLm
0

Maybe some people disagree here. But I would reach for a solution using CSS-IDs or additional CSS-Classes.

Thus you are not restricted to the nesting and the order of the buttons.

v1 Using CSS-IDs 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>
...

v1 Using CSS-IDs 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
}

v2 Using additional CSS-Classes 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>
...

v2 Using additional CSS-Classes 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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.