Tips for positioning cards in html

I'm working on a layout with a container containing multiple cards. I want each card to align from the left border of the container to the right border, similar to the code snippet below.

<html>
<style>
  .container {
    display: flex;
    justify-content: space-between;
flex-direction: row;
flex-wrap: wrap;
  }
   .card{
 border:1px solid black;
}
</style>

<body>
  <div class="container">
    <div class="card">
      <h2>I'm a card</h2>
    </div>
    <div class="card">
      <h2>I'm a card</h2>
    </div>
    <div class="card">
      <h2>I'm a card</h2>
    </div>
    <div class="card">
      <h2>I'm a card</h2>
    </div>
    <div class="card">
      <h2>I'm a card</h2>
    </div>
    <div class="card">
      <h2>I'm a card</h2>
    </div>
    <div class="card">
      <h2>I'm a card</h2>
    </div>
  </div>
</body>

</html>

However, I noticed that the last two elements have too much space between them. Can someone help me remove this extra spacing?

I've experimented with adding margin-left/right properties to achieve the desired effect, but the corner cards still don't touch the borders. Any suggestions?

Edit: Here is what I expect:

https://i.sstatic.net/vxlcy.png

And here is what I currently get:

https://i.sstatic.net/3Fe9q.png

Answer №1

This issue arises due to the behavior of justify-content: space-between. To understand more about how justify-content works, you can refer to this informative article here

To achieve your desired layout, you can designate the container as a block element and float the children using float:left.

To eliminate any unnecessary gaps, I have removed the margin-left from the .card:first-child and margin-right from the .card:last-child

If you wish to specify the number of card elements per row, you can set a uniform width and clear out the margin space for each nth-child. Currently, I have customized the code for displaying 5 elements per row

.container {
  display: block;
  width: 100%;
}

.card {
  border: 1px solid black;
  float: left;
  margin: 1px;
  padding: 10px 3px;
  /* Setting width for each card element along with -10px to remove the margin width for 5 elements */
  width: calc(100% / 5 - 10px);
}


/* First element */

.container .card:first-child {
  margin-left: 0;
}


/* Last element of the first row */

.container .card:nth-child(5n) {
  margin-right: 0;
}


/* First-element of the 2nd row */

.container .card:nth-child(5n+1) {
  margin-left: 0;
}


/* Last element */

.container .card:last-child {
  margin-right: 0;
}
<div class="container">
  <div class="card">
    <h2>I'm a card</h2>
  </div>
  ...
</div>

Answer №2

Although I'm not entirely sure if I replicated your issue accurately, I have created a codepen example that may provide some assistance: https://codepen.io/salallegra/pen/mQbBea

<html>
<style>
    .container {
        display: flex;
        justify-content: space-between;
        border-style: solid;
        border-width: 1px;
        border-color: red;
        margin-left: 0px;
        margin-right: 0px;
        flex-direction: row;
        flex-wrap: wrap;
    }

    .card {
        border-style: solid;
        border-width: 1px;
        border-color: red;

    }

    body {
        margin-left: 0;
        margin-right: 0;
    }
</style>

<body>
    <div class="container">
        <div class="card">
            <h2>I'm a card</h2>
        </div>
        <div class="card">
            <h2>I'm a card</h2>
        </div>
        <div class="card">
            <h2>I'm a card</h2>
        </div>
        <div class="card">
            <h2>I'm a card</h2>
        </div>
        <div class="card">
            <h2>I'm a card</h2>
        </div>
        <div class="card">
            <h2>I'm a card</h2>
        </div>
        <div class="card">
            <h2>I'm a card</h2>
        </div>
    </div>
</body>

</html>  

I have eliminated the left and right margins of the body element, added borders around both the cards and the container itself.

In addition, I tested various viewport sizes using Chrome developer tools, and found that the spacing remained consistent across different sizes.

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

What purpose does the additional symbol "$()" serve in the selector "$($())"?

