您的位置:

如何在网页中使用CSS样式?-基础教程

一、选择器

CSS的核心是选择器。选择器选中HTML元素,然后将样式应用于选中的元素。常见的选择器有:

  • 元素选择器:选中所有指定元素
  • <style>
      p {
        color: red;
      }
    </style>
  • 类选择器:选中所有使用指定类的元素
  • <style>
      .example {
        font-size: 20px;
      }
    </style>
    <p class="example">这是一个示例</p>
  • ID选择器:选中指定ID的元素
  • <style>
      #example {
        background-color: gray;
      }
    </style>
    <div id="example">这是一个示例</div>

二、样式属性

CSS样式属性是CSS规则的一部分,用于定义应用于元素的样式。以下是一些常见的CSS样式属性:

  • color:定义文本颜色
  • <style>
      p {
        color: red;
      }
    </style>
  • font-size:定义字体大小
  • <style>
      p {
        font-size: 20px;
      }
    </style>
  • background-color:定义背景颜色
  • <style>
      body {
        background-color: lightblue;
      }
    </style>
  • border:定义边框
  • <style>
      div {
        border: 1px solid black;
      }
    </style>
  • margin:定义元素边框与外边距之间的空间
  • <style>
      div {
        margin: 10px;
      }
    </style>

三、CSS盒模型

CSS盒模型是指一个HTML元素所包含的所有内容,如边框、内边距、外边距和实际内容本身。盒模型由四个属性定义:

  • content:元素内容的宽度和高度
  • padding:元素内容与边框之间的空间
  • border:元素边框的宽度、样式和颜色
  • margin:元素与其他元素之间的空间

下面是一个盒模型的示例:

<style>
  div {
    width: 100px;
    height: 100px;
    padding: 10px;
    border: 1px solid black;
    margin: 10px;
  }
</style>
<div>这是一个示例</div>

四、CSS布局

CSS布局用于定位和组织HTML元素。以下是一些CSS布局技巧:

  • 浮动:float属性将元素向左或向右移动,使得其他元素可以占据它的位置
  • <style>
      .left {
        float: left;
        width: 50%;
      }
      .right {
        float: right;
        width: 50%;
      }
    </style>
    <div class="left">这是左边的内容</div>
    <div class="right">这是右边的内容</div>
  • 定位:position属性将元素相对于其父元素或浏览器窗口进行移动
  • <style>
      .box {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    </style>
    <div class="box">这是一个居中的盒子</div>
  • Flex布局:flex布局是一种基于弹性盒子模型的布局方式,允许子元素自由调整尺寸和位置。它包括一个容器和其内部的项目
  • <style>
      .container {
        display: flex;
        justify-content: center;
        align-items: center;
      }
    </style>
    <div class="container">
      <div>这是一个居中的盒子</div>
    </div>

五、响应式设计

响应式设计是指在设计网站的过程中,考虑不同设备(如计算机、平板电脑、手机)的屏幕大小和分辨率,以便网站能够在各种设备上正常显示。以下是一些实现响应式设计的技巧:

  • 媒体查询:使用@media规则,根据设备的屏幕大小或方向来应用不同的样式
  • <style>
      /* 如果屏幕宽度小于600px,将段落字体大小设置为14px */
      @media only screen and (max-width: 600px) {
        p {
          font-size: 14px;
        }
      }
    </style>
  • 灵活的图片:图片大小会影响页面的加载速度,可以使用CSS样式让图片自适应不同的屏幕大小
  • <style>
      img {
        max-width: 100%;
        height: auto;
      }
    </style>

结论

以上是如何在网页中使用CSS样式的基础教程。学会了这些基础知识后,可以创建各种各样的网站页面。

如何在网页中使用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)