您的位置:

CSS单位:掌握测量网页元素的新方法

CSS(Cascading Style Sheets)是在网页制作中使用最广泛的样式表语言,它实现了页面的布局与样式的分离,让页面可维护性提高,同时也提高了网页的加载速度。

CSS中有许多单位,在网页开发中使用单位是非常重要的,因为单位不仅能够使页面更加美观,还能够保证不同浏览器展现的效果一致。这篇文章将介绍网页开发中最常用的CSS单位,帮助读者掌握测量网页元素的新方法。

一、像素(px)

像素是CSS中最常用的单位,它是相对于显示器屏幕分辨率而言的。一个像素相当于显示器上的一个点,像素单位与屏幕密度相关。在普通显示器或笔记本电脑上,1个像素可以表示为每英寸96个点(dpi),而在高分辨率的Retina屏幕上,1个像素可以表示为每英寸192个点(dpi)。

/*使用像素设置字体大小*/
h3 {
  font-size: 24px;
}

/*使用像素设置边框线粗细*/
div {
  border: 1px solid #000;
}

/*使用像素设置图片大小*/
img {
  width: 200px;
}

二、百分比(%)

百分比单位是相对于父元素的宽度而言的。如果一个元素的宽度设置为100%,那么它将会占用其父元素的全部宽度。

/*将子元素宽度设置为父元素的50%*/
.parent {
  width: 500px;
}
.child {
  width: 50%;
  height: 100px;
  background-color: red;
}

三、视口单位(vw、vh、vmin和vmax)

视口单位是相对于浏览器视口而言的,其中vw和vh表示视口宽度和高度的百分比,vmin和vmax分别表示vw和vh中较小和较大的值。

/*设置页面宽度为浏览器视口宽度的50%*/
body {
  width: 50vw;
}

/*设置三个div元素宽度分别为屏幕宽度的1/3*/
.container {
  display: flex;
  flex-wrap: wrap;
}
.box {
  width: 33.33vw;
  height: 300px;
  background-color: blue;
  margin: 10px;
}

四、字体相对单位(em和rem)

相对单位是基于元素自身的字体大小而言的,其中em单位是相对于父元素的字体大小而言的,而rem单位是相对于根元素(即html元素)的字体大小而言的。

/*将元素的字体大小设置为父元素字体大小的2倍*/
.parent {
  font-size: 16px;
}
.child {
  font-size: 2em;
}

/*将根元素的字体大小设置为16px*/
html {
  font-size: 16px;
}

/*将元素的字体大小设置为根元素字体大小的2倍*/
.child {
  font-size: 2rem;
}

五、其他单位(pt、mm和cm)

除了上述常用的单位之外,CSS还支持pt、mm和cm等其他测量单位。pt是“point”的缩写,表示1/72英寸,通常用于印刷行业。mm和cm分别表示毫米和厘米,通常应用于页面的打印样式表。

/*设置元素的字体大小为12pt*/
h3 {
  font-size: 12pt;
}

/*设置元素的宽度为30mm*/
div {
  width: 30mm;
  height: 50mm;
  background-color: red;
}

总结一下,掌握CSS单位是网页开发中一个非常重要的方面。本文介绍了常用的CSS单位,包括像素、百分比、视口单位、字体相对单位和其他测量单位。如果我们能够熟练地使用这些单位,就能够更加灵活地控制网页的元素大小和布局。

CSS单位:掌握测量网页元素的新方法

2023-05-12
CSS测量单位一览表

2023-05-12
掌握CSS高阶技巧,让你的网站脱颖而出

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