search
HomeWeb Front-endHTML Tutorialflexbox 伸缩布局盒_html/css_WEB-ITnose

Flexbox(伸缩布局盒) 是 CSS3 中一个新的布局模式,为了现代网络中更为复杂的网页需求而设计。

Flexbox 由 伸缩容器 和 伸缩项目 组成。通过设置元素的 display 属性为    flex  或  inline-flex     可以得到一个伸缩容器

            设置为flex的容器被渲染为一个块级元素,而设置为inline-flex的容器则渲染为一个行内元素。

例如:此处将 类名为   flex-container   的元素设置为伸缩容器

.flex-container {

display: -webkit-flex;

display: flex;

}

伸缩容器中的每一个子元素都是一个伸缩项目。伸缩项目可以是任意数量的。伸缩容器外和伸缩项目内的一切元素都不受影响。

简单地说,Flexbox 定义了伸缩容器内伸缩项目该如何布局。

 

Flex Lines 伸缩行       

伸缩项目沿着伸缩容器内的一个 伸缩行 定位。通常每个伸缩容器只有一个伸缩行。  默认伸缩行从左往右

例子:

 1 <div class="flex-container"> 2 <div class="flex-item">flex item 1</div> 3 <div class="flex-item">flex item 2</div> 4 </div> 5  6 样式: 7  8 .flex-container { 9 display: -webkit-flex;10 display: flex;11 width: 300px;12 height: 240px;13 background-color: Silver;14 }15 16 .flex-item {17 background-color: DeepSkyBlue;18 width: 100px;19 height: 100px;20 margin: 5px;21 }

 

效果图:

再加

<div class="flex-item">flex item 3</div>
<div class="flex-item">flex item 4</div><br /><br />伸缩项目的宽度超过了 伸缩容器的宽度,伸缩容器的宽度不会改变,伸缩项目的宽度减小<br /><br /><br /><br />

 1 <div class="flex-container"> 2 <div class="flex-item">flex item 1</div> 3 <div class="flex-item">flex item 2</div> 4 </div> 5  6  7 CSS样式 8 .flex-container { 9    direction: rtl;10     display: -webkit-flex;11     display: flex;12     width: 300px;13     height: 240px;14     background-color: Silver;15     16 }17 18 .flex-item {19     background-color: DeepSkyBlue;20     width: 100px;21     height: 100px;22     margin: 5px;23 }

效果:

伸缩行从右往左,文字也从右往左

 

 

为了描述抽象的书写模式,Flexbox 使用 主轴 和 侧轴的概念。伸缩行跟随主轴。侧轴则垂直于主轴。

flex-direction 伸缩流的方向

默认值  row   

再次提醒,默认是从左至右,从上到下。其他的值如下:

  • row-reverse: 主轴起点和主轴终点交换。如果书写模式是从左至右,伸缩项目则是从左往右显示。
  • column: 主轴和侧轴交换。如果书写系统是垂直的,那么伸缩项目也是垂直显示的。
  • column-reverse: 和 column 一样,但是方向相反。
  •  

    justify-content 主轴对齐

     

    用于调整主轴上伸缩项目的位置。可能的值为:

  • flex-start (默认)
  • flex-end
  • center
  • space-between
  • space-around
  •  

     align-items 侧轴对齐

    调整伸缩项目在侧轴上的定位方式。可能的值有:

  • flex-start (默认)
  • flex-end
  • center
  • baseline
  • stretch
  •  

    flex-wrap 伸缩行换行

    为伸缩容器创建多个伸缩行。这个属性接受以下值:

  • nowrap (默认)
  • wrap
  • wrap-reverse
  •  

    align-content 堆栈伸缩行

    它对齐的是伸缩行,它接受的值:

  • stretch (默认)
  • flex-start
  • flex-end
  • center
  • space-between
  • space-around
  •  

    flex-flow 伸缩方向与换行    是flex-direction和flex-wrap的缩写。

     

    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 HTML tags?What is the purpose of HTML tags?Apr 28, 2025 am 12:02 AM

    HTMLtagsareessentialforstructuringwebpages,enhancingaccessibility,SEO,andperformance.1)Theyareenclosedinanglebracketsandusedinpairstocreateahierarchicalstructure.2)SemantictagslikeandimproveuserexperienceandSEO.3)Creativetagslikeenabledynamicgraphics

    What are self-closing tags? Give an example.What are self-closing tags? Give an example.Apr 27, 2025 am 12:04 AM

    Self-closingtagsinHTMLandXMLaretagsthatclosethemselveswithoutneedingaseparateclosingtag,simplifyingmarkupstructureandenhancingcodingefficiency.1)TheyareessentialinXMLforelementswithoutcontent,ensuringwell-formeddocuments.2)InHTML5,usageisflexiblebutr

    Beyond HTML: Essential Technologies for Web DevelopmentBeyond HTML: Essential Technologies for Web DevelopmentApr 26, 2025 am 12:04 AM

    To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

    What are boolean attributes in HTML? Give some examples.What are boolean attributes in HTML? Give some examples.Apr 25, 2025 am 12:01 AM

    Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values ​​need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

    How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

    HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

    HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

    HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

    HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

    The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

    The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    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

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function