Detailed explanation of the default values ​​of width and height in CSS: auto and %

Detailed explanation of the default values ​​of width and height in CSS: auto and %

in conclusion

  • % of width: defines the percentage width based on the width of the containing block (parent element), which will exceed the parent's limit
  • Width auto: Try to be wrapped by the parent
  • % of height: Based on the percentage height of the block-level object containing it, it will exceed the limit of the parent
  • Height auto: Try to be wrapped by the parent

Case

Be sure to copy the code and run it again, and spend a few minutes to experience how to be wrapped by the parent and break through the parent's restrictions

Width Case

<!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;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
    <div class="box2"></div>
</div>
</body>
</html>

Height Case

<!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: 50 0px;
            height: 400px;
        }
        .box1 {
            width: 200px;
            height:auto;
            background: pink;
            padding: 50px 0px;
            margin: 50px 0px;
            border-width: 50px 0px;
            border-style: solid;
            border-color: green;
        }
        .box2 {
            width: 200px;
            height:100%;
            background: gold;
            padding: 50px 0px;
            margin: 50px 0px;
            border-width: 50px 0px;
            border-style: solid;
            border-color: green;
        }
     
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
    <div class="box2"></div>
</div>
</body>
</html>

This is the end of this article about the default values ​​of width and height in CSS, auto and % cases. For more relevant CSS width and height default values, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  How to automatically start RabbitMq software when centos starts

>>:  Practice of implementing custom search bar and clearing search events in avue

Recommend

How to use JavaScript to get the most repeated characters in a string

Table of contents topic analyze Objects of use So...

How to deploy Angular project using Docker

There are two ways to deploy Angular projects wit...

WeChat applet wxs date and time processing implementation example

Table of contents 1. Timestamp to date 2. Convert...

Detailed usage of Vue timer

This article example shares the specific code of ...

Some notes on installing fastdfs image in docker

1. Prepare the Docker environment 2. Search for f...

Writing daily automatic backup of MySQL database using mysqldump in Centos7

1. Requirements: Database backup is particularly ...

Example of usage of keep-alive component in Vue

Problem description (what is keep-alive) keep-ali...

How to create a swap partition file in Linux

Introduction to Swap Swap (i.e. swap partition) i...

Complete steps to use samba to share folders in CentOS 7

Preface Samba is a free software that implements ...

Example code for implementing card waterfall layout with css3 column

This article introduces the sample code of CSS3 c...

Detailed method of using goaccess to analyze nginx logs

Recently I want to use goaccess to analyze nginx ...

View the command to modify the MySQL table structure

Brief description The editor often encounters som...

How to select all child elements and add styles to them in CSS

method: Take less in the actual project as an exa...