您的位置:

使用 CSS 2 创建网页布局的详细指南

一、选择适当的布局方式

CSS 2 中提供了多种布局方式,如浮动布局、定位布局、弹性布局等,我们需要选择适合当前场景的布局方式。

其中,浮动布局是最为常见的一种方式,通过对元素使用浮动(float)属性,将元素从普通文档流中独立出来,实现布局的效果。

例如,下面的代码实现了一个两栏的布局,其中左侧宽度为30%,右侧宽度为70%:

<div class="container">
  <div class="left"></div>
  <div class="right"></div>
</div>

.container {
  width: 100%;
}
.left {
  width: 30%;
  float: left;
}
.right {
  width: 70%;
  float: left;
}

二、合理使用盒模型

CSS 2 中的盒模型指的是一个元素所占据的空间以及该元素周围的空白区域,其中包含了四个部分:内容区域(content)、内边距区域(padding)、边框区域(border)和外边距区域(margin)。

我们需要合理使用盒模型,以达到更好的布局效果。例如,当我们需要设置一个元素的宽度时,通常应该考虑元素的总宽度,而非仅仅考虑内容区域的宽度。

以下是一个包含了内边距和边框的盒模型的示例:

<div class="box">
  <p>This is a box.</p>
</div>

.box {
  width: 200px;
  padding: 10px;
  border: 1px solid #ddd;
}

三、掌握清除浮动的技巧

当我们使用浮动布局时,可能会遇到高度塌陷(元素高度不受内容撑开而导致布局混乱)或者浮动元素内部子元素的包裹问题。

为解决这些问题,我们需要掌握清除浮动的技巧,如在浮动元素的父元素中添加一个空的块级元素,并对其应用 clear 属性,从而清除浮动影响:

<div class="container">
  <div class="left"></div>
  <div class="right"></div>
  <div class="clear"></div>
</div>

.clear {
  clear: both;
}

四、响应式设计的实现

随着移动设备的普及,需要设计适应不同屏幕大小和分辨率的网页布局,这就需要使用响应式设计的方法。

其中,媒体查询(Media Queries)是实现响应式设计的关键技术,它可以根据屏幕大小、分辨率或者设备类型等条件,对不同的布局进行设置。

以下是一个基于媒体查询实现的响应式布局示例:

/* 默认样式 */
.container {
  width: 960px;
  margin: 0 auto;
}
.box {
  width: 50%;
  float: left;
}

/* 媒体查询 */
@media (max-width: 768px) {
  .container {
    width: 100%;
  }
  .box {
    width: 100%;
    float: none;
  }
}

五、实现多列布局

在一些特殊的场景中,需要实现多列的网页布局,例如报纸或者杂志等。

可以使用 CSS 2 中的多列布局(column layout)来实现,通过设置 column-count 和 column-width 属性,即可实现分列布局的效果。

以下是一个简单的两列布局示例:

<div class="container">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit auctor dui a ornare. Quisque ultricies velit ac blandit volutpat. Vivamus interdum risus vel odio mattis, eu porttitor risus congue. Morbi tristique vulputate lectus, nec malesuada mi malesuada ut. Morbi faucibus odio nec mauris malesuada, a lacinia velit consequat. Proin eget euismod ipsum. Donec tincidunt dolor nec leo fermentum porta. Aenean sed tortor eget mauris iaculis gravida. Sed quam magna, finibus eu odio sed, eleifend placerat dolor. Ut aliquet enim id elit cursus gravida. Sed eget pulvinar ipsum.</p>
  <p>Vestibulum vel vestibulum erat. Cras interdum, orci vitae ornare euismod, dui elit vehicula mauris, eu rutrum justo elit vel odio. Nunc auctor varius pellentesque. In hac habitasse platea dictumst. Sed bibendum vitae erat ac finibus. Maecenas vel lobortis purus. Morbi sagittis lorem nunc. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</div>

.container {
  column-count: 2;
  column-width: 50%;
}

总结

通过选择适当的布局方式、合理使用盒模型、掌握清除浮动的技巧、实现响应式设计和多列布局,我们可以更好地使用 CSS 2 实现网页布局,并且适应各种不同的布局需要。

使用 CSS 2 创建网页布局的详细指南

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