What is the best way to change the background color for my photo gallery?

I'm currently working on a project to create a unique photo gallery where each image has a different colored background. I have six images in total, but right now all of them have a pink background. I've attempted adding another .popup class in the CSS with a new background color, but it didn't work as expected. Additionally, I tried using JavaScript and getElementsByClassName to change the background color when the user clicks on the left or right arrow. While this did change the color from pink to another color, it didn't allow me to set a different color for each individual background. Can anyone provide some assistance? Thanks in advance.

Answer №1

To apply custom styling to specific elements, you can utilize the :nth-child(n) selector in your CSS stylesheet. For a more detailed explanation, check out this link.

.gallery-image:nth-child(1) {
  background: red;
} 

.gallery-image:nth-child(2) {
  background: green;
}

.gallery-image:nth-child(3) {
  background: blue;
} 

If this solution doesn't meet your requirements, please don't hesitate to let me know. I'm here to assist you further.

Answer №2

Clicking on a photo and opening it in a popup modal will result in a different background color each time. The left or right arrow can also be used to change the background color again.

If you add this line to your JS code:

randomBackgroundColor(); //<-- NEW LINE

The randomBackgroundColor() function will assign a new color to the background of the popup element.

I trust this information has been useful.

For example:

// JavaScript code snippet

const images = [...document.querySelectorAll('.image')];

// Other JavaScript code goes here...
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Additional CSS styles go here... */
<div class="popup">

    <div class="top-bar">
        <p class="image-name">img1.png</p>
        <span class="close-btn">
            <img src="Graphics/gallery_icon.svg" width="50px" class="close">
        </span>
    </div>

    <button class="arrow-btn left-arrow"><img src="img/arrow.png" alt=""></button>
    <button class="arrow-btn right-arrow"><img src="img/arrow.png" alt=""></button>

    <img src="img/img1.png" class="large-image" alt="">

    <h1 class="index">01</h1>
</div>

<div class="gallery">
    <div class="gallery-image">
        <img src="img/img1.png" alt="" class="image">
    </div>
    <div class="gallery-image">
        <img src="img/img2.png" alt="" class="image">
        <div class="bk"></div>
    </div>
    <div class="gallery-image">
        <img src="img/img3.png" alt="" class="image">
    </div>
    <div class="gallery-image">
        <img src="img/img4.png" alt="" class="image">
    </div>
    <div class="gallery-image">
        <img src="img/img5.png" alt="" class="image">
    </div>
    <div class="gallery-image">
        <img src="img/img6.png" alt="" class="image">
    </div>
</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

Utilize a variable within an HTML attribute

Currently utilizing Angular and I have the following HTML snippet <input onKeyDown="if(this.value.length==12 && event.keyCode!=8) return false;"/> Is there a way for me to incorporate the variable "myNum" instead of the ...

I am attempting to retrieve the initial three results from my MySQL database using Node.js, but I keep encountering an error

