您的位置:

CSS位置属性之relative定位详解

一、relative定位的基本概念

relative定位是CSS中的一种位置属性,它可以让元素相对于其正常位置进行定位。当某个元素使用relative定位后,其他元素的位置不会发生改变,但是我们可以利用left、right、top和bottom属性来对元素进行微调。

<div class="example">
   <h2>我是相对定位的标题</h2>
   <p>
      我是相对定位的段落,并且稍微调整了我的位置。
   </p>
</div>

<style>
   .example {
       position: relative;
       left: 20px;
       top: 10px;
   }
</style>

二、relative定位的实现方法

我们可以使用CSS的position属性设置元素的定位方式,使用relative来实现相对定位。下面的例子中,我们创建了一个div元素,并将其进行相对定位。

<div class="example">
   <h2>我是相对定位的标题</h2>
   <p>
      我是相对定位的段落,并且稍微调整了我的位置。
   </p>
</div>

<style>
   .example {
        position: relative;
        left: 20px;
        top: 10px;
    }
</style>

三、relative定位的应用

1. 嵌套元素位置调整

相对定位可以让我们对某个元素进行微调,使其更好地嵌套在其他元素中。下面的例子中,我们给一个div元素添加了相对定位,并调整了它的位置,以让它和另一个div元素重叠。

<div class="example">
   <h2>我是相对定位的标题</h2>
   <p>
      我是相对定位的段落,并且稍微调整了我的位置。
   </p>
</div>
<div class="overlap">
   <p>我和上面那个div元素发生了重叠。</p>
</div>

<style>
   .example {
        position: relative;
        left: 20px;
        top: 10px;
    }
    .overlap {
        position: relative;
        top: -20px;
    }
</style>

2. 悬浮效果

通过将某个元素相对定位,并使用left、right、top、bottom等属性,我们可以轻松实现一些悬浮效果,比如鼠标悬浮在元素上时出现的提示框。下面的例子中,我们使用相对定位和top、left属性实现了一个简单的悬浮效果。

<div class="example">
   <p>鼠标悬浮在这个元素上时,我会在下面出现一个提示框。</p>
   <div class="tooltip">
      <p>我是一个提示框。</p>
   </div>
</div>

<style>
   .example {
       position: relative;
   }
   .tooltip {
       position: absolute;
       background-color: #fff;
       border: 1px solid #ccc;
       padding: 10px;
       top: 30px;
       left: -10px;
       display: none;
    }
    .example:hover .tooltip {
        display: block;
    }
</style>

3. 快速制作画廊

相对定位还可以用来快速制作一个简单的画廊效果。下面的例子中,我们将多个图片进行相对定位,使它们重叠在一起,只显示最上方的一个图片。当鼠标移到不同的图片上时,通过改变其top和left属性实现从下方滑入的效果。

<div class="gallery">
   <img src="img1.jpg" alt="图片1">
   <img src="img2.jpg" alt="图片2">
   <img src="img3.jpg" alt="图片3">
   <img src="img4.jpg" alt="图片4">
</div>

<style>
   .gallery {
       position: relative;
       height: 400px;
   }
   .gallery img {
       position: absolute;
       top: 0;
       left: 0;
       transition: all 0.3s ease-in-out;
   }
   .gallery img:hover {
       top: -10px;
       left: -10px;
       z-index: 1;
   }
</style>

四、总结

相对定位是CSS中最常用的定位方式之一,它可以方便地实现元素位置微调、悬浮效果、画廊效果等功能。无论在什么场景下使用,相对定位都可以帮助我们更好地掌控元素的位置和布局,提高网站的用户体验。

CSS位置属性之relative定位详解

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)