您的位置:

CSS 文本阴影效果

CSS (Cascading Style Sheets) 是用于网页设计的样式表语言。它可以为网页添加各种各样的效果。本文将从多个方面详细阐述如何在 CSS 中为文本添加阴影效果。

一、使用 text-shadow 属性添加简单阴影效果

  
  

<style>

h1 {

  text-shadow: 2px 2px 4px #000000;

}

</style>

text-shadow 属性是添加文本阴影的基本方法。它接受四个值:

  • 第一个值表示阴影的水平位置。
  • 第二个值表示阴影的垂直位置。
  • 第三个值表示阴影的模糊程度。
  • 第四个值表示阴影的颜色。

可以使用逗号分隔多个阴影效果:

  
  

<style>

h1 {

  text-shadow: 2px 2px 4px #000000,

            -2px -2px 4px #ffffff;

}

</style>

二、使用 box-shadow 属性添加阴影效果

  
  

<style>

h1 {

  box-shadow: 2px 2px 4px #000000;

}

</style>

box-shadow 属性是为盒子元素添加阴影效果的方法。它可以接受四个值:

  • 第一个值表示阴影的水平位置。
  • 第二个值表示阴影的垂直位置。
  • 第三个值表示阴影的模糊程度。
  • 第四个值表示阴影的颜色。

与 text-shadow 不同的是,box-shadow 会为盒子及其内容添加阴影效果。

三、 使用 outline 及 text-shadow 组合实现描边效果

  
  

<style>

h1 {

  outline: 2px solid #000000;

  text-shadow: -1px -1px 0 #ffffff, 1px -1px 0 #ffffff, -1px 1px 0 #ffffff, 1px 1px 0 #ffffff;

}

</style>

outline 是用于添加轮廓线的属性。通过设置相同或相似的文本阴影,可以实现描边效果。

四、使用网页字体库实现阴影效果

除了手动添加阴影效果外,很多网页字体库也提供了阴影效果的选项。通过引入这些字体库,可以轻松实现不同种类的文本阴影效果。

  
  

<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed:700" rel="stylesheet">

<style>

h1 {

  font-family: 'Roboto Condensed', sans-serif;

  text-shadow: 2px 2px 4px #000000;

}

</style>

以上代码将 Roboto Condensed 字体库的 700 样式引入,并实现了简单的阴影效果。

五、结论

本文详细介绍了 CSS 中常用的文本阴影效果的实现方法,包括 text-shadow 属性、box-shadow 属性、outline 及 text-shadow 组合和网页字体库的使用。读者可以根据需要选择合适的方法实现自己的文本阴影效果。

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-20
如何在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)