您的位置:

CSS Box Sizing Basics

一、可视区域

CSS中的box-sizing属性确定了元素框架的计算方式。默认的box-sizing属性值是content-box,这也是浏览器的默认值。

在content-box模式中,元素的宽度和高度值仅仅包含了元素内容,并不包括padding值、border值和margin值。

举个例子,我们创建一个包含50px的padding值的盒子:

.box {
  width: 200px;
  height: 200px;
  padding: 50px;
  border: 1px solid black;
}

在当前的设置中,盒子的总宽度和高度为:

width: 200px + (50px*2的padding值) + (2*1px的border值) = 302px
height: 200px + (50px*2的padding值) + (2*1px的border值) = 302px

我们可以通过修改box-sizing属性,将元素的宽度和高度包括padding值和border值,如下所示:

.box {
  width: 200px;
  height: 200px;
  padding: 50px;
  border: 1px solid black;
  box-sizing: border-box;
}

在此示例中,元素的宽度和高度值包括了padding值和border值。

二、浏览器兼容性

该功能在Internet Explorer 8及更早版本中不被支持。IE8支持box-sizing,但只支持设置值为border-box,而不支持设置值为content-box。

我们可以使用box-sizing的两种方式提供跨浏览器支持,如下所示:

box-sizing: border-box;
-moz-box-sizing: border-box; 
-webkit-box-sizing: border-box;

该示例代码适用于IE8及更早版本、Firefox和Safari等WebKit浏览器。

三、盒子模型的布局

如果我们掌握了盒子模型的工作方式,就可以更容易地完成Web布局。我们可以通过使用box-sizing属性来更改盒子模型。如果我们使用content-box模型,则需要使用大量的计算和调整,才能获得预期的效果。但是,如果我们将box-sizing设为border-box,就可以更轻松地布局网页元素。例如,我们可以使用flex-box来创建自适应的布局,如下所示:

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.box {
  width: 33.33%;
  padding: 20px;
  box-sizing: border-box;
}

在此示例中,我们使用了伸缩布局(flex box)来创建一个自适应的栅格系统。通过将box-sizing属性设置为border-box,我们可以轻松地计算盒子的大小和位置,使网页呈现出更加直观和美观的界面。

CSS Box Sizing Basics

2023-05-12
CSS Box Sizing

2023-05-12
CSS Box Sizing: Border Box

2023-05-12
CSS Box Sizing in HTML

2023-05-12
了解CSS box-sizing属性

2023-05-12
box-sizing: border-box在CSS中的作用

2023-05-12
探究CSS Box-sizing属性的作用

2023-05-12
CSS中box-sizing属性的正确使用方法

一、什么是box-sizing属性 在CSS中,box-sizing属性作用于盒模型,用来控制盒子的尺寸计算方式。传统的盒模型中,一个元素的尺寸 = 内容尺寸 + padding + border,而

2023-12-08
CSS HTML Box Text

2023-05-12
使用CSS box-sizing属性进行盒模型设置

2023-05-12
CSS Box to the Left

2023-05-12
如何优雅地使用box-sizing属性

2023-05-12
CSS Box Model 中的 Height

2023-05-12
使用border-sizing CSS属性优化网站布局

2023-05-12
CSS的border-sizing属性使用指南

2023-05-12
CSS sizing教程:如何设置网站元素的尺寸?

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)