Apply styles based on screen size
Posted: Thu Jan 08, 2026 9:31 pm
Here in one kind of solution to apply some styles to desktop, tablet and mobile.
You are free to test any css.
There is three options to use code: button, resize and Dom already loaded. The end result may be different.
The resize option is on in example but you can change it.
You are free to test any css.
There is three options to use code: button, resize and Dom already loaded. The end result may be different.
The resize option is on in example but you can change it.
Code: Select all
EnableJS
var button = document.createElement('button');
button.textContent = 'Modify Css';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '9999';
button.style.padding = '10px 20px';
button.style.backgroundColor = '#0076ff';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
document.body.appendChild(button);
function applyStylesBasedOnScreenSize() {
var isDesktop = window.matchMedia('(min-width: 1024px)').matches;
var isTablet = window.matchMedia('(min-width: 768px) and (max-width: 1023px)').matches;
var isMobile = window.matchMedia('(max-width: 767px)').matches;
var content = document.querySelector('.page__content');
var cont = document.querySelector('.page');
if (isDesktop) {
// Desktop styles
if (content) {
content.style.left = '45px';
content.style.right = '45px';
content.style.top = '100px';
content.style.border = '1px solid red';
}
if (cont) {
cont.style.left = '10%';
cont.style.right = '10%';
cont.style.top = '10px';
}
} else if (isTablet) {
// Tablet styles
if (content) {
content.style.left = '30px';
content.style.right = '30px';
content.style.top = '80px';
content.style.border = '2px dotted blue';
}
if (cont) {
cont.style.left = '5%';
cont.style.right = '5%';
cont.style.top = '10px';
cont.style.boxShadow = '15px 20px 30px cornflowerblue';
}
} else {
// Mobile styles
if (content) {
content.style.left = '15px';
content.style.right = '15px';
content.style.top = '60px';
content.style.border = '1px solid green';
}
if (cont) {
cont.style.left = '2%';
cont.style.right = '2%';
cont.style.top = '10px';
}
}
}
//button eventListener
//button.addEventListener('click', applyStylesBasedOnScreenSize);
// Also apply on window resize
window.addEventListener('resize', applyStylesBasedOnScreenSize);
/*
// Apply on page load with timer (wait for OnsenUI to create elements)
function applyOnLoad() {
var content = document.querySelector('.page__content');
var cont = document.querySelector('.page');
if (content || cont) {
// Elements exist, apply styles
applyStylesBasedOnScreenSize();
} else {
// Elements don't exist yet, check again in 100ms
setTimeout(applyOnLoad, 100);
}
}
// Start checking when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
setTimeout(applyOnLoad, 200);
});
} else {
// DOM already loaded, start checking
setTimeout(applyOnLoad, 200);
}
*/
DisableJS