Detailed explanation of the difference between the default value of the CSS attribute width: auto and width: 100%

Detailed explanation of the difference between the default value of the CSS attribute width: auto and width: 100%

width: auto

  • The child element (including content+padding+border+margin) fills the entire content area of ​​the parent element.
  • When a sub-element has margin, border, or padding, the width value corresponding to the sub-element content area will be subtracted.
  • Parent element's content = child element (content + padding + border + margin)

width: 100%

  • Force the content area of ​​the child element to fill the content area of ​​the parent element
  • When a child element has margin, border, or padding, the width of the child element's content area does not change, but overflows the parent box and maintains the original value.
  • Parent element's content = child element's content

An example is worth a thousand words:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        * {
            margin: 0;padding: 0;
        }
        body {
            background: #dcdcdc;
        }
        .box {
            width: 400px;
            border: 3px solid red;
            padding: 0 50px;
        }
        .box1 {
            width: auto;
            height: 100px;
            background: pink;
            padding: 0 50px;
            margin: 0 50px;
            border-width: 0 50px;
            border-style: solid;
            border-color: green;
        }
        .box2 {
            width: 100%;
            height: 100px;
            background: gold;
            padding: 0 50px;
            margin: 0 50px;
            border-width: 0 50px;
            border-style: solid;
            border-color: green;
        }
        .box3 {
            width: 100px;height: 100px;background: orange;
        }
        .box4 {
            float: left;
            width: 50px;height: 50px;background: pink;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
    <div class="box2"></div>
</div>
</body>
</html> 

width:auto

width:100%

Note: Image sub-width is changed to sub-content

This is the end of this article about the detailed explanation of the difference between the default values ​​of the CSS attribute width, width: auto and width: 100%. For more relevant content about CSS width: auto and width: 100%, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. We hope that everyone will support 123WORDPRESS.COM in the future!

<<:  How to migrate the data directory in Docker

>>:  Setting z-index property for flash overlay popup layer in web design does not work

Recommend

Vue implements tab label (label exceeds automatic scrolling)

When the created tab label exceeds the visible ar...

Solve the docker.socket permission problem of vscode docker plugin

Solution: Kill all .vscode related processes in t...

js realizes 3D sound effects through audioContext

This article shares the specific code of js to ac...

A brief discussion on JavaScript shallow copy and deep copy

Table of contents 1. Direct assignment 2. Shallow...

A brief comparison of Props in React

Table of contents Props comparison of class compo...

A graphic tutorial on how to install MySQL in Windows

Abstract: This article mainly explains how to ins...

Several common ways to deploy Tomcat projects [tested]

1 / Copy the web project files directly to the we...

Linux View File System Type Example Method

How to check the file system type of a partition ...

HTML6 implements folding menu and accordion menu example code

The main part of the page: <body> <ul id...

Key issues and solutions for web page access speed

<br /> The website access speed can directly...

A brief discussion on the solution to excessive data in ElementUI el-select

Table of contents 1. Scenario Description 2. Solu...

How to deploy ElasticSearch in Docker

1. What is ElasticSearch? Elasticsearch is also d...

Analysis of the reasons why MySQL field definitions should not use null

Why is NULL so often used? (1) Java's null Nu...