Margin

    Add margin to a single side

    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.

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

    Margin - 图2

    1. <div class="bg-gray-400"><span class="mx-8 bg-yellow-200">Target</span></div>

    Add vertical margin

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

    1. <div class="bg-gray-400"><span class="my-8 bg-yellow-200">Target</span></div>

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

    Margin - 图4

    Negative margins

    Control the negative margin of an element using the -m{side?}-{size} utilities.

    1. <div class="bg-gray-400 h-16 w-32"></div>
    2. -mt-8
    3. </div>

    For more information about Tailwind’s responsive design features, check out the documentation.

    Margin - 图6

    all

    sm

    Margin - 图8

    md

    lg

    Margin - 图10

    xl

    1. <span class="mt-8 sm:mr-6 md:mb-4 lg:ml-2 xl:m-0 bg-yellow-200">Target</span>
    2. </div>

    Customizing

    By default Tailwind provides 20 margin utilities for each side and axis.

    If you’d like to customize these values for padding, margin, width, and height all at once, use the theme.spacing section of your tailwind.config.js file.

    To customize only the margin values, use the theme.margin section of your tailwind.config.js file.

    1. // tailwind.config.js
    2. module.exports = {
    3. theme: {
    4. margin: {
    5. + sm: '8px',
    6. + md: '16px',
    7. + lg: '24px',
    8. + xl: '48px',
    9. }
    10. }

    Learn more about customizing the default theme in the .

    If you’d like to add additional negative margin classes (taking the form -m{side?}-{size}), prefix the keys in your config file with a dash:

    1. // tailwind.config.js
    2. module.exports = {
    3. theme: {
    4. margin: {
    5. + '-72': '-18rem',
    6. }
    7. }
    8. }
    9. }

    Tailwind is smart enough to generate classes like -mx-72 when it sees the leading dash, not mx--72 like you might expect.

    By default, only responsive variants are generated for margin utilities.

    You can control which variants are generated for the margin utilities by modifying the margin property in the variants section of your tailwind.config.js file.

    For example, this config will also generate hover and focus variants:

    If you don’t plan to use the margin utilities in your project, you can disable them entirely by setting the margin property to false in the corePlugins section of your config file:

    1. // tailwind.config.js
    2. module.exports = {
    3. corePlugins: {
    4. // ...
    5. + margin: false,
    6. }

    ← Padding