How to make an image expand to fit the screen when clicked using HTML and CSS

Is there a way to make the images on this page grow to screen size when clicked, rather than extending past the boundaries of the screen? Currently, the images enlarge to twice their original size on click. How can I ensure that they expand responsively to fit the screen?

Below is the HTML code being used:

<div data-tilt class="thumbnail rounded js-tilt">
  <img
    alt="Project Image"
    class="img-fluid"
    src="assets/skateboard.jpg"
    onnclick="enlargeImg()"
    id="img1"
  />
</div>

And here is the corresponding CSS:

img {
  transition: -webkit-transform 0.25s ease;
  transition: transform 0.25s ease;
}

img:active {
    -webkit-transform: scale(2);
    transform: scale(2);   
}

Answer №1

One potential solution is as follows:

HTML:

<img src="[YourSource]" id="MyPicture" onclick="enlargeImage()"/>

CSS:

.enlarged{
  height: 100%;
}

body, html {
  height: 100%;
  margin: 0;
}

Javascript:

function enlargeImage(){
    document.getElementById("MyPicture").classList.add('enlarged');
}

You can view a live demo of this functionality here: https://jsfiddle.net/8qro5zae/22/

Answer №2

When working on my code, I implemented a functionality using a class called active along with vanilla Javascript to determine if the class is present or not, and then create a toggle effect based on that.

The styling for the active class is designed to display an image in fullscreen mode, centered and proportionally scaled.

function enlargeImg(id) {
  let img = document.getElementById(id);
  if (img.className.match(/\bactive\b/)) {
    img.classList.remove("active");
  } else {
    img.classList.add("active");
  }
}
img {
  position: relative;
}

img.active {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  object-fit: contain;
}
<div data-tilt class="thumbnail rounded js-tilt">
  <img alt="Project Image" class="img-fluid" src="http://placekitten.com/g/200/300" onclick="enlargeImg(this.id)" id="img1" />
</div>

<div data-tilt class="thumbnail rounded js-tilt">
  <img alt="Project Image" class="img-fluid" src="http://placekitten.com/g/300/200" onclick="enlargeImg(this.id)" id="img2" />
</div>

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

Confusing Accessibility Rules: A Guide that Leaves Me Scratching My Head

I've been reviewing a set of rules to make sure we're in compliance with the Americans with Disabilities Act. One guideline has me scratching my head: Use the title attribute when text is not accessible I'm confused - how can text be ina ...

Validation on the client side will now support the input of comma-separated values

Is there a way to use the jQuery.validator.addClassRules method in order to validate input fields and restrict my textbox to only accept comma-separated values? I also want it to display a default message if incorrect. <input type="text" cla ...

In React, I'm unable to navigate to a different page

My English may not be the best, but I am working on creating a Login Page. The issue I'm facing is that when I click the Login button, I want to navigate to the Home Page I designed using React. However, whenever I try to implement Link or Route comma ...

Is it possible to set a unique identifier in Vue.js that persists even after the page is reloaded?

Currently, I'm working on a small project diving into Vue js - a Habittracker. Unfortunately, there is a bug in my code. When the page is reloaded and new habits are added, the function that should change the background doesn't work as expected. ...

Issue with maintaining image aspect ratio; unable to utilize background-image: cover property

If what I am attempting is not feasible, please inform me. I am currently working on displaying images of various sizes without distortion. While most images look good, those with a smaller width than the container of 285px are getting distorted. I am fin ...

AngularJS: Utilizing bold text to enhance the separation of concerns between controllers and templates

In my AngularJS version 1 application with Ecmascript 6, I have a requirement to display a string where one part is in normal weight and the other part is bold. For instance, consider the following scenarios: Will start now Will start in 6 minutes Will ...

What is the best way to obtain a direct file link from a server URL using JavaScript?

After acquiring a file located at /home/johndoe/index.html, I am utilizing a tool like XAMPP to host, with the folder /home being hosted on localhost. The variables in play are as follows: $server_addr = "localhost"; $server_root = "/home"; $file_dir = " ...

Changing the size of various types of images

Is there a way in JavaScript to automatically resize and fill in a block with fixed width using different images with various dimensions? I came across something similar, but it was in AS2. I'm unsure if it can be translated to JavaScript. var _loc3 ...

I cannot understand why I keep receiving <input type="email" value="required"> when all I want is <input type="email" value="" required>

Currently, I am working with PHP code and here is my complete PHP code: echo "<input type=\"email\" class=\"profil_input_text form-control\" name=\"user_email\" pattern=\"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(& ...

changing the value of a text input based on the selected option in a dropdown using ajax

I attempted to update the text input value with data from the database after selecting an option from the dropdown list. However, I encountered an issue where the data is not populating the text input field. <?php //Including the database configuration ...

Guide to creating a contenteditable div that automatically generates smart quotes instead of traditional dumb quotes

I've observed that when I write in Google Docs, I get smart quotes. However, when I create contenteditable divs on my own, I only see dumb quotes. Is there a way to make my contenteditable divs display smart quotes instead of dumb quotes? ...

The issue with Angular.js webpage routing seems to persist in my single-page application (SPA) despite my utilization of ng-view

I'm having trouble inserting a view into my index.html using $routeProvider and ng-view from the AngularJS libraries. Can anyone assist me in figuring out why it's not being inserted? Below is the code for my index.html, views, and app.js files: ...

Changing the style of a single element in various components in Vue

I need to alter the design of a specific div that is used in different components within my Vue.js application. The #app div should have a padding of 172px only in the Hello.vue component, while it should remain at 0px in all other components. Is there a w ...

List containing ionic items that spans the full height

I am trying to create a full-height list in Ionic that will always have 3 items. I want to ensure that it displays as full screen on different screen sizes. I have attempted using height:100%, but have not had success. Here is an example of what I am tryin ...

Modify the appearance of the element that is currently chosen

I have a navigation menu and I would like to change the style of the element that is selected from the list in the navigation menu. I am using React with the Grid element from material-ui. NavigationMenu.tsx: import React, { useState, useEffect } from "r ...

Bootstrap: Easily align elements to the right with the .pull-right class, avoiding the need to manually adjust margins

This is the code snippet I am working with (you can view the fiddle here): <div class='container'> <div class='hero-unit'> <h2>Welcome</h2> <p>Please log in</p> <div i ...

Element with absolute position does not expand along with scrollable content (over 100%)

In my popup window, the fade element is supposed to stretch to the full width of the screen. However, when the content exceeds 100% of the browser window height, the element does not extend to the full page height. If I set html, body { height: 100%; over ...

Tips for centering content in the middle of a line

How can I center align 3 buttons on a line using CSS and HTML? This is what I have tried so far: 1/ CSS : .center-buttons { display: flex; justify-content: center; } .button { margin: 0 10px; } 2/ HTML: <div class="row"> <di ...

Like the Word outline view, the list view can be easily collapsed and expanded with a simple click

I've seen a few examples, but I haven't been able to achieve what I'm looking for. I need to extract text from a field and convert it into an outline view similar to the one in Word. Is this possible? Any help would be greatly appreciated. I ...

Attempting to align two blocks side by side

As I work on developing my website, I encountered an issue with positioning div tags. I set up a side-navigation div and a body div within a site that is 1500px wide and 1000px tall, with the side-navigation at 300px and the body at 1200px in width. To my ...