您的位置:

CSS的box-sizing:border-box属性如何影响页面布局和盒模型?

一、什么是盒模型?

CSS的盒模型是用来描述HTML元素所占空间的模型,其中包括了元素的内容区、内边距、边框和外边距四个部分。

在标准的盒模型中,元素宽度的计算是不包括内边距和边框的。例如:

.box {
  width: 200px;
  padding: 20px;
  border: 2px solid black;
}

那么.box元素的实际宽度为:

200 + 20 + 20 + 2 + 2 = 244px

其中,20px是上下内边距和左右内边距的总和,2px是上下边框和左右边框的总和。

二、box-sizing属性的作用

box-sizing属性用来描述盒子模型的计算方式,有两个取值:content-boxborder-box

  • content-box:标准盒模型,宽度 = 内容区宽度 + 左右内边距和 + 左右边框宽度
  • border-box:怪异盒模型,宽度 = 内容区宽度,包括内边距和边框在内,不再增加盒子的实际占用空间

三、border-box的应用场景

border-box最常用的场景是响应式布局,可以在不同屏幕尺寸下保持元素的宽度不变,而不会受到边框、内边距的影响。例如,下面的代码实现了一个宽度为100%的响应式图片,图片的内边距和边框不会使得其宽度超出容器:

.img-container {
  width: 100%;
  height: auto;
  overflow: hidden;
  box-sizing: border-box;
  padding: 10px;
  border: 2px solid black;
}

.img {
  width: 100%;
  height: auto;
  box-sizing: border-box;
  padding: 10px;
}

在这个例子中,.img-container是包裹图片的容器,使用了border-box模型,所以无论给容器添加内边距、边框,都不会影响其宽度。而图片本身也使用了border-box模型,使得其实际占用空间不会超出父容器。

四、内容溢出的解决方法

在标准盒模型下,元素的实际宽度可能会超出容器的宽度,从而导致内容溢出,需要使用overflow属性进行裁剪。而在border-box模型下,只需要保证内边距和边框的大小不超过父容器的宽度即可。例如:

.container {
  width: 300px;
  height: 300px;
  border: 2px solid black;
  box-sizing: border-box;
}

.content {
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  overflow: auto;
}

在这个例子中,.container是外围容器,使用了border-box模型,保证其实际占用空间不会超出300px。.content是内容区,也使用了border-box模型,但是添加了20px的内边距,使得实际宽度为260px,可以通过overflow属性裁剪内容。

五、总结

box-sizing属性用来控制盒子模型的计算方式,一般建议使用border-box模型,可以方便响应式布局和内容的溢出裁剪。但是需要注意,内外边距和边框的大小要小于父容器的宽度,在设计时需要进行精确计算。

CSS的box-sizing:border-box属性如何影

2023-05-12
CSS Box Sizing: Border Box

2023-05-12
使用border-box盒模型实现更好的CSS布局

2023-05-12
CSS Border Box Design:如何优化盒子模型

2023-05-12
CSS Box Sizing

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

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

2023-05-12
CSS Box Sizing in HTML

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

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

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

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

2023-05-12
使用Padding Right样式属性来调整页面布局

2023-05-12
优化盒模型的CSS属性,提高网页加载速度

2023-05-12
CSS box-sizing:border-box应用详解

2023-05-12
CSS HTML Box Text

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)