您的位置:

如何实现CSS布局中的垂直对齐?

一、vertical-align属性详解

在CSS中,有一个用于控制行内元素垂直对齐的属性:vertical-align。这个属性的取值很多,常用的如下:

vertical-align: baseline;
vertical-align: sub;
vertical-align: super;
vertical-align: text-top;
vertical-align: text-bottom;
vertical-align: middle;
vertical-align: top;
vertical-align: bottom;

其中,vertical-align: middle;是最常用也最简单的控制垂直居中的方式。举个例子:

<div class="container">
  <div class="child">
    <p>这里是要垂直居中的文字</p>
  </div>
</div>

.container {
  height: 300px;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

.child {
  display: inline-block;
}

其中,父元素.container的高度要先规定好,然后使用display: table-cell;和vertical-align: middle;就能实现这个元素内部的垂直居中。注意,这个方法只针对行内元素、内联块元素、表格单元格等这些可以应用vertical-align属性的元素有效。

二、flex布局中的垂直居中

Flex布局(Flexible Box)是CSS3新增的一种布局方式,它常用于解决常规布局难以解决的问题,比如垂直居中。

在Flex布局中,实现元素垂直居中的方法非常简单,只需要在父元素上加上display: flex;align-items: center;即可:

<div class="container">
  <div class="child">
    <p>这里是要垂直居中的文字</p>
  </div>
</div>

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

.child {
  display: inline-block;
}

其中,父元素.container也要先规定好高度,然后使用display: flex;align-items: center;就能实现所有的直接子元素垂直居中。

三、absolute+transform方式

我们还可以使用absolute+transform方式实现垂直居中,只需要这样写:

<div class="container">
  <div class="child">
    <p>这里是要垂直居中的文字</p>
  </div>
</div>

.container {
  position: relative;
  height: 300px;
}

.child {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  display: inline-block;
}

其中,父元素.container要先设置定位方式为absolute和高度,子元素.child则使用定位方式absolute、top:50%和transform: translateY(-50%);就能实现垂直居中效果了。只不过要注意,这个方法只能用于已知高度和宽度的元素进行垂直居中。

四、line-height方式

对于行内元素而言,还有一种更容易上手的垂直居中方法:line-height。

代码如下:

<p class="box">这里是要垂直居中的文字</p>

.box {
  height: 100px;
  line-height: 100px;
}

其中,设置好元素高度后,将line-height设置为相同的高度,就能实现垂直居中。

五、表格单元格方法

最后,还有一种方法是使用表格单元格方式实现垂直居中。代码如下:

<table class="table">
  <tbody>
    <tr>
      <td class="cell">这里是要垂直居中的文字</td>
    </tr>
  </tbody>
</table>

.table {
  height: 300px;
  border-collapse: collapse;
}

.cell {
  text-align: center;
  vertical-align: middle;
}

其中,使用table标签、border-collapse: collapse;隐藏边框后,再将单元格td的vertical-align: middle;,就能实现单元格内的元素垂直居中。

如何垂直对齐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表格中垂直对齐?

2023-05-12
如何在CSS中垂直对齐文本?

2023-05-12
如何实现文本的垂直居中对齐

2023-05-12
CSS垂直居中对齐元素的实现方法

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

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