Adjusting the top margin of columns in Bootstrap 3 for smaller screens

I am working with a row that has the potential for multiple columns.

<div class="container">
        <div class="row">
            <div class="col-lg-3 col-sm-4 col-xs-6">content</div>
            <div class="col-lg-3 col-sm-4 col-xs-6">content</div>
            <div class="col-lg-3 col-sm-4 col-xs-6">content</div>
            <div class="col-lg-3 col-sm-4 col-xs-6">content</div>
            <div class="col-lg-3 col-sm-4 col-xs-6">content</div>
            <div class="col-lg-3 col-sm-4 col-xs-6">content</div>
            <!-- ... and so on ... -->
        </div>
    </div>
    

Now, I want to apply a margin-top:20px to all small screen size columns and also to big screen columns if there are more than 4 of them, creating two "rows" requiring some spacing in between. Can this be achieved using only CSS?

Answer №1

If you need to adjust the top margin in certain screen sizes, consider using a media query.

@media (max-width: 767px) {
    .col-xs-6 {
        margin-top:20px;
    }
}

Check out this example on Bootply

Just a heads up - it's okay if the total columns in a row exceed 12 in Bootstrap. It will just wrap around. However, excessive nested rows may not be ideal. Learn more at Bootstrap Grid Examples.

Answer №2

After encountering a similar need, I devised the following solution and am documenting it here for both future readers and my own reference.

$resolutions: (xs: (0px, 767px), sm: (768px, 991px), md: (992px, 1199px), lg: (1200px, 9999px));
$directions: (t: top, r: right, b: bottom, l: left);

@for $r from 1 through 10{
  @each $size-abbr, $size-values in $resolutions{
    @media (min-width: nth($size-values, 1)) and (max-width: nth($size-values, 2)) {
      @each $dir-abbr, $dir-name in $directions{
        $x: $r * 5;
        .m#{$dir-abbr}-#{$size-abbr}-#{$x}{
          margin-#{$dir-name}: #{$x}px;
        }
        .m#{$dir-abbr}-#{$size-abbr}-#{$r}p{
          margin-#{$dir-name}: #{$r}unquote('%');
        }
      }
    }
  }
}

The SASS code provided above generates classes that can be used like this:

@media (min-width: 0px) and (max-width: 767px) {
    .mt-xs-5 { margin-top: 5px; }
    .mt-xs-1p { margin-top: 1%; }
    .mr-xs-5 { margin-right: 5px; }
    .mr-xs-1p { margin-right: 1%; }
    .mb-xs-5 { margin-bottom: 5px; }
    .mb-xs-1p { margin-bottom: 1%; }
    .ml-xs-5 { margin-left: 5px; }
    .ml-xs-1p { margin-left: 1%; } 
}

This allows the content editor to easily apply specific margins to elements based on screen size with classes such as .mt-xs-10 representing margin-top: 10px on an extra-small screen.

I trust this will benefit someone in need of a similar solution.

Answer №3

Although this post may be dated, I have come up with a more efficient solution below.

[class*="col-"] {
  margin-bottom: 15px;
}

While this code works in certain cases, it can result in unnecessary margins being added where not required. To address this issue, I have devised a new CSS class called .row-grid.

.row.row-grid [class*="col-"] + [class*="col-"] {
  margin-top: 15px;
}

@media (min-width: 1200px) {
  .row.row-grid [class*="col-lg-"] + [class*="col-lg"] {
    margin-top: 0;
  }
}
@media (min-width: 992px) {
  .row.row-grid [class*="col-md-"] + [class*="col-md"] {
    margin-top: 0;
  }
}
@media (min-width: 768px) {
  .row.row-grid [class*="col-sm-"] + [class*="col-sm"] {
    margin-top: 0;
  }
}

Answer №4

Here is a straightforward and neat solution that I frequently use:

.row { margin-top: -15px; }
.row > div { margin-top: 15px; }

This method ensures that each <div class='col-*-*'> will have a 15px margin on the top, with the exception of those in the first row (or, on smaller screens, except for the one at the very top).

Answer №5

This easy fix automatically adds a top margin to all columns except the initial one on small screens. No need for special class names or changes to HTML or CSS. Simply adjust the margin-top value below to suit your needs.

@media (max-width: 767px) {
  [class*="col-"]:not(:first-child) {
    margin-top: 30px;
  }
}

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

issue with IE's (Internet Explorer) filter invert function not functioning

Why does the filter invert not work in Internet Explorer (IE)? <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> </script> <sty ...

How can you reduce the size of the bootstrap navbar without utilizing less?

I've been attempting to reduce the height of Bootstrap 3.1.1's navbar without utilizing the less version. Despite trying various methods from different sources, I haven't been able to decrease the CSS's .navbar height. However, I did m ...

