Margin

    Utilities for controlling an element’s margin.

    Show all classes

    Control the margin on one side of an element using the utilities.

    For example, mt-6 would add 1.5rem of margin to the top of an element, mr-4 would add 1rem of margin to the right of an element, mb-8 would add 2rem of margin to the bottom of an element, and ml-2 would add 0.5rem of margin to the left of an element.

    Add horizontal margin

    Control the horizontal margin of an element using the mx-{size} utilities.

    Margin - 图2

    1. <div class="mx-8 ...">mx-8</div>

    Add vertical margin

    Control the vertical margin of an element using the my-{size} utilities.

    1. <div class="my-8 ...">my-8</div>

    Control the margin on all sides of an element using the m-{size} utilities.

    Using negative values

    To use a negative margin value, prefix the class name with a dash to convert it to a negative value.

    Margin - 图5

    1. <div class="w-36 h-16 bg-sky-400 opacity-20 ..."></div>
    2. <div class="-mt-8 bg-sky-300 ...">-mt-8</div>

    Using logical properties

    Use the ms-* and me-* utilities to set the and margin-inline-end logical properties, which map to either the left or right side based on the text direction.

    1. <div dir="ltr">
    2. <div class="ms-8 ...">ms-8</div>
    3. <div class="me-8 ...">me-8</div>
    4. <div>
    5. <div dir="rtl">
    6. <div class="ms-8 ...">ms-8</div>
    7. <div class="me-8 ...">me-8</div>
    8. <div>

    For more control, you can also use the to conditionally apply specific styles depending on the current text direction.


    Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:mt-8 to only apply the mt-8 utility on hover.

    Breakpoints and media queries

    You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:mt-8 to apply the mt-8 utility at only medium screen sizes and above.

    1. <!-- ... -->
    2. </div>

    To learn more, check out the documentation on , Dark Mode and .


    Customizing your theme

    By default, Tailwind’s margin scale uses the default spacing scale. You can customize your spacing scale by editing or theme.extend.spacing in your tailwind.config.js file.

    tailwind.config.js

    1. module.exports = {
    2. theme: {
    3. extend: {
    4. spacing: {
    5. '5px': '5px',
    6. }
    7. }
    8. }
    9. }

    Alternatively, you can customize just the margin scale by editing theme.margin or theme.extend.margin in your tailwind.config.js file.

    tailwind.config.js

    Learn more about customizing the default theme in the documentation.

    If you need to use a one-off margin value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

    1. <div class="m-[5px]">
    2. <!-- ... -->