Customizing element borders based on device size using bootstrap

I'm currently working with Bootstrap to design a mobile-first responsive layout. I have set up multiple rows and columns that I adjust based on different screen size categories.

Is there a way to style borders using just Bootstrap classes for the various sizes without having to write my own CSS media queries?

For example, let's say I want to add a border-right to the first column in a row only when it's floated at medium size... Is there a built-in feature that achieves this? Please note that the code below is just an illustration:

<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 utilizing the maxcdn CSS, so I can't access Sass. Any recommendations or suggestions would be greatly appreciated!

Answer №1

Responsive borders are not natively supported in Bootstrap. To implement responsive borders in Bootstrap, you can refer to the guidelines provided in this link - https://github.com/twbs/bootstrap/issues/23892#issuecomment-378172751

The following code snippet illustrates how to achieve responsive borders:

@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;
    }
  }
}

Answer №2

There is currently no built-in support for this feature, but incorporating your own custom code can resolve the issue effectively. Credit to @sharan for the initial solution (thank you!) with some modifications made by me. If you prefer defining all borders at once, check out the updated version below: Original:

My customized version using 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;
    }
    // additional support for .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 revised CSS adaptation:

@media (min-width: 576px) {
  .border-sm-top {
    border-top: 1px solid #e3e7eb !important;
  }
  .border-sm-right {
    border-right: 1px solid #e3e7eb !important;
  }
  // Additional styles omitted for brevity
}

@media (min-width: 768px) {
  .border-md-top {
    border-top: 1px solid #e3e7eb !important;
  }
  .border-md-right {
    border-right: 1px solid #e3e7eb !important;
  }
  // Additional styles omitted for brevity
}

// Repeat similar styling blocks for different breakpoints

Answer №3

In Bootstrap5 you have the ability to customize the utility classes: Click here for more information

@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 ),
    ),
  )
);

You can then see the compiled CSS output:

  .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;
  }

...the list goes on...

Answer №4

This CSS code applies to various 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;
    }
}

Answer №5

Sorry, but Bootstrap does not provide specific styling classes for adding or removing borders. You will need to customize your own rules to achieve the desired effect.

Answer №6

Here is an updated SASS version for Bootstrap 5 based on the great answer from @Sharan (successfully 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

Answer №7

In case you didn't explicitly mention avoiding JavaScript solutions, I will provide a solution using JS for the sake of completeness. Below is an example of how you can create a border-start-md class in JS on top of the base Bootstrap 5:

// Implementing border-start-md class with JS
const mediaQuery = window.matchMedia('(min-width: 768px)'); // Targeting medium devices in Bootstrap 5
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);

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

How to hide text within a contenteditable element

I'm trying to create a contenteditable span that behaves like a password field in a Linux terminal; the text is entered, but not visible. I've experimented with different CSS properties like visibility: hidden and display: none, but they don&apos ...

Activate animation when a user clicks on a link

Hey there, I'm a bit unsure about how to word this and I'm still getting the hang of StackExchange so I hope I've posted in the correct place. Currently, I am developing a mobile HTML5 app that utilizes Phonegap/Cordova (as well as Bootstra ...

Creating a zigzag pattern with a linear gradient in CSS

When attempting to create a background color for my body element using linear gradient, I noticed that it resulted in a zigzag effect I experimented with the following code: Body{ background: linear-gradient(45deg, Blue 50%, red 50%) } The outcome wa ...

What is the best way to remove a border piece with CSS?

Currently, I'm attempting to achieve a matrix effect purely through HTML and CSS. One method I have come across involves applying a solid border and then removing certain parts at the top and bottom. Does anyone know if it's possible to create th ...

Ways to distinguish between two div elements that have scroll bars and those that do not

