Margin
A collection of utility classes to add spacing between elements.
Usage
To apply this component, add one of the following classes to any element. If the element is preceded by another element, a top margin will be applied. Otherwise, a bottom margin will always be applied.
Class | Size |
---|---|
.uk-margin-xsmall | 0.25rem or 4px |
.uk-margin-small | 0.5rem or 8px |
.uk-margin | 1rem or 16px |
.uk-margin-medium | 2.5rem or 40px |
.uk-margin-large | 5rem or 80px |
.uk-margin-xlarge | 10rem or 160px |
<div class="uk-margin"></div>
Position
To add margin to a specific side, use the uk-margin-[size]-[side]
format. For example, uk-margin-small-top
, uk-margin-xsmall-bottom
, etc.
<div class="uk-margin-small-top"></div>
Remove margin
Add one of the following classes to remove the margin from block elements.
Class | Description |
---|---|
.uk-margin-remove | Removes all margins. |
.uk-margin-remove-top | Removes top margin. |
.uk-margin-remove-bottom | Removes bottom margin. |
.uk-margin-remove-left | Removes left margin. |
.uk-margin-remove-right | Removes right margin. |
.uk-margin-remove-vertical | Removes all vertical margins. |
.uk-margin-remove-adjacent | Removes the top margin of the directly succeeding element. |
.uk-margin-remove-first-child | Removes the top margin of the first child element. |
.uk-margin-remove-last-child | Removes the bottom margin of the last child element. |
<h1 class="uk-margin-remove"></h1>
Responsive
UIkit provides a number of responsive classes to remove margins. Basically, they work just like the usual margin remove classes, except that they have suffixes that represent the breakpoint from which they come into effect.
Class | Description |
---|---|
.uk-margin-remove-left@s .uk-margin-remove-right@s | Affects device widths of 640px and larger. |
.uk-margin-remove-left@m .uk-margin-remove-right@m | Affects device widths of 768px and larger. |
.uk-margin-remove-left@l .uk-margin-remove-right@l | Affects device widths of 1024px and larger. |
.uk-margin-remove-left@xl .uk-margin-remove-right@xl | Affects device widths of 1280px and larger. |
Auto margin
Add one of the following classes to set auto margin. This can be useful to center or otherwise align block elements with a fixed width as well as flex elements.
Class | Description |
---|---|
.uk-margin-auto | Sets left and right margin to auto, horizontally centering block and flex elements. |
.uk-margin-auto-top | Sets top margin to auto, pushing block and flex elements to the bottom. |
.uk-margin-auto-bottom | Sets bottom margin to auto, pushing block and flex elements to the top. |
.uk-margin-auto-left | Sets left margin to auto, pushing block and flex elements to the right. |
.uk-margin-auto-right | Sets right margin to auto, pushing block and flex elements to the left |
.uk-margin-auto-vertical | Sets top and bottom margin to auto, vertically centering only flex elements. |
- Block elementFlex item
-
<div class="uk-width-1-2@s uk-margin uk-margin-auto-left uk-text-center"> Block element </div> <div class="uk-background-muted uk-flex uk-height-medium uk-margin uk-text-center" > <div class="uk-width-1-2@s uk-margin-auto uk-margin-auto-vertical"> Flex item </div> </div>
Responsive
UIkit provides a number of responsive margin auto classes. Basically, they work just like the usual margin auto classes, except that they have suffixes that represent the breakpoint from which they come into effect.
Class | Description |
---|---|
.uk-margin-auto-left@s .uk-margin-auto@s .uk-margin-auto-right@s | Affects device widths of 640px and larger. |
.uk-margin-auto-left@m .uk-margin-auto@m .uk-margin-auto-right@m | Affects device widths of 768px and larger. |
.uk-margin-auto-left@l .uk-margin-auto@l .uk-margin-auto-right@l | Affects device widths of 1024px and larger. |
.uk-margin-auto-left@xl .uk-margin-auto@xl .uk-margin-auto-right@xl | Affects device widths of 1280px and larger. |
- Block elementBlock elementBlock element
-
<div class="uk-width-1-2@s uk-margin-auto@m uk-margin uk-margin-auto-right uk-text-center" > Block element </div> <div class="uk-width-1-2@s uk-margin-auto-left@m uk-margin uk-margin-auto uk-text-center" > Block element </div> <div class="uk-width-1-2@s uk-margin-auto-right@m uk-margin uk-margin-auto-left uk-text-center" > Block element </div>
Dynamic wrapping
To add spacing to stacking elements, for example inline elements that wrap on smaller viewports, just add the uk-margin
attribute to their parent container. It will automatically add the .uk-margin-small-top
class to the lower element.
<div uk-margin>
<button></button>
<button></button>
</div>
-
-
<div uk-margin> <button class="uk-button uk-button-default">Button</button> <button class="uk-button uk-button-default">Button</button> <button class="uk-button uk-button-default">Button</button> <button class="uk-button uk-button-default">Button</button> <button class="uk-button uk-button-default">Button</button> <button class="uk-button uk-button-default">Button</button> <button class="uk-button uk-button-default">Button</button> <button class="uk-button uk-button-default">Button</button> <button class="uk-button uk-button-default">Button</button> <button class="uk-button uk-button-default">Button</button> </div>
Component options
Any of these options can be applied to the component attribute. Separate multiple options with a semicolon. Learn more
Option | Value | Default | Description |
---|---|---|---|
margin | String | uk-margin-small-top | This class is added to items that break into the next row, typically to create a margin for the previous row. |
first-column | String | uk-first-column | This class is added to the first element in each row. |
JavaScript
Learn more about JavaScript components.
Initialization
UIkit.margin(element, options);