您的位置:

绝对定位的详解

一、什么是绝对定位?

在HTML和CSS中,绝对定位是一种CSS定位机制,用于定位元素的位置。在绝对定位中,元素的位置由top、right、bottom和left属性来定位,相对于最近的已定位父元素或body元素而言。如果没有已定位的父元素,则相对于文档的初始包含块。绝对定位是相对于包含块来进行定位,可以在top、right、bottom和left属性中同时指定值,以确定元素所在的位置。

二、CSS绝对定位的相关属性

在CSS中,用于控制绝对定位的属性如下:

position:指定元素的定位类型,可能的值有static、relative、fixed、absolute和sticky。

top:指定元素顶部边缘与包含块顶部边缘之间的距离。

right:指定元素右侧边缘与包含块右侧边缘之间的距离。

bottom:指定元素底部边缘与包含块底部边缘之间的距离。

left:指定元素左侧边缘与包含块左侧边缘之间的距离。

三、绝对定位的典型应用场景

绝对定位最常用于创建浮动元素,比如弹框、下拉菜单、轮播图等等。

四、绝对定位示例代码

/* HTML代码 */
<div class="container">
  <h1>使用绝对定位的标题</h1>
  <p>这里放置文本内容。</p>
  <a href="#">Read More</a>
</div>

/* CSS代码 */
.container {
  position: relative;
  width: 300px;
  height: 200px;
  background-color: #ccc;
  margin: 0 auto;
}

h1 {
  position: absolute;
  top: 20px;
  left: 20px;
}

p {
  position: absolute;
  top: 50px;
  left: 20px;
}

a {
  position: absolute;
  bottom: 20px;
  right: 20px;
}

在这个例子中,我们将div元素定位为相对,这样内部元素就可以相对于它进行绝对定位。标题、文本和链接元素的位置由它们各自的top、left、right和bottom属性来进行定位。在这个例子中,标题元素相对于包含块的左上角定位,而文本元素则向下移动30px,链接元素相对于包含块的右下角进行定位。

五、绝对定位与其他定位属性之间的关系

在CSS中,不同的位置属性可以组合使用来实现所需的布局效果。如果元素既有static属性,又有absolute、fixed或sticky属性,那么绝对定位将会覆盖其他定位属性。

举个例子,在下面的代码中,元素被设置为position: relative和position: absolute。因为绝对定位具有更高的优先级,所以元素实际上是绝对定位而不是相对定位。

.container {
  position: relative;
}

h1 {
  position: absolute;
  top: 20px;
  left: 20px;
}

六、总结

绝对定位是CSS中一种非常重要的定位方式,常用于定位弹出框、下拉框、轮播图等元素。掌握好绝对定位的使用方法,可以让我们更好地进行页面布局。

CSS绝对定位图片

2023-05-12
CSS绝对定位元素的指定位置

2023-05-12
绝对定位的详解

2023-05-12
CSS中相对定位和绝对定位的区别

2023-05-12
CSS相对定位和绝对定位

2023-05-12
CSS绝对定位图像

2023-05-12
CSS position属性的相对/绝对定位

2023-05-12
绝对定位的图片

2023-05-12
div绝对定位详解

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