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

Use of js array filter() method

 Arrays are frequently used in development, and processing data in arrays is one of the more common and important operations. Therefore, processing data in arrays during development is an important skill. Every developer Everyone should master the operations of arrays, especially for junior developers who have just entered the industry, so be sure to master the relevant skills. This article mainly shares some operations to filter the data in the array, about the use of the filter() method.

vue routing global guard beforeEach and afterEach

 Global routing front guard (beforeEach) This function is used the most. Its function is to perform permission-related verification before routing jumps. This function contains three parameters: to: the object of the target route that is about to enter; from: the route that the current route is leaving; next: confirm the release. It can be used to log in and register, to determine whether there is a token before logging in, and release if it exists. , if it does not exist, it will not be released. The post routing guard (afterEach), its role is to trigger after the routing jump.