Home >Web Front-end >CSS Tutorial >How Can I Create a 100% Width Div with Margins?

How Can I Create a 100% Width Div with Margins?

Barbara Streisand
Barbara StreisandOriginal
2024-12-24 12:58:18457browse

How Can I Create a 100% Width Div with Margins?

Displaying a Margined 100% Width Div

To display an expandable div with a width of 100% and margins, the following methods can be employed:

Using the calc() Function

This method requires browser support for the calc() CSS function. The following code can be used:

#page {
  background: red;
  float: left;
  width: 100%;
  height: 300px;
}

#margin {
  background: green;
  float: left;
  width: calc(100% - 10px);
  height: 300px;
  margin: 10px;
}

Using Padding and Box-Sizing

This method requires browser support for the box-sizing CSS property and the border-box value:

#page {
    background: red;
    width: 100%;
    height: 300px;
    padding: 10px;
}

#margin {
    box-sizing: border-box;
    background: green;
    width: 100%;
    height: 300px;
}

Using padding and box-sizing: border-box allows the margin to be included within the width of the element.

The above is the detailed content of How Can I Create a 100% Width Div with Margins?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn