I’m currently working on a project in which I have elements that are displayed as cards with an aspect-ratio of 5:6 on desktop. On mobile, the same elements are displayed as entries of a stylized list that are very different with a aspect-ratio 1:10.
To accommodate for this situation, each element is currently split in two – one desktop version, and one mobile version. I’m currently trying to merge both versions to lay groundworks for future code changes, and to reduce maintenance time when adding new elements. This is an excerpt of the current code (in actuality there are more than 15 cards already, see the extended version and CSS here):
<div class="desktop-wrapper">
<div class="card" style="background-image: url('[name]-l.webp')">
<div class="card-flexbox">
<h2 class="titel">Example 1</h2>
<p class="hover-text">
Lorem ipsum dolor sit amet, consectetur adipisici elit
</p>
<a href="#" class="card-btn">More info</a>
</div>
<!--some overlays for aesthetics-->
<div class="img-overlay-2"></div>
<div class="img-overlay-1"></div>
<!--this link makes everything clickable-->
<a href="#">
<span class="link-span"></span>
</a>
</div>
(...)
</div>
<!--mobile version-->
<div class="mobile-wrapper">
<a href="#" class="mobile-card-link">
<div class="mobile-karte" style="background-image: url('[name]-s.webp')">
<h3 class="titel">Example 1</h3>
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width="20px">
<path class="chevron" d="(...)" /></svg>
<!--overlay for aesthetics-->
<div class="mobile-card-overlay"></div>
</div>
</a>
(...)
</div>
The problem: Each version of the cards uses a different picture for art direction reasons. A wide version "[name]-s.webp" is used for the mobile version, and a taller version for desktop, "[name]-l.webp".
My preferred way of solving the issue would be having a script that takes advantage of the above mentioned naming convention. Ideally I’d just write <div class="mobile-karte" style="background-image: url('./img/stamp')">...</div>
in my HTML and have the script extend the background-image url based on screen size — for example by adding "-s.webp" to the background-image url below a breakpoint of 600px. I’m thinking of something like this, just more automated.
Is this possible? If so, how?
Other options I thought about:
- Giving each element an individual ID and creating media queries to change out the background-image at a certain breakpoint. A good fallback-option, but I’d prefer a script to simplify things and reduce manual labour.
- Using image-set(). Seems like a lot of manual labour each time a new card has to be added. Also, it doesn’t allow for specific breakpoints to the best of my knowledge.
- srcset. While it would allow for specific breakpoints, I’d have to create my background-images as img-elements, which seems a bit complicated.
2
Answers
I ended up finding a solution on my own. Using data-name attributes for the cards and then having a small script generate the according background-images (and even links) automatically. It looks something like this:
To be honest, I don’t think you need any JS code to handle your problem. Just better media-query management.
I created a codepen with a working solution, here
Basically, I entirely removed the mobile section.
Then, I moved all the
:hover
interactions inside a media query, since they are used only for the desktop version.After that, inside a media query, I modified the already present sections to fit correctly inside the mobile view:
Most important parts for the mobile media queries:
flex-direction: row
to handle the new orientationThe are also some others small changes as the SVG being visible only in the mobile view.
The codepen is usable and gets a result nearly identical to yours, but using only one version of each card