Skip to main content

Margin & Padding

Bulma provides a comprehensive spacing system using margin and padding helpers. These utilities allow you to add consistent spacing throughout your application without writing custom CSS.

tip

All components in bestax-bulma have access to these spacing properties through the useBulmaClasses hook. You can apply margin and padding to any component in the library for consistent spacing.

Margin

Use margin props to add external spacing around elements. Margin creates space outside the element's border.

All Sides Margin

PropertyBulma ClassValue
m="0"m-00
m="1"m-10.25rem
m="2"m-20.5rem
m="3"m-30.75rem
m="4"m-41rem
m="5"m-51.5rem
m="6"m-63rem
m="auto"m-autoauto

Top Margin

PropertyBulma ClassValue
mt="0"mt-00
mt="1"mt-10.25rem
mt="2"mt-20.5rem
mt="3"mt-30.75rem
mt="4"mt-41rem
mt="5"mt-51.5rem
mt="6"mt-63rem
mt="auto"mt-autoauto

Right Margin

PropertyBulma ClassValue
mr="0"mr-00
mr="1"mr-10.25rem
mr="2"mr-20.5rem
mr="3"mr-30.75rem
mr="4"mr-41rem
mr="5"mr-51.5rem
mr="6"mr-63rem
mr="auto"mr-autoauto

Bottom Margin

PropertyBulma ClassValue
mb="0"mb-00
mb="1"mb-10.25rem
mb="2"mb-20.5rem
mb="3"mb-30.75rem
mb="4"mb-41rem
mb="5"mb-51.5rem
mb="6"mb-63rem
mb="auto"mb-autoauto

Left Margin

PropertyBulma ClassValue
ml="0"ml-00
ml="1"ml-10.25rem
ml="2"ml-20.5rem
ml="3"ml-30.75rem
ml="4"ml-41rem
ml="5"ml-51.5rem
ml="6"ml-63rem
ml="auto"ml-autoauto

Horizontal Margin (Left + Right)

PropertyBulma ClassValue
mx="0"mx-00
mx="1"mx-10.25rem
mx="2"mx-20.5rem
mx="3"mx-30.75rem
mx="4"mx-41rem
mx="5"mx-51.5rem
mx="6"mx-63rem
mx="auto"mx-autoauto

Vertical Margin (Top + Bottom)

PropertyBulma ClassValue
my="0"my-00
my="1"my-10.25rem
my="2"my-20.5rem
my="3"my-30.75rem
my="4"my-41rem
my="5"my-51.5rem
my="6"my-63rem
my="auto"my-autoauto

Margin Examples

import { Box, Button, Title, Card } from '@allxsmith/bestax-bulma';

function MarginExamples() {
  return (
    <Box p="4">
      <Title mb="4">Margin Examples</Title>

      {/* All sides margin */}
      <Box backgroundColor="light" color="dark" p="2" m="4" mb="4">
        Box with margin on all sides (m="4")
      </Box>

      {/* Specific side margins */}
      <Box
        backgroundColor="info"
        color="dark"
        p="2"
        mt="3"
        mr="6"
        mb="2"
        ml="1"
      >
        Box with different margins on each side
      </Box>

      {/* Horizontal and vertical margins */}
      <Box
        backgroundColor="success"
        color="white"
        p="2"
        mx="auto"
        my="3"
        style={{ width: '200px' }}
      >
        Centered box with vertical margin
      </Box>

      {/* Button spacing */}
      <div className="mt-4">
        <Button mr="2">First Button</Button>
        <Button mr="2">Second Button</Button>
        <Button>Third Button</Button>
      </div>

      {/* Card spacing */}
      <Card mt="5" mb="3">
        <Card.Content>Card with top and bottom margins</Card.Content>
      </Card>
    </Box>
  );
}

Padding

Use padding props to add internal spacing within elements. Padding creates space inside the element's border.

All Sides Padding

PropertyBulma ClassValue
p="0"p-00
p="1"p-10.25rem
p="2"p-20.5rem
p="3"p-30.75rem
p="4"p-41rem
p="5"p-51.5rem
p="6"p-63rem

Top Padding

PropertyBulma ClassValue
pt="0"pt-00
pt="1"pt-10.25rem
pt="2"pt-20.5rem
pt="3"pt-30.75rem
pt="4"pt-41rem
pt="5"pt-51.5rem
pt="6"pt-63rem

Right Padding

PropertyBulma ClassValue
pr="0"pr-00
pr="1"pr-10.25rem
pr="2"pr-20.5rem
pr="3"pr-30.75rem
pr="4"pr-41rem
pr="5"pr-51.5rem
pr="6"pr-63rem

Bottom Padding

PropertyBulma ClassValue
pb="0"pb-00
pb="1"pb-10.25rem
pb="2"pb-20.5rem
pb="3"pb-30.75rem
pb="4"pb-41rem
pb="5"pb-51.5rem
pb="6"pb-63rem

Left Padding

PropertyBulma ClassValue
pl="0"pl-00
pl="1"pl-10.25rem
pl="2"pl-20.5rem
pl="3"pl-30.75rem
pl="4"pl-41rem
pl="5"pl-51.5rem
pl="6"pl-63rem

Horizontal Padding (Left + Right)

PropertyBulma ClassValue
px="0"px-00
px="1"px-10.25rem
px="2"px-20.5rem
px="3"px-30.75rem
px="4"px-41rem
px="5"px-51.5rem
px="6"px-63rem

