您的位置:

CSS 盒子的定位

一、绪论

CSS 盒子的定位是指在网页中对盒子进行位置的移动和调整。网页设计中经常需要调整元素的位置,例如图片和文字的位置调整、弹出层和模态框的位置控制。可以使用 CSS 盒子的定位来实现这些效果。使用 CSS 盒子的定位,可以使页面的排版更加灵活,从而适应不同分辨率和设备的屏幕大小。

二、CSS 定位方式

CSS 盒子的定位主要有四种方式:

1. 静态定位(static)

静态定位是默认的盒子定位方式,盒子按照文档流排列。静态定位盒子的 left、top、right、bottom、z-index 属性无效。

2. 相对定位(relative)

.box {
  position: relative;
  left: 30px;
  top: 50px;
}

相对定位盒子的定位相对于自己的位置进行调整,使用 left、top、right、bottom 属性控制位置的偏移。偏移量会影响其他元素的定位,但不影响其占用的空间。

3. 绝对定位(absolute)

.box {
  position: absolute;
  left: 30px;
  top: 50px;
}

绝对定位盒子的定位相对于最近的非 static 属性的祖先元素进行调整,如果没有祖先元素,则相对于浏览器窗口。绝对定位盒子从文档流中脱离,不占用空间。使用 left、top、right、bottom 属性控制位置的偏移。

4. 固定定位(fixed)

.box {
  position: fixed;
  left: 30px;
  top: 50px;
}

固定定位盒子的定位相对于浏览器窗口,不随页面滚动而移动。固定定位的盒子与绝对定位类似,不占用文档流中的位置,对其他元素的定位没有影响。使用 left、top、right、bottom 属性控制位置的偏移。

三、CSS 定位属性

CSS 盒子的定位主要通过以下属性实现:

1. left

left 属性指定盒子左侧边框的水平位置。

2. top

top 属性指定盒子顶部边框的垂直位置。

3. right

right 属性指定盒子右侧边框的水平位置。

4. bottom

bottom 属性指定盒子底部边框的垂直位置。

5. z-index

z-index 属性指定盒子的层叠顺序。

四、CSS 定位案例

以下是一个使用 CSS 盒子定位的实例,实现了一个弹出层的效果:



  
  
  CSS 盒子定位示例
  


  
  
CSS 盒子的定位

2023-05-12
CSS 圆角盒子

2023-05-12
用HTML和CSS创建盒子

2023-05-12
如何将文本内容放置在有框的 CSS HTML 盒子中

2023-05-12
如何使用CSS控制盒子的边框?

2023-05-12
CSS Border Box Design:如何优化盒子模型

2023-05-12
CSS怪异盒模型详解

2023-05-22
如何运用CSS创建具有美感的盒子

2023-05-12
CSS标准盒模型详解

2023-05-12
js设置盒子样式,js设置盒子样式不变

2022-11-24
css设置的js对话框(js 选择文件对话框)

本文目录一览: 1、怎样用 CSS + JS 美化网页中的 select 下拉框 2、笔记:JS设置CSS样式的几种方式 3、怎么用div+css和js制作回复窗口 4、如何用JS点击超链接弹出对话框

2023-12-08
如何使用CSS中的位置属性进行页面定位

2023-05-12
阿平的python小笔记吖,python 阿里巴巴

2022-11-18
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)