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

Dynamic background image changes when checkbox is selected

Greetings! I am a newcomer to the stackoverflow community and I am facing an issue with dynamically changing the background image of my webpage when a checkbox is checked. Here is the HTML code snippet: <ul> <li><label for="Lines">A ...

Deactivating Node.js files in vsCode for client-side JavaScript files

I'm facing a major challenge when it comes to coding with JavaScript. I have a JavaScript file that is using Node.js, which means I am unable to manipulate the DOM elements. Take this code snippet for example: var form = document.getElementsByClassNa ...

Customize the List Box Appearance for a Specific HTML Item (Option)

I am working on achieving a specific behavior. When using a listBox (or comboBox), the first element (such as "choose one") should have a unique style. Here is an example of code to test: <style type="text/css"> .testX {font-style: italic;} </ ...

Switching from a click event to a hover event in JavaScript

I've been experimenting with animating a burger bar on hover and came across an example online that I managed to adapt for mouseenter functionality. However, I'm struggling to make it revert back to the burger bar shape once the mouse leaves on m ...

Incorporate a vertical scrollbar in the tbody while keeping the thead fixed for smooth vertical scrolling

I'm seeking a solution to implement horizontal and vertical scroll for my table. Currently, the horizontal scroll is working fine, but when trying to add vertical scroll, the table header also moves along with it. Is there a way to keep the table hea ...

Design a background image that is optimized for both high-resolution retina displays and standard non-ret

Scenario I am working on a web page where I want the background image to be fixed, covering the entire screen or window (excluding tablets and smartphones). The background image has been created using ImageShack. Everything is running smoothly so far. T ...

Turn off hover opacity effect for a specific image

Currently, I've implemented a hover opacity effect on my images using the following CSS: img { opacity: 1.0; filter: alpha(opacity=1.0); } img:hover { opacity: 0.6; filter: alpha(opacity=0.6); } However, I now have a specific image for whi ...

Tips for positioning a div between two vertically aligned input elements

I'm trying to figure out how to place a div between two inputs with the same height and vertically aligned. It seems like a simple task, but for some reason, the code below isn't working: input{ width: 35%; border: 1px solid #A7A7A7; ...

Is there a more efficient method for creating HTML with coffeescript / jQuery besides using strings?

Today marks my first attempt at creating a "answer your own question" post, so I hope I am on the right track. The burning question in my mind was, "Is there a more efficient way to generate HTML with jQuery rather than using tag strings?" My goal is to c ...

How can I align rectangles with different heights to display side by side using Javascript?

Currently, I am designing a press page for a website where the headlines/articles are displayed in rectangles. To achieve this layout, I am using the following CSS: .press-blocks{ column-count: 4; column-gap: 2em; padding-left: 10%; padding ...

Customizing text appearance with innerHTML in JavaScript: A guide to styling

Below is the code I have for a header: <div id="title-text"> The Cuttlefisher Paradise </div> <div id="choices"> <ul> <li id="home"><a href="#">Home</a></li> <li id="contact">&l ...

Conceal and unveil stationary navigation when scrolling back to the very top of the screen

My current setup includes a navigation that dynamically hides as the user scrolls down and reappears when scrolling up. Additionally, I have applied the class .fixed on #top-wrapper to alter the layout/styling of this specific div. However, I am facing dif ...

Printing a specific column of a particular row in an HTML table to the printer: Tips and tricks

How can I print a specific column from a particular row in an HTML table? I've attempted various CSS and jQuery methods I found online, but none of them seem to be effective. ...

Tips for preventing overlap between two divs when utilizing position: absolute in CSS

In this CodePen link [https://codepen.io/anon/pen/qvQpaY], there is an issue with overlay between the two divs that have a class name of box-container. The black rectangle is being counted as the overall height by box-container because the red rectangle is ...

Align the text to the center beneath the image using Jquery's append function

I am looking to showcase a collection of fruits and vegetables through images, with the names displayed centered underneath each image. Additionally, I want to implement jQuery append functionality so that the names only appear when a specific button is cl ...

Instructions on how to make the image within this div expand to full screen in order to cover up the blue background color

I created a div and set its background image, but the image is not covering the full screen. There's some space around it. The blue color is the body color. Please refer to the image here I am very new to web development, so please forgive my silly ...

"Resolving the duplication issue of a textbox in Phonegap app

I encountered a strange issue recently. While trying to focus on the textbox in my PhoneGap application using touch, I noticed that there are actually 2 textboxes appearing. Please refer to the attached image: ...

What is the best way to retrieve the href and id values dynamically from a card in React JS?

Hello everyone, I'm new to stackoverflow and still in the process of learning how to code. Please bear with me if this question sounds like a newbie one. My question is about having dynamic values for href and id when mapping data using axios to crea ...

To emphasize code in an HTML document, you can use the <font color="#xxx"> tag

Can anyone guide me on how to add color highlights to my HTML code in this way? <font color="#1773cc">#include </font><font color="#4a6f8b">&lt;NTL/ZZ.h&gt;</font><br> <br> <font color=&quo ...

Enhance Your Website with HTML5 Video Transition Effects

Is it possible to recreate video transition effects using HTML5 similar to the ones shown in this example: Can this be achieved across all major browsers, including Safari, Firefox, Chrome, and IE9? ...