• ;
  • A Coding Boy
  • Blog
  • Videos
  • Projects
  • Home
HTML AND CSS
LOGIN PAGE
WEBSITE DESGIN
API PROJECTS
CARD DESGIN
JAVASCRIPTS GAMES
JAVASCRIPT PROJECTS
JAVA PROJECTS
PYTHON PROJECTS
demo post
only for demo nasa prospect
only for demo the gonnies
most popular
how to create parallax website
how to create a beautiful card
how to create a netflix login page
how to create flipping ui card
how to create image generator website
How to Check Internet Connection in HTML CSS & JavaScript
In today’s world, the internet is essential for many websites and applications to function properly. To provide a seamless experience for users, it’s important to check the internet connection and take the necessary actions.
In this blog post, we’ll show you how to check internet connection and display relevant notifications using HTML, CSS, and JavaScript. Whether you’re new to web development or an experienced developer, you’re sure to learn something new about detecting network status with plain JavaScript.
Check Internet Connection [Source Code]
First, paste the following codes into your index.html file.
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<!-- Coding By CodingNepal - youtube.com/codingnepal -->
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Check Internet Connection | CodingNepal</title>
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Font Awesome CDN link for icons -->
<link rel="stylesheet" href="https://unicons.iconscout.com/release/v3.0.6/css/line.css">
<script src="script.js" defer></script>
</head>
<body>
<div class="notification">
<div class="content">
<div class="wifi-icon"><i class=""></i></div>
<div class="details">
<h2 class="title"></h2>
<p class="desc"></p>
<button class="reconnect-btn">Reconnect Now</button>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE html> <!-- Coding By CodingNepal - youtube.com/codingnepal --> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Check Internet Connection | CodingNepal</title> <link rel="stylesheet" href="style.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Font Awesome CDN link for icons --> <link rel="stylesheet" href="https://unicons.iconscout.com/release/v3.0.6/css/line.css"> <script src="script.js" defer></script> </head> <body> <div class="notification"> <div class="content"> <div class="wifi-icon"><i class=""></i></div> <div class="details"> <h2 class="title"></h2> <p class="desc"></p> <button class="reconnect-btn">Reconnect Now</button> </div> </div> </div> </body> </html>
Second, paste the following codes into your style.css file
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
/* Import Google font - Poppins */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
background: #E3F2FD;
}
.popup {
position: absolute;
left: 50%;
top: -25%;
visibility: hidden;
width: 490px;
border-radius: 5px;
padding: 13px 17px 20px;
background: #fff;
display: flex;
border-top: 3px solid #EA4D67;
transform: translateX(-50%);
box-shadow: 0 10px 25px rgba(52,87,220,0.1);
transition: all 0.25s ease;
}
.popup.show {
top: 0;
visibility: visible;
}
.popup.online {
border-color: #2ECC71;
}
.popup .icon i {
width: 40px;
height: 40px;
display: flex;
color: #fff;
margin-right: 15px;
font-size: 1.2rem;
align-items: center;
justify-content: center;
border-radius: 50%;
background: #EA4D67;
}
.popup.online .icon i {
background: #2ECC71;
}
.popup .title {
font-size: 1.2rem;
}
.popup .desc {
color: #474747;
margin: 3px 0 10px;
font-size: 1.04rem;
}
.popup .reconnect {
border: none;
outline: none;
color: #fff;
cursor: pointer;
font-weight: 500;
font-size: 0.95rem;
padding: 8px 13px;
border-radius: 4px;
background: #5372F0;
}
.popup.online .reconnect {
background: #bfbfbf;
pointer-events: none;
}
@media screen and (max-width: 550px) {
.popup {
width: 100%;
padding: 10px 15px 17px;
}
}
/* Import Google font - Poppins */ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap'); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { background: #E3F2FD; } .popup { position: absolute; left: 50%; top: -25%; visibility: hidden; width: 490px; border-radius: 5px; padding: 13px 17px 20px; background: #fff; display: flex; border-top: 3px solid #EA4D67; transform: translateX(-50%); box-shadow: 0 10px 25px rgba(52,87,220,0.1); transition: all 0.25s ease; } .popup.show { top: 0; visibility: visible; } .popup.online { border-color: #2ECC71; } .popup .icon i { width: 40px; height: 40px; display: flex; color: #fff; margin-right: 15px; font-size: 1.2rem; align-items: center; justify-content: center; border-radius: 50%; background: #EA4D67; } .popup.online .icon i { background: #2ECC71; } .popup .title { font-size: 1.2rem; } .popup .desc { color: #474747; margin: 3px 0 10px; font-size: 1.04rem; } .popup .reconnect { border: none; outline: none; color: #fff; cursor: pointer; font-weight: 500; font-size: 0.95rem; padding: 8px 13px; border-radius: 4px; background: #5372F0; } .popup.online .reconnect { background: #bfbfbf; pointer-events: none; } @media screen and (max-width: 550px) { .popup { width: 100%; padding: 10px 15px 17px; } }
Last, paste the following codes into your script.js file.
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
const popup = document.querySelector(".popup"),
wifiIcon = document.querySelector(".icon i"),
popupTitle = document.querySelector(".popup .title"),
popupDesc = document.querySelector(".desc"),
reconnectBtn = document.querySelector(".reconnect");
let isOnline = true, intervalId, timer = 10;
const checkConnection = async () => {
try {
// Try to fetch random data from the API. If the status code is between
// 200 and 300, the network connection is considered online
const response = await fetch("https://jsonplaceholder.typicode.com/posts");
isOnline = response.status >= 200 && response.status < 300;
} catch (error) {
isOnline = false; // If there is an error, the connection is considered offline
}
timer = 10;
clearInterval(intervalId);
handlePopup(isOnline);
}
const handlePopup = (status) => {
if(status) { // If the status is true (online), update icon, title, and description accordingly
wifiIcon.className = "uil uil-wifi";
popupTitle.innerText = "Restored Connection";
popupDesc.innerHTML = "Your device is now successfully connected to the internet.";
popup.classList.add("online");
return setTimeout(() => popup.classList.remove("show"), 2000);
}
// If the status is false (offline), update the icon, title, and description accordingly
wifiIcon.className = "uil uil-wifi-slash";
popupTitle.innerText = "Lost Connection";
popupDesc.innerHTML = "Your network is unavailable. We will attempt to reconnect you in <b>10</b> seconds.";
popup.className = "popup show";
intervalId = setInterval(() => { // Set an interval to decrease the timer by 1 every second
timer--;
if(timer === 0) checkConnection(); // If the timer reaches 0, check the connection again
popup.querySelector(".desc b").innerText = timer;
}, 1000);
}
// Only if isOnline is true, check the connection status every 3 seconds
setInterval(() => isOnline && checkConnection(), 3000);
reconnectBtn.addEventListener("click", checkConnection);
const popup = document.querySelector(".popup"), wifiIcon = document.querySelector(".icon i"), popupTitle = document.querySelector(".popup .title"), popupDesc = document.querySelector(".desc"), reconnectBtn = document.querySelector(".reconnect"); let isOnline = true, intervalId, timer = 10; const checkConnection = async () => { try { // Try to fetch random data from the API. If the status code is between // 200 and 300, the network connection is considered online const response = await fetch("https://jsonplaceholder.typicode.com/posts"); isOnline = response.status >= 200 && response.status < 300; } catch (error) { isOnline = false; // If there is an error, the connection is considered offline } timer = 10; clearInterval(intervalId); handlePopup(isOnline); } const handlePopup = (status) => { if(status) { // If the status is true (online), update icon, title, and description accordingly wifiIcon.className = "uil uil-wifi"; popupTitle.innerText = "Restored Connection"; popupDesc.innerHTML = "Your device is now successfully connected to the internet."; popup.classList.add("online"); return setTimeout(() => popup.classList.remove("show"), 2000); } // If the status is false (offline), update the icon, title, and description accordingly wifiIcon.className = "uil uil-wifi-slash"; popupTitle.innerText = "Lost Connection"; popupDesc.innerHTML = "Your network is unavailable. We will attempt to reconnect you in <b>10</b> seconds."; popup.className = "popup show"; intervalId = setInterval(() => { // Set an interval to decrease the timer by 1 every second timer--; if(timer === 0) checkConnection(); // If the timer reaches 0, check the connection again popup.querySelector(".desc b").innerText = timer; }, 1000); } // Only if isOnline is true, check the connection status every 3 seconds setInterval(() => isOnline && checkConnection(), 3000); reconnectBtn.addEventListener("click", checkConnection);
Conclusion and Final Words
By following the steps in this blog post, you’ve learned how to use HTML, CSS, and JavaScript to check the internet connection and display appropriate notifications to the user. However, keep in mind that there are alternative methods to check an internet connection, such as the JavaScript navigator.onLine method.
If you encounter any problems or your code is not working as expected, you can download the source code files of this Internet checker project by clicking on the given download button. It’s free, and a zip file will be downloaded that contains the project folder with source code files.
Download files
Info
Home
Projects
Blogs
Videos
About us
Follow us for more!
© 2023 All Rights Reserved A Coding Boy

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Glassmorphism Login Form | CodingNepal</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="wrapper"> <form action="#"> <h2>Login</h2> <div class="input-field"> <input type="text" required> <label>Enter your email</label> </div> <div class="input-field"> <input type="password" required> <label>Enter your password</label> </div> <div class="forget"> <label for="remember"> <input type="checkbox" id="remember"> <p>Remember me</p> </label> <a href="#">Forgot password?</a> </div> <button type="submit">Log In</button> <div class="register"> <p>Don't have an account? <a href="#">Register</a></p> </div> </form> </div> </body> </html>

@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@200;300;400;500;600;700&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Open Sans", sans-serif; } body { display: flex; align-items: center; justify-content: center; min-height: 100vh; width: 100%; padding: 0 10px; } body::before { content: ""; position: absolute; width: 100%; height: 100%; background: url("https://www.codingnepalweb.com/demos/create-glassmorphism-login-form-html-css/hero-bg.jpg"), #000; background-position: center; background-size: cover; } .wrapper { width: 400px; border-radius: 8px; padding: 30px; text-align: center; border: 1px solid rgba(255, 255, 255, 0.5); backdrop-filter: blur(9px); -webkit-backdrop-filter: blur(9px); } form { display: flex; flex-direction: column; } h2 { font-size: 2rem; margin-bottom: 20px; color: #fff; } .input-field { position: relative; border-bottom: 2px solid #ccc; margin: 15px 0; } .input-field label { position: absolute; top: 50%; left: 0; transform: translateY(-50%); color: #fff; font-size: 16px; pointer-events: none; transition: 0.15s ease; } .input-field input { width: 100%; height: 40px; background: transparent; border: none; outline: none; font-size: 16px; color: #fff; } .input-field input:focus~label, .input-field input:valid~label { font-size: 0.8rem; top: 10px; transform: translateY(-120%); } .forget { display: flex; align-items: center; justify-content: space-between; margin: 25px 0 35px 0; color: #fff; } #remember { accent-color: #fff; } .forget label { display: flex; align-items: center; } .forget label p { margin-left: 8px; } .wrapper a { color: #efefef; text-decoration: none; } .wrapper a:hover { text-decoration: underline; } button { background: #fff; color: #000; font-weight: 600; border: none; padding: 12px 20px; cursor: pointer; border-radius: 3px; font-size: 16px; border: 2px solid transparent; transition: 0.3s ease; } button:hover { color: #fff; border-color: #fff; background: rgba(255, 255, 255, 0.15); } .register { text-align: center; margin-top: 30px; color: #fff;}