search
HomeWeb Front-endHTML TutorialCSS经典布局之弹性布局_html/css_WEB-ITnose

  当我们在浏览浏览器的时候,常常会放大/缩小浏览器的显示比例,或者在不同的设备上,所处的分辨率也不尽相同。因此,我们需要学习一个新的知识:弹性盒模型。

弹性盒模型

实现项目对齐,方向,排序(即使项目大小位置动态生成),能够动态修改子元素的宽度和高度,具有良好的适配性。


如图就是弹性布局一个大概范围。

弹性容器属性

属性 说明
flex-direction 设置主轴方向,确定弹性子元素排列方式
flex-wrap 当弹性子元素超出弹性容器范围是是否换行
flex-flow 复合属性,flex-direction和flex-wrap双重属性
justify-content 主轴上的对齐方式
align-items 侧轴上的对齐方式
align-content 侧轴上有空白,侧轴对齐方式









下面我们用弹性布局的方式实现响应式菜单,先来看代码:

    <meta charset="UTF-8">    <title>Flexbox响应式菜单</title>    <link rel="stylesheet" type="text/css" href="mycss.css">
.menu{    list-style-type: none;    padding: 0;    margin: 0;    display: -webkit-flex;    display: -ms-flexbox;    /*display: flex;    //激活flex方式    flex-flow: row wrap;     //弹性容器的属性*/}.menu li{    width: auto;    height: 40px;    text-align: center;    line-height: 40px;    flex: 1 1 100%;      //扩展比例为1,收缩比例为1,初始宽度为100%}.menu li:nth-child(1){    background-color: pink;}.menu li:nth-child(2){    background-color: plum;}.menu li:nth-child(3){    background-color: hotpink;}.menu li:nth-child(4){    background-color: palevioletred;}.menu li:nth-child(5){    background-color: deeppink;}.menu li:nth-child(6){    background-color: purple;}.menu li a{    color: black;    text-decoration: none;}@media (max-width: 480px) {    .menu {        flex: 1 1 100%;        flex-flow: row wrap;    }}@media (min-width: 768px){    .menu {        flex-flow: row nowrap;    }}

以上我们就实现了一个弹性布局的一个大概模型。

再说一个我之前看到过的一面试题,也是要求写一道弹性布局的题,但是我们也可以另辟蹊径:

实现如下图所示的布局要求:sidebar固定宽度200px,content和header宽度自适应,当window宽度小于600px时,变成三列布局


我们实现的思路也跟弹性布局是一样的,看代码:

    <meta charset="UTF-8">    <title>flex布局</title>    <style>        *{            margin: 0;            padding: 0;        }       @media (min-width: 600px) {           .header {               width: auto;               background-color: green;           }           .sidebar {               float: left;               width: 200px;               margin-right: -200px;               background-color: gold;           }           .content {               float: left;               width: 100%;               margin-left: 200px;               background-color: red;           }       }        @media (max-width: 600px) {            .header {                width: auto;                background-color: green;            }            .sidebar {                width: auto;                background-color: gold;            }            .content {                width: auto;                background-color: red;            }        }    </style><div class="header">header</div><div class="sidebar">sidebar</div><div class="content">content</div>



版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement
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
What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design?What is the viewport meta tag? Why is it important for responsive design?Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

See all articles

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 Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!