What is the best way to ensure that a bootstrap collapse feature is fully

How can I display the collapse button and text only on mobile devices while showing the uncollapsed content on desktop?

I am considering using an example to display part of the text initially, with the rest revealed upon clicking a button. This method is suitable for mobile devices.

<p>
Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
</p>
<p>
    <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
        Link with href
    </a>
    <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
        Button with data-target
    </button>
</p>
<div class="collapse" id="collapseExample">
    <div class="card card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
    </div>
</div>

However, I'm unsure about how to make all the text visible on desktop while hiding the buttons.

<p class="mobile-show-more">
    Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
</p>

To hide the buttons, you can simply add:

.mobile-show-more {
    display: none;
}

I was thinking of using a media query like:

@media only screen and (max-width: 600px) {

But I'm not sure what the class should look like in this case. Using the '.mobile-show-more' class for desktop text would lead to duplicate content.

Answer №1

To achieve the desired effect of hiding certain elements on smaller screens and displaying them on larger screens, as well as the opposite scenario, Bootstrap’s display properties can be utilized. These display properties adhere to Bootstrap’s standard breakpoints, but customization is possible through SCSS.

A practical example from Bootstrap's documentation:

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb8984849f989f998a9babdfc5ddc5db">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2808d8d969196908392a2d6ccd4ccd2">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

<p>
        <a class="btn btn-primary d-md-none" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
            Link with href
        </a>
        <button class="btn btn-primary d-md-none" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
            Button with data-target
        </button>
    </p>
    <div class="collapse d-md-block" id="collapseExample">
        <div class="card card-body">
            Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
        </div>
    </div>

Enhancement 1

Addressing the query regarding displaying content solely on wider screens while being hidden on smaller screens until activated by a button, a specific row/column layout for the concealed content must be established.

$(".collapse").on("shown.bs.collapse", function (e) {
    var $buttonElement = $($(e.target).data('bs.collapse')._triggerArray);

    $buttonElement.parent().hide("fast");
})
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c3e3333282f282e3d2c1c68726a726c">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="670d161202151e275449524956">[email&#160;protected]</a>/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="791b16160d0a0d0b1809394d574f5749">[email\xa0protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

<div class="container-sm my-3">
<h1>One column hides</h1>
... (additional content)
...
<div class="container-sm">
<h1>Two columns hide</h1>
... (additional content)
...

Enhancement 2

Regarding your secondary inquiry, I have enhanced the snippet above to include a listener for Bootstrap’s collapse elements utilizing the shown.bs.collapse event. Upon revealing a collapsed division, a jQuery command triggers the hiding of the associated button that triggered the expansion.

Opting for a hide animation ensures an unobtrusive experience for sighted users, providing a visual cue of the button's concealment. This approach caters to visitors using assistive technology who may benefit from a more subtle transition.

Although leaving the button visible post-expansion is generally acceptable, concealing it entirely is indeed feasible, albeit requiring the full version of jQuery for its animation capabilities, unlike the slim version.

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

Python Selenium for Element Detection

I am a beginner when it comes to using Selenium and I am currently seeking guidance on how to locate the element that is highlighted in this image: https://i.sstatic.net/uXf4u.png Despite my attempts, the following code resulted in an error message being ...

In Firefox mobile, a fixed positioned element will move with the page until the scrolling comes to a halt

I am facing an issue with a CSS element set to position: fixed. While it works perfectly on desktop browsers, it behaves poorly on mobile browsers, especially Firefox. The main problem is that the fixed element scrolls along with the page and then abrupt ...

Prevent the line from crossing over the circle in CSS

How can I prevent the line from intersecting the circle? I want the line to be outside the circle. The background (in this case, of the li tag) will be transparent. #project-wrapper { width: 100%; /* Adjusting the position of the element on the pa ...

Adjust image source based on media query (CSS or JavaScript)

Is there a way to update the image src based on a media query with a maximum width of 515px? I'm considering using JavaScript, jQuery, or even AngularJS if there is an event equivalent to a CSS media query that can achieve this. I want to have differ ...

Refreshing the Table to Update Data

I am working on a jquery application that includes a table with time and number fields. My goal is to increment the values every 3 seconds by creating a function to update the numbers automatically. Initially, the data in the table looks like this: Kobe ...

How can I create a reverse animation using CSS?

Is there a way to reverse the animation of the circular notification in the code provided? I want the circle to move forward, covering up the info and fading out. I've tried switching the animation around but haven't had success. Any suggestions? ...

Adjust picture to fit in div and align vertically

JSFiddle In my fluid layout, I have a div with a required aspect ratio of 1:1. This div can contain text, an image, or both. The challenge is to vertically align the text and ensure the image fits within the div without exceeding its boundaries or losing ...

Searching and indexing HTML content using Solrj in Java

I have knowledge on how to index a downloaded HTML page using the following code: ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract"); up.addFile(new File(fileName), solrId); up.setParam("literal.id", solrId); up ...

problem arises when I attempt to use the code individually, as well as when I incorporate it into my existing

<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>App Title</title> <!-- Framework's CSS Fil ...

What is the title of a document that is linked behind an HTML link?

I am seeking a way to automatically retrieve documents from web pages using a Python script. The links in the HTML pages appear as follows: href="https://foo.bar/view.php?id=123456" When clicked on in a web browser, these links open the document with its ...

Angular Offline App Failing to Function Properly in Offline Mode

Our Single Page HTML5 offline app utilizes AngularJS, Breeze, and ASP.NET MVC to store all files in the cache for seamless offline use. A manifest file lists all the necessary files, including Angular templates, which are loaded using standard Angular rout ...

Organize data in a Vue.js table

Currently facing an issue with sorting my table in vue.js. Looking to organize the table so that campaigns with the highest spend are displayed at the top in descending order. Here is the code I'm working with: <template> <div class=" ...

Is there a way for me to design a button that includes an image?

I'm looking to create a button on my webpage using an image that will link to other pages. I want the flexibility to use both small and large text on this button. The specific image I want to use is: So far, I've attempted some coding but haven ...

When I click on my navbar toggle button, why isn't my text displaying?

I'm experiencing an issue with my navbar toggle. The toggle button works fine, but when I click on it, the text inside the button doesn't show up. I've checked the Bootstrap docs to make sure I didn't miss any steps, but I can't se ...

The functionality of the "this" keyword appears to be malfunctioning

I've been grappling with the concept of the this keyword in JavaScript and decided to create this script to test it out: function click(){ this.innerHTML="changed"; } However, when I tried to use it in this HTML: <button id="clicker" onclick ...

When using Javascript's querySelector, often times it returns null

Here is the HTML code I am working with: <button class="pop-btn"> Pop </button> While I was able to style this button using CSS, I encountered a problem when trying to select it in Javascript: const Population_div_Button=document. ...

How to dynamically insert an em tag into a table header cell using Asp.net

In my code, I have a table with dynamically generated table headers. One of the headers is for a textbox column. var thc = new TableHeaderCell { Text = "Iteration", CssClass = "iteration" } Now, I need to modify the logic to display a label indicating wh ...

Changing the order of columns in Bootstrap 4 beta

My issue mirrors a situation discussed in this thread - Bootstrap: reorganizing 3 columns on smaller devices The solution provided in that conversation was sufficient, however it seems to be incompatible with the most recent Bootstrap version. Is there a ...

Obtain the complete source code by using the responseText property of the XMLHttpRequest object

I am looking to retrieve the entire source code of a webpage, such as www.google.com. Currently, I have a PHP file that can copy the source code of a page: <?php echo file_get_contents('http://www.google.com'); ?> Here is my function ...

I am experiencing difficulties when attempting to insert an email link using Codeigniter 3's framework-specific syntax. The function does not

In my Codeignieter 3 project, I am faced with the task of displaying email addresses stored in the database. When using the code <a href="mailto:<?php echo $record->email; ?>"><?php echo $record->email; ?></a></td>, I e ...