您的位置:

理解CSS中的position: absolute和relative

在前端开发中,绝对定位(position: absolute)和相对定位(position: relative)是非常常见的CSS属性。这两个属性可以让开发者更好地控制元素的位置、布局等,同时又很容易被混淆使用或者使用不恰当。本文将从多个方面对绝对定位和相对定位进行详细的阐述,帮助读者更好地理解和掌握这两个属性。

一、绝对定位(position: absolute)的特点

在CSS中,绝对定位是指将元素从文档流中拖出来,并根据其最近的非static(默认定位方式)定位的祖先元素(或者是元素)进行定位。

具体来说,当一个元素的position属性为absolute时,它的位置是相对于最近的position属性不为static的祖先元素的,而不是相对于当前元素在文档流中原本应该出现的位置。如果不存在非static祖先元素,则相对于文档的初始包含块(initial containing block)进行定位。

使用绝对定位能够让开发者更好地控制元素的布局,可以使元素脱离文档流,更好地进行定位、层叠操作等。

二、相对定位(position: relative)的特点

相对定位是指将元素相对于其在文档流中的原本位置进行定位,在文档流中仍保留该元素在原本位置的空间。相对定位不会影响元素的层叠级别,若与其他元素重叠,仍按照文档流中的次序进行渲染。

相对定位本身不会使元素脱离文档流,可以通过top、bottom、left、right等属性来控制元素相对于原本位置的偏移量和方向。相对定位的元素可以设置z-index属性来改变层叠顺序,但其不同于绝对定位可设置的层叠上下文(stacking context)。

三、两种定位方式的区别与联系

相对定位和绝对定位的使用场景和效果不同,但是二者也存在联系和相互影响之处。

相对定位可以被视为一种“局部绝对定位”,即相对于原显示位置为基础,以top、bottom、left、right为基准进行定位。相对定位的元素并未被完全脱离文档流,仍占据文档流原本的位置,但其偏移量可以通过绝对定位来进行微调。相对定位给与了绝对定位更好的参照框架,而绝对定位则弥补了相对定位无法脱离文档流和层叠顺序受限的不足。

四、实例展示

下面是一个简单的例子,通过使用相对定位和绝对定位两个属性,使得两个元素可以层叠显示和定位到正确的位置:

<div style="position: relative" id="outer">
    <div style="position: absolute; left: 50px; top: 50px;">Hello</div>
    <div style="position: relative; top: 20px;">world</div>
</div>

在上述代码中,我们将一个父元素设置为相对定位,并在其内部放置了两个子元素。其中一个子元素(即第一个<div>元素)设置为绝对定位,其位置相对于父元素左上角向右偏移50px和向下偏移50px的位置。另一个子元素(即第二个<div>元素)相对于原始位置向下偏移了20px的距离。

理解CSS中的position: absolute和rela

2023-05-12
细说CSS中的absolute和relative定位

2023-05-12
CSS Position Absolute vs Fixed

2023-05-12
CSS Absolute居中详解

2023-05-17
CSS中的Absolute定位

2023-05-12
使用relative position css实现网页元素定

2023-05-12
CSS Position - W3Schools

2023-05-12
CSS 属性 - Position

2023-05-12
CSS的定位position属性详解

2023-05-12
CSS中position的精准定位

2023-05-12
CSS position属性的应用场景

2023-05-12
CSS Position属性详解

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)