Utilizing Bootstrap 4 to strategically adjust element positioning based on device size and enhance responsiveness

I utilize bootstrap4 to develop a responsive layout that prioritizes mobile devices, featuring elements with different positioning. I have set up multiple rows and columns which I adjust based on the screen size categories.

My curiosity lies in whether there are ready-to-use bootstrap styling classes that would allow me to easily apply and remove absolute, fixed, or relative positioning for various screen sizes without having to craft my own CSS media queries.

For instance, if I wish to have a container that is fixed on mobile but becomes absolute on desktop only for medium-sized screens...

<div id="back-to-top" class="position-fixed position-sm-absolute position-md-relative">
    <a href="#">
        <i class="fa fa-chevron-up"></i>
    </a>
</div>

Additionally, I am utilizing WordPress along with CSS and cannot easily access SASS. Any recommendations?

Answer №1

If you happen upon this information in the future, here are the essential arrays required for the loop to function correctly, compatible with both Bootstrap 4 and Bootstrap 5:

$grid-breakpoints: (
  xs: 0,
  sm: 576px,
  md: 768px,
  lg: 992px,
  xl: 1200px,
  xxl: 1400px
);

$positions: (
  static   : static,
  absolute : absolute,
  relative : relative,
  fixed    : fixed,
  sticky   : sticky,
);

Regrettably, there are no built-in Bootstrap media classes available to apply or remove positions. Custom rules must be defined to override Bootstrap's default position classes—it's a common challenge faced by developers.

https://getbootstrap.com/

For implementing this functionality, below is the SCSS code snippet:

@each $breakpoint in map-keys($grid-breakpoints) {
  @include media-breakpoint-up($breakpoint) {
    $infix: breakpoint-infix($breakpoint, $grid-breakpoints);
    // Common values
    @each $position in $positions {
      .position#{$infix}-#{$position} { position: $position !important; }
    }
  }
}

And here is the compiled CSS version:

@media (min-width: 576px) {
  .position-sm-static {
    position: static !important;
  }
  .position-sm-relative {
    position: relative !important;
  }
  .position-sm-absolute {
    position: absolute !important;
  }
  .position-sm-fixed {
    position: fixed !important;
  }
  .position-sm-sticky {
    position: sticky !important;
  }
}

@media (min-width: 768px) {
  .position-md-static {
    position: static !important;
  }
  .position-md-relative {
    position: relative !important;
  }
  .position-md-absolute {
    position: absolute !important;
  }
  .position-md-fixed {
    position: fixed !important;
  }
  .position-md-sticky {
    position: sticky !important;
  }
}

@media (min-width: 992px) {
  .position-lg-static {
    position: static !important;
  }
  .position-lg-relative {
    position: relative !important;
  }
  .position-lg-absolute {
    position: absolute !important;
  }
  .position-lg-fixed {
    position: fixed !important;
  }
  .position-lg-sticky {
    position: sticky !important;
  }
}

@media (min-width: 1200px) {
  .position-xl-static {
    position: static !important;
  }
  .position-xl-relative {
    position: relative !important;
  }
  .position-xl-absolute {
    position: absolute !important;
  }
  .position-xl-fixed {
    position: fixed !important;
  }
  .position-xl-sticky {
    position: sticky !important;
  }
}

Answer №2

After reading @Max Kaps's response, I decided to try out BS5 and found that it functions perfectly.

@media (min-width: 576px) {
  .position-sm-static {
    position: static !important;
  }
  .position-sm-relative {
    position: relative !important;
  }
  .position-sm-absolute {
    position: absolute !important;
  }
  .position-sm-fixed {
    position: fixed !important;
  }
  .position-sm-sticky {
    position: sticky !important;
  }
}
@media (min-width: 768px) {
  .position-md-static {
    position: static !important;
  }
  .position-md-relative {
    position: relative !important;
  }
  .position-md-absolute {
    position: absolute !important;
  }
  .position-md-fixed {
    position: fixed !important;
  }
  .position-md-sticky {
    position: sticky !important;
  }
}

@media (min-width: 992px) {
  .position-lg-static {
    position: static !important;
  }
  .position-lg-relative {
    position: relative !important;
  }
  .position-lg-absolute {
    position: absolute !important;
  }
  .position-lg-fixed {
    position: fixed !important;
  }
  .position-lg-sticky {
    position: sticky !important;
  }
}

@media (min-width: 1200px) {
  .position-xl-static {
    position: static !important;
  }
  .position-xl-relative {
    position: relative !important;
  }
  .position-xl-absolute {
    position: absolute !important;
  }
  .position-xl-fixed {
    position: fixed !important;
  }
  .position-xl-sticky {
    position: sticky !important;
  }
}

Answer №3

After consulting @Max Kaps's input, it is crucial to include the $positions variable in your SCSS code!

$positions: (
    fixed,
    absolute,
    relative,
    sticky,
    static
);

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

Locating the elusive comma - unraveling the mystery of Javascript Arrays nested within Objects

I've managed to put together something that functions as intended, but I'm facing an issue where I get a comma between entries when there are multiple ones. Here's the code snippet: chrome.runtime.onMessage.addListener(function (message, s ...

