Detailed explanation of the solution for HTML layout with fixed left and right widths and adaptive middle

Detailed explanation of the solution for HTML layout with fixed left and right widths and adaptive middle

This article introduces a detailed explanation of the left and right width fixed middle adaptive HTML layout solution, shared with everyone, as follows:

a. Use floating layout

The html structure is as follows

<div class="box">
    <div class="left">left</div>
    <div class="right">right</div>
    <div class="center">center</div>
</div>   
//Note here that the left and right floating elements should be rendered first, and then the middle element. After the element is floated, the remaining sibling block-level elements will occupy the width of the parent element <style>
   .box{
        height:200px;
    }    
    .left{
        float:left;
        width:300px;
    }
    .right{
        float:right;
        width:300px;
    }
</style>

b. Use fixed positioning

The html structure is as follows

<div class="box">
    <div class="left">left</div>
    <div class="right">right</div>
     <div class="center">center</div>
</div> 
//Similar to the floating layout, render the left and right elements first, so that they are positioned at the left and right ends of the parent element, and the remaining middle elements occupy the remaining width of the parent element.
<style>
    .box{
        position: relative;
      }
      .left{
        position: absolute;
        width: 100px;
        left: 0;
      }
      .right{
        width:100px;
        position: absolute;
        right: 0;
      }
      .center{
        margin: 0 100px;
        background: red;
      }
</style>

c. Table layout

Setting the parent element to display:table and the child element to display:table-cell will turn it into an inline block.

The advantage of this layout is good compatibility.

<div class="box">
  <div class="left">
    left
  </div>
  <div class="center">
    center
  </div>
  <div class="right">
    right
  </div>
</div>
<style>
    .box{
        display: table;
        width: 100%;
      }
      .left{
        display: table-cell;
        width: 100px;
        left: 0;
      }
      .right{
        width:100px;
        display: table-cell;
      }
      .center{
        width: 100%;
        background: red;
      }
</style>

d. Flexible layout

The parent element display:flex child elements will all be arranged in a row.

The width of flex:n in the child element will be the width of the parent element/n

If flex:1, the width is equal to the height of the parent element.

The disadvantage of elastic layout is that it is not compatible. Currently, IE browser cannot use elastic layout.

<div class="box">
  <div class="left">
    left
  </div>
  <div class="center">
    center
  </div>
  <div class="right">
    right
  </div>
</div>
<style>
    .box{
        display: flex;
        width: 100%;
      }
      .left{
      
        width: 100px;
        left: 0;
      }
      .right{
        width:100px;
      }
      .center{
        flex:1;
      }
</style>

e. Grid layout

Parent element display:grid;

grid-templatecolumns:100px auto 100px;

The first child element is 100px wide, the second is adaptive, and the third is 100px wide;

The advantage of grid layout is that it is extremely simple and is directly determined by the parent element style. The disadvantage is that it is not highly compatible.

<div class="box">
  <div class="left">
    left
  </div>
  <div class="center">
    center
  </div>
  <div class="right">
    right
  </div>
</div>
<style>
  .box{
        display: grid;
        grid-template-columns: 100px auto 100px;
        width: 100%;
      }
</style>

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.

<<:  How to view the execution time of SQL statements in MySQL

>>:  How to let https website send referrer https and http jump referrer

Recommend

JavaScript to implement input box content prompt and hidden function

Sometimes the input box is small, and you want to...

MySQL optimization query_cache_limit parameter description

query_cache_limit query_cache_limit specifies the...

Gallery function implemented by native Js

Table of contents The first The second Native Js ...

JavaScript ES6 Module Detailed Explanation

Table of contents 0. What is Module 1.Module load...

Vue3 based on script setup syntax $refs usage

Table of contents 1. Vue2 syntax 2. Use of Vue3 1...

Getting Started: A brief introduction to HTML's basic tags and attributes

HTML is made up of tags and attributes, which are...

MySQL high concurrency method to generate unique order number

Preface After this blog post was published, some ...

Hidden overhead of Unix/Linux forks

Table of contents 1. The origin of fork 2. Early ...

Example of pre-rendering method for Vue single page application

Table of contents Preface vue-cli 2.0 version vue...

Linux implements automatic and scheduled backup of MySQL database every day

Overview Backup is the basis of disaster recovery...

Using CSS3 to achieve transition and animation effects

Why should we use CSS animation to replace JS ani...

Detailed explanation of Vue3 sandbox mechanism

Table of contents Preface Browser compiled versio...

A simple example of creating a thin line table in html

Regarding how to create this thin-line table, a s...

A detailed introduction to Linux system operation levels

Table of contents 1. Introduction to Linux system...