A plugin that provides utilities for centering an element on x/y axis.
Centering an element using Tailwindcss classes can rapidly become verbose.
Dedicated centering classes at component layer that cover the most common situations.
$ npm i @marcoguidara/tailwindcss-quick-center
// tailwind.config.js
module.exports = {
plugins: [require('@marcoguidara/tailwindcss-quick-center')],
};
<div class="relative">
<div class="center-absolute">
<!-- Centered element -->
</div>
</div>
<!-- or -->
<div class="center-flex">
<div><!-- Centered element --></div>
</div>
<div class="relative">
<div class="center-absolute-x">
<!-- Centered element -->
</div>
</div>
<!-- or -->
<div class="center-flex-x">
<div><!-- Centered element --></div>
</div>
<div class="relative">
<div class="center-absolute-y">
<!-- Centered element -->
</div>
</div>
<!-- or -->
<div class="center-flex-y">
<div><!-- Centered element --></div>
</div>
// tailwind.config.js
module.exports = {
variants: {
// default 'responsive'
quickCenter: ['first'],
},
};
.center-absolute-y {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.center-absolute-x {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.center-absolute {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.center-flex-y {
display: flex;
flex-direction: column;
justify-content: center;
}
.center-flex-x {
display: flex;
flex-direction: column;
align-items: center;
}
.center-flex {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
Tailwind Quick Center is licensed under the MIT License.