Every time I refresh the page, new data is inserted into the MySQL database

After clicking the button, I expect data to be inserted into my MySQL database. However, it only inserts the data when I manually reload the page. Below is the code snippet: <script> function minuscredits() { alert("hah"); <?php mysql ...

What is the highest possible z-index value that can be applied in

Is there a specific limit to the CSS property z-index? Are there variations in accepted values among different browsers? How do browsers typically handle extreme z-index values? I recall reading about a potential maximum value for z-index somewhere, but I ...

When I open my website in the browser, the bootstrap card-deck design is not being displayed correctly

I encountered a strange issue while designing my website. The code I wrote for the Bootstrap card-deck class in codeply.com was working perfectly fine. However, when I copied the same code into my PyCharm code editor and then pasted the file link into my b ...

Encountering a Bootstrap <div> error while testing my AngularJS/nodejs project

After launching the project to check out the login/registration interface, the following screen is displayed: Login/Registration: My code already includes Bootstrap and jQuery: <script type="text/javascript" src="assets/js/jquery.min.js"></scr ...

multiple elements sharing identical CSS styling

For each component, I made separate CSS files and imported them accordingly. However, despite the individual styling efforts, all components seem to have the same appearance. I specifically worked on styling an image differently for two components, but w ...

`Addressing issues with table cell layout for improved horizontal scrolling`

I am currently facing a challenge involving fixing the first and last column (.headcol, .lastcol) in a horizontally scrolling table, ensuring they always align to the left & right of the visible panel. My understanding was that position: absolute elements ...

Retrieving text content from an HTML tag using C#

I'm working with a variable that contains the following tag. My goal is to extract the values of type and id into separate variables using C#. What is the most effective approach to accomplish this task? <a href="gana:$type=FlexiPage;id=c828c4ea- ...

Add several converted links as variables in each section

The title may not be the clearest, but I am facing a challenge with an ecommerce site that has unmodifiable HTML. My goal is to include additional links for each product displayed on a page showcasing multiple products. Each link should be unique to its re ...

Ui Bootstrap (angularjs) sidebar is not functioning properly

I am encountering an issue with a sidenav that I am in the process of creating for one of my projects. The goal is to develop a side menu that shifts the content to the right when opened and returns it to the left when closed, essentially functioning as a ...

Hey there! I'm experiencing an issue with the jquery function $(this).childen().val()

Here is my HTML and JS/jQuery code: (function($) { $('.list-group li').click(function(){ console.log("push li"); console.log($(this).attr('class')); console.log($(this).children('.hidden_input&ap ...

Having trouble converting customized tabs into Bootstrap navigation tabs?

Creating custom tabs with unique tab logic <!-- <lntds-tabs [selectedIndex]="selectedTabIndex" (selectedTabChange)="tabChanged($event)"> --> <!-- <lntds-tab class="custom-tab-group lntdstabsco ...

Issues with rendering HTML5 drag and drop styles are not visible on a Windows Server 2003 platform

I am currently developing a file upload tool utilizing Valum's Ajax-Uploader as the foundation. The concept is reminiscent of how attaching files works in Gmail. Users should be able to simply drag and drop a file from their desktop into the browser w ...

Tips for generating a numeric whole number input field with Vuetify

After researching the topic on Vuetify's specific number input component, I am attempting to create a numeric input field. The value for both input and output is currently unknown, meaning it could be either undefined or null in order to allow cleari ...

Outlook fails to recognize table cell widths when there is an image present

Although this question has been asked previously, the solutions provided did not solve my issue. I am trying to set the width of the <td> element to 70%, but when I insert an image it is affecting the widths of received emails in Outlook (I use MS O ...

Reset the value of the input type file when the modal is closed and ensure that the Save All Changes button is deactivated

Hey there! Working on a simple project and trying to incorporate a neat trick in my app. I want to achieve the following: Clear the input type file value when the modal is closed. Essentially, if the user decides to cancel the upload and closes the modal, ...

<H2> along with <Sup>

So I have an H2 heading with a company name and next to that on the same line, we need to include a Superscript tag (TM). Here is the code: <h1 class="Red">Company Name</h1><sup>&#8482;</sup> When rendered, it looks like this: ...

There was a SyntaxError that caught me by surprise in my Javascript code when it unexpectedly encountered

I have been encountering an error in my code consistently, and I am struggling to comprehend why this is happening. The problematic area seems to be in line 29 of my Javascript file. HTML <link href="Styles12.css"; type="text/css" rel="stylesheet"> ...

Angular, a Self-sufficient Module of Cascading Style Sheets

I have developed a factory that generates HTML content. I want to showcase this generated HTML within a specific section of my application, without its CSS affecting the rest of the site, and vice versa. While using an iframe is one option, I believe there ...

What is the best way to show the initial image within every div that has the class name .className?

I am looking to only show the first image in each div with the class name "className." This... <div class="className"> <p>Yo yo yo</p> <p><img src="snoop.jpg" /></p> </div> <div class="className"> Hel ...