What is the best way to adjust the spacing between posts on a website

https://i.stack.imgur.com/VderY.jpg

Looking at the image, there are 3 posts lined up in a row. I'm trying to adjust the spacing between each item to be about 20%. Specifically, I want the distance highlighted by the red dashes, instead of the current distance marked by the blue arrows.
I've been exploring within the .container and .post classes but haven't quite nailed it down yet.

This is the code snippet:

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.post {
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border-radius: 5px;
  overflow: hidden;
  margin-bottom: 40px;
  width: calc(33.33% - 20px); /* 3 posts per row */
  background-color: #fff;
  transition: background-color 0.3s, transform 0.3s ease;
  display: flex;
  flex-direction: column;
  height: auto;
}

.post:hover {
  transform: translateY(-5px);
  background-color: #f8f8f8;
}

.post:active {
  transform: translateY(0);
  background-color: #fff;
}

.anime-image {
  width: 100%;
  height: auto;
  flex-shrink: 0; /* Prevent image from shrinking */
}

.anime-details {
  padding: 20px;
  display: flex;
  flex-direction: column;
  flex-grow: 1;
}

.title {
  font-weight: bold;
  font-size: 18px;
  margin-bottom: 10px;
}

.rating {
  font-weight: bold;
  font-size: 14px;
}
<div class="container">
@foreach (var anime in Model.Animes)
{
  <div class="post">
    <a href="url">
      <div class="anime-image-container">
        <img class="anime-image" src="@anime.Image">
      </div>
    </a>
    <div class="anime-details">
      <p class="title">@anime.Title</p>
      <p class="rating">Рейтинг</p>
    </div>
  </div>
}
</div>

Answer №1

Have you heard of the masonry layout? Look it up on Google for more information.
Check out this example to see how it works:

CSS code:

html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}

.masonry {
  column-count: 3;
  column-gap: 1em;
  margin: 1.5em auto;
  max-width: 768px;
}

.item {
  display: inline-block;
  background-color: skyblue;
  width:100px;
  height: auto;
  padding: 1em;
  margin: 0 0 1em;
}

HTML code:

<div class="masonry">
  <div class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
  <div class="item">Some text here to fill in the gap</div>
  <div class="item">This text however is pretty longgg....... More random text to make height bigger. This is example.</div>
  <div class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
  <div class="item">Some text here to fill in the gap</div>
  <div class="item">Thger. This is example.</div>
  <div class="item">Lorem ipsum dolor sit amet, consectetur adipisicintetur adipisicing eg elit.</div>
  <div class="item">Some text here to fill in the gap</div>
  <div class="item">This text however is pret...... More random text to make height bigger. This is example.</div>
  <div class="item">Lorem ipsum dolor sit amet, conseclit.</div>
  <div class="item">Some text here to fill in the gap</div>
  <div class="item">This texexample.</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

Show a table with rows that display an array from a JSON object using JavaScript

My current code includes JSON data that I need to display in table rows, but I'm struggling to understand how to do so effectively. The output I am currently seeing is all the rows from the array stacked in one row instead of five separate rows as in ...

Clicking on AngularJS ng-click to navigate to a different page within an Ionic framework application

My goal is to navigate to another page when clicking on a button in the Ionic navbar at the top. However, I am encountering an issue where upon clicking, all nav bar buttons disappear. Interestingly, using dummy codes triggers an alert successfully. But w ...

In Angular/Typescript, dynamically add a class to a `td` element when it is clicked. Toggle the class on and off

My problem arises when trying to individually control the arrow icons for each column in my COVID-19 data table. By using Angular methods, I aim to show ascending and descending arrows upon sorting but run into the challenge of changing arrows across all c ...

CSS for Page Header with Scroll-Over Content

Having trouble creating a "collapsing header" design where the page content scrolls over the banner image. I've tried adjusting z-index and positioning properties in CSS, but can't achieve the desired effect. Any help or guidance would be greatly ...

Generate a unique identifier for the HTML content

