Collapse
Vue Bootstrap 5 Collapse component
Toggle the visibility of content across your project with a few classes and our JavaScript plugins.
Note: Read the API tab to find all available options and advanced customization
How it works
The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used
as triggers that are mapped to specific elements you toggle. Collapsing an element will
animate the height
from its current value to 0
. Given how CSS
handles animations, you cannot use padding
on a .collapse
element.
Instead, use the class as an independent wrapping element.
Basic example
Click the buttons below to show and hide another element via class changes:
We use MDBCollapse
component to create collapsible elements.
<template>
<MDBBtn
tag="a"
color="primary"
@click="collapse1 = !collapse1"
aria-controls="collapsibleContent1"
:aria-expanded="collapse1"
>Link</MDBBtn
>
<MDBBtn
color="primary"
@click="collapse1 = !collapse1"
aria-controls="collapsibleContent1"
:aria-expanded="collapse1"
>Button</MDBBtn
>
<MDBCollapse
id="collapsibleContent1"
v-model="collapse1"
>
<div class="mt-3">
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>
</MDBCollapse>
</template>
<script>
import { MDBCollapse, MDBBtn } from "mdb-vue-ui-kit";
import { ref } from 'vue';
export default {
components: {
MDBCollapse,
MDBBtn
},
setup() {
const collapse1 = ref(false);
return {
collapse1
}
}
};
</script>
<script setup lang="ts">
import { MDBCollapse, MDBBtn } from "mdb-vue-ui-kit";
import { ref } from 'vue';
const collapse1 = ref(false);
</script>
Horizontal
The collapse plugin also supports horizontal collapsing. Add the
horizontal
prop and set
width
on the
immediate child element. Feel free to write your own custom Sass, use inline styles, or use
our width utilities.
Please note that while the example below has a min-height
set to avoid
excessive repaints in our docs, this is not explicitly required.
Only the width
on the child element is required.
<template>
<MDBBtn
color="primary"
class="mb-4"
aria-controls="collapsibleContent1"
:aria-expanded="collapse4"
@click="collapse4 = !collapse4">
Button
</MDBBtn>
<MDBCollapse
id="collapsibleContent1"
v-model="collapse4"
horizontal
>
<div style="width: 300px">
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>
</MDBCollapse>
</template>
<script>
import { MDBCollapse, MDBBtn } from "mdb-vue-ui-kit";
import { ref } from 'vue';
export default {
components: {
MDBCollapse,
MDBBtn
},
setup() {
const collapse4 = ref(false);
return {
collapse4
}
}
};
</script>
<script setup lang="ts">
import { MDBCollapse, MDBBtn } from "mdb-vue-ui-kit";
import { ref } from 'vue';
const collapse4 = ref(false);
</script>
Multiple targets
Each collapsible element can target multiple collapsbible contents and each collapsbile content can be targeted by multiple collapsible elements.
<template>
<MDBContainer>
<MDBBtn
color="primary"
@click="collapse2 = !collapse2"
aria-controls="multiCollapseExample2"
:aria-expanded="collapse2"
>Toggle first element</MDBBtn
>
<MDBBtn
color="primary"
@click="collapse3 = !collapse3"
aria-controls="multiCollapseExample3"
:aria-expanded="collapse3"
>Toggle second element</MDBBtn
>
<MDBBtn
color="primary"
@click="
() => {
collapse2 = !collapse2;
collapse3 = !collapse3;
}
"
aria-controls="multiCollapseExample2 multiCollapseExample3"
>Toggle both elements</MDBBtn
>
<MDBRow>
<MDBCol sm="6">
<MDBCollapse v-model="collapse2" id="multiCollapseExample2">
<MDBCard class="mt-2">
<MDBCardBody>
<h5 class="pt-2">Collapsing text</h5>
<p class="pb-2">
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.
</p>
</MDBCardBody>
</MDBCard>
</MDBCollapse>
</MDBCol>
<MDBCol sm="6">
<MDBCollapse v-model="collapse3" id="multiCollapseExample3">
<MDBCard class="mt-2">
<MDBCardBody>
<h5 class="pt-2">Collapsing text</h5>
<p class="pb-2">
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.
</p>
</MDBCardBody>
</MDBCard>
</MDBCollapse>
</MDBCol>
</MDBRow>
</MDBContainer>
</template>
<script>
import { MDBCollapse, MDBContainer, MDBRow, MDBCol, MDBBtn, MDBCard, MDBCardBody } from "mdb-vue-ui-kit";
import { ref } from 'vue';
export default {
components: {
MDBCollapse,
MDBContainer,
MDBRow,
MDBCol,
MDBBtn,
MDBCard,
MDBCardBody
},
setup() {
const collapse2 = ref(false);
const collapse3 = ref(false);
return {
collapse2,
collapse3
}
}
};
</script>
<script setup lang="ts">
import { MDBCollapse, MDBContainer, MDBRow, MDBCol, MDBBtn, MDBCard, MDBCardBody } from "mdb-vue-ui-kit";
import { ref } from 'vue';
const collapse2 = ref(false);
const collapse3 = ref(false);
</script>
Accessibility
Be sure to add aria-expanded
to the control element. This attribute explicitly
conveys the current state of the collapsible element tied to the control to screen readers and
similar assistive technologies. If the collapsible element is closed by default, the attribute
on the control element should have a value of aria-expanded="false"
. If you’ve
set the collapsible element to be open by default using the show
class, set
aria-expanded="true"
on the control instead. The plugin will automatically toggle
this attribute on the control based on whether or not the collapsible element has been opened
or closed (via JavaScript, or because the user triggered another control element also tied to
the same collapsible element). If the control element’s HTML element is not a button (e.g., an
<a>
or <div>
), the attribute
role="button"
should be added to the element.
Note that Bootstrap’s current implementation does not cover the various keyboard interactions described in the WAI-ARIA Authoring Practices 1.1 accordion pattern - you will need to include these yourself with custom JavaScript.
Collapse - API
Import
<script>
import {
MDBCollapse
} from 'mdb-vue-ui-kit';
</script>
Properties
Property | Type | Default | Description | Example |
---|---|---|---|---|
tag |
String | "div" |
Defines tag element wrapping collapsible content. |
<MDBCollapse v-model="collapse">Collapsing
content</MDBCollapse>
|
collapseClass |
String | "" |
Adds custom class for collapsible content wrapper |
<MDBCollapse v-model="collapse"
collapseClass="customClass">Collapsing
content</MDBCollapse>
|
duration |
Number | 300 |
Sets time in milisecond for collapsing animation |
<MDBCollapse v-model="collapse"
:duration="400">Collapsing content</MDBCollapse>
|
horizontal |
Boolean | false |
Sets horizontal animation. |
<MDBCollapse v-model="collapse"
horizontal width="300px">Collapsing content</MDBCollapse>
|
SCSS variables
$transition-collapse: height .35s ease;
$transition-collapse-width: width .35s ease;