您的位置:

CSS排版特性之弹性布局的折行设置

一、弹性布局的概述

弹性布局(flex布局)是CSS3的新特性之一,最早出现在2009年,旨在为Web设计提供一种更加灵活的排版方式。通过弹性容器和弹性项的定义,可以实现对元素的灵活布局和对齐方式的动态调整。

弹性布局相较于传统的盒模型布局,具有更加灵活、高效、便捷的优点。在移动端响应式开发和多设备适配中,弹性布局已经成为开发者们广泛使用的一种布局方式。

二、弹性容器的设置

定义弹性容器,需要使用display属性,属性值为flex或inline-flex,其中flex为块级元素,inline-flex为行内元素。我们可以通过给父元素设置display: flex;,将其设置为弹性容器。以下为弹性容器的示例代码:

.parent {
  display: flex;
}

在弹性容器中,父元素也可以通过设置flex-direction、justify-content、align-items、align-content等属性来调整子元素的排列和对齐方式。

三、弹性项的设置

弹性项是指弹性容器中的子元素,通过设置弹性容器的属性,可以动态地调整弹性项的排版方式。在弹性容器中,弹性项的默认值为1,即每个弹性项平均分配父容器的宽度。

以下是弹性项常用的属性:

  • flex-grow:定义弹性项的放大比例,默认值为0,即不放大。
  • flex-shrink:定义弹性项的缩小比例,默认值为1,即自动缩小。
  • flex-basis:定义弹性项在主轴方向上的初始大小,默认值为auto,即本来的大小。
  • flex:定义弹性项的三个属性值:flex-grow、flex-shrink、flex-basis,其中flex: 1;等价于flex: 1 1 0%;
  • order:定义弹性项的显示顺序,数值越小越先显示,默认值为0。
  • align-self:定义弹性项在交叉轴方向上的对齐方式,默认值为auto,即继承父级元素的属性。

以下为弹性项的示例代码:

.child {
  flex: 1 2 200px;
  order: 1;
  align-self: center;
}

四、弹性布局的折行设置

常规情况下,弹性容器中的弹性项会根据父容器的大小调整自身的大小和位置。当弹性项的宽度超过父容器的宽度时,弹性项会自动缩小,以适应父容器的大小,但不会自动换行。在这种情况下,可以通过flex-wrap属性来设置弹性容器的折行行为。

以下为flex-wrap属性的取值和对应的描述:

  • nowrap:默认值,表示不允许折行。
  • wrap:允许弹性项在必要的时候进行折行。
  • wrap-reverse:允许弹性项在必要的时候进行折行,但是按照相反的顺序显示。

以下为弹性容器的折行设置示例代码:

.parent {
  display: flex;
  flex-wrap: wrap;
}

五、结语

弹性布局可以方便地实现不同设备上的响应式布局和动态调整控件间的间距和大小,而且兼容性非常高。在实际开发中,灵活掌握弹性容器和弹性项的设置,能够使页面的排版效果更加美观、实用。

CSS排版特性之弹性布局的折行设置

2023-05-18
优化你的网站页面布局:掌握border-spacing CS

当我们创建网站时,页面布局是非常重要的,因为它可以使整个网站看起来更吸引人,并且更易于阅读。然而,在进行布局时,有时需要使用表格来安排内容。了解如何使用CSS中的border-spacing属性可以帮

2023-12-08
CSS布局指南:如何创建适应性页面布局

2023-05-12
CSSWrap: 简化文本布局的神器

2023-05-20
HTML弹性布局详解

2023-05-20
提高网站内容排版的技巧之CSS空格属性

2023-05-12
CSS3 Flex布局详解

2023-05-19
CSS响应式布局

2023-05-12
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)