The first child selector in combination with nth-of-type is not functioning as expected

I'm attempting to apply a radius property to the first, third, and fifth image or div element. I've tried using :first-child and .image img:nth-of-type(1), but both options are applying the radius to every five elements. Can anyone explain why this is happening?

.image img:first-child {
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
}
<div className="container">
    <div className="gallery-container w-2 h-2">
        <div className="gallery-item">
            <div className="image">
                <img src={this.props.photos.room_photos[0].imageUrl} />
            </div>
        </div>
    </div>
    <div className="gallery-container">
        <div className="gallery-item">
            <div className="image">
                <img src={this.props.photos.room_photos[1].imageUrl} />
            </div>
        </div>
    </div>
    <div className="gallery-container">
        <div className="gallery-item">
            <div className="image">
                <img src={this.props.photos.room_photos[2].imageUrl} />
            </div>
        </div>
    </div>
    <div className="gallery-container">
        <div className="gallery-item">
            <div className="image">
                <img src={this.props.photos.room_photos[3].imageUrl} />
            </div>
        </div>
    </div>
    <div className="gallery-container">
        <div className="gallery-item">
            <div className="image">
                <img src={this.props.photos.room_photos[4].imageUrl} />
            </div>
        </div>
    </div>
</div>

Answer №1

Incorrect selector usage is causing the issue here. Instead of using .image img:first-child, target the first, third, and fifth children of the parent .container div to apply the border radius only to those images.

Try this revised code:

/* Select every other div item starting with first */
.container .gallery-container:nth-child(odd) .image img{
    border-top-left-radius: 10px;
    border-bottom-left-radius: 10px;
}

Give it a shot and see if it resolves the problem.

Answer №2

Give this a shot:

If you're looking to style every other element, the CSS property 'nth-child()' can come in handy. You can find more information about it here.

In your case, simply apply the parameter odd to the parent class like so:

.gallery-container:nth-child(odd) img
. Feel free to modify according to your needs.

.gallery-container:nth-child(odd) img {
  display: inline-block;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
}
<div class="container">
  <div class="gallery-container w-2 h-2">
    <div class="gallery-item">
      <div class="image">
        <img src="http://placekitten.com/201/201" />
      </div>
    </div>
  </div>
  <div class="gallery-container">
    <div class="gallery-item">
      <div class="image">
        <img src="http://placekitten.com/301/301" />
      </div>
    </div>
  </div>
  <div class="gallery-container">
    <div class="gallery-item">
      <div class="image">
        <img src="http://placekitten.com/201/201" />
      </div>
    </div>
  </div>
  <div class="gallery-container">
    <div class="gallery-item">
      <div class="image">
        <img src="http://placekitten.com/301/301" />
      </div>
    </div>
  </div>
  <div class="gallery-container">
    <div class="gallery-item">
      <div class="image">
        <img src="http://placekitten.com/201/201" />
      </div>
    </div>
  </div>
</div>

Answer №3

Have you considered using the :nth-child(3n+3) selector?
(3 x 0) + 3 = 3, which represents the 3rd element
(3 x 1) + 3 = 6, representing the 6th element
(3 x 2) + 3 = 9, indicating the 9th element
and so on.

What about the :nth-child(2n+1) selector?

(2 x 0) + 1 = 1, signifying the 1st element
(2 x 1) + 1 = 3, denoting the 3rd element
(2 x 2) + 1 = 5, showing the 5th element
and so forth.

# For elements 3, 6, 9
.container .gallery-container:nth-child(3n+3) .image img{
    border-top-left-radius: 10px;
    border-bottom-left-radius: 10px;
}

# For elements 1, 3, 5
.container .gallery-container:nth-child(2n+1) .image img{
    border-top-left-radius: 10px;
    border-bottom-left-radius: 10px;
}

Use these selectors to target specific positions in your layout.

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

Animate each item on the map once it becomes visible while scrolling

Struggling with animating 3 divs individually using array.map. My goal is to animate each div once it comes into view using framer motion. The problem I'm encountering is that when I try to animate a single div using inView, all 3 divs animate simult ...

The bootstrap modal is appearing correctly, but the content within it is not displaying properly

I've been working on a website and added a modal for signup. However, when the user clicks on the signup button, the modal appears but the content inside it is not clickable. Can someone please help me with this issue? Here is the code snippet I am us ...

The mp4 video on the website will only load if the Chrome developer tools are activated

Whenever I try to play the video set as the background of the div, it only starts playing if I refresh the page with Chrome dev tools open. HTML: <div id="videoDiv"> <div id="videoBlock"> <img src="" id="vidSub"> <video preload="prel ...

