Home Web Front-end CSS Tutorial CSS adaptive layout: How to implement CSS width adaptation?

CSS adaptive layout: How to implement CSS width adaptation?

Sep 10, 2018 pm 02:01 PM
css width Adaptive

Today’s web page layout needs to be adapted to various screens, so it is necessary to implement adaptation so that the content in the web page can be fully displayed. Therefore, today’s article will introduce to you about CSS width auto- Adaptable content. Let’s talk in detail about how to implement CSS width adaptation in CSS adaptive layout.

Recommended related articles:
1.How to achieve high adaptability of css? A simple way to adapt css height to content
2.What are the common adaptive layouts in CSS
Related video recommendations:
1.CSS Video Tutorial-Jade Girl Heart Sutra Edition

We often see pages like this. The left (or right) side is a fixed navigation or menu bar, and the other side will adaptively change its size as the browser zooms. This is actually the automatic width. Implementation of adaptation.

There are two most common implementation methods in css width adaptation, One is a two-column layout, and the other is a three-column layout

Let’s do it below Let’s briefly introduce these two methods respectively.

1. CSS width adaptive two-column layout:

Let’s take the right width as fixed and the left width as adaptive as an example:

1. The fixed width area is floating, and the adaptive area does not set a width but sets margin

<div id="wrap">
  <div id="sidebar" style="height:500px;background:red;color:#fff;">固定宽度区</div>
  <div id="content" style="height:500px;background:#000;color:#fff;">自适应区</div>
</div>
Copy after login
#sidebar {
  float: right; width: 300px;
}#content {
  margin-right: 300px;
}
Copy after login

Note:

The right side is always fixed, and the left side is based on the remaining screen space. Adaptable size.

But in fact this method has limitations, that is, the sidebar must be before the content in the html structure.

2. Use float and margin together

<div id="wrap">
  <div id="content" style="height:500px;background:#000;color:#fff;">
    <div class="contentInner">
       自适应区    </div>
  </div>
  <div id="sidebar" style="height:500px;background:red;color:#fff;">固定宽度区</div>
</div>
Copy after login
#content {
  margin-left: -300px; float: left; width: 100%;
}#content .contentInner{
  margin-left:300px;
}#sidebar {
  float: right; width: 300px;
}
Copy after login

Explanation: In this way, the actual width of contentInner is the screen width-300px.

3. Use absolute positioning in the fixed width area and set margin in the adaptive area

<div id="wrap">
  <div id="content" style="height:500px;background:#000;color:#fff;">我现在的结构是在前面</div>
  <div id="sidebar" style="height:500px;background:red;color:#fff;">固定宽度区</div>
  </div>
Copy after login
#wrap{
  position:relative;
}#content {
  margin-right:300px;
}#sidebar {
  position:absolute;
  width:300px;
  right:0;
  top:0;
}
Copy after login

4. Use display:table to achieve

<div id="wrap">
  <div id="content" style="height:500px;background:#000;color:#fff;">我现在的结构是在前面</div>
  <div id="sidebar" style="height:500px;background:red;color:#fff;">固定宽度区</div>
</div>
Copy after login
#wrap{
  display:table;
  width:100%;
}#content {
  display:table-cell;
}#sidebar {
 width:300px;
  display:table-cell;
}
Copy after login

Note: This method is not compatible with IE7 and below browsers, because IE7 does not recognize the setting of display to table.

2. CSS width adaptive three-column layout:

1. Fixed-width three-column layout

    <div class="div0">
        <div class="left">left</div>
        <div class="middle">middle</div>
        <div class="right">right</div>
    </div>
Copy after login
*{
    padding: 0;
    margin: 0;
}
.div0{
    width: 800px;
    height: 500px;/*设置高度只为结果更直观,高度可根据内容自适应*/
    margin: 50px auto;
    border: 2px solid #E51414;/*添加边框只为结果更直观*/
}
.left{
    width: 200px;
    height: 500px;/*设置高度只为结果更直观,高度可根据内容自适应*/
    background: #6E6C8A;
    float: left;/*设为左浮动*/
    text-align: center;
}
.middle{
    width: 430px;
    height: 500px;/*设置高度只为结果更直观,高度可根据内容自适应*/
    background: #806155;
    float: left;/*设为左浮动*/
    margin: 0 10px 0 10px;/*左右各加10px使得三列之间有间隙*/
    text-align: center;
}
.right{
    width: 150px;
    height: 500px;/*设置高度只为结果更直观,高度可根据内容自适应*/
    background: #8F9068;
    float: right;/*设为右浮动*/
    text-align: center;
}
Copy after login

2. Three-column layout with fixed width on the left and right and adaptive width in the middle

 <!--<div class="div0">-->
 <div class="left">left</div>
        <div class="middle">middle</div>
        <div class="right">right</div>
<!--</div>-->
Copy after login
*{
    padding: 0;    
    margin: 0;}
    /*.div0{
    width: 800px;
    height: 500px;
    margin: 50px auto;
    position: relative;
    border: 2px solid #E51414;
}
可以不要这个父元素div0(即默认父元素为body),如果有,需将这个父元素设置为相对定位*/
.left{
    width: 200px;    
    height: 500px;    
    background: #6E6C8A;    
    position: absolute;    
    top: 0;    l
    eft: 0;
    /*设为绝对定位并且与其父元素的top、left距离都为0*/
    text-align: center;
    }
.middle{
    height: 500px;    
    background: #806155;    
    margin: 0 160px 0 210px;
    /*左右各加10px使得三列之间有间隙*/
    text-align: center;
    }
    .right{
        width: 150px;    
        height: 500px;    
        background: #8F9068;    
        position: absolute;    
        top: 0;    
        right: 0;
        /*设为绝对定位并且与其父元素的top、right距离都为0*/
    text-align: center;
    }
Copy after login

Explanation: When the width of the left and right divs is fixed and the width of the middle div is unknown, the three-column layout cannot be achieved using floating . Use absolute positioning to achieve a three-column layout: you need to set the left and right elements to absolute positioning, and set the left and right margin values ​​of the middle element to the width of the right element and the left element respectively. You can achieve a three-column layout without wrapping the parent element. If there is a parent element, you need to set the parent element to relative positioning. (For positioning content, please refer to css manual)

Related recommendations:

How does CSS implement div width adaptive according to content_html/css_WEB-ITnose

css implements fixed width on the right side and adaptive width on the left side_html/css_WEB-ITnose

The above is the detailed content of CSS adaptive layout: How to implement CSS width adaptation?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

See all articles