- Understand the considerations for using Optimizely Web Experimentation with responsive sites
- Use media queries, style sheets, or JavaScript to accommodate responsive sites
- Create audience targeting conditions to include or exclude visitors based on specific screen measurements
Responsive sites are designed to respond and adapt to different devices and screen sizes seamlessly. They resize elements and change layouts to provide the best experience for every case.
Optimizely Web Experimentation lets you see how changes that you make with the Visual Editor look on a responsive site.
The following methods are generally used to create responsive sites:
-
Edit the CSS of media queries
-
Add a custom class to elements you want to be responsive, then add media queries to Experiment CSS to apply the changes across variations
-
Replace style sheets based on device
-
Custom JavaScript
See the links to tutorials at the bottom of this article to learn more.
Make CSS changes
You can use Optimizely Web Experimentation to make a CSS change to a media query.
For example, here's how to edit the class .showcase-left
above to change the max-width property of its @media="all" query parameter to 50%:
-
Go to your variation.
-
In the Editor, select the Variation Code Editor icon (
).
-
Edit the media query for the img attribute.
-
Click Save to apply.
Audience targeting
If you want to dramatically change your visitors' experience based on the device they use to visit your site, use audience conditions to target your experiments by device or by platform.
In some cases, targeting by device type may still be too general for your responsive design. Instead, you might choose to include or exclude certain visitors entirely based on specific screen sizes.
Custom JavaScript audience targeting
You can also use a custom JavaScript audience condition to target visitors who view screen sizes above or below a certain pixel count.
Below are two code samples for audience conditions that target screen size. Learn more about how screen definitions in JavaScript and jQuery align to CSS media attributes. Then, use these code examples target screen size with the custom JavaScript:
//target desktop viewports
//matches CSS media queries for height/width or max/min-height or
//-width
window.innerWidth > 1400 && window.innerHeight > 800
//target mobile phones
//matches CSS media queries using device-height and device-width
screen.width >= 320 && screen.width <= 480 && screen.height <= 640
It is best to set breakpoints and targeting conditions based on your design and not based on the device. The above examples use device measurements to illustrate how to write the JavaScript audience condition.
Related articles
-
Google Developers, “Use CSS media queries for responsiveness”
-
One Extra Pixel, "Responsive Web Design: Layouts and Media Queries"
-
Stack Overflow, "Responsive Web Design is Working on Desktop but not on Mobile Device"
Please sign in to leave a comment.