CSS and CSS3 flexible box model to achieve element width (height) adaptation

CSS and CSS3 flexible box model to achieve element width (height) adaptation

1. CSS realizes fixed width on the left and adaptive width on the right

1. Positioning

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Adaptive</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .left{
            background: red;
            width: 200px;
            height: 200px;
            position: absolute;/*positioning*/
            left: 0;
            top:0;
        }
        .right{
            background: blue;
            height: 200px;
            margin-left: 210px;
        }
    </style>
</head>
<body>
    <div class="left">
        Fixed width</div>
    <div class="right">
        Adaptive
</body>
</html>

2. Floating

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Adaptive</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .left{
            background: red;
            width: 200px;
            height: 200px;
            float: left;/*float*/
        }
        .right{
            background: blue;
            height: 200px;
            margin-left: 210px;
        }
    </style>
</head>
<body>
    <div class="left">
        Fixed width</div>
    <div class="right">
        Adaptive
</body>
</html>

3. Margin

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Adaptive</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .left{
            background: red;
            width: 200px;
            height: 200px;
        }
        .right{
            background: blue;
            height: 200px;
            margin-top: -200px;/*margin*/
            margin-left: 210px;
        }
    </style>
</head>
<body>
    <div class="left">
        Fixed width</div>
    <div class="right">
        Adaptive
</body>
</html>

2. CSS3 elastic box model realizes adaptation

1. Fixed upper and lower heights, adaptive middle height

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body,html{
            height: 100%;
        }
        #contain{
            display: flex;
            flex-direction: column;/*column vertical direction*/
            height: 100%;/*Full screen effect: this element and its parent element and html, body height: 100%*/
        }
        #top{
           height: 200px;
            background: red;
        }
        #center {
            flex: 1;
            background: blue;
        }
        #bottom{
            height: 100px;
            background: green;
        }
    </style>
 
</head>
<body>
<div id="contain">
    <div id="top">Hello</div>
    <div id="center">Hello</div>
    <div id="bottom">Hello too</div>
</div>
</body>
</html>

2. The left width is fixed and the right width is adaptive

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
 
        #contain {
            display: flex; /*The parent element sets this attribute*/
        }
 
        #left {
            width: 100px;
            height: 200px;
            background: #fff8a8;
            margin-right: 10px;
        }
 
        #right {
            flex: 1; /*Proportion/number of copies*/
            height: 200px;
            background: #ff9bad;
        }
    </style>
</head>
<body>
<div id="contain">
    <div id="left"></div>
    <div id="right"></div>
</div>
</body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Basic knowledge of website design: newbies please read this

>>:  Repair solution for inconsistent MySQL GTID master and slave

Recommend

Comparing Document Locations

<br />A great blog post by PPK two years ago...

How to configure MySQL scheduled tasks (EVENT events) in detail

Table of contents 1. What is an event? 2. Enable ...

How to use lodop print control in Vue to achieve browser compatible printing

Preface This control will have a watermark at the...

CSS method of controlling element height from bottom to top and from top to bottom

Let’s start the discussion from a common question...

Advantages and Problems of XHTML CSS Website Design

XHTML is the standard website design language cur...

Detailed example of using the distinct method in MySQL

A distinct Meaning: distinct is used to query the...

Detailed steps to install the NERDTree plugin in Vim on Ubuntu

NERDTree is a file system browser for Vim. With t...

In-depth understanding of MySQL long transactions

Preface: This article mainly introduces the conte...

Summary of commonly used time, date and conversion functions in Mysql

This article mainly summarizes some commonly used...

HTML tags explained

HTML tags explained 1. HTML tags Tag: !DOCTYPE De...

MySQL multi-instance configuration solution

1.1 What is MySQL multi-instance? Simply put, MyS...

Design Theory: Text Legibility and Readability

<br />Not long ago, due to business needs, I...