Arranging buttons beside an image

I had previously inquired about a similar issue, but I've since made some style changes and now I'm unsure of how to position the close button in the top-right corner of the image, along with the previous and next buttons on either side of the image with specific spacing.

This is just above the footer of my page:

<div class="modal" id="modal">
    <div id="modal-content">
        <span class="gallery-button" id="close">✖</span>
        <span class="gallery-button" id="previous">◄</span>
        <span class="gallery-button" id="next">►</span>
        <img id="modal-image">
    </div>
</div>

How I display the modal:

function showModal(directory, response, i) {
    modal.style.display = "block";
    modalImg.src = document.getElementById(path(directory, response[i])).src;

    /* previousButton.style.display = "block"; */
    /* nextButton.style.display = "block"; */
}

And here is my CSS styling:

/* Background when the image is displayed */
#modal {
    display: none; /* Hidden by default */
    /* Positioning */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    /* Sizing */
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(119, 119, 119); /* Fallback color */
    background-color: rgba(119, 119, 119, 0.7); /* Black with opacity */
}

/* Image wrapper */
#modal-content {
    /* Sizing */
    width: 100%;
    height: 100%;
}

/* Image */
#modal-image {
    /* Sizing */
    max-width: 80%;
    height: calc(100vh * 0.6);
    /* Style */
    border: 10px solid #ffffff;
    box-shadow: rgba(0, 0, 0, 0.54) 0px 0px 7px 4px;
    /* Horizontal center image */
    margin: auto;
    /* Zoom (image enlarges) on open */
    animation-name: zoom;
    animation-duration: 0.6s;
    /* Vertical center image */
    position: absolute;
    top: 50%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

@keyframes zoom {
    from {
        transform: scale(0)
    }
    to {
        transform: scale(1)
    }
}

/* Gallery Buttons */
.gallery-button {
    /* Style */
    color: #ffffff;
    transition: 0.3s;
    background-color: #000000;
    border: 2px solid #ffffff;
    box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 25px 3px;
    /* Make the button round */
    width: 20px;
    height: 20px;
    line-height: 20px;
    border-radius: 50%;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
}

/* Previous Button */
#previous {
    font-size: 12px;
    /* Position */
    position: absolute;
    /* Horizontally */
    left: 30px;
    /* Vertically */
    top: 50%;
    transform: translateY(-50%);
}

/* Next Button */
#next {
    font-size: 12px;
    /* Position */
    position: absolute;
    /* Horizontally */
    right: 30px;
    /* Vertically */
    top: 50%;
    transform: translateY(-50%);
}

/* Close Button */
#close {
    font-size: 15px;
    /* Position */
    position: absolute;
    top: 25px;
    right: 30px;
}

/* Change cursor when hovering over button */
.gallery-button:hover,
.gallery-button:focus {
    text-decoration: none;
    cursor: pointer;
}

/* Display image at full width on smaller screens */
@media only screen and (max-width: 700px) {
    .modal-content {
        width: 100%;
    }
}

Currently, the close button is positioned in the top right corner of the entire page, and the previous and next buttons are already vertically centered (which is good), but not yet aligned near the image. How can I attach these buttons to the image so that they move with it? Adjusting the image size was challenging, and I am uncertain of an alternative approach to accommodate both the sized image and buttons within a div.

Answer №1

I have eliminated the CSS you provided as it appears to be disorganized. However, by reviewing the code snippet below, you can gain insight on how positioning is set. I trust you will be able to complete the remaining tasks.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #modal-content{
      position: relative;
      width:50%;
      height:50%;
    }
    #modal-image{
      width:100%;
      height:auto;
    }
    #close{
      position: absolute;
      right: 0px;
      top: 0px;
    }
    #previous{
      position: absolute;
      top: 50%;
      left: 1%;
      
    }
    #next{
      position: absolute;
      top: 50%;
      right: 1%;
      
    }
  </style>
</head>
<body>
  <div class="modal" id="modal">
    <div id="modal-content">
        
        
        <img id="modal-image" src="https://dummyimage.com/vga" alt="">
        <span class="gallery-button" id="close">✖</span>
        <span class="gallery-button" id="previous">◄</span>
        <span class="gallery-button" id="next">►</span>
        
    </div>
</div>
<script>
  function showModal(directory, response, i) {
    modal.style.display = "block";
    modalImg.src = document.getElementById(path(directory, response[i])).src;

    /* previousButton.style.display = "block"; */
    /* nextButton.style.display = "block"; */
}
</script>
</body>
</html>

Update code (as a requirement)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #modal{
          width:100vw;
          height: 100vh;
      }
    #modal-content{
      position: relative;
      width:35%;
      height:90%;
    }
    #modal-image{
      width:100%;
      height:100%;
    }
    #close{
      position: absolute;
      right: 0px;
      top: 0px;
    }
    #previous{
      position: absolute;
      top: 50%;
      left: 1%;
      
    }
    #next{
      position: absolute;
      top: 50%;
      right: 1%;
      
    }
  </style>
</head>
<body>
  <div class="modal" id="modal">
    <div id="modal-content">
        
        
        <img id="modal-image" src="https://dummyimage.com/vga" alt="">
        <span class="gallery-button" id="close">✖</span>
        <span class="gallery-button" id="previous">◄</span>
        <span class="gallery-button" id="next">►</span>
        
    </div>
</div>
<script>
  function showModal(directory, response, i) {
    modal.style.display = "block";
    modalImg.src = document.getElementById(path(directory, response[i])).src;

    /* previousButton.style.display = "block"; */
    /* nextButton.style.display = "block"; */
}
</script>
</body>
</html>

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