Vertical Padding (Top + Bottom)

PropertyBulma ClassValue
py="0"py-00
py="1"py-10.25rem
py="2"py-20.5rem
py="3"py-30.75rem
py="4"py-41rem
py="5"py-51.5rem
py="6"py-63rem

Padding Examples

// import {
//   Box,
//   Card,
//   Notification,
//   Button,
//   Title,
// } from '@allxsmith/bestax-bulma';

function PaddingExamples() {
  return (
    <Box p="4">
      <Title mb="4">Padding Examples</Title>

      {/* All sides padding */}
      <Box backgroundColor="primary" color="white" p="6" mb="4">
        Box with large padding on all sides (p="6")
      </Box>

      {/* Specific side padding */}
      <Box
        backgroundColor="info"
        color="dark"
        pt="1"
        pr="4"
        pb="2"
        pl="6"
        mb="4"
      >
        Box with different padding on each side
      </Box>

      {/* Horizontal and vertical padding */}
      <Box backgroundColor="success" color="white" px="5" py="2" mb="4">
        Box with horizontal and vertical padding
      </Box>

      {/* Card with custom padding */}
      <Card mb="4">
        <Card.Content p="6">
          <Title size="5" mb="3">
            Card with Custom Padding
          </Title>
          <p>
            This card content has extra padding (p="6") for a more spacious
            feel.
          </p>
        </Card.Content>
      </Card>

      {/* Notification with minimal padding */}
      <Notification color="warning" p="2" mb="4">
        Compact notification with minimal padding
      </Notification>

      {/* Button with custom padding */}
      <Button px="6" py="3" backgroundColor="danger" color="white">
        Button with Custom Padding
      </Button>
    </Box>
  );
}

Combining Margin and Padding

You can combine margin and padding properties to create sophisticated spacing layouts:

import { Box, Card, Title, Content, Button } from '@allxsmith/bestax-bulma';

function CombinedSpacingExample() {
  return (
    <Box p="4">
      <Title mb="5">Combined Spacing Example</Title>

      {/* Article-style layout */}
      <Card mx="auto" my="4" p="0" style={{ maxWidth: '600px' }}>
        <Card.Content pt="6" px="6" pb="4">
          <Title size="3" mb="3">
            Article Title
          </Title>
          <Content mb="4">
            <p>
              This is an example of combining margin and padding for a clean
              article layout with proper spacing.
            </p>
          </Content>
        </Card.Content>

        <Card.Content pt="0" px="6" pb="6">
          <Box backgroundColor="light" color="dark" p="4" mb="4">
            <Content>
              <p>
                This is a highlighted section with its own padding and margin
                within the card.
              </p>
            </Content>
          </Box>

          <Button mr="3" px="4" py="2">
            Read More
          </Button>
          <Button variant="outlined" px="4" py="2" color="dark">
            Share
          </Button>
        </Card.Content>
      </Card>

      {/* Grid-like layout with spacing */}
      <Box mt="6">
        <Title size="4" mb="4">
          Grid Layout with Spacing
        </Title>
        <Box display="flex" flexWrap="wrap" mx="-2">
          {[1, 2, 3, 4, 5, 6].map(num => (
            <Box key={num} style={{ width: '33.333%' }} px="2" mb="4">
              <Card p="4" backgroundColor="light" color="dark">
                <Title size="6" mb="2" color="dark">
                  Item {num}
                </Title>
                <Content>
                  Content with consistent spacing using margin and padding.
                </Content>
              </Card>
            </Box>
          ))}
        </Box>
      </Box>
    </Box>
  );
}

Best Practices

Spacing Guidelines
  1. Default First: Prefer using component default spacing before customizing
  2. Consistency: Use the spacing scale consistently throughout your application
  3. Hierarchy: Use larger spacing values to create visual hierarchy
  4. Rhythm: Establish vertical rhythm with consistent margin values
  5. Semantic spacing: Use spacing to group related content together
Best Practice

The recommended approach is to use the default padding and spacing that comes built into Bulma components. Components are designed with appropriate spacing out of the box. Only override spacing when absolutely necessary for your specific design requirements.

// PREFERRED: Use default component spacing
<Card> // Uses default Card spacing
<Card.Content> // Uses default Content spacing
<Title> // Uses default Title spacing
<Content> // Uses default Content spacing
<Button> // Uses default Button spacing
</Card>

// ACCEPTABLE: Override only when needed
// (In an imperfect world, sometimes customization is necessary)
<Card m="3"> // Override only when layout requires it
<Card.Content p="5"> // Override only for specific design needs
<Title mb="3"> // Override only when default doesn't work
<Content mb="4"> // Override sparingly
<Button mt="3"> // Override as last resort
</Card>

// AVOID: Excessive spacing overrides
<Card m="3" p="0" mx="auto" my="5"> // Too many overrides
<Card.Content p="5" pt="6" pb="4"> // Overly complex spacing
<Title mb="3" mt="2" mx="1"> // Unnecessary customization
Override with Caution

While spacing overrides are available and sometimes necessary, they should be used sparingly. Excessive customization can lead to inconsistent designs and maintenance challenges. When you do override default spacing, document your reasoning and ensure it aligns with your design system.

Learn More

For detailed API information about spacing properties, see the useBulmaClasses API documentation.

See Also