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

A record of pitfalls in JS regular matching

I recently discovered a pitfall in regular expres...

CSS3 border effects

What is CSS# CSS (abbreviation of Cascading Style...

How to use Vue cache function

Table of contents Cache function in vue2 Transfor...

js+Html to realize table editable operation

This article shares the specific code of js+Html ...

Solution to the Docker container cannot be stopped and deleted

Find the running container id docker ps Find the ...

Very practical Tomcat startup script implementation method

Preface There is a scenario where, for the sake o...

Details of the order in which MySQL reads my.cnf

Table of contents The order in which MySQL reads ...

VMware virtualization kvm installation and deployment tutorial summary

Virtualization 1. Environment Centos7.3 Disable s...

Get / delete method to pass array parameters in Vue

When the front-end and back-end interact, sometim...

Installation of Docker CE on Ubuntu

This article is used to record the installation o...

Native JS to implement real-time clock

Share a real-time clock effect implemented with n...

Vue3.x uses mitt.js for component communication

Table of contents Quick Start How to use Core Pri...

Three ways to refresh iframe

Copy code The code is as follows: <iframe src=...

JS implements simple calendar effect

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