Applying borders on elements depending on device size with bootstrap
Asked Answered
A

7

22

I'm using bootstrap to create a mobile first responsive layout and have various rows and columns set up that I adjust for the different categories of screen size.

I am wondering if there are pure bootstrap styling classes that would allow me to apply and remove borders for the different sizes, without having to create my own css media queries.

For example, if I wanted to have a border-right on the first column in a row only when it's floated at medium size... I know this is not real code, but is there something to the same affect that I'm too dumb to find?

<div class="row">
    <div class="col-12 col-md-6 border border-top-0 border-right-0 border-bottom-0 border-left-0 border-md-right-1">
        <p>Lorem ipsum</p>
    </div>
    <div class="col-12 col-md-6">
        <p>Dolor sit amet</p>
    </div>
</div>

I'm also using the maxcdn css so can't get into the sass. Any suggestions?

Antifebrile answered 25/9, 2017 at 19:11 Comment(1)
See my answer to a similar question here: stackoverflow.com/questions/23583713 As per community policy i cannot duplicate my answer here but you may achieve this by using the overflow-hidden attribute to the parent container to hide the border that you do not want to display on some specific size screens.Operative
G
28

Bootstrap doesn't support responsive borders out of the box. You can follow the instructions here for the responsive border in bootstrap - https://github.com/twbs/bootstrap/issues/23892#issuecomment-378172751

Here is the code:

@each $breakpoint in map-keys($grid-breakpoints) {
  @include media-breakpoint-up($breakpoint) {
    $infix: breakpoint-infix($breakpoint, $grid-breakpoints);

    .border#{$infix}-top {      border-top: $border-width solid $border-color !important; }
    .border#{$infix}-right {    border-right: $border-width solid $border-color !important; }
    .border#{$infix}-bottom {   border-bottom: $border-width solid $border-color !important; }
    .border#{$infix}-left {     border-left: $border-width solid $border-color !important; }

    .border#{$infix}-top-0 {    border-top: 0 !important; }
    .border#{$infix}-right-0 {  border-right: 0 !important; }
    .border#{$infix}-bottom-0 { border-bottom: 0 !important; }
    .border#{$infix}-left-0 {   border-left: 0 !important; }

    .border#{$infix}-x {
      border-left: $border-width solid $border-color !important;
      border-right: $border-width solid $border-color !important;
    }

    .border#{$infix}-y {
      border-top: $border-width solid $border-color !important;
      border-bottom: $border-width solid $border-color !important;
    }
  }
}
Gathers answered 18/7, 2019 at 7:59 Comment(2)
~2 years later a great option comes up! I added this to my codebase and it works!Whitford
If someone using CDN of bootstrap instead of a local device, In that case, what should be the solution?Noneffective
A
16

There is no support for it. But adding own code will fix it. Based on @sharan answer (thx!) I've made some modifications if you want to define all border at once. Original: https://mcmap.net/q/569932/-applying-borders-on-elements-depending-on-device-size-with-bootstrap

My version for SCSS:

@each $breakpoint in map-keys($grid-breakpoints) {
  @include media-breakpoint-up($breakpoint) {
    $infix: breakpoint-infix($breakpoint, $grid-breakpoints);

    .border#{$infix}-top {      border-top: $border-width solid $border-color !important; }
    .border#{$infix}-right {    border-right: $border-width solid $border-color !important; }
    .border#{$infix}-bottom {   border-bottom: $border-width solid $border-color !important; }
    .border#{$infix}-left {     border-left: $border-width solid $border-color !important; }

    .border#{$infix}-top-0 {    border-top: 0 !important; }
    .border#{$infix}-right-0 {  border-right: 0 !important; }
    .border#{$infix}-bottom-0 { border-bottom: 0 !important; }
    .border#{$infix}-left-0 {   border-left: 0 !important; }

    .border#{$infix}-x {
      border-left: $border-width solid $border-color !important;
      border-right: $border-width solid $border-color !important;
    }

    .border#{$infix}-y {
      border-top: $border-width solid $border-color !important;
      border-bottom: $border-width solid $border-color !important;
    }
    // support of .border-sm (-md, -lg, -xl)
    .border#{$infix} {
      border-top: $border-width solid $border-color !important;
      border-bottom: $border-width solid $border-color !important;
      border-left: $border-width solid $border-color !important;
      border-right: $border-width solid $border-color !important;
    }
    .border#{$infix}-0 {
      border-top: 0 !important;
      border-bottom: 0 !important;
      border-left: 0 !important;
      border-right: 0 !important;
    }
  }
}

