您的位置:

CSS Box Sizing in HTML

一、CSS盒模型简介

盒子模型 是指将 HTML 中所有元素看作盒子的概念,每个盒子由 内容区域内边距边框外边距 四个部分组成。这是 CSS 的一种基本布局模型,用于在网页中控制元素的大小、位置和属性。

在 CSS 中,默认的盒模型是 W3C 盒模型(可参考 box-sizing:content-box;)。这种盒模型将元素的整体宽度和高度计算为 内容区域 宽度和高度加上 内边距边框外边距,因此,如果这些部分太宽或太高,可能会导致整个元素过大,或无法与其他元素进行良好的布局。

随着网页布局越来越复杂,开发人员更频繁地使用 flexboxgrid 等弹性布局模型,需要在这些布局模型中使用更多的百分比和自适应宽度。这使得开发人员越来越关注怎样更精细地控制元素的大小,以及如何通过 CSS 更好地控制盒模型的行为。

二、解决盒模型的问题:box-sizing

CSS3 的 box-sizing 属性可以改变 W3C 盒模型的行为,实现更自然的布局,其使用方法如下:

selector {
    box-sizing: content-box | border-box | inherit;
}

其中:

  • content-box 是默认值,代表 W3C 盒模型,即整个元素宽度和高度都由 内容区域内边距边框外边距 共同决定;
  • border-box 代表 IE 盒模型,即整个元素宽度和高度由 内容区域内边距边框 共同决定,也就是说,元素的宽度等于 外边距 + 边框 + 内边距 + 内容区域 的宽度;
  • inherit 继承父元素的盒模型属性。

使用 border-box 盒模型可以避免太宽或太高的问题,使得所有盒子的宽度和高度都可以更好地控制,即使改变内部的 内边距边框 也不会影响元素的大小,这可以更轻松地构建灵活且可维护的布局。

三、CSS 实例

下面是一个简单的 CSS 实例,使用不同的盒模型来构建一个矩形:

.box {
    width: 200px;
    height: 100px;
    padding: 20px;
    border: 5px solid black;
    margin: 20px;
    background-color: lightblue;
    float: left;
}

.content-box {
    box-sizing: content-box;
}

.border-box {
    box-sizing: border-box;
}

HTML 实现如下:

<div class="content-box box">
    <p>content-box</p>
</div>

<div class="border-box box">
    <p>border-box</p>
</div>

如上所述,如果使用 content-box,可以看到 内边距边框 都会影响盒子的大小:

content-box

而使用 border-box,可以只改变 内容区域 的大小,而不会影响元素的大小:

border-box

四、总结

通过使用 box-sizing 属性,可以改变盒子大小的计算方式,从而更精细地控制元素的大小和布局。当使用 flex 或 grid 等弹性布局模型时,更多地考虑改变盒子模型以实现更好的可维护性和可伸缩性,这对于构建现代 web 应用程序至关重要。

CSS Box Sizing in HTML

2023-05-12
CSS Box Sizing

2023-05-12
CSS HTML Box Text

2023-05-12
CSS Box Sizing: Border Box

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

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

2023-05-12
CSS Box Sizing Basics

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

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

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

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

2023-12-08
CSS Box to the Left

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

2023-05-12
CSS Box in HTML

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

2023-05-12
CSS Box Model 中的 Height

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

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

2023-05-21
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)