I created a div with CSS styling. .comment-list { margin: 20px 0; max-height: 100px; min-height: 100px; overflow-y: scroll; width: 100%; background-color:#000; } Here is the HTML code for the div: <div class="comment-list"> </div> When the ...

Maintaining the image's aspect ratio using the Next Image component

Currently, I am using Next's Image component to display a logo. Although the image itself is 1843x550 px, I have specified the width and height properties to adjust it to fit within a space of 83x24 px. However, due to a slightly different aspect rati ...

Both position absolute and position relative are failing to work effectively

Whenever I click on a div called "test", another div named "outside" appears, along with a div named "inside" that has a higher z-index. The problem arises when I try to set the position of "inside" to absolute, as I am unable to assign a margin-bottom. A ...

Issue with embedding icon within input field in Bootstrap version 4.1

I am currently implementing a project using Angular 6 along with the latest version of Bootstrap v4.1. I am attempting to create a reactive form with icons within input fields. As there is no direct way in bootstrap to place icons on the left side of input ...

Display a section of the picture at maximum width

Can anyone help with displaying only a portion of an image at full width (100%) without any overflow issues? The code I've tried is not working: .intro_sea { position: absolute; width: 101%; clip-path: inset(30% 50% 0 0); margin: 0; paddi ...

Dealing with Sideways Overflow Using slideDown() and slideUp()

Attempting to use slideUp() and slideDown() for an animated reveal of page elements, I encountered difficulty when dealing with a relatively positioned icon placed outside the element. During these animations, overflow is set to hidden, resulting in my ico ...

Ways to verify an iCheck located on the following page

Here is the code snippet I am currently working with: $("#add-new-size-grp").click( function (event) { event.preventDefault(); $.ajax({ type: "get", url:"ajax-get-sizes.php", success: function(result){ $("#sizeg ...

Tips for generating an ordered list that begins with 01

Currently, I am developing a Ruby on Rails application in which I am iterating over the tuto. .container .row .col-xs-12.col-sm-12 h1.text-gray Tutorials br .col-xs-10.col-sm-5.index-background - @tut ...

How is the height and width of the header determined?

When examining the theme by clicking "inspect element" on the green portion (<header> ), you will notice that the height and width properties have pixel values that change dynamically as the window is resized. So, where do these values originate fr ...

How to close a Bootstrap JS dialog in WPF without using an onclick event

I need to close a JavaScript modal without requiring a click. Instead, I want to be able to click on the close button in a WPF application. Additionally, I want to be able to click within a WebBrowser. https://i.sstatic.net/go6mf.png I want to ensure th ...

Easy Div Centering with jQuery Toggle for Internet Explorer 6

Press the button to center the div, press it again to align it to the left. This feature is compatible with all browsers except for IE6, which does not support margin: 0 auto;. How can I find a solution to this issue? The width of the div is not fixed and ...

Text box size in user input not adapting on mobile devices

Website: Visit the Cutter Travel Calculator here I've encountered an issue with my user input text boxes on mobile. Despite setting their width using the formidable forms plugin, the size adjustment doesn't seem to apply when viewed on mobile de ...

Troubleshooting Owl Carousel's width problems upon initial loading

Currently, I am utilizing the Owl Carousel and have the following script: $("#owl-demo").owlCarousel({ items : 3 }); My intention is for the container to display 3 images. However, upon initial load, this is what appears: https://i.sstatic.net/ePLAQ. ...

What is the ideal event for a slider (range) to trigger in order to function effectively?

After converting my input into a slider, I noticed a strange behavior with the (click) event in Angular. When dragging the slider and releasing it without the mouse pointer hovering over the slider, the onStart() method does not execute. I experimented wi ...

What is the best way to rearrange Bootstrap columns for extra small screens?

In my bootstrap layout, I have a row with two columns: <div class="row"> <div class="col-md-5 col-md-push-7"> Content in this column should display on the right </div> <div class="col-md-7 col-md-pull-5"> ...

Increase the size of a div to equal the combined width of two divs located directly below it

I need help aligning 3 divs so that the top div stretches to match the width of the bottom 2 divs combined. Here is an image showing the expected div positioning. I attempted to use display: table-row for the divs. <div id="main_div" style="display: ta ...