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

I'm trying to create a transparent v-card, but for some reason it's not turning out the way I want

How can I make the v-card transparent while keeping its content opaque using CSS? card.vue <v-card class="cardColor"> <v-card-text> TEXT </v-card-text> <v-card-actions> <v-btn color="prima ...

The subfolder and HTML page have identical names and are not functioning properly

I recently updated my webpage by removing the .html extensions using htaccess. My website includes a page named "blog.html" and a subfolder called "blog" with additional html pages inside. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ ...

Is there a way to adjust the height of a DIV based on the background image and keep the text centered at the same time?

Currently in the process of designing a landing page for my website, and I am interested in incorporating a Jumbotron to provide a more modern and tech-savvy touch. Successfully managed to center my text both horizontally and vertically, now looking into s ...

Retrieve several values with a limit of 1 in SQL

Consider the following table structure: ID | ident | product 1 | cucu1 | 99867 | 2 | kkju7 | 88987 | 3 | sjdu4 | 66754 | 4 | kjhu6 | 76654 | 5 | cucu1 | 98876 | Running the query: SELECT ident,COUNT(*) FROM sales WHERE status=? AND team=? AND DATE(da ...

Is it possible to include number padding in a Bootstrap number input field?

When using an input box like this: <input type="number" class="form-control" value="00001" /> Is there a way to make it so that when the arrow up key is pressed, it changes to 00002 instead of just 2? https://i.sstati ...

What is the best way to maximize the size of an image without causing the parent div to expand in height?

I have utilized the code below directly from the example provided at https://getbootstrap.com/docs/5.2/utilities/flex/#media-object. This code is meant to generate a box containing an image on the left and text on the right: <div class="mycard bord ...

"Enhanced interactivity: Hover effects and selection states on an image map

Hello there, I need assistance with my code. Here it is: <img id="body_image" usemap="#body_map" src="assets/images/body.jpg" alt=""> <map name="body_map"> <area shape="poly" alt="d" href="#body_chart" name="ad" coords="153, 153, 145, 1 ...

Sticky positioning for dropdown menu in table column body

Greetings, I need some assistance with a formatting issue. My goal is to make the first and second columns of my table sticky by applying a "sticky" position to them. However, one of the columns contains a dropdown menu and the problem is that the content ...

Tips on solving the Navigation bar burger problem with HTML and CSS

## I am having trouble making my navigation bar responsive using HTML and CSS as the navigation burger does not appear in mobile view ## To take a look at my code on jsfiddle, click here: [jsfiddle] (https://jsfiddle.net/abhishekpakhare/nxcdso7k/1/ ...

Can the information from a form be automatically submitted to the server upon selection?

<div class="modal-body modal-body-step-2"> <form action="prenotazioni.php" method="post"> <div class="input-group"> <input type="radio" name="spettacolo" value="Lo Schiaccianoci">Lo Schiaccianoci<br> ...

Conceal the div element if the value exceeds 0

I'm looking for a way to display a div when the number of remaining days is less than 0. I got it working on jsfiddle, but for some reason, it doesn't work anywhere else. if ($('.daysrem&a ...

Embedding a table row within an anchor element in Angular 4

Currently, I am facing an issue with a table that contains [routerLink] on each <tr>. This setup is preventing the "open link in a new tab" option from appearing when I right-click because there is no <a> tag. I attempted to enclose the table r ...

Is there a solution for the noticeable delay in loading fonts on a web page?

I've encountered an issue in my React project where a Google font I'm using seems to briefly load as a different font before switching to the correct one. How can I prevent this from happening? This is how I'm loading the font in public/ind ...

What is the best way to troubleshoot an unresponsive button element to determine the listeners it has?

In a previous situation, I encountered an issue with the event modifier of a nested b-input not working as expected. To resolve this, I had to add .native because I was interacting with a component: b-dropdown(text="Actions") b-drop ...

Problem involving the h1 tag nested within a div container

I have two files, one in .html format and the other in .css format. Here is a snippet of their content: #left, #right { display: inline-block; } #left { width: 15%; height: 550px; margin-bottom: 100px; margin-left: 10px; border: 1px solid bl ...

What is the best way to conceal the border and background color of the dropdown arrow in an HTML <select> tag?

Upon browsing the Mozilla homepage, I noticed a peculiar element in the center - a <select> option with an arrow lacking border and background color. In contrast, when visiting the Facebook sign-up page, the drop-down arrow adheres to the Windows sta ...

What is the reason behind the issue where max-height disrupts border-radius?

My inline svg is styled with the following CSS: .img { border-radius: 15px; overflow: hidden; max-width: 70vw; margin-top: 6vh; max-height: 50vh; z-index: -1; text-align: center; } Everything seems to work as expected, except f ...

Trying to incorporate a PHP echo statement into the innerHTML of a button is ineffective

I have a piece of jQuery code that is not functioning as expected: $("#erase").click(function(){ $("#erase").attr("disabled", "disabled"); // Prevent double-clicking $(this).html("Erasing..."); $.post("erase.php", {target: $("#targ ...

Is there a more intelligent approach to incorporating Bootstrap classes (or classes from another less.css file) into my less file through inheritance?

I am trying to find a more efficient way to integrate bootstrap classes into my less file. Currently, I am doing the following, but it has the disadvantage that not every "subclass" or connected class is inherited for my own control: .BWForm_form-control ...

tips on encrypting css/js file names

Can the filename of the css/js file be encrypted? I have noticed that when I view the source of the website, the filenames of the css and js files look encrypted like this. <link href="/assets/css/builds/73e15c8a3cf6409214bbf8a742e9b5d41403226617.css" ...