The QR code disappears from the screen once the button is no longer pressed

After following an online tutorial on creating a QR code generator, the code works fine and I am able to display the image. However, I'm facing an issue where the image disappears from the screen once the button is released.

Here is the code snippet:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./style2.css">
    <style>
        .container.active .qr-code {
            display: block;
            text-align: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
   <div class="container">
    <div class="header">
        <h1>QR Code Generator</h1>
        <p>Type a URL or text to generate a QR Code</p>
    </div>
    <div class="input-form">
        <input type="text" class="qr-input" placeholder="Enter URL or text">
        <button class="generate-btn">Generate QR Code</button> 
    </div>
    <div class="qr-code">
        <img class="qr-image" alt="">
    </div>
   </div>

<script>
    var container = document.querySelector(".container");
    var generateBtn = document.querySelector(".generate-btn");
    var qrInput = document.querySelector(".qr-input");
    var qrImg = document.querySelector(".qr-image");

    generateBtn.addEventListener("click", function(event) {
        if (qrInput.value.length > 0) {
            event.preventDefault(); // Prevent form submission
            container.classList.add("active");
            var qrUrl = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + encodeURIComponent(qrInput.value);
            qrImg.src = qrUrl;
        }
    });
</script>
</body>
</html>


CSS:

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: 'Poppins';
}

body {
    width: 100%;
    height: 100vh;
    background-color: #ff676d;
    display: flex;
    align-items: center;
    justify-content: center;
}   

.container {
    background-color: #fff;
    width: 400px;
    border-radius: 7px;
    padding: 20px;
    height: 400px;
    transition: .1s;
}

.header h1{
    font-size: 23px;
    font-weight: 500;
    margin-bottom: 5px;
}

.header p {
    font-size: 16px;
    margin-bottom: 10px;
}

input, button {
    width: 100%;
    height: 50px;
    outline: none;
    border-radius: 5px;
}

button {
    border: none;
    background-color: #1d68d8;
    font-size: 15px;
    columns: #fff;
    cursor: pointer;
}

input {
    border: 1px solid #8b8a8a;
    padding-left: 10px;
    margin-bottom: 15px;
    font-size: 15px;
}

.qr-code { 
    padding: 25px 0;
    border: 1px solid #ccc;
    margin-top: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    opacity: 0;
    pointer-events: none;
    transition: .5s;
}

.container:active {
    height: 490px;
}

.container:active .qr-code {
    opacity: 1;
    pointer-events: auto;
}

I'm relatively new to this and have tried modifying the display property within the CSS for the `qr-code` section. But upon further reading, it seems like that might not be the correct approach.

Perhaps I'm overlooking something in the HTML structure.

Answer №1

You seem to have confused two concepts.

The :active pseudo-class in CSS is activated when the user 'presses' a button and deactivated when they 'release' it.

In your stylesheet, you have set the opacity to 1 for the :active state.

However, in your Javascript code, you are adding a class named active to the element.

To ensure that your stylesheet recognizes this class rather than the pseudo-class, you should use .active in your CSS rules.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: 'Poppins';
}

body {
  width: 100%;
  height: 100vh;
  background-color: #ff676d;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container {
  background-color: #fff;
  width: 400px;
  border-radius: 7px;
  padding: 20px;
  height: 400px;
  transition: .1s;
}

.header h1 {
  font-size: 23px;
  font-weight: 500;
  margin-bottom: 5px;
}

.header p {
  font-size: 16px;
  margin-bottom: 10px;
}

input,
button {
  width: 100%;
  height: 50px;
  outline: none;
  border-radius: 5px;
}

button {
  border: none;
  background-color: #1d68d8;
  font-size: 15px;
  columns: #fff;
  cursor: pointer;
}

input {
  border: 1px solid #8b8a8a;
  padding-left: 10px;
  margin-bottom: 15px;
  font-size: 15px;
}

.qr-code {
  padding: 25px 0;
  border: 1px solid #ccc;
  margin-top: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  opacity: 0;
  pointer-events: none;
  transition: .5s;
}

.container.active {
  height: 490px;
}

