How should I add some space between the text and the border using CSS?
Categories:
Adding Space Between Text and Border with CSS

Learn how to effectively use CSS properties like padding, margin, and box-sizing to create visual space around text within bordered elements.
When designing web layouts, controlling the spacing around elements is crucial for readability and aesthetics. A common requirement is to add space between the content (like text) and its surrounding border. This article explores the primary CSS properties used to achieve this: padding and margin, along with the important role of box-sizing.
Understanding the CSS Box Model
Before diving into specific properties, it's essential to understand the CSS Box Model. Every HTML element is treated as a rectangular box, comprising four layers: content, padding, border, and margin. The padding property adds space inside the border, between the content and the border. The margin property adds space outside the border, separating the element from other elements.
graph TD
A[Content] --> B[Padding]
B --> C[Border]
C --> D[Margin]
style A fill:#e0f7fa,stroke:#00bcd4,stroke-width:2px
style B fill:#b2ebf2,stroke:#0097a7,stroke-width:2px
style C fill:#80deea,stroke:#006064,stroke-width:2px
style D fill:#4dd0e1,stroke:#004d40,stroke-width:2pxThe CSS Box Model layers
Using padding for Inner Spacing
The padding property is your primary tool for adding space between the text and the border. It creates an inner buffer zone. You can apply padding uniformly to all sides or specify different values for top, right, bottom, and left. This property directly affects the element's total size unless box-sizing: border-box; is used.
.my-element {
border: 1px solid #333;
padding: 15px; /* Adds 15px space on all sides between text and border */
}
.specific-padding {
border: 2px dashed blue;
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
/* Shorthand for the above: padding: 10px 20px; */
}
Examples of using the padding property
padding to create space inside the element's border. This keeps the content visually separated from the border itself.The Role of box-sizing
By default, CSS uses box-sizing: content-box;. This means that padding and border are added to the specified width and height of an element, making the element larger than its declared dimensions. To prevent this and ensure width and height include padding and border, use box-sizing: border-box;.
/* Apply border-box globally for easier layout management */
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
.my-element-with-border-box {
width: 200px; /* This 200px now includes padding and border */
border: 2px solid green;
padding: 10px;
/* The content area will be 200px - (2*10px padding) - (2*2px border) = 176px */
}
Implementing box-sizing: border-box;
margin also creates space, it does so outside the border, pushing other elements away. It does not create space between the text and the element's own border. For that, padding is the correct choice.Practical Application and Best Practices
When applying these concepts, consider the overall layout. For individual elements like buttons, input fields, or text blocks, padding is ideal. For spacing between distinct elements, margin is more appropriate. Always test your designs across different browsers and devices to ensure consistent rendering.
1. Identify the target element
Determine which HTML element needs space between its text content and its border.
2. Apply padding
Use the padding CSS property (e.g., padding: 10px; or padding: 5px 10px 15px 20px;) to add the desired inner space.
3. Consider box-sizing
If you want the element's declared width and height to include padding and border, ensure box-sizing: border-box; is applied to the element or globally.
4. Verify visually
Inspect the element in your browser's developer tools to confirm the padding is applied correctly and the layout behaves as expected.