您的位置:

深入了解 display: -webkit-box

一、外观排列

display: -webkit-box 可以让元素按照横向或者纵向接排列。通过设置不同的属性,还可以控制元素间的间距、对齐方式等。

.box {
  display: -webkit-box;
  -webkit-box-orient: horizontal;
  -webkit-box-pack: center;
  -webkit-box-align: center;
  -webkit-box-flex: 1;
}

-webkit-box-orient属性指定子元素排列方向,可以取值horizontal或vertical;-webkit-box-pack属性指定子元素在水平方向的排列方式,可以取值start、end、center、justify、distribute等;-webkit-box-align属性指定子元素在垂直方向的排列方式,可以取值start、end、center、baseline、stretch等;-webkit-box-flex属性指定子元素的拉伸系数,决定元素占据剩余空间的比例。

二、文本溢出

在一些容器宽度固定的场景中,长字符串的文本内容可能会出现溢出的问题。display: -webkit-box 提供了一个属性可以解决这个问题,即 -webkit-box-orient: vertical; 结合 -webkit-box-lines: n;(n为数字),即可实现文本省略号显示。

.box {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-box-lines: 2;
}

以上代码指定元素内最多显示两行内容,超出部分将被省略,并显示省略号。(需要注意的是,只有在webkit内核的浏览器中支持该属性)

三、元素排序

display: -webkit-box 提供了一个属性-webkit-box-ordinal-group可以调整元素在父容器中的排列顺序,其值越小排列越靠前。

.box2 {
  display: -webkit-box;
}
.box2 div:nth-child(2) {
  -webkit-box-ordinal-group: 1;
}
.box2 div:nth-child(3) {
  -webkit-box-ordinal-group: 2;
}
.box2 div:nth-child(1) {
  -webkit-box-ordinal-group: 3;
}

以上代码可以将容器中的三个子元素的排列顺序进行排列。

四、重量计算

-webkit-box-flex可以设置子元素占据剩余空间的比例,这里介绍一个特殊的用法:通过为不同的子元素设置-webkit-box-flex不同的值,可以实现盒子内部元素宽度的自适应。当然,这只是一个粗略计算,它并不支持布spring。如果要实现更完美的自适应效果,建议使用CSS 3的弹性盒子布局(Flex)。

.box3 {
  display: -webkit-box;
}
.box3 div:nth-child(1) {
  -webkit-box-flex: 1;
}
.box3 div:nth-child(2) {
  -webkit-box-flex: 2;
}
.box3 div:nth-child(3) {
  -webkit-box-flex: 4;
}

以上代码中,第一个子元素占据1/7空间,第二个子元素占据2/7空间,第三个子元素占据4/7空间。

五、等高排列

display: -webkit-box 结合 -webkit-box-pack:justify; 可以实现容器中的元素等高排列。

.box {
  display:-webkit-box;  /* 将此 div 设置为 `flex` 容器 */ 
  -webkit-box-pack:justify;  /* 将容器内的元素均分多余空间,撑满容器 */ 
  text-align:justify;  /* 让子元素左右两端对齐 */ 
}

以上代码可以使容器中的元素等高排列,并且自动适应容器的高度。

深入了解 display: -webkit-box

2023-05-24
-webkit-box-shadow的全面分析

2023-05-20
如何完美实现元素布局控制-webkit-box-orient

2023-05-19
深入了解HTML box-shadow

2023-05-21
深入剖析Flex Box

2023-05-21
深入了解Less语言

2023-05-20
CSS Box Sizing Basics

2023-05-12
深入了解box-sizing:border-box

2023-05-21
java学习笔记(java初学笔记)

2022-11-14
深入了解Node-Webkit

2023-05-20
阿平的python小笔记吖,python 阿里巴巴

2022-11-18
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)