My version for CSS, compiled version:

@media (min-width: 576px) {
  .border-sm-top {
    border-top: 1px solid #e3e7eb !important;
  }
  .border-sm-right {
    border-right: 1px solid #e3e7eb !important;
  }
  .border-sm-bottom {
    border-bottom: 1px solid #e3e7eb !important;
  }
  .border-sm-left {
    border-left: 1px solid #e3e7eb !important;
  }
  .border-sm-top-0 {
    border-top: 0 !important;
  }
  .border-sm-right-0 {
    border-right: 0 !important;
  }
  .border-sm-bottom-0 {
    border-bottom: 0 !important;
  }
  .border-sm-left-0 {
    border-left: 0 !important;
  }
  .border-sm-x {
    border-left: 1px solid #e3e7eb !important;
    border-right: 1px solid #e3e7eb !important;
  }
  .border-sm-y {
    border-top: 1px solid #e3e7eb !important;
    border-bottom: 1px solid #e3e7eb !important;
  }
  .border-sm {
    border-top: 1px solid #e3e7eb !important;
    border-bottom: 1px solid #e3e7eb !important;
    border-left: 1px solid #e3e7eb !important;
    border-right: 1px solid #e3e7eb !important;
  }
  .border-sm-0 {
    border-top: 0 !important;
    border-bottom: 0 !important;
    border-left: 0 !important;
    border-right: 0 !important;
  }
}

@media (min-width: 768px) {
  .border-md-top {
    border-top: 1px solid #e3e7eb !important;
  }
  .border-md-right {
    border-right: 1px solid #e3e7eb !important;
  }
  .border-md-bottom {
    border-bottom: 1px solid #e3e7eb !important;
  }
  .border-md-left {
    border-left: 1px solid #e3e7eb !important;
  }
  .border-md-top-0 {
    border-top: 0 !important;
  }
  .border-md-right-0 {
    border-right: 0 !important;
  }
  .border-md-bottom-0 {
    border-bottom: 0 !important;
  }
  .border-md-left-0 {
    border-left: 0 !important;
  }
  .border-md-x {
    border-left: 1px solid #e3e7eb !important;
    border-right: 1px solid #e3e7eb !important;
  }
  .border-md-y {
    border-top: 1px solid #e3e7eb !important;
    border-bottom: 1px solid #e3e7eb !important;
  }
  .border-md {
    border-top: 1px solid #e3e7eb !important;
    border-bottom: 1px solid #e3e7eb !important;
    border-left: 1px solid #e3e7eb !important;
    border-right: 1px solid #e3e7eb !important;
  }
  .border-md-0 {
    border-top: 0 !important;
    border-bottom: 0 !important;
    border-left: 0 !important;
    border-right: 0 !important;
  }
}

@media (min-width: 992px) {
  .border-lg-top {
    border-top: 1px solid #e3e7eb !important;
  }
  .border-lg-right {
    border-right: 1px solid #e3e7eb !important;
  }
  .border-lg-bottom {
    border-bottom: 1px solid #e3e7eb !important;
  }
  .border-lg-left {
    border-left: 1px solid #e3e7eb !important;
  }
  .border-lg-top-0 {
    border-top: 0 !important;
  }
  .border-lg-right-0 {
    border-right: 0 !important;
  }
  .border-lg-bottom-0 {
    border-bottom: 0 !important;
  }
  .border-lg-left-0 {
    border-left: 0 !important;
  }
  .border-lg-x {
    border-left: 1px solid #e3e7eb !important;
    border-right: 1px solid #e3e7eb !important;
  }
  .border-lg-y {
    border-top: 1px solid #e3e7eb !important;
    border-bottom: 1px solid #e3e7eb !important;
  }
  .border-lg {
    border-top: 1px solid #e3e7eb !important;
    border-bottom: 1px solid #e3e7eb !important;
    border-left: 1px solid #e3e7eb !important;
    border-right: 1px solid #e3e7eb !important;
  }
  .border-lg-0 {
    border-top: 0 !important;
    border-bottom: 0 !important;
    border-left: 0 !important;
    border-right: 0 !important;
  }
}

