Skip to content

Instantly share code, notes, and snippets.

@asrorbekh
Created June 27, 2024 08:04
Show Gist options
  • Save asrorbekh/831c68cc9ffe9c7fc382fbe911b51b3d to your computer and use it in GitHub Desktop.
Save asrorbekh/831c68cc9ffe9c7fc382fbe911b51b3d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Device Type Detection</title>
<style>
/* Base styles */
body {
font-family: Arial, sans-serif;
}
/* Styles for phones */
@media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
/* Styles for tablets */
@media only screen and (min-width: 601px) and (max-width: 1024px) {
body {
background-color: lightgreen;
}
}
/* Styles for desktops */
@media only screen and (min-width: 1025px) {
body {
background-color: lightcoral;
}
}
</style>
</head>
<body>
<h1 id="device-type">Loading...</h1>
<script>
function getDeviceType() {
const width = window.innerWidth;
if (width <= 600) {
return 'phone';
} else if (width > 600 && width <= 1024) {
return 'tablet';
} else {
return 'desktop';
}
}
window.onload = function () {
const deviceType = getDeviceType();
document.getElementById('device-type').innerText = `This device is a ${deviceType}`;
};
window.onresize = function () {
const deviceType = getDeviceType();
document.getElementById('device-type').innerText = `This device is a ${deviceType}`;
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment