What is the best way to ensure consistent spacing between columns in a Bootstrap 3 grid layout, similar to the spacing on the left and right sides?

Is there a way to achieve equal spacing between all blocks in a row, similar to the left and right padding that Bootstrap automatically applies? Currently, Bootstrap adds padding to all columns on both sides, but does not render extra spacing on the left side of the first column and right side of the last column in a row due to the following CSS:

.row {
margin-right: -15px;
margin-left: -15px;}

This results in double spacing within the row which may not be desired in some cases.

I came across a snippet called .no-gutter that removes all padding, but I specifically do not want all padding removed:

.no-gutter > [class*='col-'] {
    padding-right:0;
    padding-left:0;
}

I attempted to solve this issue by adding padding-left:0 to the second column. However, this also removes the padding for mobile view (when all columns take up the entire width). While I can address this with media queries for each breakpoint, I am wondering if there is an existing Bootstrap snippet that can handle this scenario. I would also like to hear from others who have tried to achieve this.

I have created a test case on JSFiddle - http://jsbin.com/wucaho/1/edit?html,css,output

This screenshot illustrates the issue:

Answer №1

Although this post is old, it was incredibly helpful to me and hopefully will assist others in the future.

.no-gutter-left > [class*='col-'] {
    padding-left:0;
    padding-right:6px;
}

.no-gutter-right > [class*='col-'] {
    padding-right:0;
    padding-left:6px;
}

To implement this styling, use .no-gutter-left for the first field (left) and apply .no-gutter-right to the second field (right).

<div class="form-group no-gutter-left">
    <div class="form-group col-sm-6">
        <label class="sr-only">First Name</label>
        <input type="text" class="form-control" placeholder="First Name" name="fname">
    </div>
</div>

<div class="form-group no-gutter-right ">
    <div class="form-group col-sm-6">
        <label class="sr-only">Last Name</label>
        <input type="text" class="form-control" placeholder="Last Name" name="lname">
    </div>
</div>

If you require 3 columns within a row, consider adding a ".no-gutter-both" class with 3px padding on both sides.

Answer №2

To clarify, you can enclose the row within a personalized container named "wrap".

.wrap {
    padding: 0 15px;
}

Thus, the structure would appear as follows:

<div class="wrap my-custom-wrapper">
    <div class="row no-gutter">
        <div class="col-sm-4">...</div>
        <div class="col-sm-8">...</div>
    </div>
</div>

Refer to the visual representation provided below:

Answer №3

Update: In order to achieve the desired layout for smaller screens, it is necessary to implement media queries. While there isn't a straightforward solution available, even the developers behind the framework utilize media queries.

To address this issue, include the following code snippet at the end of your CSS file:

@media only screen and ( max-width: 479px ) {  
  .no-left-pad { padding-left: 15px; }

  .no-gutter > [class*='col-'] {
     padding-right: 15px;
     padding-left: 15px;
  }
}



Additional Note:

Alternatively, JavaScript libraries can be employed to detect mobile devices or small screens. By adding specific classes to the HTML or body tag upon page load, such as 'mobile' or 'small-screen', these classes can be used as substitutes for media queries.

Answer №4

To address the issue of gutters in a specific column, I created a custom class and applied it to that column:

[class*='col-'].gutterless {
    padding-right:0;
    padding-left:0;
}

Here's an example of how I implemented this solution in my markup:

<div class="row">
  <div class="col-sm-3"> left box </div>
  <div class="col-sm-9 gutterless"> right box </div>
</div>

If you're looking for a demonstration, you can check out this example: http://jsbin.com/dovag/1

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

Connecting CSS styles and images to my EJS templates on a distant Express server using Node.js (Ubuntu 18.04)

I have been hunting for a solution here extensively, but every attempt has failed. So, here's the issue I'm facing: I have created a basic Express server locally to display a static page until I finish full integration. The structure of my site ...

Displaying dropdown options based on the previous selection made by the user

Can I link the data in my second dropdown to the selection made in the first dropdown? I tried a similar solution from stackoverflow without success. You can find the reference here. The code works when directly copied and pasted but not within my system. ...

Tips for sending parameters to a web service URL through a Phonegap HTML page

In my Phone Gap application, I have an HTML page where I need to pass the values like name, contact, etc. to a Webservice URL. However, I am unsure of where to write the code and how to call the webservice URL. Here is a snippet of my HTML code: <div ...

