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

Why isn't the background-image displaying with the use of the url() function?

I've been attempting to set an image as the background using background-img:url(imageLing), but it's not working properly. When I inspect the element, it shows me background-image: url(assets/backgrounds/5.jpg);. What could be causing this issue? ...

Instead of only one menu icon, now there are three menu icons displayed on the screen. (Additionally, two more divs containing

When visiting on a smartphone browser, you may notice that instead of one menu icon, three icons appear. Upon inspecting the elements, you will find that there are three div's that look like this: <div class="responsive-menu-icon">&l ...

Tips for adding spacing when the sidebar collapses and expands in a React application

I am attempting to achieve a layout where the body adjusts its space when the sidebar collapses and expands. Here is how it appears when the sidebar expands: see expanded sidebar here And this is how it looks when the sidebar collapses: see collapsed s ...

Guide to including objects into your project without the need for babel through the use of CDN

I'm struggling with getting my Vue code to transpile properly due to some issues. I have resorted to loading Vue and other packages directly using CDN links, like this: <script src="https://cdnjs.cloudflare.com/ajax/libs/survey-vue/1.8.33/surv ...

Retrieving intricate JSON data from a specific web address

I need assistance in extracting and printing the date value from the JSON content available at the specified URL. Specifically, I am looking to retrieve the date value of "data.content.containers.container.locationDateTime" only if the "data.content.conta ...

PHP mail function not working properly when ajax is used

On my MAC, I am attempting to utilize php mail with ajax implementation. I have simplified my code to just strings: In my .js file : function sendMail() { $('#sending').show(); $.ajax({ type:'POST', ...

In PHP, what is the best way to close the HTML tags found in this array?

I am struggling with an array that contains HTML tags and I need to create a formatted output using a PHP function. However, I am having trouble closing the tags correctly and would appreciate some assistance. The existing array is structured line by line ...

Navigate the user to various pages in a Node.js application based on login status

Looking for guidance on a login system within my application? I need to redirect users based on their roles. Below is the code for the login controller, along with node.js snippets and the login HTML page. Any help would be appreciated. Login.html - < ...

Creating Layouts with Bootstrap 3

I'm exploring the codepen below in an attempt to mimic the appearance of the image provided consistently across all screen sizes. However, there are two issues that I am encountering - firstly, the green numbers on the first line which represent the d ...

Run various CSS animations repeatedly

I recently created a captivating text fade-in, fade-out animation consisting of four lines. Each line elegantly appears one after the other, creating a mesmerizing effect. However, there's now a request to have these animations run in an infinite loop ...

A mysterious property appearing in a Webpack plugin

Here is my webpack configuration file and package.json. When I execute the command webpack -w, I encounter the following error (shown below). I suspect it may be related to path strings. Any help would be greatly appreciated. webpack.config.js const HtmlW ...

Combining several JSON objects into a single JSON object using JavaScript

I am in need of merging JSON objects from various sources into a single JSON object using JavaScript. For example, I have two JSON objects with different values that I need to combine and add a new value to the final result. json1= { "b":"t ...

Ways to show alternative data from a database in Laravel 8

I am working on a project where I need to display additional data based on the option selected from a dropdown menu populated from a database. Can anyone guide me on how to achieve this using javascript or jquery? https://i.stack.imgur.com/k3WLl.png Belo ...

Spinning html/css content using JavaScript

Greetings! I am currently working on a demo site using just HTML, CSS, and JavaScript. Typically, I would use Ruby and render partials to handle this issue, but I wanted to challenge myself with JavaScript practice. So far, I have created a code block that ...

Learn how to use canvas and JavaScript to draw lines that display x and y coordinates on top of the mouse pointer at the same time

Implement a functionality in which lines are drawn while the mouse button is held down and simultaneously display x & y coordinates on top of the mouse pointer during mouse movement using canvas and JavaScript. The issue arises when attempting to draw lin ...

`Balance in structure`

Having trouble with CSS. I would like to make this form symmetrical. Buttons should be in one column, textfields in another, and the question mark should also have its own column. Apologies if the ticket structure is not perfect, this is only my second on ...

Verify the values in the dropdown menu and then cross-reference them with the user's input

Is there a way to verify if the textfield, newTeamName, is already included in a list of teamnames that are stored within a select box? I seem to be encountering issues with my code - can you point out what might be the problem? Just to note, there are no ...

Ways to determine the count of arrays within a JSON data structure

Displayed below is a JSON object with multiple arrays. The goal is to extract the number and name of each array within this object. Since the object is dynamically generated, the quantity and names of the arrays are unknown. In this example, there are tw ...

Save an automatically generated number into a variable and use it to reference an image file for display. This process can be accomplished using JavaScript

I'm having trouble getting my images to display randomly on a page. The images are named 0 - 9.png and I am using a pre-made function for random number generation. However, when I try to call on this function later down the page, nothing appears. It ...

Difference among rows

I am currently working on a table design with 2 rows that have different colors. There seems to be some space between the two rows and I am looking for a way to eliminate that gap. Can anyone provide me with a solution? https://i.stack.imgur.com/8N8Rw.png ...