@media (min-width: 1200px) {
  .border-xl-top {
    border-top: 1px solid #e3e7eb !important;
  }
  .border-xl-right {
    border-right: 1px solid #e3e7eb !important;
  }
  .border-xl-bottom {
    border-bottom: 1px solid #e3e7eb !important;
  }
  .border-xl-left {
    border-left: 1px solid #e3e7eb !important;
  }
  .border-xl-top-0 {
    border-top: 0 !important;
  }
  .border-xl-right-0 {
    border-right: 0 !important;
  }
  .border-xl-bottom-0 {
    border-bottom: 0 !important;
  }
  .border-xl-left-0 {
    border-left: 0 !important;
  }
  .border-xl-x {
    border-left: 1px solid #e3e7eb !important;
    border-right: 1px solid #e3e7eb !important;
  }
  .border-xl-y {
    border-top: 1px solid #e3e7eb !important;
    border-bottom: 1px solid #e3e7eb !important;
  }
  .border-xl {
    border-top: 1px solid #e3e7eb !important;
    border-bottom: 1px solid #e3e7eb !important;
    border-left: 1px solid #e3e7eb !important;
    border-right: 1px solid #e3e7eb !important;
  }
  .border-xl-0 {
    border-top: 0 !important;
    border-bottom: 0 !important;
    border-left: 0 !important;
    border-right: 0 !important;
  }
}
Andersen answered 1/7, 2020 at 10:53 Comment(0)
W
13

In Bootstrap5 you can modify the utilities : https://getbootstrap.com/docs/5.1/utilities/api/#enable-responsive

@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/utilities";

$utilities: map-merge(
  $utilities, 
  (
    "border": map-merge(
      map-get($utilities, "border"),
      ( responsive: true ),
    ),
    "border-top": map-merge(
      map-get($utilities, "border-top"),
      ( responsive: true ),
    ),
    "border-end": map-merge(
      map-get($utilities, "border-end"),
      ( responsive: true ),
    ),
    "border-bottom": map-merge(
      map-get($utilities, "border-bottom"),
      ( responsive: true ),
    ),
    "border-start": map-merge(
      map-get($utilities, "border-start"),
      ( responsive: true ),
    ),
  )
);

Compiled CSS will be like:

  .border-sm {
    border: 1px solid #e9ecef !important;
  }

  .border-sm-0 {
    border: 0 !important;
  }

  .border-top-sm {
    border-top: 1px solid #e9ecef !important;
  }

  .border-top-sm-0 {
    border-top: 0 !important;
  }

  .border-end-sm {
    border-right: 1px solid #e9ecef !important;
  }

  .border-end-sm-0 {
    border-right: 0 !important;
  }

  .border-bottom-sm {
    border-bottom: 1px solid #e9ecef !important;
  }

  .border-bottom-sm-0 {
    border-bottom: 0 !important;
  }

  .border-start-sm {
    border-left: 1px solid #e9ecef !important;
  }

  .border-start-sm-0 {
    border-left: 0 !important;
  }

...continues...
Weekend answered 7/9, 2021 at 6:40 Comment(2)
Very good solution. Only issue is it breaks some classes such as 'border-dark' because the media query of the breakpoint is "stronger" than the border-dark classAbbeyabbi
@Abbeyabbi border-dark is only for border's color while you can override just border to exist or not with the above utilities. i.e. if you only need to switch color for specific breakpoint, then you can use for example border-dark border-lg-light which will toggle border color for specific breakpoint.Bimetallic
B
2

This is the css for all screen sizes

@media (max-width: 575px) {
    .border-top {
        border-top: 1px solid #424242;
    }
    .border-left {
        border-left: 1px solid #424242;
    }
    .border-bottom {
        border-bottom: 1px solid #424242;
    }
    .border-right {
        border-right: 1px solid #424242;
    }
    .border-top-0 {
        border-top: none!important;
    }
    .border-left-0 {
        border-left: none!important;
    }
    .border-bottom-0 {
        border-bottom: none!important;
    }
    .border-right-0 {
        border-right: none!important;
    }
}

@media (min-width: 576px) {
    .border-sm-top {
        border-top: 1px solid #424242;
    }
    .border-sm-left {
        border-left: 1px solid #424242;
    }
    .border-sm-bottom {
        border-bottom: 1px solid #424242;
    }
    .border-sm-right {
        border-right: 1px solid #424242;
    }
    .border-sm-top-0 {
        border-top: none!important;
    }
    .border-sm-left-0 {
        border-left: none!important;
    }
    .border-sm-bottom-0 {
        border-bottom: none!important;
    }
    .border-sm-right-0 {
        border-right: none!important;
    }
}

