CSS Styled Product Index

Currently, I am working on creating a product catalog using React and CSS. Everything is going smoothly except for the last row of products.

The issue with the last row is that it only contains 1 element, and due to the flex-grow: 1 setting, it ends up taking up all the available width.

I'm looking for a way to set an equal width for all elements in that row. Is there a solution for this?

ul {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  }
  .products__product {
     flex: 1 0 20%;
     margin: 1.5em 0.75em 0 0.75em;
     min-height: 250px;
     background:grey;
  }
<div class="products">
  <div class="container">
    <div class="products__content">
      <ul>
        <a class="products__product" href="/products/21/stone-in-the-night"
          ><li><h1>Stone in the Night</h1></li></a
        ><a class="products__product" href="/products/21/the-dying-of-the-spirits"
          ><li><h1>The Dying of the Spirits</h1></li></a
        ><a class="products__product" href="/products/21/the-beginnings-guardian"
          ><li><h1>The Beginning&amp;#8217;s Guardian</h1></li></a
        ><a class="products__product" href="/products/21/death-of-light"
          ><li><h1>Death of Light</h1></li></a
        ><a class="products__product" href="/products/21/the-lost-soul"
          ><li><h1>The Lost Soul</h1></li></a
        ><a class="products__product" href="/products/21/first-husband"
          ><li><h1>First Husband</h1></li></a
        ><a class="products__product" href="/products/21/verzliaraktis"
          ><li><h1>Veržliaraktis</h1></li></a
        ><a class="products__product" href="/products/21/raktas"
          ><li><h1>Raktas</h1></li></a
        >
      </ul>
    </div>
  </div>
</div>

Answer №1

To establish the initial width as a percentage, you can utilize the following method:

.items__item {
  ...
  flex: 0 0 30%
  ...
}

This directive ensures that the items neither expand nor contract but instead occupy 30% of the container width. Even if there is only one item in the container, it will still consume just 30% of the width. The percentage can be adjusted according to the number of items present. Sadly, fractional notation cannot be employed for this purpose.

Answer №2

If you want to maintain consistency, consider adjusting the width of the products__product class.

ul {
  display: flex;
  flex-wrap: wrap;
  list-style: none;

  .products__product {
     width: 30%; // Displaying 3 items per row
     margin: 1.5em 0.75em 0 0.75em;
     min-height: 250px;
  }
}

Answer №3

What do you think of this new layout?

ul {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
}

.products__product {
  flex: 0 0 20%;
  min-width: 20%;
  margin: 1.5em 0.75em 0 0.75em;
  min-height: 250px;
  background: red;
}
<div class="products">
  <div class="container">
    <div class="products__content">
      <ul>
        <a class="products__product" href="/products/21/stone-in-the-night">
          <li>
            <h1>Stone in the Night</h1>
          </li>
        </a>
        <a class="products__product" href="/products/21/the-dying-of-the-spirits">
          <li>
            <h1>The Dying of the Spirits</h1>
          </li>
        </a>
        <a class="products__product" href="/products/21/the-beginnings-guardian">
          <li>
            <h1>The Beginning's Guardian</h1>
          </li>
        </a>
        <a class="products__product" href="/products/21/death-of-light">
          <li>
            <h1>Death of Light</h1>
          </li>
        </a>
        <a class="products__product" href="/products/21/the-lost-soul">
          <li>
            <h1>The Lost Soul</h1>
          </li>
        </a>
        <a class="products__product" href="/products/21/first-husband">
          <li>
            <h1>First Husband</h1>
          </li>
        </a>
        <a class="products__product" href="/products/21/verzliaraktis">
          <li>
            <h1>Veržliaraktis</h1>
          </li>
        </a>
        <a class="products__product" href="/products/21/raktas">
          <li>
            <h1>Raktas</h1>
          </li>
        </a>
      </ul>
    </div>
  </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

Is it possible to modify a portion of the zod schema object according to the value of a

My form consists of several fields and a switch component that toggles the visibility of certain parts of the form, as shown below: <Field name="field1" value={value1} /> <Field name="field2" value={value2} /> &l ...

Minimize the gaps between items within a CSS grid design

I'm struggling with adjusting the whitespace between 'Job Role' and 'Company Name' in my grid layout. Here's a snippet of what I have: https://i.sstatic.net/l55oW.png The container css is set up like this: .experience-child ...

Angular ensures that the fixed display element matches the size of its neighboring sibling

I have a unique challenge where I want to fix a div to the bottom of the screen, but its width should always match the content it scrolls past. Visualize the scenario in this image: https://i.sstatic.net/i7eZT.png The issue arises when setting the div&apo ...

