How do I eliminate the gap between flex elements in my layout?

Is there a way to remove the violet margin that I'm trying to modify?

Additionally, can I specify how many items I want in a certain row using CSS? For example, instead of having 3 items followed by 1 item at 1500px, I would like to have 3 items followed by another 3 items.

Thank you for your help!

Image: https://i.sstatic.net/dcK3i.jpg

* {
  padding: 0;
  margin: 0;
  font-family: Georgia;
}

.row {
  width: 95%;
  height: auto;
  display: flex;
  margin: 0 auto;
  flex-flow: wrap;
  justify-content: space-around;
}

.items {
  width: 95%;
  margin: 0 auto;
}

.item img {
  vertical-align: top;
  width: 290px;
  height: 400px;
  margin: 2px auto 14px auto;
  box-shadow: 0px 0px 12px rgba(120, 17, 25, .7);
  display: block;
  opacity: 0.95;
}

.item {
  box-shadow: 0px 0px 8px #000;
  margin: 0px auto 30px auto;
  padding: 15px 0;
  width: 330px;
}

@media screen and (max-width:800px) {
  .search form {
    width: 95%;
  }
  .search input {
    width: 100%;
  }
  .search select {
    width: 100%;
    margin-top: 5px;
  }
}
<html>
<body>
  <main>
    <div class="items">
      <div class="row">
        <div class="item">
          <img src="img/1.jpg" />
          <h2>Lorem Ipsum</h2>
          <h3>$101</h3>
          <div class="buy_buttons">
            <button class="b_buy">Comprar</button>
            <button class="b_vista">Vista</button>
          </div>
        </div>
        ...
      </div>
      <div class="row">
        ...
      </div>
    </div>
  </main>

Answer №1

If you want to achieve this layout using media queries, I have provided a sample code for you below. Feel free to use it and let me know if it works for you!

.w {
  display: flex;
  flex-wrap: wrap;
  gap:2px;
  justify-content: center;
}

.w > div {
  margin-bottom:2px;
  background: gray;
  width: 24%;
}

@media only screen and (max-width: 1500px) {
  .w {
    background-color: lightblue;
  }
  .w > div {
    margin-bottom:2px;
    background: gray;
    width: 33%;
  }  
}

@media only screen and (max-width: 1100px) {
  .w {
    background-color: lightgreen;
  }
  .w > div {
    margin-bottom:2px;
    background: gray;
    width: 49%;
  }  
}
<div class="w">
  <div>1</div>
  <div>1</div>
  <div>1</div>
  <div>1</div>
  
  <div>2</div>
  <div>2</div>
  <div>2</div>
  <div>2</div>
  
  <div>3</div>
  <div>3</div>
  <div>3</div>
  <div>3</div>
  
</div>

Answer №2

The lavender section isn't considered as margin, it's simply the extra space between items caused by your justify-content property. To eliminate this spacing, you can adjust the property to justify-content:center.

.row{
    border: 1px solid black;
    display: flex;
    flex-flow: wrap;
    justify-content: center;
}
.item{
  width:33.33%; 
}
.a{
    background-color:blue;
}
.b{
    background-color:red;
}
.c{
    background-color:green;
}
...
<div class="row">
    <div class="item a">a</div>
    <div class="item b">b</div>
    <div class="item c">c</div> 
    ...
</div>

If you wish to control the number of items per row, consider adding a new row or adjusting the width of your items accordingly.

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

z-index in CSS causes links to conflict

I am currently working on a project that requires an image to be displayed prominently on the website along with some links positioned behind it. However, I'm facing an issue where I can't click on the links after implementing z-indexes to layer ...

Having trouble adjusting the width of a Material-UI modal in React?

I'm having trouble adjusting the width of the dialog box to show all of the content without adding a horizontal scroll bar. I noticed that the blur effect is applied to most of the page, so why isn't the form extending with it? Any assistance on ...

HTML elements within the parent are seemingly enlarged compared to the parent element

I'm currently working on a project for an angular 2 recipe application, and I've encountered an issue with one of my HTML elements. The list items within the unordered list (which contain checkboxes) appear to be larger than the parent UL element ...

How to swap one image for another using jQuery on click

