您的位置:

Cascading Style Sheets

Cascading Style Sheets,也称为CSS,是一种旨在为Web页定义样式的语言。CSS样式定义了对文本、字体、白色、背景和其他网页元素该如何显示和放置的规则。

一、样式表的分类

CSS样式表主要分为三类:

1、内联样式表:将CSS样式直接写在HTML页面标签的style属性中。

    <h1 style="color:blue;margin-left:30px;font-size:20px;">标题</h1>

2、文档样式表: CSS代码放置在一个独立的外部文档中,以.css后缀命名。

    /*style.css*/
    h1 {
        color:blue;
        margin-left:30px;
        font-size:20px;
    }

    /* index.html */
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <h1>标题</h1>
    </body>

3、内部样式表:CSS代码写在HTML文档的heade标签中,以style标签包裹。

    <head>
        <style>
            h1 {
                color:blue;
                margin-left:30px;
                font-size:20px;
            }
        </style>
    </head>
    <body>
        <h1>标题</h1>
    </body>

二、CSS选择器

选择器是CSS中非常重要的一部分,它用于定位HTML文档中的元素。

1、元素选择器:使用元素名称来选择一组元素。

    h1 {
        color:blue;
        font-size:20px;
    }

2、类选择器:使用class属性选择匹配的元素。

    .big {
        font-size:20px;
    }
    <p class="big">这是一个大号字体的段落</p>

3、ID选择器:使用id属性选择匹配的元素。

    #main-logo {
        width:200px;
        height:100px;
    }
    <img id="main-logo" src="logo.jpg">

三、CSS盒模型

CSS盒模型封装了HTML元素的内容、内边距、边框和外边距。将Web文档的各个部分视为一个矩形框,即盒子(box),盒模型的主要作用就是对这些矩形框进行布局,以及设置每个盒子的大小、位置。

盒模型主要分为两种模型:

1、标准盒模型:盒子总宽度=内容宽度+内边距宽度+边框宽度+外边距宽度。

    .box {
        width:200px;
        height:200px;
        padding:20px;
        border:1px solid #000;
        margin:10px;
    }
    <div class="box"></div>

2、IE盒模型:盒子总宽度=内容宽度+外边距宽度+边框宽度。padding 和 border都在里面。

    .box {
        width:200px;
        height:200px;
        padding:20px;
        border:1px solid #000;
        margin:10px;
        box-sizing:border-box; /*IE盒模型*/
    }
    <div class="box"></div>

四、响应式布局

响应式设计是一种设计理念,可以自动适应不同设备和屏幕大小的网站。有两种方法可以实现响应式布局:

1、媒体查询:可以为不同设备设置不同的CSS样式。

    @media screen and (max-width:768px) {
        body {
            background:blue;
        }
    }
    @media screen and (min-width:768px) {
        body {
            background:green;
        }
    }

2、弹性布局:使网页元素的大小和位置随屏幕大小的变化而变化。

    .parent {
        display:flex;
        justify-content:space-between;
        align-items:center;
    }
    .child {
        flex:1; /*平均分配空间*/
        height:50px;
        margin:5px;
    }
    <div class="parent">
        <div class="child">1</div>
        <div class="child">2</div>
        <div class="child">3</div>
    </div>

五、定位和浮动

1、定位:使用position属性设置元素的定位方式。

    .box {
        width:200px;
        height:200px;
        position:absolute;
        top:50%;
        left:50%;
        transform:translate(-50%,-50%);
    }
    <div class="box"></div>

2、浮动:使元素向左或向右浮动,可以使多个元素在同一行显示。

    .left {
        float:left;
        width:200px;
        height:200px;
        background:red;
    }
    .right {
        float:right;
        width:200px;
        height:200px;
        background:blue;
    }
    <div class="left"></div>
    <div class="right"></div>

六、CSS动画

使用CSS动画可以让元素在页面上显示出流畅的运动效果。

    @keyframes move {
        0% {transform:translateX(0);}
        50% {transform:translateX(500px);}
        100% {transform:translateX(0);}
    }
    .box {
        width:100px;
        height:100px;
        background:red;
        animation:move 3s linear infinite;
    }
    <div class="box"></div>

七、总结

CSS作为网页的样式表,对于美化网页、提高用户体验起着至关重要的作用。在学习CSS时,需要掌握基本的语法、选择器、盒模型等知识,并学习响应式布局、定位和浮动、CSS动画等高级应用。

Cascading Style Sheets简介

2023-05-12
Cascading Style Sheets

2023-05-12
CSS Cascading Style Sheets

2023-05-12
Cascading Style Sheet

2023-05-12
Cascading Style Sheet

2023-05-12
什么是CSS样式

2023-05-12
如何给HTML元素添加CSS样式

2023-05-12
CSS行内样式详解

2023-05-21
CSS单位:掌握测量网页元素的新方法

2023-05-12
HTML与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)