Border added to Bootstrap layout with 5 columns

Trying to figure out the best way to create a 5 column layout in Bootstrap with borders and spacing.

https://i.sstatic.net/CG5MX.jpg

A custom class has been created to accommodate the 5 column grid setup:

.col-xs-15,
.col-sm-15,
.col-md-15,
.col-lg-15 {
    position: relative;
    min-height: 1px;
}
.col-xs-15 {
    width: 20%;
    float: left;
}

@media (min-width: 768px) {
.col-sm-15 {
        width: 20%;
        float: left;
    }
}
@media (min-width: 992px) {
    .col-md-15 {
        width: 20%;
        float: left;
    }
}
@media (min-width: 1200px) {
    .col-lg-15 {
        width: 20%;
        float: left;
    }
}

<div class="item col-md-15">
  <div class="item-wrap">
   .....
  </div>
</div>

The goal is to have a 10px margin on the right of each column (except for the last one) and apply a 1px border to each column or item-wrap.

Despite multiple attempts, the margins are not showing up as intended.

.item {
  border: 1px solid #efefef;
  padding:10px;
}
.item.last {
  margin-right: 0;
}

Check out the fiddle

Answer №1

In the realm of native Bootstrap, 5, 7, and 9 column layouts face a bit of a challenge due to the default 12 column structure not being evenly divisible by those numbers. If you're aiming for a 5 column layout, the solution lies in adjusting the @grid-columns value on http://getbootstrap.com/customize/#grid-system to a number like 15 (or any multiple of 5).

Once you've personalized and downloaded your altered version of Bootstrap, crafting a 5 column layout can be achieved with ease:

<div class="col-xs-3">Column 1</div>
<div class="col-xs-3">Column 2</div>
<div class="col-xs-3">Column 3</div>
<div class="col-xs-3">Column 4</div>
<div class="col-xs-3">Column 5</div>

This method eliminates the need to tinker with CSS in order to replicate existing Bootstrap classes and aesthetics. However, proceed cautiously as this adjustment may impact any pre-existing column-based layouts.

Answer №2

To create spacing between all columns, you can utilize pseudo classes like this:

.item:not(:last-child) {
    margin-right:10px;
}

This code will apply a margin to the right of all elements with the .item class, except for the last element. You can see the updated fiddle here.

Answer №3

When bootstrap-4 was released, it brought a plethora of new features and changes. One significant change was the shift away from floats and percentages in favor of using flex-box for layouts in bootstrap-4.

Bootstrap-4 relies heavily on the flex-layout.

In instances where the standard 12-grid layout of bootstrap-4 doesn't quite fit our needs, we have the option to utilize customized CSS (utilizing flexbox properties) to create responsive layouts with any number of columns (such as a 5 column layout). Flexbox provides the versatility needed to achieve this. Below is the code I've prepared to demonstrate this concept.

HTML5:

<!DOCTYPE html>
<html lang="en>
 <head>
   <meta charset="UTF-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <meta http-equiv="X-UA-Compatible" content="ie=edge" />
   <link rel="stylesheet" href="./style.css" />
   <title>5 column layout in flexbox</title>
</head>

<body>
 <main class="flex-container">
  <div class="flex-item col1">col1</div>
  <div class="flex-item col2">col1</div>
  <div class="flex-item col3">col3</div>
  <div class="flex-item col4">col4</div>
  <div class="flex-item col5">col5</div>
 </main>
</body>
</html>

CSS:

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

body {
 padding-top: 1rem;
}

/* --- This much code will do it for you --- */
.flex-container {
  display: flex;
 }

.flex-item {
  min-height: 400px;
  border: 4px solid #03a503;
  margin: 0 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
}

.flex-item:first-of-type,
.flex-item:last-of-type {
  margin-right: 1rem;
}

 /* ----- Basic break-point ----- */
@media screen and (max-width: 768px) {
 .flex-container {
   flex-direction: column;
 }

.flex-item {
  min-height: 100px;
  margin: 0.5rem;
}
.flex-item:first-of-type,
.flex-item:last-of-type {
   margin: 0.5rem;
 }
}

Access the complete code at this link: https://jsfiddle.net/cLmq0otv/3/

I hope this solution resolves your issue.

Answer №4

To create a responsive layout with multiple columns in Bootstrap, you can utilize the Auto-layout columns feature which allows you to specify column classes without specific numbers. This enables you to use predefined grid classes, grid mixins, or inline widths to define the width of a single column while letting the sibling columns adjust automatically.