Steps for Disabling col-sm and col-xs in Bootstrap 3

Hey there! I'm currently working on a template using Bootstrap 3. Initially, I planned to make it responsive for all devices, but I have since changed my mind and decided that the template is already complete. I've utilized col-md in each row, h ...

Is it possible to effortlessly download videos stored in Firebase directly from my website by simply clicking on an "a" tag?

Recently, I came across a web page that contained a list of videos. There was an icon resembling an arrow next to the "a" tag. You can view this image by clicking here. My intention was to download one of these videos hosted on Firebase by clicking on ...

Increase the height of the div element by a specified number of

Is there a way to dynamically expand a div's height using CSS without knowing its initial height? I want to increase the height by a specific amount, such as "x px taller" regardless of its starting size. For example, if the div starts at 100px tall, ...

Every time I attempt to launch my React app, an error message pops up

Encountered an issue during compilation. [eslint] The "react" plugin was in conflict between "package.json » eslint-config-react-app » C:\Users\lenovo\Desktop\achievers\reactProjects\My-app\node_modules\eslint-con ...

Exploring the shift in state management across various single-page applications

A common challenge in single page applications is maintaining state between different pages. For example, imagine having a login page as one SPA and then transitioning to another SPA while still needing to manage the user's logged in information. This ...

The visibility of overflow-y does not seem to be working properly when overflow-x is

https://i.sstatic.net/hihjC.png .iati-list-table { overflow-x: auto; overflow-y: visible; } When I apply overflow-visible, a scroll bar appears. But when I use overflowy-hidden, the tooltip is cropped. How can I set it so that overflow x is auto a ...

Introducing a novel class designed to leverage the attributes of another class

Is there a way to use the @extend feature in CSS to inherit properties from one class to another? Imagine having two CSS files loading on a page in this order: <link rel="stylesheet" href="/css/one.css" media="screen"> <link rel="stylesheet" href ...

Learn how to change the input source in Ratchet's chat app to come from a text box on the webpage instead of the console

I followed the guidelines on to create a chat app. However, I now want to input data from a text box instead of using conn.send() in the console. How can I achieve this using php? I was successful in redirecting the sent message to an html element like ...

The media query in Css3 is not functioning as expected when using max-width

Struggling to hide a specific element when the screen width reaches 980px or less using media queries. Despite my attempts, the element continues to display. Oddly enough, if I use a media query with min-width instead of max-width, the element disappears ...

sending the useState hook to a component as a prop

I need to share the useState values from my parent component with multiple children using the code below. App.js const [inSystem, setIn] = useState(false); <Route exact path="/" functions={[inSystem, setIn]} component={Loading} /> Then i ...

Troubleshooting Highcharts container selection problem on Nexus 7 running version 4.2.1

Having a problem with Highcharts on my Nexus 7. When I touch the chart, the entire thing gets selected with a blue overlay, but this doesn't happen on other devices like the Nexus 4. Even when I try accessing demos from Highcharts website, the issue ...

Execute webpack without using the sudo command

Having an issue with my React application - when trying to create the bundle using the webpack command, I keep encountering an access error: emittingError: EACCES: permission denied If I run the command with sudo it works fine. Is there a way to avoid us ...

What is the best way to implement a link instead of a string within a custom JavaScript function?

I am looking for a way to replace parameters in a string with the values provided using a helper function - type FormatStringParameter = string | number; /** * Helper function to substitute parameters in a string with the specified values * * @param in ...

What is the process for inserting images into a vertical timeline format?

I am currently working on building a timeline and incorporating images into it. I came across a solution on W3 that I'm trying to implement, but I'm facing some challenges. I've been experimenting with the code but I feel like there are part ...

Troubleshooting problem with custom Angular directive integrating KineticJS

Hey there, I'm currently working on putting together a KineticJS application in Angular and need to resolve a minor issue. While following an example for setting up a draggable shape in AngularJS using KineticJS from this link: , I encountered this er ...

Activating events with Jquery

Can anyone help me with this HTML and jQuery issue I'm facing? When I click on an image for the first time (when it has the class 'hide'), the first jQuery click function is executed. However, when I click on the image again, the second func ...

The Label method in the Html helper does not display the id

The creation of the checkbox and its label was done using an HTML helper: @Html.CheckBoxFor(m=>m[i].checkExport) @Html.LabelFor(m=>m[i].checkExport) To customize the label, I added the following CSS in a separate file: input[type="checkbox"] + l ...