What is the best way to separate colored boxes vertically while using width and padding in CSS?

I'm new to WebDev, so please bear with me as I explain my issue. I've created colored squares on a website using CSS properties – 70% width, 0 height, and 70% padding. These squares form a box grid, each containing an h2 element styled as follows:

  color: $white;
  position: absolute;
  bottom: -3%;
  margin-left: -0.5%;

The styling for the boxes themselves is as follows:

  background-color: $primary;
  width: 70%;
  height: 0;
  padding-bottom: 70%;

Currently, these boxes align perfectly in terms of height, thanks to Bootstrap's three boxes per row setup. However, I'm struggling to add vertical spacing between each box.

I've attempted to use margin, but it interferes with the positioning of the h2 element and doesn't allow me to move it further down. Padding also doesn't seem to create the desired effect.

My objective is to introduce a gap between each box vertically and include a single line p tag within that space. Here is a snippet of the source code:

.box-1-1 {
  background-color: $primary;
  width: 70%;
  height: 0;
  padding-bottom: 70%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.min.js" integrity="sha512-3dZ9wIrMMij8rOH7X3kLfXAzwtcHpuYpEgQg1OA4QAob1e81H8ntUQmQm3pBudqIoySO5j0tHN4ENzA6+n2r4w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<div class="col-4">
  <div class="box-1-1">
    <h2 class="boxes">RDA EXPO</h2>
  </div>
  <div>
    <p>Lorem ipsum dolor sit amet</p>
  </div>
</div>

I have repeated this process 12 times, creating three boxes per row.

Answer №1

If you're looking to enhance your layout designs, I highly suggest exploring the grid system.

div {
    display: grid;
    grid-template-columns: 1fr 1fr;
    border: 2px solid yellow;
}

I updated your CSS with this code, which surrounds both divs with a box and aligns them side by side. Simply using empty p tags for spacing won't achieve the desired effect. It's crucial to grasp fundamental CSS concepts to create your desired layouts - whether it's float: left for traditional methods, display: flex for newer techniques, or display: grid for cutting-edge approaches.

  1. Floats
  2. Flexbox
  3. Grid
  4. All three

Understanding the basics of these layout tools will significantly enhance your CSS proficiency. Furthermore,

How can I add vertical space between each box?

If you opt for grid or flex, utilize gap properties to introduce spacing between elements vertically and horizontally. While floating elements may be trickier, there are methods to vertically center and position them. Best of luck!

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 max-height property is not applied to a border-box div with padding

One technique we use is the percentage trick on paddings to maintain aspect ratio in a div as the user scales their window. Here's an example: .div { background: red; width: 80%; margin: 0 auto 10px; -webkit-box-sizing: border-box; ...

Title: "Customizing Labels on Stack Bars and Lines in D3.js Visualization"

Currently working on a stacked bar chart with a line chart on dual axis using D3.js and facing difficulty aligning labels correctly. Check out the code I have experimented with so far: https://plnkr.co/edit/doobXBC5hgzvGwDLvArF?p=preview I am looking to ...

Unfortunately, the jquery Calendar plugin is malfunctioning

After much consideration, I decided to implement this calendar plugin on my website Plugin Unfortunately, it isn't working as expected. I am encountering the following error: $(...).pignoseCalendar is not a function at HTMLDocument. (Index ...

Alternative background for browsers that do not have support for border-image styling

I am experimenting with CSS3 border-image to create a simple button design: the left slice of the image is intended to be the left border of the text, the right slice as the right border, and the middle slice should either repeat or stretch as a background ...

Utilize a CompareValidator to evaluate conditions only when necessary

I am dealing with a checkbox containing a javascript function that triggers onclick to toggle the visibility of a div. Within this div, there is a textbox with a CompareValidator that validates the input as a Double. The issue arises when the checkbox is ...

How to Handle Empty Input Data in JQuery Serialization

Having an issue with a form that triggers a modal window containing another form when a button is clicked. The second form includes an input field and send/cancel buttons. The goal is to serialize the data from the modal form and send it to a server using ...

Ensuring input boxes are neatly positioned underneath one another, regardless of the chosen font size

Is there a way in CSS to ensure that these input boxes are always aligned below each other, regardless of font family, size, or window movement by the user? https://i.stack.imgur.com/Zbadh.png Currently, I am using the following approach: tab1 { paddi ...

Tips for changing the default height of Elastic UI dropdown menus

I am attempting to adjust the height of the Task combobox to match that of the Date Picker, but the styling is not being applied when done through a class or inline. https://i.sstatic.net/3Cua1.png I have tried inline styling the element as: <EuiForm ...

Guide to Automatically Transform Markdown (.md) to HTML and Display it within a Designated <div> Container using Node.js alongside markdown-it Library

I am currently working on a unique project where my client specifically requires the ability to input Markdown (MD) content into a .md file and have it dynamically converted into HTML. The resulting HTML must then be displayed within a designated element i ...

Apply the CSS style specifically to the initial row in the table

Is there a way to target the first row of a table with a different tr class using CSS? <div id="right"> <table> <tbody> <tr class="head"> <td >Date</td><td>Info</td><td>More</td> </tr> ...

Acquiring a compilation of contributions from a fundraising effort on GoFundMe

My quest for a list of donations on a GoFundMe campaign has taken me on a complicated journey. I initially hoped to access this information through an API, but it seems that GoFundMe does not offer one. This led me to explore the option of web scraping the ...

Learn how to iterate over an array and display items with a specific class when clicked using jQuery

I have an array with values that I want to display one by one on the screen when the background div is clicked. However, I also want each element to fade out when clicked and then a new element to appear. Currently, the elements are being produced but th ...

What is the best way to incorporate a hover effect on a header using HTML5?

I'm new to UI and web development and I'm trying to create simple web pages. I found the http://ionicframework.com/getting-started/ page and I'm attempting to replicate it in my demo application. I've divided my page into three parts: h ...

Customizing CSS styles for specific elements within a text area field in WordPress

Embarking on my coding journey, I have completed online courses in HTML, CSS, JavaScript, mySQL, and PHP. Now, I am eager to enhance my skills through practical application while creating a Wordpress theme. Currently, I am delving into custom fields. I ha ...

Angular is utilized to create a dynamic multiple slide carousel that features repeating items

There is a problem with the carousel I am working on where the items are duplicated after resizing the screen. The code I am using is based on Eliseo's solution from this Stack Overflow thread. In his carousel, the arrow functions show/hide depending ...

Bootstrap: Retrieve an image from a modal

I am working on a modal that contains a variety of selectable images. When an image is clicked, I want to change the text of the button in the modal to display the name of the selected image. Additionally, I would like to grab the selected image and displa ...

Using a `right: 0` value will not be effective when using absolute positioning

Currently, I am in the process of developing a website utilizing Bootstrap 3.3.6. After going through a tutorial on how to create a responsive banner slider, I found this resource helpful: http://www.jqueryscript.net/slider/Full-Width-Responsive-Carousel- ...

"Need guidance with jQuery and CSS layout alignment on the

So here's the scenario I'm dealing with: <div id="tabs"> <div id="tab_one"> Content for tab one </div> <div id="tab_two"> Content for tab two </div> <div id="tab_three"> ...

Tips on showcasing lengthy string content from JSON in a structured list format using react with typescript

Is there a way to display long string data from a JSON file in a list format when the description string is too lengthy? How can I split or format the description using React and TypeScript? The JSON data I have is as follows: "options": [ ...

Achieving compatibility between angular-bootstrap modal and angular-1.3

Wondering if anyone has managed to successfully use angular-bootstrap modals with angular-1.3.x? I've had no issues with my modals on angular-1.2.x, but as soon as I upgraded to angular-1.3.11, the modals stopped showing up. When I click the button ...