您的位置:

CSS Overflow属性的使用

一、布局控制

CSS Overflow属性是一种可以控制HTML元素内容溢出部分的属性。针对不同的情况可以使用不同的Overflow属性值。最常用的Overflow属性取值有hidden、scroll、auto和visible。下面的代码演示了如何使用Overflow属性和Overflow-x、Overflow-y子属性来控制元素内容的显示方式:

/* 隐藏溢出部分 */
.overflow-hidden {
  overflow: hidden;
}

/* 滚动条显示溢出部分 */
.overflow-scroll {
  overflow: scroll;
}

/* 自动溢出或者仅显示部分溢出部分 */
.overflow-auto {
  overflow: auto;
}

/* 显示所有溢出部分 */
.overflow-visible {
  overflow: visible;
}

/* 控制横向溢出 */
.overflow-x {
  overflow-x: scroll;
}

/* 控制纵向溢出 */
.overflow-y {
  overflow-y: scroll;
}

使用Overflow属性的好处不仅能很好的控制元素的溢出部分,还能避免出现一些奇怪的浮动效果。例如下面这段代码:

.box {
  width: 200px;
  height: 200px;
  float: left;
  border: 1px solid #ccc;
}

.content {
  width: 300px;
  height: 200px;
  float: left;
  border: 1px solid #ccc;
}

使用上述代码布局时会发现,content元素的宽度比box宽,导致box元素左侧文字溢出的显示异常。通过设置Overflow属性值可以很好的解决这个问题:

.box {
  width: 200px;
  height: 200px;
  float: left;
  border: 1px solid #ccc;
  overflow: hidden;
}

.content {
  width: 300px;
  height: 200px;
  float: left;
  border: 1px solid #ccc;
  overflow: hidden;
}

可以看到,Overflow属性可以很好的控制元素的布局,避免出现溢出的情况,并且保证浮动效果正常。

二、用户体验

Overflow属性不仅可以用来控制布局,还可以增强用户体验。例如当页面内容很多时,通过设置Overflow属性可以使用户只关注当前页面内容,避免页面滚动过多,提升用户体验。

当内容长度超过一定长度时,可以给页面添加一个竖直方向的滚动条,并隐藏横向滚动条:

.container {
  width: 600px;
  height: 500px;
  overflow-x: hidden;
  overflow-y: scroll;
}

这样可以确保页面垂直方向的滚动条正常显示,而横向滚动条不会干扰用户,给用户提供更好的阅读体验。

另外,在页面设计上也常常会使用Overflow属性。例如,当我们需要在页面中显示一组图片时,可以使用以下代码实现图片滚动效果:




  

通过设置Overflow属性值为hidden,控制页面只显示部分图片,使用animation控制图片向左滚动,给用户提供视觉上的美感,同时保证了页面中图片的正常显示。

三、总结

CSS Overflow属性是一种很常用的布局属性,它可以帮助我们有效控制页面元素内容溢出部分的显示方式,同时可以增强用户体验。合理使用Overflow属性不仅可以避免布局问题和视觉不美观,还能为用户提供更好的阅读和浏览体验。

CSS Overflow属性的使用

2023-05-12
使用CSS的overflow属性优化页面的显示效果

2023-05-12
如何在CSS中使用overflow属性来控制内容溢出

2023-05-12
清除浮动的CSS属性

2023-05-12
-webkit-overflow-scrolling属性详解

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