您的位置:

绝对定位的图片

一、HTML给图片绝对定位

在HTML中,我们可以给一张图片添加绝对定位来使其脱离文档流,从而可以在页面上自由移动。下面我们来看一个简单的示例:

<div class="wrapper">
  <h2>这是一个标题</h2>
  <img src="img1.jpg" class="positioned" alt="">
  <p>这是一个段落</p>
</div>

我们在一个div元素内部插入了一个图片和一个段落,同时设定wrapper类的position属性值为relative。图片的class属性为positioned,接下来给图片添加以下CSS样式:

.positioned {
  position: absolute;
  top: 50px;
  left: 50px;
}

这将让图片从其父元素的坐标系中脱离,相对于文档的左上角定位,从该点偏移50像素的左上角继续显示。

当然,还有其他属性可以控制绝对定位元素,例如right和bottom。right:50px;则是将元素向右移动50像素

二、CSS图片绝对定位

在CSS中,我们可以使用绝对定位来控制图片的位置和大小。下面我们来看一个示例:

<div class="wrapper">
  <h2>这是一个标题</h2>
  <img src="img1.jpg" class="positioned" alt="">
  <p>这是一个段落</p>
</div>

同样的,我们用CSS来设置图片的样式,如下所示:

.positioned {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 200px;
}

这将让图片绝对定位,并使其垂直和水平中心在父元素的中心。接下来,我们使用 translate() 函数将它们返回到它们的正确位置。它的 width 更改为 200 像素是因为它太大了,我们需要它更小些

三、CSS图片绝对定位的特殊效果

使用绝对的定位还可以实现复杂的效果,例如拼图游戏中的有序变换。下面我们来看一个简单的示例:

<div class="wrapper">
  <h2>这是一个标题</h2>
  <img src="img1.jpg" class="positioned" alt="">
  <p>这是一个段落</p>
</div>

接下来我们来添加CSS样式,初始状态左上角是0,0:如下

.positioned {
  position: absolute;
  top: 0;
  left: 0;
  width: 40%;
  transition: all 1s ease;
}

这样在后续的操作中就会使用到 transition 这个属性了。然后,我们可以通过类似以下的方式操作来将图片移到它的新位置:

.changed {
  transform: translateX(50%) rotate(-45deg);
  left: 50%;
  top: 100px;
}

然后就可以玩出丰富的效果了。实现过程就是把图片在前后两个位置中间加一个中间状态:

.initial {
  position: absolute;
  top: 0;
  left: 0;
  width: 40%;
}

.middle {
  transform: translateX(50%) rotate(-45deg);
  left: 50%;
  top: 100px;
  transition: all 1s ease;
}

.final {
  transform: translateY(200%) rotate(180deg);
  left: 80%;
  top: 0;
  transition: all 1s ease;
}

我们假设需要拼成的图形为直角三角形向右倾斜,然后我们可以在中间的效果中进行旋转、偏移和变形。它将在1秒的时间内不间断地从初始状态到中间状态,然后是最终状态。同时谨记,transition属性可以使变化更平缓,过度的效果更自然

总的来说,绝对定位的图片在网页的设计中有着非常重要的地位。它可以实现对于图片在页面布局中的自由设计,同时也可以做到一些比较炫酷的特效。所以,前端程序员在设计页面的时候,绝对定位的图片是避免不了的。
CSS绝对定位图片

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

2023-05-12
利用CSS绝对定位放置图片的最佳实践

2023-05-12
CSS相对定位图片

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

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

2023-05-12
CSS绝对定位实现移动元素

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

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

2023-05-12
CSS如何在HTML中定位图片

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