Skip to main content

Display Battery Status using HTML, CSS and Javascript

    Hey everyone, welcome to Code Instinct! In this tutorial, we're going to show you how to create a battery status indicator using HTML, CSS and Javascript. This is a great Javascript project for beginners who want to learn how to use the Battery API Javascript and create a battery level indicator. We'll also be showing you how to add a battery charging animation and CSS battery shape to make it more visually appealing.Our tutorial will cover all the steps needed to create your own battery status indicator with ease. You'll learn how to use the Battery API Javascript to get the current battery level of your device, and display it using HTML and CSS. 

    We'll also be covering the different types of battery indicators you can use and how to customize them to fit your website design. You'll learn how to create a battery charging animation using Javascript and CSS to give your battery status indicator a more dynamic look.At Code Instinct, we're dedicated to providing Javascript tutorials for beginners, and this project is perfect for anyone who wants to learn more about Javascript and web development. 

    Whether you're looking to build your portfolio or just want to add a new skill to your resume, this battery level indicator project is a great way to get started.We offer a wide range of tutorials, from Javascript basics to more advanced coding techniques, so make sure to subscribe to our channel for more great content. So what are you waiting for? Let's start creating our battery level indicator using HTML, CSS and Javascript! Don't forget to hit that subscribe button to stay up-to-date with all our latest tutorials.

index.html


  <html>
      <head>
        <meta charset="UTF-8">
        <title>Battery Status</title>
        <link rel="stylesheet" href="style.css">
      </head>
      <body>
          <div class="container">
              <div class="battery-status">
                <div class="battery-level">
                  <div class="battery-fill"></div>
                  <span class="battery-percentage">100%</span>
                </div>
                <div class="battery-status-text">Plugged In</div>
              </div>
          </div>
          <script src="script.js"></script>
      </body>
  </html>



style.css

  body{
      font-family: sans-serif;
      background-color: #20d2df;
  }

  .container{
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
  }

  .battery-status{
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 20px;
      background-color: #ffffff;
      border-radius: 10px;
      box-shadow: 0 20px 10px rgba(202,202,202,0.2);
  }

  .battery-level{
      position: relative;
      width: 150px;
      height: 60px;
      background-color: #e4e4e4;
      border-radius: 30px;
      overflow: hidden;
  }

  .battery-fill{
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      background-color: #00e09d;
      transition: width 0.5s ease;
  }

  .battery-percentage{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-size: 24px;
      font-weight: bold;
      color: #f5f5f5;
  }

  .battery-status-text{
      margin-top: 10px;
      font-size: 20px;
      font-weight: bold;
      color: #a8a8a8;
  }


script.js

navigator.getBattery().then(function(battery)
{
    updateBatteryStatus(battery);
    battery.addEventListener('levelchange',function(){
        updateBatteryStatus(battery);
    });
    battery.addEventListener('chargingchange', function(){
        updateBatteryStatus(battery);
    });
});

function updateBatteryStatus(battery) {
    var batteryFill = document.querySelector(".battery-fill");
    var batteryPercentage = document.querySelector(".battery-percentage");
    var batteryStatusText = document.querySelector(".battery-status-text");

    var fillWidth = Math.round(battery.level * 100) + "%";
    batteryFill.style.width = fillWidth;
    batteryPercentage.innerHTML = fillWidth;

    if (battery.charging){
        batteryStatusText.innerHTML = "Now is Charging";
    } else {
        batteryStatusText.innerHTML = "Not Charging";
    }
}

Watch Full Tutorial Here! 

Comments

Popular posts from this blog

Auto Image Slider using CSS Only

     Are you ready to learn how to create an image slider using only CSS? Look no further! In this tutorial, we'll walk you through the step-by-step process of building a sleek and modern image slider using HTML and CSS. No JavaScript required! In this tutorial We'll cover setting up the HTML structure for the slider.       This will include creating the container element and styling the slider with CSS. You'll also learn how to add transitions and animations to create a smooth sliding effect. By the end of this tutorial, you'll have a fully functional image slider that you can use on your own website. Whether you're a beginner or an experienced developer, this tutorial has something for you. So let's get started creating your own image slider using CSS today! index.html <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/style.css"&

Banner image/Hero Image Tutorial with HTML and CSS

     In this video, we will be creating a banner/hero image using CSS. A banner or hero image is a large, full-width image that is typically placed at the top of a website or webpage and is used to visually represent the content or purpose of the page. We will be using CSS properties such as background-image and background-size to create the banner/hero image, as well as some basic layout techniques to ensure that it looks great on all devices. By the end of this tutorial, you will have a fully functional banner/hero image that you can use on your own website. So, if you want to learn how to create a banner/hero image using CSS, be sure to follow this tutorial till the end.   index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Tutorial 6</title> <link rel="stylesheet" href="css/tutorial6.css"> <link href="https://fonts.googleapis.co

Image with Accordion Effect on Hover using CSS and HTML

     Hey there, folks! Are you looking to jazz up your website's image galleries? Well, you've come to the right place! In this tutorial, we'll show you how to create an image with accordion effect on hover using HTML and CSS. First off, let's chat about HTML and CSS. HTML is like the building blocks of web pages, while CSS adds style and personality to your website.       By combining these two, you can create some seriously cool designs that'll knock your user's socks off!In this tutorial, we're gonna be using CSS animation effects to make an accordion effect on an image when a user hovers over it. It's a slick way to add a bit of movement and interactivity to your web design. To get started, we'll walk you through creating a basic HTML structure for your image.       Then, we'll apply some CSS styles to give it that fancy accordion look. Finally, we'll create a hover effect using CSS animation that'll make your image expand and colla