您的位置:

CSS中使用伪元素

一、:before和:after伪元素

:before和:after伪元素是CSS中最常用的伪元素。它们可以在元素的内容之前或之后插入生成的内容。:

  
    .example:before {
      content: 'before';
    }

    .example:after {
      content: 'after';
    }
  

上面的例子会在class为example的元素的内容之前显示“before”,在其内容之后显示“after”,不过这个文字内容是虚拟的,应该尽量用CSS规则去控制。这两个伪元素在布局和视觉上都非常有用,比如可以在按钮的左侧或右侧添加小图标,或者为blockquote元素添加引用符号。

二、:first-letter和:first-line伪元素

:first-letter和:first-line伪元素是应用于包含文本的元素上的伪元素。:

  
    .first-letter:first-letter {
      font-size: 200%;
      font-weight: bold;
      float: left;
      margin-right: 0.5em;
    }

    .first-line:first-line {
      font-size: 150%;
      font-weight: bold;
    }
  

上面的例子会将class为first-letter元素的第一个字母加粗变大并且左浮动,将class为first-line元素的第一行文字加粗变大。这两个伪元素能够提高文本排版的效果,使文本内容具有更好的视觉效果。

三、:nth-child伪类

:nth-child伪类可以选择某一个元素的子元素,并针对所选的子元素应用样式规则。:

  
    li:nth-child(4n+1) {
      background-color: #FFA07A;
    }
  

上面的例子会在每四个li元素中选择第一个,并为其添加背景颜色。:nth-child伪类非常有用,在处理网格系统、交替行的背景色、对列表和表格单元格进行样式控制等方面起到比较重要的作用。

四、:target伪类

:target伪类选择器被用于匹配URL中标记(#id)之后的元素。:

  
    #section2:target {
      background-color: yellow;
    }
  

URL为http://example.com/page#section2时,第二个section元素就会应用上述的样式规则。使用:target伪类可以非常有效地实现基于链接文本的目标元素样式控制,例如通过将目标元素背景颜色更改为高亮显示来增强页面的交互性。

五、:not伪类

:not伪类可以选择不符合指定选择器的元素并对其进行样式控制。:

  
    p:not(.special) {
      color: red;
    }
  

上面的例子会将所有class不为special的p元素的文字颜色改为红色。:not伪类可以用于控制那些异常情况下难以控制的选择器,对于高级CSS技术非常有用。

六、:checked伪类

:checked伪类可以用于对被单选框和复选框选中的选项进行样式控制。:

  
    input[type="checkbox"]:checked + label {
      background-color: #00FF00;
    }
  

上面的例子会将被选中的复选框的后面紧贴的label元素的背景颜色更改为绿色。:checked伪类可以非常有效地实现表单控件的样式控制,使得用户界面体验更好。

七、总结

伪元素和伪类都是非常强大的CSS选择器,可以在CSS中实现非常高级的效果,提高用户界面的体验。熟练掌握这些选择器和相关属性,在CSS项目开发中非常重要,能够节省开发时间,提升开发效率。

CSS中使用伪元素

2023-05-12
如何使用CSS伪元素美化网页元素

2023-05-12
使用CSS伪元素样式化的原始标题

2023-05-12
CSS伪类

2023-05-12
利用CSS伪类优化网页元素样式的技巧

2023-05-12
CSS after伪类

2023-05-18
CSS :visited 伪类

2023-05-12
CSS选择子元素详解

2023-05-18
伪元素和伪类的区别

2023-05-20
了解 CSS Pseudo-元素的实际含义

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)