您的位置:

完美掌握CSS垂直居中技巧,提升网页优化与用户体验

在网页设计中,垂直居中是我们经常会遇到的问题。若网页元素无法垂直居中,不仅给页面带来瑕疵,也会影响用户的使用体验和页面优化。因此,掌握CSS垂直居中技巧是非常必要和重要的。

一、宽高已知的元素垂直居中

假如我们有一个宽高都为200px的元素,它位于其父元素的中心位置。那么,我们该如何让这个子元素垂直居中呢?


父元素:position: relative;
子元素:position: absolute; 
  top: 50%;
  margin-top: -100px; /* 子元素高度的一半 */

我们通过给子元素设置一个相对定位,并设置子元素的top值为50%。再加上margin-top值为子元素高度的一半,就可以实现高度宽度已知的元素居中。

二、宽高未知的元素垂直居中

如果子元素宽高未知,又该怎么办?下面我们先来看一个比较基础的例子:


父元素:display: table-cell;
  vertical-align: middle;
子元素:display: inline-block;

在本例中,我们对父元素以display: table-cell方式进行属性设置,并在其中添加vertical-align: middle,来实现元素的垂直居中。子元素设置为display: inline-block,可以避免宽度过大导致元素换行。

如果不考虑兼容性问题,我们也可以借助flex布局来实现。可以使用如下代码:


.container {
  display: flex;
  justify-content: center;
  align-items: center;  
}

使用display: flex可以将子元素集中于父元素中,justify-content: center可以使它们水平居中,align-items: center可以使它们垂直居中。

三、绝对定位元素垂直居中

常见的情况是,我们需要将绝对定位的元素居中显示。下面介绍两种方法:

第一种方法是,将绝对定位的元素上下左右都为0,使其定位于父元素中心位置。具体代码如下:


父元素:position: relative;
子元素:position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto; 

上述代码会将绝对定位元素左右、上下都挤满到父元素中心的位置。margin: auto会使其在垂直方向上自动居中。

第二种方法是,使用transform属性,将绝对定位的元素自身向上移动50%,并算出它的高度的负数值来使其居中显示。代码如下:


子元素:position: absolute;
  top: 50%;
  transform: translateY(-50%);

以上是CSS实现垂直居中的几种方法,它们的应用场景和使用方法各有不同。我们可以在实际应用中按照要求进行选择。

完美掌握CSS垂直居中技巧,提升网页优化与用户体验

2023-05-12
CSS字体垂直居中详解

2023-05-17
提高网页浏览体验的CSS技巧

2023-05-12
优化网页性能的技巧,提升用户体验

2023-05-12
优化网页排版,让段落居中显示的技巧

2023-05-12
使用CSS Flex实现垂直居中:提升网页布局表现

2023-05-16
提升网页用户体验的技巧

2023-05-12
优化网页排版的技巧:深入理解CSS样式与HTML的margi

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)