Conceal a portion of the navigation menu while viewing on mobile devices and ensure proper functioning of the mobile stylesheets

I'm facing an issue with my website's navigation bar. It has five sections on desktop, but I need only four visible on mobile devices. However, I can't seem to get my mobile style sheets to work properly. Specifically, I am struggling to hid ...

Is it possible to generate excel spreadsheets using a selection in Laravel?

In my system, I have implemented a radio group as a filter for my table which displays items as active, inactive, or both. Additionally, there is a button that allows users to export the table data into an Excel document. Currently, the export button downl ...

How do I initiate a custom button click event in React from an event handler in another component?

I have a unique React application that utilizes the Material UI framework for its user interface design. Specifically, I have developed a specialized button component that customizes the default Material UI button and integrates with Redux. Within the ren ...

Swapping Elements in To Do List based on Priority in REACT

Currently, I am creating a to-do list and I would like to implement a function that allows me to swap tasks based on their priorities. For instance: Go to the gym Learn React I want to add a button that enables me to move elements up and down resultin ...

I am having trouble establishing a connection between the MySQL Workbench database and VS Code

During my ReactJS CRUD training, I encountered an issue while trying to add an entry to the MySQL Workbench database from VS Code. Despite ensuring all names and symbols match up, the entry is not being added upon page reload. Clicking on "Execute the se ...

Error message: The variable GLOBAL is not defined in the fetch-mock code

I am currently using fetch-mock to write unit tests for functions that utilize isomorphic-fetch. Below is a sample of the test suite I have created: import connect from '../../src/utils/connect'; import fetchMock from 'fetch-mock'; ...

Image spotlight effect using HTML canvas

I am currently seeking a solution to create a spotlight effect on an HTML canvas while drawing an image. To darken the entire image, I used the following code: context.globalAlpha = 0.3; context.fillStyle = 'black'; context.fillRect(0, 0, image. ...

Display the JSON data in a table when the button is clicked

My goal is to display JSON data retrieved from a web service in a table format only after a button click. Although I have successfully fetched the data, I want it to be visible in a table only upon clicking the button. The current code displays table row n ...

What is the best way to load specific CSS in an rhtml file?

In the world of website development, we often find several pages that are generated from the same layout.rhtml file. Along with a global css file, each page may also have its own unique css file - such as page1.css for page1.rhtml and page2.css for page2.r ...

Introducing a new addition to the Material UI palette: an extra color to amplify your theme and integrate seamlessly into your design just like Primary and

One of the new custom colors I've added to the palette is blue: const rawTheme = createMuiTheme({ palette: { primary: { light: '#7ED321', main: '#417505', dark: '#2B5101', contrastText: &apo ...

Simple steps to convert Redux state to Redux Persist with the help of 'combineReducers' function

I'm facing a challenge trying to implement Redux-Persist with my Redux state, particularly because I am using combineReducers. Here is the structure of my store: import { createStore, combineReducers } from 'redux' import { usersReducer } fr ...

css3 animation keyframes timing function is exclusive to chrome

After creating a preloader animation using CSS3, I noticed that it only functions properly on Chrome. On all other browsers, the animation works, but the timing for the middle bar is delayed, throwing off the entire animation. I'm puzzled because all ...

Using Jquery Ajax to validate login information by submitting an HTML form

Working on a project involving jQuery Ajax, HTML forms, MySQL, and PHP. I have a database with registered users and I want to use a login form to retrieve user information from the database upon submission. However, I'm encountering an issue where the ...

Is there a way to perfectly center an SVG image and text within a linked button?

After creating a button that utilizes an SVG image icon on the left and text on the right, I encountered an issue where the text could not be perfectly centered in alignment with the icon. Here is how it currently appears: https://i.sstatic.net/FGGVi.png ...

I am experiencing issues with my button not responding when I click on the top few pixels

I've created a StackBlitz version to illustrate the issue I'm facing. It seems like the problem might be related to my CSS for buttons... Essentially, when you click on the button at the very top few pixels, it doesn't register the click an ...

Show and hide components in React by simply clicking on them

I'm looking to achieve the following: If the component is visible, clicking away from it should hide the component. If the component is hidden, clicking on it (or any area that it would occupy if visible) should show the component. Is there a way to ...

Automating the process of switching themes in a child component

In my Remix app, I have implemented a feature where users can choose between three modes: system, light, and dark mode. When a user selects a mode, it is saved in localStorage. However, I want the theme to change automatically when this happens (which is ...