A layout featuring a grid design that includes spacing and gutters between items, however, there is no spacing between the items

I am looking to create a grid layout where each child element represents an individual grid square.

However, I am facing an issue with spacing between the children and the parent container, which is not desired. How can I eliminate this unwanted space?

Using negative margin on the parent div shifts the entire grid, resulting in it being off-center on the page.

The main objective here is to make the grid fully responsive, hence my use of floating elements and relative widths.

This is how I envision the layout to appear (with 10px spacing):

+-----+--+-----+--+-----+--+-----+
|xxxxx|  |xxxxx|  |xxxxx|  |xxxxx|
|xxxxx|  |xxxxx|  |xxxxx|  |xxxxx|
|xxxxx|  |xxxxx|  |xxxxx|  |xxxxx|
+-----+  +-----+  +-----+  +-----+
|                                |
+-----+  +-----+                 |
|xxxxx|  |xxxxx|                 |
|xxxxx|  |xxxxx|                 |
|xxxxx|  |xxxxx|                 |
+--------------------------------+

#parent {
  width: 500px;
  height: 500px;
  background-color: #CCCCCC;
}

.child {
  box-sizing: border-box;
  /* Ensures padding expands inwards */
  padding: 5px;
  /* Using width instead of margin for relative width compatibility, resulting in 10px spacing between children */
  width: 25%;
  height: 100px;
  float: left;
}

.child>div {
  /* Represents content within each child element */
  width: 100%;
  height: 100%;
  background-color: #000000;
}
<div id=parent>
  <div class=child>
    <div></div>
  </div>
  <div class=child>
    <div></div>
  </div>
  <div class=child>
    <div></div>
  </div>
  <div class=child>
    <div></div>
  </div>
  <div class=child>
    <div></div>
  </div>
  <div class=child>
    <div></div>
  </div>
</div>

Answer №1

How about trying something like this:

.child { margin: 10px; }
.child:nth-child(odd){ border: 1px solid #000; }

Check out the live demo below:

.parent {
  width: 300px;
  height: 300px;
  background-color: #EEEEEE;
}

.child {
  box-sizing: border-box;
  width: 45%;
  height: 50px;
  display: inline-block;
  margin: 10px;
  padding: 5px;
  text-align: center;
  border-radius: 5px;
}

.child:nth-child(odd) {
  border: 1px solid #000;
}
<div class="parent">
    <div class="child">Item 1</div>
    <div class="child">Item 2</div>
    <div class="child">Item 3</div>
    <div class="child">Item 4</div>
    <div class="child">Item 5</div>
</div>

Answer №2

Here is an example of a nested div structure with CSS styling:

<div id=parent>
                <div class=child><div></div></div>
                <div class=child><div></div></div>
                <div class=child><div></div></div>
                <div class=child><div></div></div>
                <div class=child><div></div></div>
                <div class=child><div></div></div>
            </div>

            <style>
                #parent {
                    width: 500px;
                    height: 200px;
                    background-color: #CCCCCC;
                }
                
                .child {
                    box-sizing: border-box; /* So the padding expands inwards */
                    width: 25%;
                    height: 100px;
                    float: left;
                    padding:0px 5px 5px 0px;
                }
                .child:nth-child(4n) {

                    padding-right: 0;
                }
                .child:nth-child(n+5) {
                    padding:5px 5px 0px 0px;
                }
                .child > div { /* This represents content of the child */
                    width: 100%;
                    height: 100%;
                    background-color: #000000;
                }
            </style>

Answer №3

The issue at hand has been successfully resolved through the utilization of CSS Grid Layout.

When it comes to creating space between grid items, Grid offers options such as grid-column-gap, grid-row-gap, and grid-gap (the shorthand). However, these properties do not affect the area between items and the container.

10.1. Gutters: the grid-column-gap, grid-row-gap, and grid-gap properties

These properties define the gutters between grid rows and columns, respectively.

#parent {
  display: grid;
  grid-template-columns: repeat(auto-fill, 120px);
  grid-auto-rows: 120px;
  grid-gap: 10px;
  width: 510px;
  background-color: #cccccc;
}

.child {
  background-color: #000000;
}
<div id="parent">
  <div class=child></div>
  <div class=child></div>
  <div class=child></div>
  <div class=child></div>
  <div class=child></div>
  <div class=child></div>
</div>

To further understand how the aforementioned Grid properties function, refer to this post: CSS-only masonry layout but with elements ordered horizontally

If you're curious about CSS Grid browser compatibility, check out this link: http://caniuse.com/#search=grid

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

"JQuery event handlers not functioning as expected: .click and .on failing

For some time now, I've been facing this issue and I'm at a loss trying to figure it out. I have attempted various solutions such as using .click(), .on(), .delegate, and even utilizing .find() to locate the specific element causing the problem. ...