.col{
padding:1rem;
}
.col div{
  border:1px solid red;
  min-height:5rem;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="row">
    <div class="col">
      <div>content column 1</div>
    </div>
    <div class="col">
      <div>content column 2</div>
    </div>
    <div class="col">
      <div>content column 3</div>
    </div>
    <div class="col">
      <div>content column 4</div>
    </div>
    <div class="col">
      <div>content column 5</div>
    </div>
  </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

Display or conceal all sections based on selected dropdown options within the Elementor plugin

I have a dropdown menu with 9 different sections underneath 1. Section id=Hige x 3 2. Section id=Medium x 3 3. Section id=Low x 3 My goal is to show only the sections that correspond to the selection made in the dropdown menu. I am using jQuery to achi ...

Top Choice for Customizable Location Bar Dragging

I'm currently working on an app that features a draggable location bar. This feature will allow users to navigate through chapters in a book and will be displayed at the bottom of the screen, similar to Kindle or other mobile reading applications. I ...

Using the Chrome WebDriver for Selenium to interact with a text input element with dynamically changing IDs

I seem to be encountering difficulties with accessing an input element on a specific webpage. If the link has timed out, you can click on this following link: and then select the "RL_reactors" hyperlink to navigate to the page in question. On this page, ...

The combination of Chromium, raw HTML pulled directly from a C# string, and Angular leads to issues when trying to implement routing

CefSharp: 1.25.0 (Chromium 25.0.1364.152-based) Angular: 1.3.0-beta16 UIRouter: 0.2.10 Currently, I am in the process of creating a C# application that will function as a standalone program using CefSharp Chromium, Angular, and UIRouter for the graphical ...

Interpolation within ngStyle allows dynamic styling to be applied in

When creating a column component for use within a grid in Angular, I am struggling with implementing a media query using ngStyle. Here is what my code looks like so far: import { Component, Input } from '@angular/core' const smMin = '48em& ...

What is the best way to showcase JSON data while incorporating HTML tags in a React application?

My JSON data contains HTML tags interspersed within it and I have successfully fetched and displayed the data. See the code snippet below: componentDidMount() { this._callCake(); } _renderEachCake = () => { return ( <EachCake ...

Difficulty filling height with Bootstrap 4 flex-grow

I am in need of creating a layout with a header div at the top (that can be filled with content), a footer div at the bottom, and a middle div with an adaptable height to fill the entire screen. Check out my code below: <div class="container-fluid p-0 ...

Switching the chosen option in the <select> element repeatedly

I have successfully set the selected value, but now I am wondering how to keep changing it continuously. Initially, my approach was to remove the "selected" attribute from all options and then add it back to the desired option. Surprisingly, this method w ...

Generate an HTML document from a website page

Having difficulty grasping this concept. I am completely lost on where to begin. It is imperative that I determine how my website can generate a file (whether it be HTML or Text format) and enable users to download it, similar to the functionality in Goo ...

How to extract text from an HTML element using Selenium

In the following HTML snippet, I need to extract the text of elements 1 and 2 individually. <div class="sc-492bf320-0 sc-7d450bff-9 crIwBV juiSXn"> <div data-change-key="homeScore.display" class="sc-18688171-0 sc-7d450bf ...

Troubleshoot and resolve the issue of a page scroll freeze bug occurring while scrolling through an overflowed

My webpage features a Hero section with 2 columns - the left column contains a gallery slider, and the right column consists of 2 text blocks. The challenge here is that the right column needs to be 100% of the screen height while scrolling. To achieve thi ...

Node.js is experiencing difficulties loading the localhost webpage without displaying any error messages

I am having trouble getting my localhost node.js server to load in any browser. There are no errors, just a buffering symbol on the screen. The code works fine in VS Code. Here is what I have: server.js code: const http = require("http"); const ...

Deactivating a widget on one of my web pages

I'm looking to disable a widget on certain pages. Does anyone know how to do this? One idea I had was to add a condition in the HTML (using Django or PHP?) of the widget that would make it only appear if the site URL is not the one where I don't ...

AngularJS directive for incorporating additional directives lacks functionality

My current project involves creating a directive that will add form groups to a specific div. I'm working on this by linking the directive to a button in my html code. The functionality I am trying to achieve is quite simple, akin to what is shown in ...

Animate a div to sense whether it has reached the top or bottom position

Is it possible for a div to animate when it reaches almost halfway while scrolling? I'm looking to achieve this effect on a sticky sidebar, similar to how it works on this website: sample This is the code I have: $(function(){ // document ready ...

Ensure that jQuery waypoints become unstuck upon reaching the footer area

Currently, I have successfully implemented jQuery waypoints for a sticky social media navigation. The only issue is that when the footer element is reached, the sticky navigation continues to scroll. My ideal scenario would be for the sticky nav to remain ...

I am looking to implement a dropdown menu that appears after clicking on an image. Any suggestions on how to achieve this using

I've been experimenting with adding a dropdown class, but I'm feeling a bit lost on where to start. Here's a snippet of code that shows what I'd like to do to add a dropdown menu: <span id="dropdown-info" ng-init= "myVar='i ...

A guide to generating an XML file on a user's device through JavaScript or jQuery

Is there a way to generate an XML file on the client's machine using JavaScript or jQuery? I am looking to write an XML file from my web application directly onto the client's machine. What would be the best approach to achieve this across multip ...

send css as a parameter to the component

Could it be possible to develop a component with specific CSS attributes? I am aware that the following approach is not valid. <template> <div id="textContainer"> {{ content }} </div> </template> <script> export defa ...

Is it possible to modify a scss style in Prestashop?

I have encountered an issue while using a default theme in Prestashop 1.6 that I need assistance with. My goal is to move the navbar up by 25px. I understand that I should make changes to #block_top_menu { padding-top: 25px; } in blocktopmenu.scss, which ...