"Error message stating 'Unresolved variable' occurs in the code while trying to assign className={styles.nameOfClass

My current task involves adjusting the style of a button in ReactJS using the "className" property. However, I encountered an error message in WebStorm stating "Unresolved variable nameOfClass," and the desired style changes did not reflect when running we ...

Can a specific CSS rule be written in Material UI using makeStyles that will only apply if the element has both classes together?

It's common knowledge that achieving this in CSS is a possibility. .makeStyles-mainNavWrapper-67.sticky{ position: fixed; top: 0px; opacity: 1; transition: opacity 1s ease; padding: 10px; } I am curious to find out if this can be achieved ...

Create a smooth scrolling effect for a div with a fixed position

Having trouble scrolling a fixed position div on the page? Are you using this jquery script: $('html, body').animate({ scrollTop: $(".example").offset().top }, 750); I had to set the body overflow property to hidden for another issue. The ...

Header on top of table that stays in place with scrolling

I'm facing an issue with a table that contains user-generated data. We are utilizing a plugin known as Clusterize, which allows us to smoothly scroll through large amounts of rows. However, the specific markup required for this plugin seems to be caus ...

Is there a way for me to modify the color of the currently selected button?

I've been attempting to change the color of the active button when clicked, but my CSS class "activePage" isn't doing the trick. Ideally, clicking the "projects" button should turn it red, and clicking the "about" button should change it to red w ...

Using jQuery to create a script that will transition random images simultaneously within a div

I am currently working on a JavaScript script that will animate 10 images flying into a div, appearing side by side. The goal is to have each image fly in randomly with a unique transition. At the moment, my script is almost complete as I've successf ...

What could be causing the 'margin-top' property not to function as expected?

Although I understand the concept of 'margin-collapse', I am puzzled as to why there is no visible 10 px margin on the top of the first box here. The top box, which has the id 'first', should have a 10px margin above it. If this method ...

Ways to create a distinct highlight for the selected link in the navigation upon clicking

Similar Inquiry: How can I highlight a link based on the current page? Situation I have been struggling to keep a selected link in my navigation menu highlighted after it has been clicked. Unfortunately, I haven't come across any straightfor ...

Splitting Sub Menu into 2 Columns in Wordpress Twenty Seventeen Theme

I'm currently using the Twenty Seventeen theme on my WordPress site. My issue lies with trying to arrange my sub-menu items into 2 columns, as I have multiple items nested under one of my main menu options. After attempting to add the following code ...

How can I horizontally add divs until the width size is exceeded?

I am looking to make tiles flow horizontally until there is no more space, then wrap to the next line. Additionally, I want them to be responsive to different screen sizes. Here is my initial code snippet: .tile { width: 248px; height: 126px; ba ...

Get rid of the white line in the Navbar-toggler when it expands

I'm trying to get rid of an odd white line that keeps appearing at the bottom of my header's navbar. The navbar has a dark background, but whenever I click on the toggler button, this strange white line shows up at the bottom. You can view my we ...

Can someone explain the functionality of the CSS Selector > * when used inside a Vue.js component?

While exploring the Vuestic admin dashboard tool, I noticed a glitch in the UI where the donut chart appeared larger than its container. I tried to troubleshoot and fix it for submission as a PR, but I encountered an unfamiliar CSS selector > * while de ...

Easily done! Using JavaScript to generate a blinking cursor on a table

Working on a project to develop a dynamic version of the classic game Tic-Tac-Toe... However... Every table cell is displaying an irritating flashing cursor, almost like it's behaving as an input field. Any insights into why this is happening...? O ...

Exclude the footer from the index.html file by configuring it in the docusaurus.config.js file

From what I understand, the docusuarus.config.js file is responsible for translating specified information into HTML to construct your website. This translated information is then directed to the 'index.html' file. However, I am encountering an ...

Issue with Bootstrap side navbar not collapsing when clicked on a link

Currently, I'm working on creating a website for a friend. While I used to have some experience with coding in the past, it has been a while and I am a bit rusty. This time around, I decided to use bootstrap for the project. However, I'm struggli ...

Adjust the number of columns based on the minimum screen resolution using columnizer

Currently, I am utilizing the columnizer jQuery plugin to divide my content into columns on a responsive website with a fluid width container. I have implemented different JavaScript functions based on the minimum screen resolutions, similar to CSS media q ...

Reduce the height of a single column in Bootstrap

My goal is to create a footer design using Bootstrap that looks like this: https://i.sstatic.net/7xsKg.png I've successfully set up the left column (Red Background) of the footer, but I'm having trouble styling the right column (Black Backgroun ...

What is the best way to adjust the padding during a scaling transformation?

Currently, I am working on a scaling transform where the origin is centered horizontally. However, I have noticed that when I scale the element, the vertical padding appears to be scaled, but the horizontal padding remains unchanged. How can I ensure that ...