Currently, I am looking to incorporate a jQuery scrollspy feature into one of my ongoing projects. Upon coming across this jsfiddle (https://jsfiddle.net/mekwall/up4nu/), I successfully integrated it into my project. However, I have hit a roadblock while ...

Issues encountered when dealing with floating elements and margins

Having some difficulties with aligning elements on my website. As someone who is not highly skilled in coding, I am struggling to define the width and float properties for certain classes. Despite my attempts, it seems that the elements are not positioning ...

Using Laravel and Bootstrap v4, you can easily implement a feature that redirects to a specific tab after

I have a webpage featuring 3 tabs. I am trying to set up the page so that it redirects to the same tab that was active before the reload. I assume each tab should append a string to the URL, like #menu1 or #menu2, but I'm having trouble implementing t ...

Having difficulty inputting text into a text field using Selenium in Java

Here is a snippet of HTML code that I am working with: </td> <td valign=top bgcolor="#dcdcdc" class="camp"> <script>Oblog()</script>ID <br> <input ty ...

Comparison of Foundation5 source ordering: main content versus sidebar

I recently set up a layout with three columns using ZURB Foundation 5. The two sidebars are on the left and right, while the main content area is in the middle. While trying to follow the source ordering instructions in the documentation, I encountered so ...

various designs for multi-location infrastructure in angular js

Our angular app is designed with a multi-site architecture, where the base URL of each domain varies. Each site requires a unique header and footer to be incorporated, along with custom CSS and links specific to that site. Is there a way to dynamically in ...

Struggling to display the inline navbar

For the past few hours (a little over 3 hours, to be exact), I've been grappling with an issue that seems fairly simple. Despite scouring through various SO posts and other resources, I just can't seem to figure out the problem. The crux of the ...

Display or conceal content based on checkbox status

i am trying to create a system where content is hidden by default and only displayed when a checkbox is checked. The goal is to show the content when the checkbox is checked and hide it when unchecked, so that other content can be shown as well upon checki ...

Guide to accessing the updated content of a webpage

the entire document: <!doctype html> <html> <head> <meta charset='utf-8'/> <title>DATA</title> <link href='img/fav.png' rel='icon'> <link href='index.css' rel='sty ...

Attempting to ensure that my website is fully optimized for all devices and

I am currently facing an issue with my CSS code on iPad. I want to change the background-image displayed based on whether the user is using an iPad or not. Specifically, I want to use headerold.jpg as the background-image for iPads while keeping headernew. ...

Tips for creating a responsive H1 tag within the Avada WordPress theme by implementing CSS

One of my acquaintances recently upgraded his Avada WordPress theme to the latest version of WordPress without physically uploading any files. However, after this upgrade, he noticed that the page titles (H1) no longer appear responsive as they did before. ...

Elements floating in space, stretching across the entire width

In my design, I have a fixed size layout with a centered content container. My goal is to have the menu (home, about, contact, login) span 100% of the screen width. You can view the current setup in this jsfiddle: http://jsfiddle.net/Hxhc5/1/ I am aimin ...

What is the reason for the crossed out background image CSS in the Datatables header within Zurb Foundation 5.5.3?

I am facing an issue with displaying images in a wide Datatables header. The design is based on the Zurb Foundation 5.5.3 framework. Does anyone know why this CSS code is being strikethrough in the image below? (you can view it at https://i.sstatic.net/l ...

Is there a way to initiate animations seamlessly?

Instead of relying on a plugin, I decided to create a simple marquee from scratch. It was a fairly easy process and it works well overall. However, there is one thing that bothers me - the animation starts off slow and then speeds up. Is there a way to mai ...

How can you use JavaScript to create a JSON object using values from an HTML form textarea and then send

I need to create an HTML form that will send specific information in the Http-request body. { “info” : { “id” : “123” “text1” : <data from html text-box> } Therefore, my goal is to convert the provided data into a JavaScri ...

How can I align content to the left within a div that is right-aligned?

When I attempt this, the content aligns to the right as expected: <div class="text-right"> ... ... ... </div> I had hoped that this would result in left-aligned content in relation to itself, but right-aligned within the ...

Mysterious JQuery attribute

I've come across a piece of code that utilizes an unfamiliar selector that I can't seem to figure out: $("div[tag=" + someVariable + "]").css("display", "block"); From what I gather, the selector is searching for a div element with an attribute ...

Albania's Interactive Mapping Tool

I am interested in creating an interactive map similar to the one found at using jQuery and SVG, however I am not sure where to begin. I have the map saved as a .svg.xml file (available here: albania.xml) but I am unsure of what steps to take next. Can an ...

Trouble aligning buttons in Internet Explorer with center alignment

Check out this jsFiddle link using Internet Explorer and another major browser: http://jsfiddle.net/6bv7j/1/ Within the jsFiddle, click on the "Add Question" button to see a new row appear below. This row contains two buttons - one labeled "Select All An ...

Is there a way to lead to a password-protected page without making it accessible through the URL?

I'm currently honing my skills in web development and embarking on a project to create an interactive puzzle website. The premise is simple - the homepage will feature just an answer input field where users can enter the correct solution to progress t ...