您的位置:

如何使用 CSS 控制垂直对齐

一、什么是垂直对齐

垂直对齐指的是将连续行内元素的内容在垂直方向上居中对齐。垂直对齐的实现方式有多种,本文将通过CSS的属性和技巧,详细讲解如何将元素实现垂直对齐。

二、使用 CSS 属性实现垂直对齐

CSS 属性中有几个专门用于控制垂直对齐的属性,它们分别是:

  • vertical-align
  • line-height
  • text-align

其中,vertical-align 是最主要的控制元素垂直对齐的属性。它可以接受多种值,包括:

  • baseline:基线对齐
  • middle:居中对齐
  • top:顶部对齐
  • bottom:底部对齐

我们可以通过设置vertical-align属性的值为 middle 来使元素实现垂直居中对齐。例如:

<div style="height: 100px; vertical-align: middle; display: inline-block;">这是一段文本</div>

效果如下:

这是一段文本

同时,我们还可以使用line-height属性来控制行高,从而实现垂直对齐。例如:

<div style="height: 100px; line-height: 100px;">这是一段文本</div>

效果如下:

这是一段文本

总之,通过这些属性的灵活运用,我们可以很容易地实现垂直对齐的效果。

三、使用 CSS 技巧实现垂直对齐

除了上述的 CSS 属性之外,还有一些 CSS 技巧也能够帮助我们实现垂直对齐。

1. 行内元素垂直对齐

对于行内元素,我们可以通过将父元素的line-height属性设置为和元素的height属性一样的值来实现垂直居中对齐。例如:

<div style="height: 100px; line-height: 100px;"><span style="display: inline-block; vertical-align: middle;">这是一段文本</span></div>

效果如下:

这是一段文本

我们可以发现,通过设置span元素的display为inline-block,再在父元素中设置line-height,就能够完美实现行内元素的垂直对齐。

2. 绝对定位元素垂直对齐

对于绝对定位的元素,我们可以使用 CSS3 中的transform属性来控制元素在垂直方向上的位置。例如:

<div style="position: relative; height: 200px;">
  <div style="position: absolute; top: 0; bottom: 0; margin: auto; transform: translateY(-50%);">这是一段文本</div>
</div>

效果如下:

这是一段文本

我们可以通过将绝对定位元素的 top 和 bottom 值都设置为 0,再通过设置 margin: auto 来使元素水平居中。最后,通过transform: translateY(-50%)来控制元素在垂直方向上向上偏移50%的距离,从而实现垂直对齐。

结语

掌握垂直对齐的技巧是前端开发中非常重要的技能之一。通过本文的介绍,相信大家已经能够熟练地使用 CSS 来实现元素的垂直对齐了。

如何使用 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
CSS中的垂直对齐方式

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

2023-05-12
CSS中的垂直对齐:如何使用valign top调整元素位置

2023-05-12
如何垂直对齐图片和文本 - HTML 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)