Place any image in the center without moving

After searching for a satisfactory solution to my query in vain, I am resorting to posting it here.

I want the ability to click on any image on my website and have it displayed at full size within a static viewport with an opaque background.

This is what my current code looks like:

= link_to hide_image_path, :remote => :true, :title => "Close" do
  .full_screen_image
  .opaque
    %img.image-middle{:src => UrlSafeBase64.decode64(image), :alt => ""}

Accompanied by the following CSS:

/* This segment creates a static semi-transparent grey layer, functioning as intended */

.full_screen_image {
  position: fixed;
  background-color: #6c6c6c;
  opacity: 0.8;
  z-index: 2000;
  top: 5%;
  left: 5%;
  bottom: 5%;
  right: 5%;
}

/* The intention here is to add a 100% opaque layer on top of the previous one

.opaque {
  position: relative;
  z-index: 2005;
  opacity: 1;

}

/* Finally, we define the styling for the image itself

.image-middle {
  position: static;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

However, the current setup presents some issues: - The image is correctly displayed as 100% opaque, but not centered on the screen as intended by the .image-middle rule - The first layer remains static while scrolling (as expected), but the image scrolls along (not the desired behavior)

My objective is to achieve the desired outcomes using only CSS/HTML, without involving Jquery or JavaScript.

Answer №1

If you want to implement this feature, just use jQuery.

<script>
            $(document).ready(function() {
                $('.popup-with-zoom-anim').magnificPopup({
                    type: 'inline',
                    fixedContentPos: false,
                    fixedBgPos: true,
                    overflowY: 'auto',
                    closeBtnInside: true,
                    preloader: false,
                    midClick: true,
                    removalDelay: 300,
                    mainClass: 'my-mfp-zoom-in'
            });
        });
        </script>

To display a picture and have it expand when clicked, include the following HTML code:

<div class="mfp-hide">
            <div id="small-dialog1" class="pop_up">
                <h2>Your Work</h2>
                <img src="yourpic.JPG" alt="Images "/>


        <div class="gallerylist-wrapper">               
                <a class="popup-with-zoom-anim" href="#small-dialog1">
                    <img src="yourpic.JPG" height="200px" width="200px"  alt="Image 1"/>
                    <span><img src="yourpic.JPG" alt="Image "/> </span>
                    <div class="desc">

                    </div>
                    </a>
                    </div>

Answer №2

My solution is simply KISS!

Here is the correct code snippet:

= link_to hide_image_path, :remote => :true, :title => "Close" do
  .full_screen_image
  %img.image-middle{:src => UrlSafeBase64.decode64(image), :alt => ""}

Accompanied by these dynamic CSS rules:

.full_screen_image {
  position: fixed;
  background-color: #6c6c6c;
  opacity: 0.8;
  z-index: 2000;
  top: 5%;
  left: 5%;
  bottom: 5%;
  right: 5%;
}

.image-middle {
  position: fixed;
  opacity: 1;
  z-index: 2005;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%); 
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

Many thanks for the assistance provided, and I trust this answer will benefit others facing a similar issue or question.

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

Rotating the Orientation of Cellphone Images

Currently facing an issue where the images I am attempting to display on a basic html page appear rotated to the left, likely due to being taken by a cellphone. These images are stored on my computer. I have tried using the css property image-orientation: ...

Bring your Electronic Catalogue to life with the addition of dynamic HTML content showcasing the latest

I am creating a digital magazine but I am facing the challenge of having to deal with 200 pages in jpg format. To streamline the process, I am looking for a way to use a combination of JavaScript and PHP to avoid manually coding for each of the 200 pages. ...

Applying a row class to a Bootstrap panel-heading causes the border to adhere to the padding width of the

I need to create a collapsible panel within another collapsible panel, with the nested panel requiring 3 columns in the heading. To achieve this, I am using a row element. However, when I add the row and container-fluid classes, the bottom border of the pa ...

Comparing the positions of elements in Selenium WebDriver with PHP

One of the elements on my webpage serves as a button that reveals a drop-down menu. Due to various factors affecting the page layout, there were some alignment issues between the button and the menu until a few bugs were fixed. I am now looking for a way t ...

ContentEditable in HTML5 allows users to directly edit the

I'm in need of some guidance when it comes to HTML5 ContentEditable and PHP. It would be greatly appreciated if you could provide me with examples to help me better understand what I am trying to achieve. On my website, I want the ability to edit ...

Prevent selection on all elements except for input fields with the type of text

I am facing a challenge where I need to prevent selection on a website page for everything except input[type=text] elements. The answer provided in this accepted response to a similar query almost solves the issue. However, it doesn't stop users from ...

What's the method for positioning the footer at the bottom of the grid without impacting the layout of the other rows?

I am trying to achieve the placement of the footer at the bottom of the layout page when the height is shorter than the window. While it is often suggested to use min-height:100vh, this approach ends up increasing the height of other elements as well. I am ...

Extracting information from a division using Selenium

I am attempting to choose an element for clicking on a page (). The element in question is a date selection within a calendar pop-up. Specifically, the element is: <div class="day unit in-range" data-time="1698822000000">1</d ...

Determine the dimensions of child components within Svelte templates

I am currently in the process of transitioning a VanillaJS website to Svelte, and I need to have divs randomly positioned on the webpage. It's important to note that each div's size can vary depending on its content, so they cannot have a fixed s ...

Is there a way to turn off alerts from Aspx files for HTML and CSS?

Dealing with annoying warnings in my aspx files has been a constant struggle. The "CSS Value is not defined" message pops up when I reference CSS files from different projects, causing unnecessary frustration. Even more frustrating are the warnings about i ...

How can I align a group of four checkboxes in HTML?

Code for HTML <div id=checkbox> <div id=groupfour> <div id=groupone> <input type="checkbox" checked="checked" name="Mapmashup" value="Jan" id="EJan"> <label>Jan </label> < ...

Add navigation bar to every page - using HTML and CSS

Is there a way to smoothly insert HTML codes into all website pages without compromising the CSS design? I've attempted using but found it messy. Is there a simpler method to insert the HTML file into other files? The HTML file I'm trying to lo ...

Wrapping words in php code

Despite using word-wrap: break-word in the content-full class, the text is not appearing correctly. What could be causing this issue? Is there a solution to ensure long words break and wrap onto the next line? Interestingly, word-wrap: break-word works for ...

How can you define a div that has a width of 100% along with specific padding?

I currently have a div with the following CSS styling: .mydiv{ position:absolute; top:0; left:0; width:100%; padding:7px; }​ When viewed in the browser, there is horizontal scrolling due to the padding (width 100% + 7px) See it ...

Display a webpage with updated information transmitted from Angular to Flask

I am currently working on updating my data on a page when the date changes. I am using an AngularJS date picker and Flask on the backend to achieve this functionality. However, after passing the changed date from Angular to Flask, I am able to generate the ...

The gaps separating rows

I'm struggling with getting my divs to look like a table, especially when it comes to highlighting a selected row. I've tried adjusting padding and margins without success. Does anyone have any suggestions on how I can achieve this effect? .ta ...

What might be causing the invisibility of a particular div element?

Here is the layout in the div element: <div class=" wrap clear"> <div class="block pink float"></div> <div class="block blue float"></div> <div class="block orange float"& ...

Having difficulty in successfully transmitting a dictionary via AJAX (POST) to Python (Django views) resulting in an empty dictionary every time

When I include this script in the head of the HTML : <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> and then use a button to call a function: <div> ...

What is the best method for transferring a Validation Error to an HTML document?

I need help with displaying an error message in my HTML code. Despite following the suggestions from other posts, I still see the error on the traceback screen instead of the HTML page. Can anyone assist me with this? This is the function in my view: def ...

How can Media Queries help make a search box adapt to different screen sizes?

Currently, this is my code snippet. My goal is to make it drop below my logo on the left side as the screen size decreases. Thank you in advance for any suggestions! @media screen and (max-width:959px){ #wrapper{ width: 100%; ...