您的位置:

CSS display属性的用法详解

CSS display属性是CSS的一个重要属性之一,它决定了一个元素应该生成什么类型的框。在CSS中,默认情况下,每个元素都是以某种框类型进行显示的。在这篇文章中,我们将从多个方面对CSS display属性的用法进行详细的阐述。

一、块级元素与行内元素

在CSS中,元素被分为块级元素和行内元素两种。css中每个元素都有display属性,这个属性指定了元素的类型。最基本的是块元素(block)和行内元素(inline)。

块级元素的特点是:

  • 会新起一行,一般占满父容器的宽度
  • 可以设置宽高,边距、内边距等样式,也可以嵌套其他元素
  • 默认情况下,块元素会将其前后元素都隔开一定的距离,可以使用margin属性进行微调

与块级元素不同的是,行内元素的特点是:

  • 不会新起一行,水平方向排列,宽度由内容撑开;
  • 设置宽高、内外边距,vertical-align属性、box-shadow等样式不会影响它们的布局;
  • 不能嵌套块状元素,只能包含数据和其它行内元素。
示例代码:
.block {
  display: block;
  width: 100px;
  height: 100px;
  background-color: red;
  margin-bottom: 20px;
}

.inline {
  display: inline;
  width: 100px;
  height: 100px;
  background-color: blue;
  margin-right: 20px;
}

二、行内块级元素

在CSS中,还有一种元素类型就是行内块级元素(inline-block)。行内块级元素与行内元素的布局方式相同,但可以设置宽度和高度等样式属性。行内块级元素最常见的应用是实现文本和图片的水平居中。

行内块级元素的特点是:

  • 水平方向排列,宽度由内容撑开,高度也可以设置。
  • 可以设置边框、背景、内边距等样式属性。
  • 可以添加上下内边距(padding)和左右边距(margin),但需要注意它们之间的间隔(px)会影响行内块元素在父容器中的布局。
示例代码:
.inline-block {
  display: inline-block;
  width: 100px;
  height: 100px;
  background-color: red;
  margin-right: 20px;
  vertical-align: middle;
}

三、none和inherit属性

CSS display属性还有两个特殊的值,一个是none,另一个是inherit。

none是display的默认值,表示元素不会被呈现出来,对应的元素就好像不存在一样。使用none时,元素不占据任何空间,它们失去了在文档流中的位置。这个属性在特殊情况下很有用,比如隐藏一些元素。

inherit表示继承其父元素的display属性。如果一个元素的display属性设置为inherit,则它会继承自己的父容器的display属性值。这样可以让元素在不同父元素下面有相同的布局效果。

示例代码:
.hide {
  display: none;
}

.parent {
  display: flex;
}

.child {
  display: inherit;
}

总结

CSS display属性有着非常广泛的应用。通过使用不同的display值,我们可以改变元素的布局方式,使其适应不同的布局需求。在实际开发中,需要根据页面需求和实际情况灵活运用display属性,以达到良好的用户体验。
CSS display属性的用法详解

2023-05-12
CSS display属性全面解析

2023-05-23
利用CSS的display属性提升网站排名

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)