Using the ID selector to create a media query

My website is live, and I'm working on making it mobile-friendly. Most of the site works well on mobile, but I'm having trouble centering a jquery lightbox function using media queries. I'm not sure if my syntax is wrong or if there's a fundamental issue with how the media query interacts with the jquery code.

Here's the CSS I have:

#lightbox{position: fixed;
      top: 0;
      left: 0;
      width:100%;
      overflow-x: scroll;
      height: 100%;
      background: rgba(0, 0, 0, .95);}

#lightbox img {box-shadow:0 0 25px #111;
           -webkit-box-shadow:0 0 25px #111;
           -moz-box-shadow:0 0 25px #111;
           height: auto;
           width: 750px;
           margin-top: 2%;
           margin-left: 21%;
           margin-bottom: 2%;}

    @media only screen and (max-width: 500px){
        #lightbox img {margin-top: 10%;
                       margin-left: 15%;}
    }
}

And here's the Jquery code:

jQuery(document).ready(function($){
            $('.lightbox_trigger').click(function(e){
                var image_src = $(this).attr("src");
                if($('#lightbox').length > 0) {
                    $('#content').html('<img src="' + image_src + '" />');
                    $('#lightbox').show();
                }
                else{
                    var lightbox =
                    '<div id="lightbox">' + 
                        '<div id="content">' + 
                            '<img src="' + image_src + '" />'
                        '</div>' +  
                    '</div>';
                    $('body').append(lightbox);
                }
            });
            $('#lightbox').live('click', function(){
                $('#lightbox').hide();
            });
        });

I've tried different variations like moving the media query outside the closing bracket of the #lightbox img tag, as well as removing #lightbox from the media query completely, but nothing seems to affect the mobile display. I'm new to web design and struggling to articulate my question due to my limited understanding. Any help would be appreciated!

Answer №1

Here's a code snippet that should do the trick;

@media (max-width: 700px){
  #lightbox img {
    margin-top: 15%;
    margin-left: 20%;
  }
}

Update: http://codepen.io/anon/pen/DKJfWE

Answer №2

    @media (max-width: 500px){
          #lightbox img {
          position:absolute;
          top: 10%;
          left: 15%;
      }
}

Your JavaScript code is heavily reliant on HTML tags, consider using templating engines instead. Check out Mustache JS.

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

Enhance your WordPress site by implementing infinite scroll with AJAX to seamlessly load more posts

Currently, I have implemented a feature that loads more posts through AJAX when the user clicks on a 'load more' button. The code I utilized is based on a tutorial found at this link. My goal now is to enhance this functionality so that instead ...

Rendering JSON data in a dropdown menu

When I select an option, all the data is being combined into one row. I want the data to fill each row based on the select option. Any suggestions on what code I should modify or add? Is it related to the loop or should I create a dynamic id for the selec ...

I am having trouble getting my textarea to accept width attributes

Strangely, the text area on my website just won't cooperate with the width I specify. I've double-checked the CSS multiple times to no avail! Take a look at this jsfiddle example that demonstrates what I'm trying to achieve. However, on my ...

A table featuring an HTML select element that allows users to choose from preset options or manually enter

My goal is to incorporate a select box where users can either choose from a set list of options or input their own custom value. The select element is nested within a table. Unfortunately, using datalist is not a viable solution for me in this case. I have ...

What could be the reason behind receiving a 406 Not Acceptable status at the client side from the server, and why is my Spring controller not being triggered?

Here is the code for an AJAX GET request: $("#tabsss2").click(function tab1() { $.ajax({ type: "get", traditional: true, dataType: 'json', url: "DataGridServlet.htm", cache: false, ...

Performing an AJAX call with JQuery when clicking on multiple anchor elements

I have a set of anchor tags created dynamically through PHP from a database query, like the following: <a href="#" id="reply_doc" data-doc_value="1"></a> <a href="#" id="reply_doc" data-doc_value="2"></a> <a href="#" id="reply_d ...

Swapping out a button

I tried searching for a solution to my problem but I couldn't find it because of my limited English proficiency. Therefore, I apologize if my question has already been answered. I would like to place my button in the location shown in the image. If p ...

"Troubleshooting problems with the display:none property in a media

Encountering a small dilemma with media queries within my HTML. One of the sections in my code contains the following: <div class="header-left"> </div> <div class="header-center"> <ul> <li class="end"> ...

The website lacks accessibility features such as a text size swapper, but the contrast swapper is not functioning properly

As I embark on my first website project that requires accessibility controls, I find myself in need of the ability to adjust text size and contrast settings. Specifically, I want to offer options for small, default, and large text sizes as well as normal, ...

JavaScript failing to accurately measure the length

Currently experiencing a Javascript issue where the length of an element is not displayed correctly when using .length, even though it shows up in Chrome console. Here is what it looks like in Chrome console <html xmlns="http://www.w3.o ...

Is it possible to utilize the useRef Hook for the purpose of storing and accessing previous state values?

If you have already implemented the useState and useEffect Hooks for maintaining previous state, another approach is to utilize the useRef Hook to track previous state values as well. ...

Enhancing the Syntax of If and If Else in Jquery Functions

I'm struggling to simplify and optimize this block of code. Would appreciate any help in making it more efficient! var status = "closed"; $(document).on('click', '[data-toggle-nav]', function(event) { if ($(this) && status = ...

Implementing Jquery Ajax in the CodeIgniter framework

Recently, I have been encountering some issues while attempting to utilize jQuery ajax in Codeigniter. Despite having successfully implemented jQuery ajax in previous projects, my current code seems to be causing complications. Below is the snippet that I ...

Emphasize the center row within a moving table

I am interested in developing a scrolling table where only 10 rows are visible at any given time, with the middle row set to stand out even during scrolling. The concept is that as the user scrolls down, the highlighted row changes progressively as they c ...

jQuery opacity transition not functioning properly while all other animations are working fine

I've encountered quite a peculiar issue: Using jQuery v11 on the latest version of Chrome localhost, I can successfully apply jQuery.animate() to various elements and features on my website, including opacity. However, there is one particular element ...

The navigation bar seems to be missing from the page

I am currently working on developing a responsive navigation bar with CSS animation. However, I am encountering some issues when trying to run the code on my laptop. Upon running it, only the checkbox appears while the list of navigation items is displayed ...

Visualizing live data streaming from MySQL

I am currently using this code to retrieve values from a database and create a basic line graph (showing response time to an alert vs. UTC time). Everything works smoothly when the data is static, but now I want to try retrieving the data in "real-time" (j ...

Adding scripts to a PartialView in MVC using Jquery's append function

My issue is with a JavaScript function that appends a PartialView into a div. Everything seems to work correctly, except for the fact that the PartialView contains some scripts enclosed within <script></script> tags. As a result, when these dyn ...

Steps to include a horizontal divider in the footer section of an angular-material table

Is it possible to add a line between the last row (Swimsuit) and the footer line (Total)? If so, how can I achieve this using Angular 15? https://i.stack.imgur.com/581Nf.png ...

The display function in Javascript has a tendency to work sporadically

I’ve been tasked with creating my own tic tac toe game through coding. While I am relatively new to programming, I have a strong passion for it. At the moment, I've set up a basic function to hide only the "O", leaving only the "X" visible on the gr ...