Wrap the block in a container if the screen size is less than x

I have an image and a text inside it (using bootstrap 5, but that's not important)

     <div class="carousel-item">
        <img src="https://www.tutorialrepublic.com/lib/images/bootstrap-5.0-illustration.png" class="d-block w-100" alt="...">
        <div class="carousel-caption d-none d-md-block">
          <h5>First slide label</h5>
          <p>Some representative placeholder content for the first slide.</p>
        </div>
      </div>

If the screen size is less than 800px, the text from carousel-caption disappears (display:none;), and I want to wrap

<a href=""> </a>
so people can click on the image to proceed. Therefore, all text code should be changed to this:

<a href="">
         <div class="carousel-item">
            <img src="https://www.tutorialrepublic.com/lib/images/bootstrap-5.0-illustration.png" class="d-block w-100" alt="...">
            <div class="carousel-caption d-none d-md-block">
              <h5>First slide label</h5>
              <p>Some representative placeholder content for the first slide.</p>
            </div>
          </div>
</a>

I'm uncertain if this can be achieved without using javascript.

Answer №1

To update the appearance of your website, simply modify the .carousel-item div to a, delete the pointer-events, and establish the default cursor when the screen width is greater than or equal to 800px.

@media (min-width: 800px) {
  .carousel-item {
    pointer-events: none;
    cursor: default;
  }
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="583a37372c2b2c2a3928186d7669766b">[email protected]</a>/dist/css/bootstrap.min.css">
<a href="https://stackoverflow.com" class="carousel-item d-block">
  <img src="https://www.tutorialrepublic.com/lib/images/bootstrap-5.0-illustration.png" class="d-block w-100" alt="...">
  <div class="carousel-caption d-none d-md-block">
    <h5>First slide label</h5>
    <p>Some representative placeholder content for the first slide.</p>
  </div>
</a>

Answer №2

One method entails the following steps, along with comments within the code:

/* Resetting CSS properties to align element sizes based on
   the border-box algorithm, encompassing borders and padding
   within the specified dimensions of the element,
   whether it's width, height, inline-size, or block-size,
   also eliminating default margins and padding, while setting
   all fonts to 16px: */
*,
 ::before,
::after {
  box-sizing: border-box;
  font-size: 16px;
  margin: 0;
  padding: 0;
}

/* This part is only for demonstrating the effect of media queries,
   otherwise, it's not relevant: */
a div {
  background-color: var(--bgColor);
  transition: background-color 0.3s linear;
}

/* Updating the --bgColor custom property as part of the demo to display
   functionality of media-queries; this can be removed as it's irrelevant
   to functionality: */
a[data-hidden-by-pagesize] {
  --bgColor: hsl(0deg 50% 90% / 1);
  /* Using display: contents to position elements as if the <a> tag wasn't present,
     a pseudo "unwrap": */
  display: contents;
  /* Removing pointer-events so that the functionality of <a> elements is
     disabled (even though the requirement isn't entirely clear): */
  pointer-events: none;
}


/* Employing an @media query with 'range' notation to respond when
   document width is less than 800px: */
@media screen and (width < 800px) {
  /* Updating the --bgColor property to showcase media-query functionality: */
  a[data-hidden-by-pagesize] {
    --bgColor: hsl(150deg 50% 90% / 1);
    /* Setting display back to 'default' or 'initial' state: */
    display: initial;
    /* Undoing pointer-events to restore default functionality to <a> tags: */
    pointer-events: unset;
  }
}
<!-- Using the 'date-hidden-by-pagesize' attribute for a 'meaningful'
     selector for relevant <a> elements, adjust as per your use-case: -->
<a href="#demo" data-hidden-by-pagesize="">
  <div class="carousel-item">
    <img src="https://www.tutorialrepublic.com/lib/images/bootstrap-5.0-illustration.png" class="d-block w-100" alt="...">
    <div class="carousel-caption d-none d-md-block">
      <h5>First slide label</h5>
      <p>Some placeholder content for the first slide.</p>
    </div>
  </div>
</a>

JS Fiddle demonstration.

References:

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

fill the designated column in accordance with the specific criteria

Is there a method to automatically fill in a specific column based on certain conditions? I am looking to populate the column labeled [Last] when the column index is 1 and the corresponding name is [First]. import {Component, OnInit} from '@angular ...

Explore the world of threejs with a sphere adorned with cube bumps

Currently, I am navigating through the internet in search of a solution for my problem. The example I came across is quite impressive, take a look: . My dilemma lies in converting these squares into CSS cubes while ensuring that they do not get cut off by ...

Putting the `href` attribute around an `input` of

Why is the <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" type="text/css" href=" ...

How do you position items in the center and lower right corner of a flexbox that takes up the entire

One feature on my website is a flex box that takes up the entire window with a width of 100% and a height of 100vh. Within this flex box, I've centered text and a button, but now I'd like to add a footer to the page. If I simply place the footer ...

What is causing my background image to move upward when I include this JavaScript code for FlashGallery?

There's an issue on my website where adding a flash gallery script is causing the background image to shift unexpectedly. You can view the affected page here: The culprit seems to be the "swfobject.js" script. I've identified this by toggling t ...

Exporting an HTML table to Excel with JavaScript runs into issues when it encounters the '#' character

Looking for a JavaScript solution to export HTML tables to Excel. I attempted a script that exports tables, but it stops when encountering special characters like '#'. Can someone assist me with this issue? Thank you in advance. <script src= ...

Adjust the vertical position of the image with HTML and CSS

I want to adjust the positioning of the image below the blue navigation bar so that it aligns with the main content area below, creating the appearance of a shaded box around the webpage's content. The image in question is boy_top.png. Is there a way ...

Tips for keeping the menu open even when you're not hovering over it with your cursor

I put together a stylish drop-down menu based on some web examples, but my manager pointed out that it can be inconvenient to use because the menu closes when the mouse moves off of it. I've tried various workarounds as outlined here, but none have in ...

Is there a way I can maintain the active state after clicking on a link?

Is there a way to maintain an active element state when navigating to another page after clicking a link? I am wondering if it is possible to use JavaScript to remove the active state from one link and apply it to the next one. I am facing some challenges ...

Using a combination of jQuery and JavaScript to switch image tags between different classes

I'm really struggling with this issue and could use some guidance. Essentially, I have a code that should change the class of an img tag when a user clicks on a div element. Let's take a look at the HTML structure: <li><img src ...

In JavaScript, you can update the class named "active" to become the active attribute for a link

My current menu uses superfish, but it lacks an active class to highlight the current page tab. To rectify this, I implemented the following JavaScript code. <script type="text/javascript"> var path = window.location.pathname.split('/'); p ...

How CSS can affect the layout of elements on a webpage

I am looking to achieve something similar to this: The left box should maintain a fixed width, while the right one resizes to fit the full width of the browser window. I also need the height of both boxes to be resizable. Apologies for any inconvenience ...

I am having trouble aligning the element perfectly in the center

I'm working on a radio input design featuring a circle in the middle, achieved via the pseudo element ::before However, I've noticed that when the input size is an odd number, the centering isn't quite perfect This issue seems more promine ...

Determine the amount of interconnected nodes listed in a csv file depicting their relationships

I have a sizable CSV document formatted as follows: ServerID|AppID 01 | 01 01 | 02 02 | 02 02 | 03 02 | 04 03 | 04 The information from this file is being used in a d3 force layout, demonstrated in this example. The cr ...

The function of jQuery's .prop('defaultSelected') appears to be unreliable when used in Internet Explorer 9

Below is the code I am currently using: $selects = $('select'); $selects.val( $selects.prop('defaultSelected')); The purpose of this code is to reset the values of all select elements on my webpage. However, I am facing an issue in IE ...

How can I add an SVG tooltip that appears when I hover my

I have successfully implemented an SVG map showing marked locations and polygons, with CSS styling that makes the areas stand out on hover. I achieved this without using any JavaScript code. My next goal is to create a hovering window or tooltip that disp ...

Encountered an error while using NPM Bootstrap Carousel: Uncaught TypeError - Super expression must be either null or a function

Currently using the NPM build of Bootstrap (with Gulp) for a custom WordPress theme, and I seem to be facing some issues. While I am new to Bootstrap, I have previous experience with WordPress and Gulp (primarily with Foundation). Below is how I am includ ...

Guide to switch background image using querySelector

I am currently trying to figure out how to set the background image of a div block by using querySelector. I have attempted various methods in my test code below, but unfortunately none seem to be working. Can someone please provide assistance? <!DOC ...

Reduce the size of a webpage by 10%

Have you ever noticed that when you press Ctrl+- on any browser, the page scales down to 90% of its original size? It actually looks nicer at 90%, so I want my webpage to default to opening at 90% instead of 100%. I've tried following common approach ...

Validating the existence of a file through HTTPS in Javascript

After attempting to retrieve the file using this code, I've encountered an issue - it works with HTTP requests but not HTTPS requests (which is what I require). function checkUrl(url) { var request = new XMLHttpRequest(); try { request.ope ...