Below is the code I am currently using: con.query('SELECT * FROM tables', function(err, results) { if (err) throw err console.log(results[0].rawname) for(var i= 0; i <= 3; i++) { ...

Connect two radio buttons across separate forms

I am encountering an issue with two radio buttons that I am trying to link together. The problem arises because they are located in two separate form elements, causing them not to function as intended. It is important for the forms to be utilized in Bootst ...

Transitioning with an ellipsis

Currently, I am working on a project where I am trying to utilize the CSS property text-overflow: ellipsis to achieve a specific animation. However, despite applying this property along with white-space: nowrap and overflow: hidden to .card-dish__info, I a ...

Using iScroll without animation by using scrolltoelement on an iPhone PhoneGap application

I've been working on an iOS PhoneGap app that uses Backbone and some other libraries. To handle scrolling, I implemented iScroll which has been functioning properly. However, I encountered a problem with the following code snippet: events: { &apo ...

Having trouble with SVG background image not displaying properly and positioning correctly?

I need help fitting an svg as a background into an element that is 80% of the screen width. Here is the desired final result: The svg effect only works correctly when I reduce the width, but then it breaks in two. I want the svg to appear at 80% width a ...

ensured maximum height of a div attached to the bottom

<body style="width: 100%; text-align: center;"> <div style="margin: 0 auto;">container <div style="width: 200px; margin-top: 100px; float: left;"> left </div> <div style="width: 728px; floa ...

Customizing Tabs in Material UI v5 - Give your Tabs a unique look

I am attempting to customize the MuiTabs style by targeting the flexContainer element (.MuiTabs-flexContainer). Could someone please clarify the significance of these ".css-heg063" prefixes in front of the selector? I never noticed them before upgrading my ...

Exploring and accessing elements and nested objects within a dynamically named element in JavaScript using JSON syntax

Receiving a JSON response from an API, here's what the data looks like: [{ "order_id":"1111", "restaurant_id":"ABC", "fullfillment":"Delivery", "order_channel":" ...

Disabling prefetch in next.config.js: A comprehensive guide on eliminating data prefetching in Next.js

Typically, in many cases, the disabling of prefetching is achieved by specifically setting a link to not preload. An example of this can be seen below: <Link href="/about" prefetch={false}> <a>About us</a> </Link> My go ...

Efficiently Measuring the Visibility Area of DetailsList in Fluent UI

I am currently utilizing the DetalisList component alongside the ScrollablePane to ensure that the header remains fixed while allowing the rows to scroll. However, I have encountered a challenge as I need to manually specify the height of the scrollable co ...

PHP move_uploaded_file() function encountering an issue

As a newcomer to PHP, I am exploring the move_uploaded_file() function. I initiate an HTTP POST request from JavaScript/jQuery to a PHP file hosted on a Microsoft Azure server. The request includes an audio blob like so... mediaRecorder.ondataavailab ...

The hover state of a div will not be lost if its parent element is not being hovered over

When hovering over the second, third, or fourth item, hidden text will appear on the left side. If you hover your cursor over the hidden text, it will disappear again. I want to be able to hover over the second item, move my cursor to "hide", and click o ...

Using JavaScript, display JSON data retrieved from a PHP file

Currently, I am in the process of developing a web application that displays tweets based on a city inputted by the user through an HTML form. The city value is stored in the $_SESSION['city'] variable after the form is submitted. Subsequently, ...

Is there a way to prevent my cell from resizing during the initiation of a jQuery animate effect?

After coding a 2x2 DIV table, I encountered an issue where the left side of the cell appears when hovering over the right cell. However, the left cell is hidden when the mouse leaves the right cell, utilizing a jQuery animate effect. I'm facing a pro ...

What should be transmitted to the front-end following the successful validation of a token on the server?

My process starts with a login page and then moves to the profile page. When it comes to handling the token on the backend, I use the following code: app.use(verifyToken); function verifyToken(req, res, next) { if (req.path === '/auth/google&ap ...

In NextJS, where is the best place to run sensitive code to ensure it is only executed server-side?

I'm currently exploring the world of NextJS and I am in the process of figuring out how to structure my project with a solid architecture that prioritizes security. However, I find myself at a crossroads when it comes to determining the best place to ...

Generate tables and rows dynamically

I am looking for guidance on dynamically creating a table and adding rows to it. I have successfully created a table with one row containing form fields, but I am unsure how to add additional rows. Any examples or suggestions on how this can be implemented ...

Turning off devtools in Next.js

Currently, my focus is on developing an application using Next.js and I am in search of a way to prevent the opening of devtools. While exploring npm packages, I came across a promising tool called Disable-devtool - npm link. However, Next.js keeps present ...

How the Marvel of jQuery Ignites the Power of

I require some assistance with the callbacks. It appears that they are not functioning properly. I am in the process of creating a jQuery-based game. I have designated a <div id='button'></div> for all the buttons that will be used ...