Can someone please clarify how the nth-of-type selector functions?

I can't understand why the nth-of-type selector needs to be set to 2 instead of 1 for the first sibling of this type in the code. To see the code in action, check out this jsfiddle: https://jsfiddle.net/xj5hvn16/ If anyone could kindly provide an ex ...

Generate real-time dynamic line charts using data pulled directly from a MySQL database

I am in search of guidance on developing a real-time line chart that can dynamically update based on data fetched from MySQL. I need an example or reference on how to achieve this functionality without having to refresh the webpage every time new data is ...

Use JavaScript to create a new window and load the HTML content from an external URL

Just starting out with HTML and Javascript I'm trying to use JavaScript to open a window and load content from an external source. I attempted using document.write(), but it only works when I hardcode the HTML as input. Any suggestions on how to get ...

Having trouble with enter key input not triggering?

I have scoured various resources for answers to my query, including Stackoverflow. Unfortunately, none of the posts I came across have helped me resolve my issue with getting the enter key to work in my project for FreeCodeCamp on Codepen. When I press the ...

Exploring the world of using Bootstrap containers, rows, and columns

I have a good grasp on containers and columns, but I am a bit confused about rows. The example below shows a 50/50 split. <div class="container"> <div class="row"> <div class="col-md-6"> 50 / 50 Content </ ...

assigning attributes to web addresses

Is there a way to set a style property for webpages by targeting addresses that contain /index.php/projecten/ instead of specifying each complete address in the code? Currently, I am using the following code: <ul class="subnavlist" style="display: &l ...

What are the best methods for detecting devices in order to create customized CSS styles for iPad and Galaxy Tab?

Ensuring my web application is compatible with various devices is crucial. While I have created a common CSS for all mobile and tablet devices, it seems to be causing some problems specifically on the iPad. After finding a fix tailored for the iPad, I now ...

My React component seems to be unable to locate and process my CSS file

I am encountering an issue where my React Component is not recognizing my CSS file. Here is the code snippet from my React component: import React, { Component } from 'react'; import './Jumbotron.css'; class Jumbotron extends Compone ...

An unexpected error has occurred in the browser console: The character '@' is not valid

I recently made the decision to dive into learning about Unit Testing with JavaScript. To aid in this process, I started using both Mocha.js and Chai.js frameworks. I downloaded the latest versions of these frameworks onto my index.html from cdnjs.com. How ...

Opening a new window in JavaScript and passing additional parameters

I am facing an issue with my JavaScript link There is a hidden input field named "domain" Below is a div with an onclick script: <div id="button" onclick="window.open('http://www.mylink.nl/?domein=' + document.getElementById('domein&ap ...

Is it possible to create a masonry-style layout with two columns that is responsive without

Is there a way to organize multiple divs of varying heights into two columns that display immediately one after the other, without relying on JavaScript or libraries like packery or masonry? I've experimented with using display: inline-block in this ...

Managing active dropdown menus in VueJS

I'm having trouble figuring out why my navigation menu and method to open subitems on click are not working correctly. [![dropdown_menu][1]][1] new Vue({ el: '#app', data: { //menu "menu_title": "a", "child_ro ...

jQuery wrapAll issue

I have a repeating group of three divs in my code that I need to wrap together. Here's an example from my HTML: <div class="one" /> <div class="two" /> <div class="three" /> <div class="one" /> <div class="two" /> <d ...

Why won't my JavaScript addEventListener activate on form submission?

I have a basic question as a beginner in JavaScript. I am facing some challenges with my first task involving JavaScript. I decided to learn JS by creating a TODO List, but I am stuck right at the beginning. The event listener that should respond when the ...

Having trouble positioning a specific element at the top right of the header navbar in Laravel Bootstrap 5

Is there a way to create a top navbar with the project name and menu on the left, and user management on the right? Currently, both options are aligned to the left. Here is an example of what I have: top navbar This navbar is placed in the header, and her ...

Detecting coordinates (x, y) on a canvas

I'm currently working on a mini-game and encountering an issue with the player movement around the green square. My character seems to be unable to move past the x, y coordinates of the square, even though it can approach it closely. I would really ap ...

When utilizing the build command in TailwindCSS, the resulting output file appears to be missing any content

I recently attempted to use TailwindCSS on my MacOS system. To install it, I used the command: npm install -D tailwindcss Following that, I generated the configuration file by executing the command: npx tailwindcss init I then proceeded to customize my ...