Hiding the overflow conceals the entire image in one direction, while leaving the other exposed

Here is the code I have written for my project (view fiddle here): img { width: 200px; height: 200px; } .image-container { width: 600px; height: 200px; overflow: hidden; } I am working with multiple images, all sized at 200px by 200p ...

``When you click, the image vanishes into thin

Could you please explain why the image disappears once I close the lightbox? Access with password: chough ...

Web-based client services

Context: An HTML file I'm working with takes in multiple parameters and utilizes JavaScript to dynamically render the content. The page pulls data from various local XML files for processing. For instance, accessing service.html?ID=123 displays info ...

Exploring the versatility of Bootstrap 4's grid system with varying column widths

I am encountering an issue with div alignment in my code. The first two col-3 classes seem to have different widths than the next two col-3 classes, causing elements to appear unaligned. Here is the code snippet: <link href="https://stackpath.bootstr ...

Execute script when on a specific webpage AND navigating away from another specific webpage

I created a CSS code that adds a fade-in effect to the title of my website, and it works perfectly. However, I now want to customize the fade-in and fade-out effect based on the page I am transitioning from. My desired outcome: If I am leaving the "Biolo ...

JavaScript/DOM - What sets apart a "CSS Selector" from an attribute?

When it comes to excluding declarative event handlers: <a href='#' onclick=<handler> ... /> Is there a significant difference between an Attribute and a CSS Selector? For example, if I define my own attribute: <a href='#&a ...

My CSS horizontal drop-down menu is causing the sub-navigation items to overlap each other

I'm having an issue where my sub navigation menu items are stacking on top of each other instead of displaying properly. Here is the code snippet causing the problem: /* STYLING FOR NAVIGATION MENU */ .nav-bar { width: 100%; height: 80px; ...

Issue with React-Route not displaying components

Below is the code snippet from my app.js file: <Router> <Header title="My Todos List" /> <Routes> <Route path="/about" element={<About />} /> <Route path="/" ...

What are the steps to resize a YouTube embedded video?

I am currently attempting to use a YouTube video as the background for breadcrumbs on my website. However, I am facing an issue with setting the video width to cover the full screen. The image below shows that the video is displayed in the center of the if ...

Is it possible to modify the concealed global attribute using CSS?

Imagine this scenario: <div hidden>blah blah blah</div> If I hide the <div> element like that, can CSS be used to make it visible again? Alternatively, is there an HTML-only method to hide a <div> that can later be reversed with ...

PHP jQuery buttons for popovers

With a simple click of a button, I can generate 8 presentations and then edit each one individually by clicking on its respective name. Within this editing process, there is another form where I would like to include additional buttons that allow me to cus ...

Manipulate the contents of children divs within a parent div using JavaScript or JQuery

<div id="abc"> <div id="a_b"> abcd </div> <div id="c_d"> xyz </div> </div> I have a challenge where the divs on my page are generated dynamically and their IDs change every time the page loads. When the window i ...

What is the technique for filtering multiple values using the OR operation in ng-model functions?

Currently, I am using an ng-modal labeled as "myQuery." At the moment, there are two filters in place that look like this: <accordion-group heading="" ng-repeat="hungry_pets in Pets" | filter:{hungry:false} | filter:{name:myQuery}" ... > I have ...

Ways to emphasize the four edges of a table cell using border-collapse collapse

I am trying to emphasize the borders of cells with the class active. The challenge arises from the fact that the table's border-collapse property is set to collapse, causing the top and left borders of cells (except for the leftmost and top row cells ...

The functionality of classes containing <ul> elements is ineffective

Having recently embarked on the journey of learning html/css, I've encountered some confusion along the way. Allow me to showcase my basic web-page layout: <html> <head> <meta charset="utf-8"> <link rel = "stylesheet" type="text ...

What steps can I take to streamline and simplify this tab controller code?

I'm looking to simplify this jQuery code because I feel like there's repetition. As someone new to JavaScript and jQuery, I've created two tabs with their respective containers containing miscellaneous information. My goal is to have each co ...

guide on implementing a horizontal scrolling/swipe navbar with Cordova

Currently, I am working on creating a "category list" with multiple items for a Cordova app. The initial approach was to use a simple HTML list, but unfortunately, it seems that my list is causing some unexpected behavior. Desired swipe navbar layout: ht ...

The content of the HTML request may not always be displayed exactly as it appears in the browser

I've been trying to extract comments from a website using Python and urllib. Although I am successful in obtaining the HTML, I noticed that the comment section is missing. Here's what my Python code retrieves: <div data-bv-product-id="681012 ...

Continuous Div Carousel with Looping in jQuery

I have developed a basic carousel to cycle through a series of divs, but I am facing some issues. I want the carousel to loop continuously so that after reaching "slide 07," it goes back to "slide 01." Unlike using a carousel plugin, I prefer having an ove ...