How to create a four-box grid with a specific right margin for every 4th child using CSS

What is the proper way to add a margin of 20px to every fourth list item in a four-box layout using CSS?

Answer №1

*{
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}

body{

}

h1,h2,h3,h4,h5,h6,p{
margin: 0 0 10px;
}

.clr::after{
content: "";
display: table;
clear: both;
}

.fleft{
float: left;
}

.fright{
float: right;
}

.txt_center{
text-align: center;
}

.txt_right{
text-align: right;
}

.list_inline li{
display: inline-block;
}

/* =========================== styling =========================== */
.container{
max-width: 1170px;
margin: 0 auto;
}

.grid_list li{
float: left;
width: calc(100% / 4);
margin: 0 0 20px;
}

.grid_list li .product_item{
border: 2px solid #ccc;
min-height: 300px;
padding: 10px;
}

.grid_list li:not(:nth-child(4n+4)) .product_item{
margin: 0 20px 0 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Repository</title>
<link rel="stylesheet" href="css/common.css">
</head>
<body>
<div class="container">
<ul class="grid_list list_inline clr">
<li class="item">
<div class="product_item">
<h3>Product heading</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est... etc.

`

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 is the best way to create a text input that is both secure and non-editable?

While I am aware of the <input type="text" readonly /> possibility, it is not entirely secure. For example, if there is an input field that must remain uneditable by users, making it "readonly" can still be bypassed by inspecting the code and remov ...

Adjust the transparency of a navigation bar independently from the transparency of the text within the navigation bar

Seeking assistance on adjusting the opacity of this navbar without affecting the text within it. Any tips on changing the text color as well? <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#" ...

Having trouble with CSS not displaying correctly on mobile devices, causing layout issues

Recently, I created a webpage that functions perfectly on all browsers except for mobile devices. While the design may appear unprofessional, it was purposely done for testing purposes. I suspect that the issue lies within the viewport settings in the CSS. ...

Is there a way to track the amount a user scrolls once they have reached the bottom of a page using Javascript?

It's a popular UI pattern on mobile devices to have a draggable element containing a scrollable element. Once the user reaches the end of the scrollable content, further scrolling should transition into dragging the outer element. For example, in this ...

Is there a way to customize the font size of Material UI Autocomplete?

I'm currently trying to customize the Material UI Autocomplete component with my own CSS. Specifically, I aim to adjust the font size of the input field. Below is my current implementation: <Autocomplete style={{ width: 200, height: 60, ...

The popup is unable to remain in the center of the screen because the background page keeps scrolling back to the top

Whenever I click a button to open a popup window, the page unexpectedly scrolls to the top. It's frustrating because if I'm at the bottom of the page and press the 'submit' button, the popup doesn't display in the center as intende ...

The power of CSS optimization: Benefits of using multiple files instead of just one

Is it true that fewer HTTP requests result in better performance? According to Google's best practice guidelines, reducing the number of unused CSS rules also improves performance. The browser's CSS engine must assess each rule in the file to ...

Embracing the flexibility of flexbox nesting

Flexbox has been a game-changer for me, and I can't help but want to incorporate it into all my projects since I first started using it. Curious Thought I'm wondering if nesting flex-boxes is considered best practice. Could having numerous, espe ...

Ways to make a div grow to occupy the leftover width without resorting to the overflow: hidden technique

<div id="container" style="width:100%;"> <div id="left1" style="float:left; width:100px;">float left</div> <div id="left2" style="float:left; width:100px;">float left</div> <div id="remaining">Remaining width&l ...

The skipping of crucial text in tab focus is adversely affecting the accessibility of the website

My appbar contains an image link, a warning indicating the user's current environment, and details about the logged-in user. While testing accessibility with a screen reader, I noticed that tab focus skips over the user details in favor of a link in ...

CSS - Adjusting width based on height ratio

I'm in the process of developing a website and could use some guidance. I have a specific section where I am aiming for the height to be 79.6% of the body, with the width of that div being dependent on its height. Thus far, my research has only yielde ...

Increase the font size of the navigation bar on smaller screens

On smaller screens like mobile devices, I am having trouble increasing the font size of the navigation bar menu items. While it displays well on desktop screens, the font size becomes very small and difficult to read when the screen collapses to a mobile v ...

What is the best way to adjust the size of the footer container?

Utilizing Bootstrap to practice coding as a beginner has been a learning process for me. Recently, I encountered an issue with the container size of the footer. view image description here Upon inspecting the photo in the link, I realized that the foote ...

CSS - Is there a way to alter the arrangement of DIVs depending on the size of the screen?

My inquiry pertains to a CSS layout. I currently have 3 divs positioned in a horizontal line next to each other, as depicted below... div1 div2 div3 I am aiming for the divs to reorganize themselves as the screen size decreases. In one scenario, they sho ...

Performing the addition operation on two floating point numbers in JavaScript

The JavaScript code goes as follows: var receivedamt = parseFloat($('#cashRecText').val()).toFixed(2); console.log(receivedamt); var addon = parseFloat('5.00').toFixed(2); console.log(addon); addon = parseFloat(receivedamt).toFixed(2 ...

Neglecting Margins: The Challenge of Specificity

While utilizing the Foundation 4 framework, I've encountered an issue where the margins are being overridden by the framework's default settings. This situation forces me to use the !important keyword to apply margins to specific elements. Below ...

Incorporating a CSS drop-down menu that appears or changes upon clicking

I am in the midst of designing a webpage with a main div that is 1000px by 1000px. Within this main div, there is a horizontal bar located at the top, divided into four sections - each occupying 1/4 of the space. In the center of each section, there is tex ...

Tailored Bootstrap form styles for different screen sizes

Can someone please help me understand how to create a custom Bootstrap class that applies to a specific breakpoint? I've tried using w-md-50, w-sm-100, w-lg-75 for a form, but it doesn't seem to be working for the targeted breakpoint. Are there a ...

What is the reason for having a space between the <body> and the <html> tags?

Can you explain why there is a gap between the top of the green box and the browser window in Chrome and Firefox? I've tried various CSS configurations to eliminate the apparent space between the html and body elements, but it seems that some padding ...

What is the best way to format log lines in a GWT custom logging section?

In GWT, a personalized logging area can be created by implementing a HasWidgetsLogHandler. In my situation, I am using a VerticalPanel. However, the logging output is too wide and distorts the rest of the page. How can I wrap the lines? Each log entry add ...