您的位置:

CSS input选中样式详解

一、选中样式概述

CSS中常用的选中样式有focus, hover, active等,在input元素上也有自己的选中样式——选中样式(:checked)。所谓选中样式,是指当input元素被选中时,改变元素的外观、颜色、大小等等属性来给用户明确的反馈。下面就从样式特征、兼容性等角度来谈谈CSS input选中样式。

二、选中样式的样式特征

:checked 只能应用于radio buttons 和 checkboxes。当元素被选中后,它会被标记为:checked。在CSS中,我们可以使用:checked伪类来修改选中样式。下面给出一个示例:

/* 改变选中状态的背景颜色和字体颜色 */
input[type="checkbox"]:checked + label {
  background-color: #2196F3;
  color: #fff;
}

代码中+号连接的是该元素的下一个兄弟元素,这里用来控制label标签的样式。这种样式修改方式在开发响应式网站时经常用到。

三、选中样式的兼容性

由于:checked选择器是CSS3的相关内容,因此在不同的浏览器中的效果会有所不同。在某些浏览器下(如IE6-IE8),:checked不支持,此时可以通过jQuery等JS框架来实现该功能。在样式应用上,我们可以通过CSS hack来达到相同的效果,如下:

/* 控制IE下选中样式 */
input[type=checkbox] {  
    background: yellowgreen;
}  
  
input[type=checkbox]:checked {  
    background: #4CAF50\9; /* IE下特有的Hack写法 */  
} 

我们可以通过\9使CSS标记为IE下特有的Hack写法,这同样适用于IE6-IE8。

四、选中样式带来的用户体验

选中样式的加入不仅可以增强网站的页面效果,同时也可以让用户更加方便地知道他们已经做出了哪些选择。例如,在一个表单提交页面中,勾选一个复选框后,页面通过选中状态的改变来提示用户已经完成选择了,从而增强用户的体验感。

五、选中样式的应用场景

选中样式常用在需要用户输入数据的场景中,例如表单页、注册页、登录页等。除此之外,它还可以用在列表页面中,用来提示用户哪些文章、商品已经被选中了,为用户提供更好的操作体验。下面是一个示例代码:

/* 选中状态下,改变商品外框样式 */
input[type="checkbox"]:checked + .goods {
    border: 3px solid #2196F3;
}

六、总结

CSS input选中样式是一个简单而实用的功能。它不仅可以达到美化页面的目的,同时还可以为用户提供更加智能化、直观化的操作体验。通过该功能的使用,可使网站的用户体验更加良好。

CSS input选中样式详解

2023-05-18
深入探讨CSS input placeholder样式(cs

2023-05-20
印象笔记记录java学习(Java成长笔记)

2022-11-12
java方法整理笔记(java总结)

2022-11-08
CSS样式详解

2023-05-21
重学java笔记,java笔记总结

2022-11-23
详解input的placeholder样式

2023-05-20
发篇java复习笔记(java课程笔记)

2022-11-09
CSS HTML鼠标样式

2023-05-12
input选中时会有边框的详细阐述

2023-05-21
input复选框详解

2023-05-18
python基础学习整理笔记,Python课堂笔记

2022-11-21
java学习笔记(java初学笔记)

2022-11-14
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)