Skip to main content

What is the difference between width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

The following is the actual code demonstration, the picture intuitively reflects the difference between the two.

<style>
        .outer{
            background-color: aquamarine;
            width: 200px;
            height: 200px;
        }
        .inner1{
            height: 50px;
            width: auto;
            background-color: pink;
            padding: 10px;
            border: 5px solid red;
            margin: 5px;
        }
        .inner2{
            height: 50px;
            width: 100%;
            padding: 10px;
            background-color: yellow;
            border: 5px solid red;
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner1">

        </div>
        <hr>
        <div class="inner2">
           
        </div>
    </div>



Comments

Popular posts from this blog

Destructuring assignment in ES6

 In the past, when we wanted to assign a value to a variable, such as an array type and an object type, to assign a value to a variable, we could only specify the value directly. It is very troublesome to write in this way, but under the ES6 syntax specification, it is allowed to directly extract the required values from arrays and objects according to a certain pattern, and directly assign variables to variables. This method is called Destructuring, which is simple to understand. That is, the left and right sides of the equal sign are equal.

favicon.ico 404 Not Found Causes and Solutions

 What is favicon? It is the abbreviation of Favorites Icon. Its function is that in addition to displaying the corresponding title in the browser's favorites, icons can also be used to distinguish different websites. When I encounter this kind of error, I feel very uncomfortable. There is obviously no problem. Why is a 404 error reported? The following is the solution.