您的位置:

使用CSS的方法将样式表导入到HTML页面中

一、内嵌式样式

内嵌式样式是将CSS样式直接写在HTML页面中的一种方式。在HTML文档的头部加入style标签,然后将CSS代码写在其中,该样式会应用到HTML文档中所有相应标签上。如下代码示例:

<!DOCTYPE html>
<html>
<head>
   <title>内嵌式样式</title>
   <style>
      h1 {
         color: red;
      }
   </style>
</head>
<body>
   <h1>这是一个标题</h1>
   <p>这是一段文字。</p>
</body> 
</html>

二、链接式样式

链接式样式是指在HTML文档头部通过link标签将CSS文件链接到HTML文件中。通过这种方式可以将CSS样式集中管理,同时可以在多个页面间共用同一份CSS样式。具体代码如下所示:

   <!DOCTYPE html>
   <html>
   <head>
      <title>链接式样式</title>
      <link href="style.css" rel="stylesheet" type="text/css">
   </head>
   <body>
      <h1>这是一个标题</h1>
      <p>这是一段文字。</p>
   </body> 
   </html>

在上述代码中,我们在head标签内使用link标签将样式文件“style.css”链接到了HTML页面中。style.css文件的代码如下所示:

   h1 {
      color: red;
   }

三、嵌入式样式

嵌入式样式是介乎于内嵌式样式和链接式样式之间的一种方式。它与内嵌式样式的区别在于,嵌入式样式的CSS代码是写在head标签内的style标签中,并且可以控制到页面内几个标签。“嵌入式”指的是样式代码位于HTML文件之中。如下是一个嵌入式样式的示例:

   <!DOCTYPE html>
   <html>
   <head>
      <title>嵌入式样式</title>
   </head>
   <body>
      <h1 style="color:red;">这是一个标题</h1>
      <p style="color:blue;">这是一段文字。</p>
   </body> 
   </html>

四、CSS优先级

当多个CSS规则应用于同一个元素时,根据不同的选择器权值,CSS规则的优先级会有所不同。总体而言,样式可以优先按照以下顺序进行应用:

  1. !important声明(权重最高)
  2. 内嵌样式表(标签内部样式)
  3. ID选择器
  4. 类、属性、伪类选择器
  5. 元素选择器、伪元素选择器、组合选择器(权重最低)

在实际开发中,我们通常需要注意不同选择器权值的使用,以避免样式的混乱。

五、选择器

CSS的选择器是决定样式作用对象的根本手段,目前一共有七种选择器:

  1. 标签选择器
  2. ID选择器
  3. 类选择器
  4. 后代选择器
  5. 相邻兄弟选择器
  6. 通用选择器
  7. 属性选择器

在实际开发中,我们需要根据需求和布局,巧妙运用这些选择器,以创造出更为优美和实用的页面效果。

六、总结

本文介绍了CSS样式表的三种导入方式和CSS的选择器的基本概念和种类,以及CSS优先级的规则。在实际开发中,需要针对具体需求合理选择样式表的导入方式,并且使用好各种选择器,打造出更为美观和实用的页面效果。

使用CSS的方法将样式表导入到HTML页面中

2023-05-12
如何通过CSS将样式表包含到HTML页面中

2023-05-12
CSS中的样式表属性和使用方法

2023-05-12
如何将CSS文件导入HTML文件中

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)