What is the best way to choose all initial elements within a single row using CSS?

Is it possible to target all the first elements in a single row? I know this is an unusual request, but I have a row that contains block elements. I want to apply CSS styling to each of the first elements.

Here is the row:

<ul class="slides">
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
  <li> ... </li>
</ul>

Here are the first elements for your reference, with a dynamic screen size.

Answer №1

Depending on the number of elements in each row, you can utilize the li:nth-child(4n+1) series to select the first element of each row if there are 4 elements in a row. If there are 3 elements in a row, you can use li:nth-child(3n+1).

 *,
*::before,
*::after {
    box-sizing: border-box;
}
body {
  margin: 0;
  padding: 0;
}
.slides {
  display: flex;
  flex-wrap: wrap;
  padding: 0;
  list-style: none;
}
li {
  flex: 0 0 25%;
  padding: 10px;
  max-width: 25%;
}
.box {
  width: 100%;
  height: 100px;
  background-color: red;
}
@media(min-width: 2px) and (max-width: 767px) {
  li:nth-child(2n+1) .box{
    background-color: green;
  }
}
@media(min-width: 768px) and (max-width: 1024px) {
  li:nth-child(3n+1) .box{
    background-color: green;
  }
}
@media(min-width: 1025px) {
  li:nth-child(4n+1) .box{
   background-color: green;
  }
}
@media(max-width: 1024px) {
  li {
    flex: 0 0 33.33%;
    max-width: 33.33%;
  }
}
@media(max-width: 767px) {
  li {
     flex: 0 0 50%;
     max-width: 50%;
  }
}     
<ul class="slides">
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
    <li>
      <div class="box"></div>
    </li>
  </ul>

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

How can you dynamically disable the submit button on a form in Angular 7+ based on the form's current status?

Let's consider a scenario with a button in our code: <button type="button" class="menu-button" [disabled]="isInvalidForm()">Save</button isInvalidForm() { console.log('I am running!'); return this.nameValidator.errors || this.last ...

Tips for retrieving the file name from the <input type="file" tag within a JSP page

I am looking to retrieve the file path from an HTML input type="file, which is selected by the user in the file dialog. <script> function OpenFileDialog(form) { var a = document.getElementById("inputfile").click(); SampleF ...

Looking for a skilled JavaScript expert to help create a script that will automatically redirect the page if the anchor is changed. Any t

Seeking a Javascript expert, In my custom templates, I have used a link as shown below: <a href='http://www.allbloggertricks.com'>Blogging Tips</a> I would like the page to redirect to example.com if the anchor text is changed from ...

Is there a way to access the rear camera on a mobile device using webcam.js?

Currently, I am utilizing webcam.js from the following link: https://github.com/jhuckaby/webcamjs. When accessing the website on mobile devices, the front camera tends to open by default. My objective is to switch this default setting to access the rear ...

The intended functionality of clicking on an image is exclusively reserved for its immediate parent element

I have a feature on my website that displays an image gallery. When a user clicks on an image, it opens up the image in full screen similar to Facebook's theatre mode. I have written code so that when the user clicks anywhere in the container of the i ...

Ways to adjust the brightness of any color when hovered over

Is it possible to create a universal effect where an element becomes darker or lighter when hovered over, regardless of the initial color? Here is an example: .change-color{ Background:green; width:100px; height:100px } .change-color:hover{ Background ...

What is the best way to ensure that this <span> maintains a consistent width, no matter what content is placed inside

So here's the deal, I've got some dynamically generated html going on where I'm assigning 1-6 scaled svgs as children of a . The span is inline with 2 other spans to give it that nice layout: I want these "boxes" to all be the same width be ...

Javascript animation functioning for a singular item within a list

As I continue practicing my skills in Javascript and HTML, I stumbled upon this intriguing list known as "item/adapter", similar to what we refer to as adapters in Android. While the process of adding them programmatically to my div is working smoothly, I& ...

Using the <video /> tag on a Next.JS page generated on the server side leads to hydration issues

My Next.js app's index page is rendered on the server side by default. During development, I encountered an error that stated: Unhandled Runtime Error Error: Hydration failed because the initial UI does not match what was rendered on the server. Wa ...

Tips for activating this effect even when the window is resized during page scrolling

There's a code snippet that enables the header to become unfixed when a specific div reaches the top of the screen and then scrolls with the rest of the content. While this solution works perfectly, I encountered a problem where the calculations for ...

The smooth scroll feature is not functioning properly on the animated mouse-scroll down button

I've recently added an Animated Mouse Scroll Down button on my website. However, when I click the button, the smooth scroll feature is not working as expected. Important Note: I already have a separate button for navigating to the next section where ...

Unable to access Form element in Firefox browser

I'm struggling with debugging unfamiliar problems. For instance, in my HTML there's a form: <form name="myForm" > <table> <tr> <td> <input type="radio" name="myType" value="val" onclick="someF ...

Storing data locally in PhoneGap using localStorage via AJAX calls is a common requirement for

Our mobile application is built with cordova [phonegap] and we are looking to implement offline saving of results into the cache memory. The results are fetched from a PHP server and displayed in a webview using ajax and javascript. I have tried using loc ...

"How can I adjust the positioning of a Materialize modal to be at the top

I am encountering an issue where the modal is being displayed automatically without any trigger when I try to set the top property in the #singlePost. Despite attempting $('#singlePost').css("top", "1%");, it does not seem to work. I have also ex ...

Is it achievable to have a background image cover size with a responsive rollover effect?

I’m currently facing a unique challenge where I want to have an image as the background of my website, with the size set to cover the entire screen. On this background image, there are elements like buildings that I want to interact with when hovered ove ...

detect mouse click coordinates within an iframe that spans multiple domains

I am currently encountering an issue with tracking click position over a cross-domain iframe. Here is my current code: <div class="poin"> <iframe width="640" height="360" src="http://cross_domain" frameborder="0" allowfullscreen id="video">< ...

What is the best way to showcase a photo selected using an HTML input within a div container?

My goal is to select a photo from a folder and display it on a webpage, but I have struggled to find a working solution. Can this be achieved using just basic HTML, CSS, and JS? I am new to web development, so please forgive me for any beginner questions. ...

I want to display a div above an image when hovering over it

.vpbutton {padding:4px;background-color:#EFEFEF;} .userbox img{padding:8px;background-color:#EFEFEF;} .userbox img:hover{opacity:.2;} <div class="userbox"> <img src='img.png' style='height:120px;width:120px;border:1p ...

Update the styling of each div element within a designated list

Looking for a way to assist my colorblind friend, I am attempting to write a script. This is the layout of the page he is on: <div class="message-pane-wrapper candy-has-subject"> <ul class="message-pane"> <li><div style=" ...

Looking for assistance in identifying the cause of CSS malfunction in Internet Explorer

I am encountering some challenges with the CSS on my website. I developed the site without considering Internet Explorer, and now that I am trying to address IE bugs, it feels like a daunting task. How do you approach debugging in situations like these? ...