I have the following in my Sightly for an AEM component.Using jQuery, how can I copy the id that is generated for the div, into the three CSS styles? There will be several of these cards on the page, so this will need to iterate through each card.
<div id="${card.id}" class="card flex-card radius-lg rel bgcolor ${card.cardTheme}"
role="region" tabindex="-1">
<sly data-sly-test.isImageTheme="${card.cardTheme == 'theme-light-bg-img' || card.cardTheme == 'theme-dark-bg-img'}">
<style data-sly-test="${isImageTheme}">
@media (min-width:1025px) {
#id-placeholder {
background-image: url(${card.imageLifestyle @ context='attribute'});
background-size: cover;
background-position: bottom right;
}
}
@media (min-width:768px) and (max-width:1024px) {
#id-placeholder {
background-image: url(${card.imageLifestyleTablet @ context='attribute'});
background-size: cover;
background-position: bottom right;
}
}
@media (max-width:767px) {
#id-placeholder {
background-image: url(${card.imageLifestyleMobile @ context='attribute'});
background-size: cover;
background-position: bottom right;
}
}
</style>
</sly>
Here’s the latest jQuery I tried, but nothing seems to be able to set the id in the CSS.
$(".card").each(function() {
var id = $(this).attr('id');
var style = $(this).find('style').html();
style = style.replace(/#id-placeholder/g, '#' + id);
$(this).find('style').html(style);
});
````
2
Answers
Changing the CSS to target an Id directly in your HTML isn’t the best Idea.
Instead, you could combine the power of css variables and the
style
attribute to define an image for each size.You can, then, access those variables in your CSS and apply the styles that are shared between all your cards.
A css variable can be used to modify the style at run time, and can be set directly on an element. You can identify css variable since they start with
--
.Note that I’m not familiar with aem or sightly, so the following example are used as a demonstration only.
First, we will add a class that will be used to apply the style to all card. Here, i’m going to use
my-responsive-card
.Then, we can use the style attribute to define 3 css variables, one for each image size. Let’s use
--image-desktop
,--image-tablet
and--image-mobile
.Those variable can be defined like any other css property:
Now, once each card has their own css variables, we can access them in the stylesheet, where we can use the media query check, just like you did previously.
To access the css variable, you need to use the
var()
css function.Note that your first mediaquery condition is not required, since it’s the default value.
You can use Sightly/HTL variables to render the ids too:
But, to me, this is really ugly. I would instead store the background images as data attributes on the card and use attr in the CSS to set the background images, then the CSS can be kept generic and defined once for all cards:
and the HTML would look like: