bulma/docs/documentation/sass/mixins.html

531 lines
15 KiB
HTML
Raw Normal View History

2021-09-19 00:23:53 +00:00
---
2024-03-21 16:11:54 +00:00
title: Bulma Sass Mixins
layout: docs
theme: sass
doc-tab: sass
2021-09-19 00:23:53 +00:00
doc-subtab: mixins
breadcrumb:
2024-03-21 16:11:54 +00:00
- home
- documentation
- sass
- sass-mixins
2021-09-19 00:23:53 +00:00
---
<div class="content">
<p>
Throughout the codebase, Bulma uses Sass mixins to <strong>enhance</strong> and facilitate the CSS output. While these mixins are mainly used within the context of Bulma, they are of course available for you to <strong>re-use</strong> in your own projects.
</p>
</div>
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor.html name="Element Mixins" %}
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
<p class="block">
These mixins create a <strong>visual</strong> HTML element.
</p>
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Arrow" %}
2021-09-19 00:23:53 +00:00
<div class="content">
<p>
The <code>arrow()</code> mixin creates a down-facing arrow. The <code>$color</code> parameter defines the color of the arrow.
</p>
</div>
{% highlight sass %}.bulma-arrow-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.arrow(purple);
2021-09-19 00:23:53 +00:00
}{% endhighlight %}
{% capture arrow %}
<span class="bulma-arrow-mixin"></span>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=arrow %}
2021-09-19 00:23:53 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Burger" %}
2021-09-19 00:23:53 +00:00
<div class="content">
<p>
2024-03-21 16:11:54 +00:00
The <code>burger()</code> mixin creates a 16x16px set of <strong>3 horizontal bars</strong> grouped within square. The dimensions of this square are defined by the <code>$dimensions</code> parameter.
2021-09-19 00:23:53 +00:00
</p>
</div>
2024-03-21 16:11:54 +00:00
{% highlight sass %}.bulma-burger-mixin {
@include mixins.burger(2.5rem);
2021-09-19 00:23:53 +00:00
}{% endhighlight %}
2024-03-21 16:11:54 +00:00
{% capture burger %}
<button class="bulma-burger-mixin">
<span></span>
2021-09-19 00:23:53 +00:00
<span></span>
<span></span>
<span></span>
</button>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=burger %}
2021-09-19 00:23:53 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Delete" %}
2021-09-19 00:23:53 +00:00
<div class="content">
<p>
The <code>delete()</code> mixin creates a 20x20px circle containing a <strong>cross</strong>. It comes with 3 modifiers: <code>is-small</code>, <code>is-medium</code> and <code>is-large</code>.
</p>
</div>
{% highlight sass %}.bulma-delete-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.delete;
2021-09-19 00:23:53 +00:00
}{% endhighlight %}
{% capture delete %}
<button class="bulma-delete-mixin is-small"></button>
<button class="bulma-delete-mixin"></button>
<button class="bulma-delete-mixin is-medium"></button>
<button class="bulma-delete-mixin is-large"></button>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=delete %}
2021-09-19 00:23:53 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Loader" %}
2021-09-19 00:23:53 +00:00
2021-09-19 00:38:25 +00:00
<div class="content">
<p>
The <code>loader()</code> mixin creates a 1em <strong>spinning circle</strong>.
</p>
</div>
{% highlight sass %}.bulma-loader-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.loader;
2021-09-19 00:38:25 +00:00
}{% endhighlight %}
{% capture loader %}
<span class="bulma-loader-mixin"></span>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=loader %}
2021-09-19 00:38:25 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Font Awesome container" %}
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
The <code>fa()</code> mixin creates a <strong>square inline-block element</strong>, ideal for containing a Font Awesome icon, or any other type of icon font.
<br>
The first <code>$size</code> parameter defines the icon font size.
<br>
The second <code>$dimensions</code> parameter defines the dimensions of the square container.
</p>
</div>
{% highlight sass %}.bulma-fa-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.fa(1rem, 2rem);
2021-09-19 17:08:24 +00:00
background-color: lavender; // For demo purposes
}{% endhighlight %}
{% capture fa %}
<span class="bulma-fa-mixin">
<i class="fas fa-thumbs-up"></i>
</span>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=fa %}
2021-09-19 17:08:24 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor.html name="CSS Mixins" %}
2021-09-19 17:08:24 +00:00
<p class="block">
These mixins add <strong>CSS rules</strong> to the element.
</p>
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Block" %}
2021-09-19 00:38:25 +00:00
<div class="content">
<p>
The <code>block()</code> mixin adds <strong>spacing</strong> below an element, but only if it's <strong>not the last child</strong>.
<br>
The <code>$spacing</code> parameter defines the value of the <code>margin-bottom</code>.
</p>
</div>
{% highlight sass %}.bulma-block-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.block(1rem);
2021-09-19 00:38:25 +00:00
}{% endhighlight %}
{% capture block %}
<p class="bulma-block-mixin">This element has a margin-bottom.</p>
<p class="bulma-block-mixin">This element too.</p>
<p class="bulma-block-mixin">Not this one because it's the last child.</p>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=block %}
2021-09-19 00:38:25 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Overlay" %}
2021-09-19 00:38:25 +00:00
<div class="content">
<p>
2021-09-19 17:08:24 +00:00
The <code>overlay()</code> mixin makes the element <strong>cover</strong> its closest positioned ancestor.
2021-09-19 00:38:25 +00:00
<br>
2021-09-19 17:08:24 +00:00
The <code>$offset</code> parameter defines how far inside the element is positioned from each edge (top, bottom, left and right).
</p>
</div>
{% highlight sass %}.bulma-overlay-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.overlay(1.5rem);
2021-09-19 17:08:24 +00:00
background-color: darkorange;
border-radius: 0.25em;
color: white;
opacity: 0.9;
padding: 1em;
}{% endhighlight %}
{% capture overlay %}
<div class="bulma-overlay-mixin-parent">
<div class="bulma-overlay-mixin">Overlay element</div>
</div>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=overlay %}
2021-09-19 17:08:24 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Center" %}
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
The <code>center()</code> mixin allows you to absolutely position an element with <strong>fixed dimensions</strong> at the <strong>center</strong> of its closest positioned ancestor.
2021-09-19 00:38:25 +00:00
<br>
2021-09-19 17:08:24 +00:00
The value of the <code>$width</code> parameter should be the width of the element the mixin is applied on.
<br>
Unless the element has square dimensions, the second parameter <code>$height</code> is required and its value should be the height of the element the mixin is applied on.
2021-09-19 00:38:25 +00:00
</p>
</div>
2021-09-19 17:08:24 +00:00
{% highlight sass %}.bulma-center-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.center;
2021-09-19 00:38:25 +00:00
}{% endhighlight %}
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
{% capture center %}
<div class="bulma-center-mixin-parent">
<img class="bulma-center-mixin" height="96" width="96" src="https://source.unsplash.com/mEZ3PoFGs_k/192x192">
</div>
2021-09-19 00:38:25 +00:00
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=center %}
2021-09-19 17:08:24 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Placeholder" %}
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
The <code>placeholder()</code> mixin allows you to change the style of an <strong>input's placeholder</strong>.
<br>
The <code>$offset</code> parameter defines how far inside the element is positioned from each edge (top, bottom, left and right).
</p>
</div>
{% highlight sass %}.bulma-placeholder-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.placeholder {
2021-09-19 17:08:24 +00:00
color: lightblue;
}
}{% endhighlight %}
{% capture placeholder %}
<input
class="input bulma-placeholder-mixin"
type="text"
placeholder="I am a styled placeholder"
>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=placeholder %}
2021-09-19 00:38:25 +00:00
<!-- -->
2021-09-19 00:23:53 +00:00
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Clearfix" %}
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
The <code>clearfix()</code> mixin adds a <code>::after</code></strong> pseudo-element with a <code>clear: both</code> rule.
</p>
</div>
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
{% highlight sass %}.bulma-clearfix-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.clearfix;
2021-09-19 17:08:24 +00:00
}{% endhighlight %}
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
{% capture clearfix %}
<div class="bulma-clearfix-mixin">
<img height="80" width="80" style="float: left;" src="https://source.unsplash.com/La2kOu2dmH4/160x160">
<p>This is a short image description.</p>
</div>
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
<p>This text is following the clearfix element and is correctly placed after.</p>
{% endcapture %}
2021-09-19 00:23:53 +00:00
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=clearfix %}
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
<!-- -->
2021-09-19 00:23:53 +00:00
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Reset" %}
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
The <code>reset()</code> mixin applies a soft style reset. This is especially useful for <code>&lt;button&gt;</code> elements.
</p>
</div>
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
{% highlight sass %}.bulma-reset-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.reset;
2021-09-19 17:08:24 +00:00
}{% endhighlight %}
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
{% capture reset %}
<button>Default button</button>
<button class="bulma-reset-mixin">Reset button</button>
{% endcapture %}
2021-09-19 00:23:53 +00:00
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=reset %}
2021-09-19 00:23:53 +00:00
2021-09-19 17:08:24 +00:00
<!-- -->
2021-09-19 00:23:53 +00:00
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Unselectable" %}
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
The <code>unselectable()</code> mixin makes an element not selectable. This is to prevent the text to be selected when double-clicked.
</p>
</div>
{% highlight sass %}.bulma-unselectable-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.unselectable;
2021-09-19 17:08:24 +00:00
}{% endhighlight %}
{% capture unselectable %}
<p>This text is selectable.</p>
<p class="bulma-unselectable-mixin">This text is not selectable.</p>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=unselectable %}
2021-09-19 17:08:24 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Overflow Touch" %}
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
The <code>overflow-touch()</code> mixin add the <code>-webkit-overflow-scrolling: touch;</code> rule to the element.
</p>
</div>
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor.html name="Direction Mixins" %}
2021-09-19 17:08:24 +00:00
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="Left-to-right and Right-to-left Mixins" %}
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
Bulma has a global <code>$rtl</code> flag, which allows the framework to output a Right-to-left version of the CSS. By default, this flag's value is set to <code>false</code>. This means the framework output a Left-to-right version.
</p>
<p>
The mixins <code>@mixin ltr</code> and <code>@mixin rtl</code> are here to output CSS rules based on the value of <code>$rtl</code>:
</p>
<ul>
<li>
2024-03-21 16:11:54 +00:00
if <code>$rtl: true</code>, <code>@include mixins.ltr</code> outputs nothing
2021-09-19 17:08:24 +00:00
</li>
<li>
2024-03-21 16:11:54 +00:00
if <code>$rtl: false</code>, <code>@include mixins.rtl</code> outputs nothing
2021-09-19 17:08:24 +00:00
</li>
</ul>
<p>
This is useful for properties that are specific to the side of an element.
</p>
</div>
{% highlight sass %}.bulma-ltr-rtl-mixin {
background-color: lightgreen;
padding: 0.5em 1em;
border: 1px solid seagreen;
margin-right: -1px;
&:first-child {
2024-03-21 16:11:54 +00:00
@include mixins.ltr {
2021-09-19 17:08:24 +00:00
border-bottom-left-radius: 0.5em;
border-top-left-radius: 0.5em;
}
2024-03-21 16:11:54 +00:00
@include mixins.rtl {
2021-09-19 17:08:24 +00:00
border-bottom-right-radius: 0.5em;
border-top-right-radius: 0.5em;
}
}
&:last-child {
2024-03-21 16:11:54 +00:00
@include mixins.ltr {
2021-09-19 17:08:24 +00:00
border-bottom-right-radius: 0.5em;
border-top-right-radius: 0.5em;
}
2024-03-21 16:11:54 +00:00
@include mixins.rtl {
2021-09-19 17:08:24 +00:00
border-bottom-left-radius: 0.5em;
border-top-left-radius: 0.5em;
}
}
}{% endhighlight %}
{% capture ltr-rtl %}
<div style="display: flex;">
<span class="bulma-ltr-rtl-mixin">One</span>
<span class="bulma-ltr-rtl-mixin">Two</span>
<span class="bulma-ltr-rtl-mixin">Three</span>
</div>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=ltr-rtl %}
2021-09-19 17:08:24 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="LTR Position" %}
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
The <code>ltr-position()</code> mixin is a quick way to switch between <code>left</code> and <code>right</code> CSS properties when dealing with positioned elements.
<br>
The first <code>$spacing</code> parameter defines the value of the offset, whether it's right or left.
<br>
The second <code>$right</code> parameter defines if the property outputs <code>right</code> (default) or <code>left</code>.
</p>
<p>
Here is what the output looks like with a <code>$spacing</code> parameter set to <code>1rem</code>:
</p>
<table class="table is-bordered">
<thead>
<tr>
<th>Flag →</th>
<th><code>$rtl: false;</code></th>
<th><code>$rtl: true;</code></th>
</tr>
</thead>
<tbody>
<tr>
2024-03-21 16:11:54 +00:00
<td><code>@include mixins.ltr-position(1rem, true)</code></td>
2021-09-19 17:08:24 +00:00
<td><code>right: 1rem</code></td>
<td><code>left: 1rem</code></td>
</tr>
<tr>
2024-03-21 16:11:54 +00:00
<td><code>@include mixins.ltr-position(1rem, false)</code></td>
2021-09-19 17:08:24 +00:00
<td><code>left: 1rem</code></td>
<td><code>right: 1rem</code></td>
</tr>
</tbody>
</table>
</div>
<p class="title is-6">Sass source</p>
{% highlight sass %}.bulma-ltr-position-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.ltr-position(1rem, false);
2021-09-19 17:08:24 +00:00
border-radius: 0.25em;
position: absolute;
top: 1rem;
}{% endhighlight %}
<p class="title is-6">CSS output</p>
{% highlight css %}.bulma-ltr-position-mixin {
left: 1rem;
border-radius: 0.25em;
position: absolute;
top: 1rem;
}{% endhighlight %}
{% capture ltr-position %}
<div class="bulma-ltr-position-mixin-parent">
<img class="bulma-ltr-position-mixin" height="48" width="48" src="https://source.unsplash.com/iFgRcqHznqg/96x96">
<p>Cras mattis consectetur purus sit amet fermentum. Nulla vitae elit libero, a pharetra augue. Aenean lacinia bibendum nulla sed consectetur. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Curabitur blandit tempus porttitor. Maecenas faucibus mollis interdum.</p>
</div>
{% endcapture %}
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=ltr-position %}
2021-09-19 17:08:24 +00:00
<!-- -->
2024-03-21 16:11:54 +00:00
{% include docs/elements/anchor-bis.html name="LTR Property" %}
2021-09-19 17:08:24 +00:00
<div class="content">
<p>
The <code>ltr-property()</code> mixin works like the <code>ltr-position()</code> mixin but allows you to choose <strong>which CSS property</strong> to set. The mixin will append <code>-right</code> or <code>-left</code> to that property. This is especially useful for <code>margin</code> and <code>padding</code>.
<br>
The first <code>$property</code> parameter which property you want to "flip" left and right.
<br>
The second <code>$spacing</code> parameter defines the value of the offset, whether it's right or left.
<br>
The third <code>$right</code> parameter defines if the property outputs <code>right</code> (default) or <code>left</code>.
</p>
<p>
Here is what the output looks like with a <code>$spacing</code> parameter set to <code>1rem</code>:
</p>
<table class="table is-bordered">
<thead>
<tr>
<th>Flag →</th>
<th><code>$rtl: false;</code></th>
<th><code>$rtl: true;</code></th>
</tr>
</thead>
<tbody>
<tr>
2024-03-21 16:11:54 +00:00
<td><code>@include mixins.ltr-property("margin", 1rem, true)</code></td>
2021-09-19 17:08:24 +00:00
<td><code>margin-right: 1rem</code></td>
<td><code>margin-left: 1rem</code></td>
</tr>
<tr>
2024-03-21 16:11:54 +00:00
<td><code>@include mixins.ltr-property("margin", 1rem, false)</code></td>
2021-09-19 17:08:24 +00:00
<td><code>margin-left: 1rem</code></td>
<td><code>margin-right: 1rem</code></td>
</tr>
</tbody>
</table>
</div>
<p class="title is-6">Sass source</p>
{% highlight sass %}.bulma-ltr-property-mixin {
2024-03-21 16:11:54 +00:00
@include mixins.ltr-property("margin", 1rem, false);
2021-09-19 17:08:24 +00:00
border-radius: 0.25em;
}{% endhighlight %}
<p class="title is-6">CSS output</p>
{% highlight css %}.bulma-ltr-property-mixin {
margin-left: 1rem;
border-radius: 0.25em;
}{% endhighlight %}
{% capture ltr-property %}
<div class="bulma-ltr-property-mixin-parent">
<p>Cras mattis consectetur purus sit amet fermentum. Nulla vitae elit libero, a pharetra augue. Aenean lacinia bibendum nulla sed consectetur. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Curabitur blandit tempus porttitor. Maecenas faucibus mollis interdum.</p>
<img class="bulma-ltr-property-mixin" height="96" width="96" src="https://source.unsplash.com/jTSf1xnsoCs/192x192">
</div>
{% endcapture %}
2021-09-19 00:23:53 +00:00
2024-03-21 16:11:54 +00:00
{% include docs/elements/snippet.html content=ltr-property %}