您的位置:

清除浮动的CSS属性

浮动是CSS布局中常用的一种方式,可以让元素在页面中浮动自由排列,常见的应用包括图文混排和导航栏。然而,浮动元素可能会对周围的元素造成影响,导致布局错乱,此时我们需要清除浮动。本文将从多个方面详细阐述清除浮动的CSS属性。

一、清除浮动的CSS属性是什么

清除浮动的CSS属性是clear。它用于规定元素的哪个侧面不允许有浮动元素。

二、清除浮动的CSS写法正确的是

清除浮动的方式有很多种写法,如下是最常见的两种写法:
.clearfix::after { 
    content: " "; 
    display: block; 
    height: 0; 
    clear: both; 
    visibility:hidden; 
}
.clearfix {
    *zoom:1;
}
在父元素上添加clearfix类,同时清除浮动:
.clearfix:after{
    clear: both;
    content: '';
    display: block;
}
.clearfix{
    *zoom: 1;
}

三、CSS如何清除浮动

1. overflow清除浮动

使用overflow属性,将容器元素的值设置为hidden、auto或者scroll,因为浮动元素会导致容器元素高度塌陷,而这些属性可以使容器元素自适应内部元素高度,从而清除浮动,如下例:
.container{
    overflow: hidden;
    /* or overflow: auto; */
    /* or overflow: scroll; */
}

2. clear清除浮动

clear属性用于清除浮动元素对周围元素的影响,取值有left、right、both、none等。如下例:
.clearfix::after{ 
    content:""; 
    display:block; 
    clear:both;
}

3. 双伪元素清除浮动

使用伪元素:after和:before,可以使用和清除浮动的宽高为0、inline-block的元素占位,防止元素塌陷。如下例:
.clearfix::after, .clearfix::before {
    content:"";
    display: block;
    height:0;
    overflow:hidden;
    clear:both;
}
.clearfix {
    zoom:1;
}

四、CSS清除所有浮动

清除所有浮动元素可以使用下面的代码:
.clear{
    clear:both;
    display:block;
    height:0;
    font-size: 0;
    content:"";
}
这里设置了字体大小为0,是为了使该元素不占用任何垂直空间。

五、CSS清除浮动的其他方法

1. 不使用浮动

最好的方法是不使用浮动,而是使用Flexbox布局、Grid布局等现代化的CSS布局方式。

2. 使用fit-content()函数

fit-content()函数可以使元素自适应内部元素高度或宽度,替代 overflow 属性,如下例:
.container{
    height: fit-content;
}

3. 使用display:table属性

在容器元素上使用display:table属性,内容元素使用display:table-cell属性可以使元素自适应内部元素高度,而不需要清除浮动。如下例:
.container{
    display: table;
}
.container .item{
    display: table-cell;
}

结语

以上是清除浮动的CSS属性和方法的详细阐述,我们可以通过多种方式达到清除浮动的目的,选择适合自己的方法可以避免布局带来的各种问题。
清除浮动的CSS属性

2023-05-12
HTML清除浮动详解

2023-05-19
CSS浮动的多方面阐述

2023-05-22
CSS自定义属性详解

2023-05-19
CSS浮动在Web设计中的应用

2023-05-12
了解CSS和HTML的定位属性

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)