I have a question about updating images on an HTML page using jQuery. Below is the code I am currently using: $(document).ready(function() { $("#img-1").click(function() { var imgsrc = $(this).attr('src'); $(".click-img").attr('s ...

Creating vibrant row displays in Vue.js: A guide to styling each row uniquely

I am not a front-end developer, so Vue and JS are new concepts for me. Currently, I am working on an application for managing sales. One of the components in my project involves displaying invoices in a list format. To make the rows visually appealing, I ...

How ASP.net can dynamically generate URLs in external CSS files

I have been trying to organize my css separately from the *.aspx file. Everything was working fine when the following code was included in the aspx file: background-image: url('<%=Page.ResolveUrl("~/Themes/Red/Images/Contestant/1.jpg)%>';) ...

Trouble with displaying the NVD3 sample chart

I seem to be missing something simple... I've copied the code for an nvd3 example exactly, but I'm getting a blank page without any error messages. Both the nvd3 and d3 libraries are showing up when I check in the console by typing "nv" or "d3", ...

I am currently engaged in a Portfolio project, where my objective is to ensure that all images are optimized for responsiveness

While Bootstrap ensures that relative images are responsive, absolute images may not be. This can cause alignment issues when the browser window is resized on mobile devices. .title-img { position: relative; width: 100%; height: 100%; } .skill-b ...

Creating a visually appealing chart similar to Excel can be achieved using JavaScript with a whopping 64382 lines of JSON data. No need for Chart.js or any additional tools - simply rely on JavaScript, HTML, and CSS to

I have a project that is due in just a few hours and I need to create a detailed chart using a large set of 64382 lines of JSON data. My knowledge of javascript is limited, so I am struggling to come up with ideas on how to approach this task. While I have ...

JavaScript encountered an issue when trying to display HTML content

Through my PHP code, I am attempting to create a popup window that displays the content of an HTML file. However, after adding script tags, no HTML content is displayed. When I tried echoing out $row2, only the word 'array' appeared on the screen ...

A guide to utilizing the jQuery .on method when initiating table sorting in DataTables

Perhaps this question has been asked before, but I find myself in a situation where I can successfully sort my data table during page loading. However, when I replace the entire table with data populated by ajax, the sorting functionality stops working. Yo ...

The size of text enclosed within div elements appears distorted

Having difficulty adjusting text size within div tags, specifically when trying to set it in pixels. For example, setting font-size to 20px while maintaining a div size of 250 by 50 pixels results in stretched out text. Here's the code snippet: .NavB ...

Strangely odd actions from a JavaScript timepiece

After answering a question, I encountered a problem with my JavaScript clock that displays the day, time, and date on three lines. To make sure these lines have the same width, I used the BigText plugin, which works well except for one issue. tday=new A ...

step-by-step tracking indicator

I need help figuring out how to change the buttons on my progress bar when users click the next button. Essentially, I want each step of the progress bar to lead to a different page with unique content - like buttons on step 1, text on step 2, and so on. ...

Use PHP to create a new JSON file on the server by submitting a form, then utilize a second form to update

My current goal is to create a json file using a form and then update that file with another form. Both of these processes are handled in the process.php file. I have managed to successfully update the json file if it is named as data.json initially in pro ...

Is it possible to merge two HTML selectors using jQuery?

In my HTML, I have two button elements defined as follows: <input type="button" class="button-a" value="Button a"/> <input type="button" class="button-b" value="Button b"/> When either of these buttons is clicked, I want to trigger the same ...

Attempting to obtain a score value from the input, but unsuccessful in doing so

Prior to posing this question, I had already managed to successfully retrieve the score value. Below is my original script: <script> $.ajax({ type: "POST", url: "https://example.com/paylist.php?acc=useraccount&json=1", ...

Maintain consistent grid proportions even when content overflows

I came across this issue: https://jsfiddle.net/w19hpqzx/ However, I'm aiming for a layout that resembles this: https://jsfiddle.net/4smwop2u/ Specifically, I'd like the yellow aside portion to stay visible on the screen and for the code block . ...

Exciting Findings of Grid in Bootstrap 4 Alpha-6

I recently switched to using the latest alpha4 6 version and decided to see what has changed. It seems that I can now use containers, rows, and columns just like in version 3. So, I set up a very basic layout - content > row > col (using col as a c ...

The div does not expand due to the lack of margin in the paragraph

Utilizing the jquery mobile framework, I have crafted a webpage about gardening services which can be found at the following link: I urge you to take notice of the shaded paragraph just below the <h1> heading. My main concern lies with the <p> ...