Is there a more efficient way to assign unique id tags to certain words using vanilla javascript? For example: <p>All the king's horses ran away.</p> For instance, "All" would have the id "term1", "King's" would have "term2", and ...

Dynamic Website (Link Relationships / Responsive Design / Content Rendering with CSS and HTML)

Hello everyone! My name is Mauro Cordeiro, and I come from Brazil. I am just diving into the world of coding and have been following various tutorials. Stack Overflow has been an invaluable resource for me in my learning journey! Aside from coding, I am a ...

Having trouble with jQuery Animate when trying to change the background-color property?

So, I was experimenting with the jQuery .animate() function and decided to change the background color of a div based on how many pixels the user scrolled. Surprisingly, it didn't work as expected. After switching to the .css() function instead, every ...

Efficiently Aligning Elements in CSS both Vertically and Horizontally

I'm struggling to align my form both vertically and horizontally. It's okay if this doesn't work on IE - I will include an IE detection script to prompt users to switch browsers. However, Firefox and Chrome support is essential. Below is m ...

How about using a circle inset clip path technique?

Can a unique inset circle clip path be created to cut a hole through the center of a div instead of just showing the center? The entire div should be visible except for a hole cut out in the center to achieve a design similar to this: https://i.sstatic.n ...

Is there a way for me to achieve a vertical page turning effect on a single page?

I am seeking to develop a unique calendar display consisting of 12 images that give the illusion of flipping up when clicked. While I am aware of turn.js, my knowledge of javascript is limited and I need guidance on how to proceed. Despite having a program ...

The MUI component with hideBackDrop={true} is preventing me from clicking outside of the Drawer component to close it

I implemented a Drawer component which opens when an icon on the Map is clicked. The drawer menu appears along with a backshadow that drops over the map. Interestingly, clicking on the map while the backshadow is displayed closes the drawer without any i ...

"Utilize CSS Flexbox to create a layout with set width and uniform

Within a container, I have organized three sections. As I adjust the size of my browser to a maximum width of 668px, I want section 1 and section 3 to be displayed in one row while section 2 is placed below them. The width of section 2 should be proportion ...

Trouble encountered when trying to use an anchor link with the .slideUp jQuery function

Would you be able to assist me with understanding why my anchor links are not functioning correctly? I have 3 anchor links, for example: Google (not working) Yahoo (not working) Facebook (working) Any insights on why Google and Yahoo are not working? &l ...

CSS transitions/transforms are failing to impact the necessary elements

I'm currently grappling with advanced transitions and transforms, and encountering some challenges with a section I'm developing. I've managed to get everything working as needed, except for the fact that there is an h5 element positioned ov ...

Seeking guidance on utilizing JavaScript for implementing an onclick event

I am exploring the realm of Event tracking with Google Analytics. To make this happen, I know that I must include an onclick attribute to select links (those specifically requested for tracking) in the following manner: <a href="http://www.example.com" ...

Having trouble with the functionality of a simple jQuery toggle menu on mobile?

I am experiencing an issue with a simple toggle menu that utilizes jQuery's on: tap feature, but it is not functioning as expected: <nav id="mobile-nav"> <ul> <li>Item 1</li> <li>Item 2</li> ...

Challenges between Django and VSCode

As I dive into practicing website development with Django, I've encountered a problem while trying to save my work in VSCode and updating my local browser. It seems that only certain changes are being reflected. For example, when I delete code in my C ...

Positioning a fixed div within a responsive div using AngularJS and Bootstrap

I'm currently working on a page layout that consists of a fixed sidebar on the left side and a main container taking up the rest of the page on the right. Inside this right-side container, which is a div, there are 2 elements. <div class="col-sm-1 ...

Content displayed in the center of a modal

Struggling to center the captcha when clicking the submit button. Check out the provided jsfiddle for more details. https://jsfiddle.net/rzant4kb/ <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class=" ...

CSS - Placeholder styling not being effective when selectors are grouped

Why is Chrome successful at executing this code: .amsearch-input::-webkit-input-placeholder { color: red; } <input class="amsearch-input" placeholder="It works!" /> ...while it doesn't work in other browsers: .amsearch-input::-we ...