您的位置:

CSS响应式布局

一、h5响应式布局

随着移动设备的普及,越来越多的网站开始采用响应式布局,以适应不同屏幕尺寸下的展示。其中,针对手机屏幕的h5响应式布局,是一种比较高效实用的方案。

h5响应式布局主要采用CSS3的弹性盒子布局及媒体查询等技术实现,通过设置不同的屏幕宽度阈值,来适配不同的设备屏幕。同时,通过CSS3的自适应单位rem,能够在不同屏幕下实现伸缩自如的布局效果。

<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
  .container{
    display:flex;
    justify-content:center;
    align-items:center;
    flex-direction:column;
  }
  .box{
    width:2rem; /* 字体大小为1rem */
    height:2rem;
    background-color:#f00;
    margin:0.5rem;
  }
  @media screen and (min-width: 768px){
    /* 大于768px的屏幕采用2列布局 */
    .container{
      flex-direction:row;
      flex-wrap:wrap;
    }
    .box{
      width:calc((100% - 1rem) / 2);
    }
  }
</style>
<div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

二、web响应式布局

web响应式布局是一种针对桌面端设备的响应式设计方案,与h5响应式布局相比,需要更加细致的调整和设计。

web响应式布局主要采用CSS3、媒体查询、弹性盒子布局、栅格布局等技术实现。其中,栅格布局是web响应式布局的一种常见解决方案,可以将整个页面划分为若干份列,并为每个列设定相应的宽度和间距。

<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
  .container{
    display:flex; /* 弹性盒子布局 */
    justify-content:center;
  }
  .left{
    width:calc(100% - 960px); /* 960px为主体部分宽度 */
    height:600px;
    background-color:#f00;
    margin:0 1rem;
  }
  .main{
    width:960px; /* 主体部分宽度 */
    height:600px;
    background-color:#0f0;
    margin:0 1rem;
  }
  .right{
    width:calc(100% - 960px); /* 960px为主体部分宽度 */
    height:600px;
    background-color:#00f;
    margin:0 1rem;
  }
  /* 媒体查询 */
  @media screen and (max-width: 1200px){
    .main{
      margin:0 0.5rem;
    }
  }
  @media screen and (max-width: 992px){
    /* 小于992px的屏幕采用单列布局 */
    .container{
      flex-direction:column;
      align-items:center;
    }
    .left,
    .right{
      width:auto;
      margin:1rem;
    }
  }
</style>
<div class="container">
  <div class="left"></div>
  <div class="main"></div>
  <div class="right"></div>
</div>

三、如何实现响应式布局

实现响应式布局有很多方法和方案,其中常用的几种方式如下:

1、CSS3的媒体查询:通过媒体类型和特定条件匹配不同的样式表,实现不同设备的布局。

@media screen and (max-width:768px) {
  /* 当屏幕宽度小于等于768px时,应用以下样式 */
  .box{
    width:100px;
    height:100px;
  }
}
@media screen and (min-width:768px) and (max-width:1024px) {
  /* 当屏幕宽度在768px到1024px之间时,应用以下样式 */
  .box{
    width:150px;
    height:150px;
  }
}
@media screen and (min-width:1024px){
  /* 当屏幕宽度大于1024px时,应用以下样式 */
  .box{
    width:200px;
    height:200px;
  }
}

2、CSS3的弹性盒子布局:通过设置弹性盒子容器和其内部项的flex属性,实现自适应伸缩的布局效果。

.container{
  display:flex;
  justify-content:center;
  align-items:center;
}
.box{
  width:2rem; /* 字体大小为1rem */
  height:2rem;
  background-color:#f00;
  margin:0.5rem;
}

3、栅格布局:将页面布局划分成若干前端网格,根据不同的屏幕尺寸,设置不同的网格布局,从而实现响应式布局。

<div class="container">
  <div class="row">
    <div class="col-md-4"></div>
    <div class="col-md-4"></div>
    <div class="col-md-4"></div>
  </div>
</div>

四、响应式布局代码

以下是一个简单的响应式布局代码示例,采用CSS3的媒体查询与弹性盒子布局实现了在不同屏幕尺寸下的自适应布局效果:

<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
  .container{
    display:flex;
    justify-content:center;
    align-items:center;
    flex-direction:column;
  }
  .box{
    width:2rem; /* 字体大小为1rem */
    height:2rem;
    background-color:#f00;
    margin:0.5rem;
  }
  @media screen and (min-width: 768px){
    /* 大于768px的屏幕采用2列布局 */
    .container{
      flex-direction:row;
      flex-wrap:wrap;
    }
    .box{
      width:calc((100% - 1rem) / 2);
    }
  }
</style>
<div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

五、媒体查询响应式布局

媒体查询是一种针对不同媒体类型和设备屏幕尺寸的CSS技术,在响应式布局中占据非常重要的位置。通过设置不同的屏幕宽度阈值,能够实现在不同屏幕尺寸下的自适应布局效果。

以下是一个媒体查询响应式布局的示例代码:

<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
  .container{
    display:flex;
    justify-content:center;
    align-items:center;
  }
  .box{
    width:100px;
    height:100px;
    background-color:#f00;
    margin:0.5rem;
  }
  @media screen and (min-width: 768px){
    /* 大于768px的屏幕采用2列布局 */
    .container{
      flex-direction:row;
    }
  }
  @media screen and (min-width: 992px){
    /* 大于992px的屏幕采用3列布局 */
    .box{
      width:calc((100% - 2.5rem) / 3);
    }
  }
  @media screen and (min-width: 1200px){
    /* 大于1200px的屏幕采用4列布局 */
    .box{
      width:calc((100% - 3.5rem) / 4);
    }
  }
</style>
<div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

六、响应式布局框架

响应式布局框架是一种封装了响应式布局实现细节的前端框架,能够使开发者在不同屏幕尺寸下轻松实现自适应布局效果。当前比较流行的响应式布局框架有Bootstrap、Foundation、Semantic UI等。

以下是一个Bootstrap响应式布局框架的示例代码:

<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
  <div class="row">
    <div class="col-md-3"></div>
    <div class="col-md-6"></div>
    <div class="col-md-3"></div>
  </div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/popper.js/2.9.3/popper.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>

七、前端响应式布局

前端响应式布局是指在前端开发中实现自适应布局效果的技术方案。

前端响应式布局主要采用CSS3、媒体查询、弹性盒子布局、栅格布局等技术实现。同时,为了提高开发效率与可维护性,也涌现出了众多前端响应式布局框架和组件库,例如Bootstrap、Vue-Element-UI、React-Antd等。

以下是一个Vue-Element-UI响应式布局的示例代码:

<div id="app">
  <el-row :gutter="20">
    <el-col :span="8"></el-col>
    <el-col :span="8"></el-col            
CSS响应式布局

2023-05-12
CSS响应式布局

2023-05-12
如何利用CSS创建响应式网站

2023-05-12
使用CSS fr单位实现响应式布局

2023-05-12
响应式布局框架

2023-05-20
如何使用HTML和CSS创建响应式网页设计

2023-05-12
CSS响应式设计

2023-05-12
CSS响应式设计

2023-05-12
CSS响应式设计

2023-05-12
CSS响应式设计

2023-05-12
CSS制作响应式网页

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)