Executing a snippet of Bootstrap 5 code under specific circumstances

I need to execute different code blocks based on the window width. When the width is 500px or more, I want to run a specific block of code, and when it's 499px or less, I need another block of code to run. How can I achieve this?

For widths of 500px or more:

<div class="card text-center d-inline-block m-2" style="width: 13rem;" >
   <img src=".." class="card-img-top" alt="...">
   <div class="card-body">
       <h5 class="card-title" style="font-size: 1rem;">name</h5>
       <p class="card-text text-muted" style="font-size: 0.8rem;">100 $</p>
       <a href="#" class="btn btn-primary" style="font-size: 0.9rem;">watch</a>
    </div>
</div>

For widths of 499px or less:

<div class="card mb-3" style="max-width: 540px;">
     <div class="row g-0">
<div class="col-md-4">
  <img src="..." class="img-fluid rounded-start" alt="...">
</div>
<div class="col-md-8">
  <div class="card-body">
    <h5 class="card-title">Card title</h5>
    <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
    <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
  </div>
</div>

Answer №1

To display both elements based on the screen size, you'll need to place each one inside its own div and conditionally render them accordingly. For the div when the viewport is less than or equal to 499px, wrap the entire content in:

<div class="d-md-none">
</div>

And for the div when the viewport is greater than or equal to 500px, wrap the entire content in:

<div class="d-none d-md-block">
</div>

JSFiddle

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

Creating a full-width layout with CSS div

Seeking the most stylish method to create a layout similar to this using divs in IE7 and other modern browsers (Chrome, Firefox, etc). Appreciate your help! ...

What is the best way to download a modified canvas with filters using the solutions from stackoverflow?

There have been numerous inquiries regarding how to apply CSS filters to an image, with some solutions that don't utilize CSS filters but still achieve the desired outcome. However, for someone new to JavaScript like myself, these answers can be confu ...

When using Ruby and Rack on Heroku to serve a static site, the CSS styling may not be

Trying to deploy a static site consisting of HTML, CSS, font, and image files with Ruby Rack has been a bit challenging. While the HTML displays perfectly in Chrome when running it locally, the CSS assets do not seem to be loading or being applied to the H ...

The CSS code for ::before is not functioning properly

Trying to enhance the style in Ionic2, I have a message box: https://i.sstatic.net/PCRtA.png I am attempting to integrate this image into it: https://i.sstatic.net/PSQID.png To create a seamless look with the message box. Facing an issue where the ima ...

Center column with header and footer flanking each side

Trying to replicate the layout of the Facebook Android app on my page. The app features a 3 column design, with the central column exclusively displaying the header (no footer included, although I require one for my version). This visual representation is ...

My jquery filter is not functioning properly

I need help making the active class work on each category when clicked, shifting to the next one. I've tried implementing it but no luck so far. $(document).ready(function() { $('.list-inline-item').click(function() { const value = ...

Are "Color Scheme" and "Color Palette" distinct from each other?

What is the difference between a color scheme and a color palette? In one of my projects, which is a web template, there are options for customers to customize. However, I find myself confused with the naming conventions for two color-related options. Op ...

Increasing the maximum width of an image in CSS is necessary to see the max-height take effect

Within my HTML structure: <Col md="6"> <div className="hero-section"> <div className={`flipper ${isFlipping ? "isFlipping" : ""}`}> <div className="front"> <div className="hero-section-content"> ...

Determine the total number of days using the bootstrap date range picker

Currently, I am utilizing the bootstrap jquery daterangepicker in order to determine the number of days between 2 dates. Within my HTML code, I have implemented 2 input fields as shown below: <div class="col-12 form-group has-feedback"> <labe ...

Issue with div in simple HTML code

I've been facing challenges with this line of HTML as I keep encountering errors and cannot pinpoint the source. The error highlight appears at the end of the last div, but once removed, another error emerges at the end of the body tag. Check out the ...

Using CSS to style the last paragraph after a heading

Trying to figure out how to style the final paragraph following a heading using CSS. I attempted using #div h1 ~ p:last-child, but it ended up skipping past the second heading and affecting the last paragraph before the list. I've been diligently sea ...

MaterialUI AppBar is missing a crucial element

I recently dived into the world of Material UI for React and it's been a good experience so far. However, I've noticed a white gap in my header that I can't seem to get rid of. https://i.sstatic.net/zAZJu.png Here is the snippet from my in ...

Hovering over a link causes malfunction in CSS drop-down menu

I have been struggling for hours to make this drop-down menu work, but it just won't cooperate. I want the list menu .dropdown to appear when you hover over .droptoggle. I'm including the entire page because I can't figure out what's wr ...

HTML textarea fails to recognize inline styles

My journey with JHipster began recently when I embarked on my first project. Two entities were created, one of which resembles a blog. This particular blog entity has a content attribute that interacts with a textarea. In my blogs.html file, I allowed the ...

Tips on displaying a div for a brief moment and then concealing it

Is it possible to create a div that will disappear after 10 seconds of the page loading using PHP or JavaScript? Here is an example of the code: <html> <head></head> <body> <div class="LOADING" id="LOADING" name="LOADING">&l ...

Tips on increasing video size upon visiting a website. HTML, JavaScript, and potentially jQuery can be used for video animation

Is there a way to create a video pop-up in the same window when visiting a website? I want the video to increase in height from 0 to a specific height and move slightly upwards. Are there JavaScript or jQuery functions that can achieve this effect? I wou ...

Incorporate a horizontally rotating preloader to enhance loading experience

I am currently in the process of developing a preloader that rotates an image horizontally. After researching different threads and ideas, I came across a solution that looks something like this: <style> .imageRotateHorizontal{ -moz-anima ...

The art of CSS Absolute Positioning and the Science of Body Sc

I'm trying to position an absolute div in the bottom right corner of a page. .absolute{ position: absolute; right:0; bottom: 0; } While this code works, I've noticed that when scrolling the page, the right/bottom position is relative t ...

Tips for effectively downsizing an HTML Canvas to achieve high-quality images

Introduction I am currently facing issues with blurry visuals in my canvas animation, especially on devices with high pixel densities like mobile and retina screens. In order to address this problem, I have researched canvas down-scaling techniques and f ...

Mobile site experiencing owl.carousel responsiveness issues after refreshing the page

I am currently working on a website located at . On the homepage, right after the slider, there is a carousel of three info boxes. However, in mobile view (developer mode), after a hard refresh or viewing the link on an actual mobile device, it displays a ...