What's your biggest current challenge? Have your say in Community Polls along the right column.

Re: Add a different css to an image when it is clicked (on custom color swatches)

Add a different css to an image when it is clicked (on custom color swatches)

MichalNicolas
Visitor
2 0 0

Hi guys, I'm trying to create a custom 'color swatches' using metafields (list of products)

 

Screenshot 2024-05-13 at 3.58.01 PM.png

 

I already worked on the html and css code for most of it, but I want to add a different css to the image that has been clicked on while the customer is on its product page. Here's the code that I have so far, I'm doing it in a "custom liquid block" on the product page template of my Theme (Pipeline):

 

HTML:

 

<div class="color-swatches-wrapper">
{% assign color_swatches = product.metafields.custom.color_swatches.value %}

{% if color_swatches %}
<p class="color-swatches-heading">Color</p>
<div class = "color-swatches-list">
{% for color_swatch in color_swatches %}
<a href = "{{shop.url | append: "/products/" | append: color_swatch.handle }}">
<div class ="color-swatch-t8">
<div class ="color-swatch-image">
<img class="t8_product_color" src={{color_swatch.featured_image | image_url: width: 100 | image_tag }}
</div>
</div>
</a>
{% endfor %}
</div>
{% endif %}
</div>

 

CSS:

 

<style>

.color-swatches-wrapper {
}

.color-swatches-list {
display: flex;
gap: 2%;
padding-bottom: 20px;
}
.color-swatch-t8 {
display: inline-block;
cursor: pointer;
position: relative;
overflow: hidden;
width: 45px;
}

.color-swatches-heading {
color: #e9e9e9;
font-weight: bold;
padding-bottom: 10px;
}

.color-swatch-image {
border: 1px solid #e9e9e9;
}

.color-swatch-image:hover {
border: 1px solid #272727;
background: #272727;
opacity: 0.4;
}

</style>

 

Here's the result so far:

 

Screenshot 2024-05-13 at 4.02.30 PM.png

 

As I am on the "Black" option of the product, I want the black swatch to have a different border.

 

Hope someone can help me.

 

Thanks.

 

Reply 1 (1)

EBOOST
Shopify Partner
1253 324 373

Hi @MichalNicolas ,

May I suggest code below:

<div class="color-swatches-wrapper">
	{% assign color_swatches = product.metafields.custom.color_swatches.value %}

	{% if color_swatches %}
		<p class="color-swatches-heading">Color</p>
		<div class = "color-swatches-list">
			{% for color_swatch in color_swatches %}
				<a class="{% if color_swatch.handle == product.handle %} active-color {% endif %}" href = "{{shop.url | append: "/products/" | append: color_swatch.handle }}">
					<div class ="color-swatch-t8">
						<div class ="color-swatch-image">
							<img class="t8_product_color" src={{color_swatch.featured_image | image_url: width: 100 | image_tag }} />
						</div>
					</div>
				</a>
			{% endfor %}
		</div>
	{% endif %}
</div>
<style>

.color-swatches-wrapper{
}

.color-swatches-list {
	display: flex;
	gap: 2%;
	padding-bottom: 20px;
}
.color-swatch-t8 {
	display: inline-block;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	width: 45px;
}

.color-swatches-heading {
	color: #e9e9e9;
	font-weight: bold;
	padding-bottom: 10px;
}

.color-swatch-image {
	border: 1px solid #e9e9e9;
}
.color-swatches-wrapper .color-swatches-list a.active-color  .color-swatch-image,
.color-swatch-image:hover {
	border: 1px solid #272727;
	background: #272727;
	opacity: 0.4;
}

</style>

EBOOST_0-1715585131014.png

 

- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free
- ❤❤DONATE ❤❤Coffee tips