Creating a versatile four-column design using CSS

I am looking to retrieve data such as title, number, and date and display them in green boxes on my website. I want these boxes to be arranged in a single line, similar to the layout shown in this screenshot.

While ideally each line would contain 4 items, due to the dynamic nature of the number of boxes, I may only have two boxes per line.

It is important to me to maintain even spacing between the boxes, even when there are only 2 boxes present. Using { justify-content: space-between } does not work for me as it causes one box to stick to the right side of the page and the other to stick to the left. Additionally, using { margin-right } or { margin-left } is not suitable because when 4 boxes are present, I want them to align with the sides as they would with { justify-content: space-between }

Any assistance you can provide on this matter would be greatly appreciated.

PS: I primarily code in reactjs

Answer №1

To replicate the layout shown in the screenshot, you can utilize the CSS property display: grid. This method allows you to specify the size of each column relative to one another:

.boxcontainer{
  display: grid;                          /* Utilizes grid layout for columns */
  grid-template-columns: 1fr 1fr 1fr 1fr; /* Sets proportions of the columns */
  gap: 20px;                              /* Gap between elements instead of margin in .box */
}
.box{
  width: 100%;    /* Occupies full width of each column */
  height: 200px;  /* Can be adjusted as needed */
  background-color: red;
}
<div class="boxcontainer">
  <div class="box">1</div>
  <div class="box">2</div>
  <div class="box">3</div>
  <div class="box">4</div>
</div>

Answer №2

To achieve your desired layout, utilize display:flex; and flex-wrap:wrap; for the Container while also applying padding:

.container{
  width:1000px;
  display:flex;
  padding:10px;
}
.box{
  width:200px;
  flex-wrap:wrap;
  height:200px;
  background-color:red;
  margin:20px;
}
<div class="container">
  <div class="box">1</div>
  <div class="box">2</div>
  <div class="box">3</div>
  <div class="box">4</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

The potential security threat posed by displaying a user's HTML content on a webpage restricted to only their viewing

Users can input any type of HTML, including JavaScript, into a textarea on my website. They can then preview their input in a browser by submitting it to a PHP backend that displays the content in a template. I understand the risks of XSS, but I only allow ...

Sending a parameter as text from .NET code-behind to a JavaScript function

So here's the situation: I have a gridview that is displayed in a new window separate from the parent window. This gridview contains multiple records, each with a "view" button that allows users to see more details about the record within the same new ...

Troubleshooting Issue: jQuery Hide/Show Functionality Not Behaving as Expected Inside If

Trying to create a jQuery slideshow but encountering some difficulties. The issue lies with a variable that should hide or show images based on the navigation buttons pressed. However, only img1 is showing and none of the other images are displaying. Des ...

Secure strings by utilizing an array of characters

I have a basic JavaScript array that contains characters. array('p','(',')','?'); I need to escape strings in JavaScript based on the values in that array. How can I achieve this? For instance, if the string is = ...

Revise the div to be clickable

I am looking to trigger a click event on a replaced element. var checks_int = "1"; $(function () { var plus = $('.fa-plus'); plus.click(function () { if (checks_int <= 5) { $(this).parent().parent().append("<li ...

What is the best way to transfer values from a nested child component up to the parent component

In my component hierarchy, there is a parent component with a child component that further contains another child component. Parent |-Child One (child of parent) |-Child Two (child of Child One) Currently, I am passing a value from Child Two to Child On ...

Executing a shell command prior to the ENTRYPOINT in the dockerfile: Tips and guidelines

My nodejs project includes the following file: FROM node:boron # Create app directory RUN mkdir -p /usr/src/app WORKDIR /usr/src/app # Install app dependencies COPY package.json /usr/src/app/ RUN npm install # Bundle app source COPY . /usr/src/app # ...

What is the best way to set a boolean value for a checkbox in a React project with Typescript?

Currently, I am working on a project involving a to-do list and I am facing an issue with assigning a boolean value to my checkbox. After array mapping my to-dos, the checkbox object displays 'on' when it is unchecked and a 'Synthetic Base E ...

Is it possible to upload numerous profiles into the MYSQL database using this WP template?

Apologies in advance if my question is unclear, but in simple terms, I am looking to automate the process of uploading hundreds of profiles and jobs to this WP template: The developer of this template has stated that it is not possible to do this through ...

Is there a way for me to automatically retrieve lost data following a POST request?

Is there a way to automatically retrieve data from the server after submitting a form via POST in the browser? I have an HTML form The form's data is sent to a PHP function like saveRecord() The PHP code stores the data in a MySQL database If the da ...

Creating HTTP-only cookies within a NextJS middleware

Having an issue with updating access token in my middleware when the current one expires; here's my approach: Generate a new token Send it to an API route which adds the token to its response header. The problem arises when the middleware receives t ...

React Bundle Error in Webpack

I rebooted my MAC and encountered some errors... After Babel switched to Babel-core, I uninstalled it and updated the loader in webpack to babel-core. Now, when I execute npm run bundle, the following errors occur: /usr/local/lib/node_modules/webpack/no ...

In a VueJS project, access an xlsx file stored in the public directory by reading its contents

My current challenge involves trying to extract a quiz template from an xlsx file in order to create the quiz within it. Unfortunately, storing the xlsx file as json in a database is not a feasible solution for me at this time. I experimented with using ...

Managing the status (e.g. 503) for Axios OPTIONS response

Note: mentions that there may be no need to worry as the issue might not occur on an actual device. In my VueJS hybrid (Cordova) app, I am utilizing Axios to make API calls. Axios correctly initiates a 'preflight' OPTIONS request before my GET ...

Exploring External Functions in Angular Beyond the Library

Transitioning from standard JavaScript to Angular has been a bit challenging for me, especially when working with the Google Places library (or any other asynchronous callback). Here is the code snippet: var sparkApp = angular.module('sparkApp' ...

How can I improve the organization of my NPM scripts for better readability?

My primary testing process tool has become NPM scripts. However, as I continue to create more NPM scripts, the order and structure are becoming increasingly difficult to quickly interpret. Below is a snapshot of my current scripts: { "scripts": { ...

Displaying a profile hover card allows users to easily discover and choose whether to follow or explore

I created a profile hover card on my website with a follow/unfollow button. However, I encountered an issue - the hover card disappears when I move away from my profile thumbnail. How can I fix this so that the hover card stays visible on mouseover and dis ...

Which is more effective for a webpage that solely calculates a formula: user input fields in DIVs or forms?

My idea is to create a webpage with initially 6 input fields, which can expand to 7, 8, 9, or 10 fields when a button is pressed. These fields will have editable preset values that will be used to calculate a final value using a formula. Should I use f ...

Tooltip not appearing in designated spot as designed

I seem to be having an issue with the positioning of my custom tooltip on a graph. The arrow mark in the tooltip is not displaying where it should be. Can anyone help me figure out what I'm doing wrong? If you'd like to see exactly what I mean, ...

Would like a modal to appear upon clicking a cell within a grid

I want to trigger a Bootstrap modal to appear when a cell in my grid is clicked. The modal has already been created, but I'm unsure how to make it pop up when a cell is clicked. <div class="container"> <div class="row"&g ...