@media (min-width: 768px) {
    .border-md-top {
        border-top: 1px solid #424242;
    }
    .border-md-left {
        border-left: 1px solid #424242;
    }
    .border-md-bottom {
        border-bottom: 1px solid #424242;
    }
    .border-md-right {
        border-right: 1px solid #424242;
    }
    .border-md-top-0 {
        border-top: none!important;
    }
    .border-md-left-0 {
        border-left: none!important;
    }
    .border-md-bottom-0 {
        border-bottom: none!important;
    }
    .border-md-right-0 {
        border-right: none!important;
    }
}

@media (min-width: 992px) {
    .border-lg-top {
        border-top: 1px solid #424242;
    }
    .border-lg-left {
        border-left: 1px solid #424242;
    }
    .border-lg-bottom {
        border-bottom: 1px solid #424242;
    }
    .border-lg-right {
        border-right: 1px solid #424242;
    }
    .border-lg-top-0 {
        border-top: none!important;
    }
    .border-lg-left-0 {
        border-left: none!important;
    }
    .border-lg-bottom-0 {
        border-bottom: none!important;
    }
    .border-lg-right-0 {
        border-right: none!important;
    }
}

@media (min-width: 1200px) {
    .border-xl-top {
        border-top: 1px solid #424242;
    }
    .border-xl-left {
        border-left: 1px solid #424242;
    }
    .border-xl-bottom {
        border-bottom: 1px solid #424242;
    }
    .border-xl-right {
        border-right: 1px solid #424242;
    }
    .border-xl-top-0 {
        border-top: none!important;
    }
    .border-xl-left-0 {
        border-left: none!important;
    }
    .border-xl-bottom-0 {
        border-bottom: none!important;
    }
    .border-xl-right-0 {
        border-right: none!important;
    }
}
Bongbongo answered 2/9, 2021 at 8:4 Comment(0)
T
0

No, there are not pure bootstrap styling classes for apply and remove borders. You have to create your own rules to override them.

Tenebrific answered 26/9, 2017 at 13:19 Comment(1)
What's with the gratuitous link? The OP is obviously already using Bootstrap.Adaliah
G
0

Updating the great @Sharan answer, here is a SASS version for Bootstrap 5 (tested with [email protected] and [email protected]):

@use 'sass:map'

@import ~bootstrap/scss/functions
@import ~bootstrap/scss/variables
@import ~bootstrap/scss/maps
@import ~bootstrap/scss/mixins
@import ~bootstrap/scss/utilities
@import ~bootstrap/scss/utilities/api

$placements: (start: 'left', end: 'right', top: 'top', bottom: 'bottom')

@each $breakpoint in map.keys($grid-breakpoints)
  @include media-breakpoint-up($breakpoint)
    $infix: breakpoint-infix($breakpoint, $grid-breakpoints)
    @each $placement-label, $placement-prop in $placements
      .border#{$infix}-#{$placement-label}
        border-#{$placement-prop}: $border-width solid $border-color !important

      .border#{$infix}-#{$placement-label}-0
        border-#{$placement-prop}: 0 !important

    .border#{$infix}-x
      border-left: $border-width solid $border-color !important
      border-right: $border-width solid $border-color !important

    .border#{$infix}-y
      border-top: $border-width solid $border-color !important
      border-bottom: $border-width solid $border-color !important

// Rewrite and import bootstrap styles
@import ~bootstrap/scss/bootstrap
Griseldagriseldis answered 4/11, 2022 at 21:26 Comment(0)
G
0

Since your question didn't explicitly forbid using a JS solution, I'm adding for completeness. Here's how to implement a border-start-md class in JS on top of base bootstrap 5:

// add border-start-md without base bootstrap
const mediaQuery = window.matchMedia('(min-width: 768px)'); // twbs *-md
mediaQuery.addEventListener('change', e => {
    document.querySelectorAll('.border-start-md')
        .forEach( el => el
            .classList[e.target.matches ? 'add' : 'remove']('border-start'));
});
const changeEvent = new Event('change');
mediaQuery.dispatchEvent(changeEvent);
Gallon answered 22/8, 2023 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.