How to Use Different Banners for Desktop and Mobile in Shopify
Published on Jul 2, 2024
In today’s mobile-first world, optimizing your Shopify store for both desktop and mobile views is crucial. One common challenge store owners face is displaying different banner images for desktop and mobile devices. This article will guide you through the process of achieving this in the Shopify Dawn theme, ensuring your store looks great on all devices.
Understanding the Challenge
The Importance of Device-Specific Banners
Having separate banner images for desktop and mobile is essential for several reasons:
- Screen size differences
- User experience optimization
- Performance considerations
Default Behavior in Dawn Theme
By default, the Dawn theme doesn’t provide a straightforward way to display different images for desktop and mobile. This can lead to compromised visuals on one or both device types.
The Goal: Seamless Banner Switching
Our objective is to implement a solution that automatically switches between desktop and mobile banner images based on the user’s device.
The Solution: Custom CSS for Banner Switching
Implementing the Code
To achieve our goal, we need to add some custom CSS to the Dawn theme. Here’s the step-by-step process:
- Go to your Shopify admin panel
- Navigate to Online Store > Themes
- Click on “Actions” for your active theme and select “Edit code”
- Locate the file
Assets > section-image-banner.css
- Add the following code at the bottom of the file:
.banner__media:first-child {
width: 100%;
}
.banner__media+.banner__media {
display: none;
}
@media screen and (max-width: 749px) {
.banner__media:first-child {
display: none;
}
.banner__media+.banner__media {
width: 100%;
display: block !important;
}
}
Understanding the Code
This CSS code does the following:
- Sets the first banner image (desktop) to 100% width
- Hides the second banner image by default
- On screens smaller than 749px (mobile devices):
- Hides the first (desktop) image
- Displays the second (mobile) image at full width
Additional Mobile Optimization
To ensure proper sizing on mobile devices, add this additional CSS:
@media screen and (max-width: 749px) {
.banner__content::before {
padding-bottom: 150% !important;
}
}
This code adjusts the aspect ratio of the banner on mobile devices, preventing image cropping.
Setting Up Your Banner Images
Preparing Your Images
To make the most of this solution:
Create two versions of your banner image:
- A landscape version for desktop
- A portrait or square version for mobile
Optimize both images for web use to ensure fast loading times
Uploading and Configuring Images
In your theme customizer:
- Add your desktop image to the left slot of the banner section
- Add your mobile image to the right slot
- Adjust text and button positioning as needed for both versions
Troubleshooting Common Issues
Blurry Desktop Images
Some users reported blurry desktop images after implementation. To fix this:
- Ensure your desktop image is high quality and properly sized
- Check that the image isn’t being stretched or compressed by the theme
Slideshow Compatibility
The provided solution may affect slideshow functionality. If you’re using slideshows:
- Consider creating a separate CSS rule for slideshows
- Test thoroughly on both desktop and mobile devices
Image Cropping on Mobile
If your mobile image appears cropped:
- Adjust the aspect ratio in the additional CSS provided earlier
- Experiment with different image dimensions to find the best fit
Advanced Customization
Adjusting Breakpoints
The current solution uses 749px as the breakpoint. You can adjust this to suit your needs:
@media screen and (max-width: YOUR_DESIRED_BREAKPOINT) {
/* existing mobile CSS rules */
}
Creating Multiple Breakpoints
For more granular control, consider adding multiple breakpoints:
@media screen and (max-width: 749px) {
/* mobile styles */
}
@media screen and (min-width: 750px) and (max-width: 989px) {
/* tablet styles */
}
@media screen and (min-width: 990px) {
/* desktop styles */
}
Best Practices for Banner Images
Optimizing for Performance
- Compress your images without sacrificing quality
- Use modern image formats like WebP where supported
- Implement lazy loading for images below the fold
Design Considerations
- Maintain brand consistency across desktop and mobile versions
- Ensure text is readable on both versions
- Test your banners on various devices and screen sizes
By following this guide, you’ll be able to display different banner images for desktop and mobile views in your Shopify store using the Dawn theme. This enhancement will significantly improve your store’s visual appeal and user experience across all devices.
Take Our Quick Quiz:
Which primary product image do you think has the highest conversion rate?