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
Release: 2024-12-24 12:58:18
Original
493 people have browsed it

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;
}
Copy after login

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template