.container.active .qr-code {
  opacity: 1;
  pointer-events: auto;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./style2.css">
  <style>
    .container.active .qr-code {
      display: block;
      text-align: center;
      margin-top: 20px;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="header">
      <h1>QR Code Generator</h1>
      <p>Type a URL or text to generate a QR Code</p>
    </div>
    <div class="input-form">
      <input type="text" class="qr-input" placeholder="Enter URL or text">
      <button class="generate-btn">Generate QR Code</button>
    </div>
    <div class="qr-code">
      <img class="qr-image" alt="">
    </div>
  </div>

  <script>
    var container = document.querySelector(".container");
    var generateBtn = document.querySelector(".generate-btn");
    var qrInput = document.querySelector(".qr-input");
    var qrImg = document.querySelector(".qr-image");

    generateBtn.addEventListener("click", function(event) {
      if (qrInput.value.length > 0) {
        event.preventDefault(); // Prevent form submission
        container.classList.add("active");
        var qrUrl = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + encodeURIComponent(qrInput.value);
        qrImg.src = qrUrl;
      }
    });
  </script>
</body>

</html>

For more information on the :active pseudo-class, visit MDN

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Concealing the submit button until a file has been selected

How can I make the submit button invisible until a file is selected? <form action="upload.php" method="POST" enctype="multipart/form-data"> <input type="file" name="imageURL[]" id="imageURL" /> <input type="submit" value="submi ...

a new webpage beginning from a different location

Starting from scratch here, I must apologize for not providing any code upfront. The issue at hand is this - I've got a page full of information sorted by ID (retrieved from the database). These IDs are linked from another page where users can click ...

children blocking clicks on their parents' divs

I previously asked a question about a parent div not responding to click events because its children were blocking it. Unfortunately, I couldn't recreate the issue without sharing a lot of code, which I didn't want to do. However, since I am stil ...

Animation event in CSS3 fails to trigger on Firefox browser

Exploring the potential of CSS3 animation to detect when an input transitions from the enable to disable state, I encountered a challenge specifically in Firefox. Here is the code snippet that showcases the issue: View Demo on jsFiddle HTML: <input t ...

The jQuery window.load() function fails to execute on iOS5 devices

I added a basic loading div to my website that fades out once the entire page has finished loading. The code I used is simple: $(window).load(function(){ $('#loading').fadeOut(500); }); While this works well on all desktop browsers and Chro ...

Hovering over overlapping spans without moving them

For the text formatting, I attempted to use the following code: <style> #items { width:400px; padding: 10px; } .esp { float: left; background-color: yellow; position:relative; z-index:0; } .le { float: left; position:rela ...

Ways to expand the tooltip width in Bootstrap-Vue

Is there a way to make the tooltip wider in Bootstrap Vue? I have a long statement to display in the tooltip, but it is only showing three words per row, resulting in a taller tooltip with less width. <div> <span id="disabled-wrapper" class=" ...

Unable to call upon JavaScript code from an external file

Just starting out with Spring and JavaScript! I've put together a JSP file https://i.sstatic.net/XemJ5.png In my first.js, you'll find the following method: function firstmethod() { window.alert("Enter a New Number"); return true; } H ...

Having divs stacked on top of each other with one positioned fixed and the other

In my HTML document, there is a main div element called container that contains several child elements also in the form of divs. One specific child div, named top_bar, has its positioning set to fixed, while the rest of the child divs have relative posit ...

How to access the CSS and JS files in the vendor folder using linemanjs

Currently, I am in the process of developing a web application utilizing linemanjs. However, I have encountered an issue where the vendor css and js files are not being referenced correctly when explicitly included. I would appreciate any guidance on what ...

Troubleshooting Hierarchical Ordering in Django-MPTT's recursetree Function

I am currently utilizing the django-mptt package within my Django application, and I have encountered an issue when trying to use the .order_by() method in conjunction with a filter. Despite attempting different criteria for ordering, the results remain un ...

Ways to embed an HTML file into another HTML document

I have been attempting to include a footer.htm file in my index.htm document without success. Below is the code I am using. index.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.d ...

Nothing is being returned when using JQuery AJAX POST

Having trouble identifying the issue, as all that seems to happen is the URL changes to "http://localhost/?search=test" when entering "test", for example. index.php <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquer ...

Is it possible to upload files without using AJAX and instead through synchronous drag-and-drop functionality in the foreground?

Currently, my website has a standard <input type="file"> file upload feature that sends data to the backend upon form submission. I am looking to enhance the functionality of the form by allowing users to drag and drop files from anywhere within the ...

How can I ensure the footer stays at the bottom of the page using a table layout?

For quite some time now, I've been working on achieving a table layout with a sticky footer. My specific requirement is for the footer to remain fixed at the bottom even when zooming in or out. However, whenever I try to adjust the zoom level of the w ...

Using jQuery to update the parent element from within an iframe

Below is a simplified test scenario: a.html <!DOCTYPE html> <html> <body> <input type="text" id="myinput"> <iframe id="frame" src="b.html" style="width:100%;height:100%" frameBorder="0"></iframe> </bod ...

Troubleshooting issues with CSS dropdown menu in Internet Explorer and Chrome

I am encountering an issue with my dropdown menu not functioning properly in Internet Explorer 10, Chrome, and compatibility mode. Surprisingly, it works flawlessly in the latest version of Firefox. CSS: #menu_items { float: left; width: 600px; } #me ...

Use CSS to insert a solid rectangle into the contents of a table cell

Struggling with the HTML code that defines a table? Here is an example: <table> <th> <td style="width:200px"> col1 </td> </th> <tr> <td id="a1"> text1 </td> </t ...

What are the steps to modify data within the root component?

I am currently working on a Vue project with vue-cli and routes. In my App.vue file, the template structure is as follows: <template> <div id="app"> {{Main}} <router-view></router-view> </div> </template&g ...

Gradual disappearance of preloader as the page loads

I'm facing an issue with setting a gif as a website preloader. Despite trying different JavaScript solutions, the preloader remains visible even after the page has finished loading. $(window).on("load", function() { $(".loading").fadeOut("slow"); ...