您的位置:

探讨overflow属性值的各种用法

overflow属性是CSS中的一个重要属性,用于控制元素内容超出容器部分的处理方式,其包括四个值:visible、hidden、scroll和auto。接下来,我们将从多个方面详细阐述其用法。

一、visible属性值

visible属性值表示元素内容会溢出到容器之外,对于溢出的内容不会进行裁剪或隐藏。该属性值是所有值的默认值。下面我们来看一个代码示例:

.container {
    width: 200px;
    height: 100px;
    border: 1px solid #000;
    overflow: visible;
}
.content {
    width: 300px;
    height: 200px;
    background-color: #f8bbd0;
}

  

当我们将.content元素的宽度设为300px,容器的宽度设为200px时,内容就会溢出到容器之外:

二、hidden属性值

hidden属性值表示元素内容会被裁剪掉,不会显示在容器中。下面我们来看一个代码示例:

.container {
    width: 200px;
    height: 100px;
    border: 1px solid #000;
    overflow: hidden;
}
.content {
    width: 300px;
    height: 200px;
    background-color: #f8bbd0;
}

  

当我们将.content元素的宽度设为300px,容器的宽度设为200px时,超出的部分就会被裁剪掉:

三、scroll属性值

scroll属性值表示元素内容会溢出到容器之外,同时会显示滚动条,用户可以通过滚动条来查看所有的内容。下面我们来看一个代码示例:

.container {
    width: 200px;
    height: 100px;
    border: 1px solid #000;
    overflow: scroll;
}
.content {
    width: 300px;
    height: 200px;
    background-color: #f8bbd0;
}

  

当我们将.content元素的宽度设为300px,容器的宽度设为200px时,超出的部分就会显示滚动条:

四、auto属性值

auto属性值表示元素内容会溢出到容器之外,只有超出的部分会显示滚动条,当内容未超出容器时,不会显示滚动条。下面我们来看一个代码示例:

.container {
    width: 200px;
    height: 100px;
    border: 1px solid #000;
    overflow: auto;
}
.content {
    width: 100px;
    height: 50px;
    background-color: #f8bbd0;
}

  

当我们将.content元素的宽度设为100px,容器的宽度设为200px时,未超出的部分不会显示滚动条,而超出的部分会显示滚动条:

五、响应式布局中的overflow属性值

在响应式布局中,我们常常需要根据屏幕的大小来进行调整,同时也需要使用overflow属性值来控制元素的溢出情况。下面我们来看一个代码示例:

.container {
    width: 100%;
    height: 50%;
    border: 1px solid #000;
    overflow: auto;
}
.content {
    width: 200%;
    height: 200%;
    background-color: #f8bbd0;
}

  

当我们将.content元素的宽度设为200%,容器的宽度设为100%时,元素会溢出到容器之外。但是,由于我们将容器的高度设为50%,因此当我们在不同的屏幕尺寸下查看时,元素的溢出情况会有所不同。同时,由于我们将overflow属性值设为auto,只有当内容超出容器时才会显示滚动条,因此在内容未超出容器时,也不会显示滚动条。

六、多列布局中的overflow属性值

在多列布局中,我们通常会使用列布局来进行分列。而在进行分列时,我们也需要使用overflow属性值来控制内容的溢出情况。下面我们来看一个代码示例:

.container {
    columns: 2 200px;
    column-gap: 20px;
    height: 100px;
    border: 1px solid #000;
    overflow: auto;
}
.content {
    height: 300px;
    background-color: #f8bbd0;
}

  

当我们将.container元素设为分为两列,每个列的宽度设为200px,同时设定了列间距为20px,内容的高度设为300px,而容器的高度设为100px时,内容会溢出到容器之外。由于我们将overflow属性值设为auto,只有当内容超出容器时才会显示滚动条。

探讨overflow属性值的各种用法

2023-05-19
CSS Overflow属性的使用

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

2023-05-23
深入探讨flex-flow属性

2023-05-20
阿平的python小笔记吖,python 阿里巴巴

2022-11-18
Mac笔记:在日常生活中高效实用的笔记工具

2023-05-18
深入探讨offsetwidth和clientwidth的区别

2023-05-20
java方法整理笔记(java总结)

2022-11-08
使用CSS的overflow属性优化页面的显示效果

2023-05-12
python的用法笔记本(笔记本学python)

2022-11-16
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)