Manipulating object information within a nested for loop

I have a variable called jobs in my scope, which is an array containing objects for each department along with their respective data. [ “Accounting”: { “foo” : “foo”, “bar” : "bar" }, “Delivery”: { ...

The error message indicates that the property 'current' is not found in the type '[boolean, Dispatch<SetStateAction<boolean>>]'

During my React/Typescript project, I encountered an issue involving cursor animations. While researching the topic, I stumbled upon a CodePen (Animated Cursor React Component) that functioned perfectly. However, when attempting to convert it into a Types ...

Calculating the computed width based on flex-basis at 0% and the flex-grow factor: what's the deal

Below is an example of HTML with default flex properties being used: <div class="container"> <div class="box"> BOX 1</div> <div class="box"> BOX 2</div> <div class="box box2"> BOX 3 .</div> </div> The ...

CSS printing displays a blank bar in white color

Attempting to generate a ticket with the dimensions of 203mm x 82mm using HTML and CSS through Python (including cgi, jinja2, weasyprint). The template will be converted into a PDF for users to download via a button on the website. The HTML Body: <body ...

The embedded iframe on YouTube is displaying the incorrect video

I am currently designing a website and I want to feature a video on the homepage. The video is hosted on YouTube and I need to embed it into my website. <html> <iframe width="560" height="315" src="https://www.youtube.com/embed/spPdelVEs_k?rel= ...

Ways to retrieve PHP variables using JavaScript from an external file

Within the <div id="chtmsg"> of a messenger page, I had this code... PHP : if($questions){ for($c=0;$c<count($questions);$c++){ $questions[$c]->type == 'F' ? $class = 'message_F' : $class = 'message_P'; ...

Adjust the cursor style using Javascript

I am currently working on a paint software program and I have a feature where pressing the ctrl key while moving the mouse turns on the eraser. However, when the key is pressed, I want the cursor (currently a crosshair) to change to none. I have tried impl ...

The viewport width in NextJS does not extend across the entire screen on mobile devices

I'm currently tackling a challenge with my NextJS Website project. It's the first time this issue has arisen for me. Typically, I set the body width to 100% or 100vw and everything works smoothly. However, upon switching to a mobile device, I not ...

TypeORM ensures that sensitive information, such as passwords, is never returned from the database when retrieving a user

I developed a REST API using NestJs and TypeORM, focusing on my user entity: @Entity('User') export class User extends BaseEntity { @PrimaryGeneratedColumn() public id: number; @Column({ unique: true }) public username: string; publi ...

Adjust the background color dynamically as you scroll, given that I am using a linear-gradient

Is it possible to change the color background using JavaScript while scrolling, especially if I have a linear gradient like this: background: linear-gradient(115deg, #42c344 0%,#42c344 40%,#c4efc4 40%,#ffffff 40%,#ffffff 100%); background-attachment: fi ...

Fetch information post search action through Ajax on a single PHP page

Looking for some guidance on using AJAX and PHP together. I've set up a page that uses AJAX to load data, and it's displayed with pagination. However, I'm struggling to redirect the search results back to birds.php. birds.php <script ty ...

What is the correct way to send a GET request in angular?

Trying to make a GET request from Angular to Spring Java, but encountering error code 415 zone.js:3243 GET http://localhost:8080/user/friend/1 415 Below is my Spring Java code for the endpoint: @RequestMapping( value = "/friend/{idUser}", ...

Utilize Tailwind CSS to design a triangular shape positioned in the lower right corner of the parent div

Is there a way to position a triangle in the bottom right corner of a parent div without it extending beyond the boundaries? I attempted to use tailwind css for this, but the sides of the triangle are crossing over the parent div. Any suggestions on how to ...

Add United States as an additional attribute to the countries retrieved from the API

I am working with an API that provides data in a specific format: [ { "id": 12, "acf": { "address": { "city": "Bandar Penawar", "state": "Johor", "country ...

One way to achieve the same functionality as onclick in an Ajax call using

I have two JSPs. In the first JSP, I am making an ajax call and receiving data (Ajax body) from the second JSP. However, I am unsure of how to execute jQuery when an argument is present in the function. Everything works fine without an argument. In the ...

Increasing Gap between Tabs in Primefaces AccordionPanel: A Guide

Is there a way to increase the spacing between the tabs of a Primefaces AccordionPanel component? ...

Troubleshooting React-Redux: Button click not triggering action dispatch

I am facing an issue where my action is not being dispatched on button click. I have checked all the relevant files including action, reducer, root reducer, configStore, Index, and Component to find the problem. If anyone can help me troubleshoot why my a ...

Scrolling to an id element in Vue.js can be achieved by passing the ID in the URL using the "?" parameter. This

My challenge involves a URL http://localhost:8080/?london that needs to load directly to the element with an id of london in the HTML section <section id="london"> on the page. Using http://localhost:8080/#london is not an option, even though it woul ...

When dynamically adding input data, the serialized array may not successfully pass through Ajax

function SubmitData(){ var postData = $("#xForm").serializeArray(); $.ajax({ type: "POST", url: "mail.php", data: postData, success:function(data){ console.log(data); }, error: function(jqXHR, textStatus, errorThrown ...

Callback function not being triggered in Jquery's getJson method

I am currently faced with a javascript conundrum. Below is the snippet of code that I have been working on: $.get("categories/json_get_cities/" + stateId, function(result) { //code here }, 'json' ); ...