Table of Contents
border-radius
box-shadow
box-sizing
text-overflow
transition(过渡)
指定属性
delay
transition-timing-function
Home Web Front-end HTML Tutorial CSS3 学习笔记_html/css_WEB-ITnose

CSS3 学习笔记_html/css_WEB-ITnose

Jun 24, 2016 am 11:15 AM

border-radius

圆角是做网页永远绕不过的话题,以前基本是通过背景图片做的,有了 CSS3 以后通过属性就 能够搞定,我们可以通过 border-radius 设置元素的圆角半径。

border-radius: 5px;
Copy after login

对于一个正方形,我们只需要设置为边长的一半就可以呈现一个圆。

div {    width: 200px;    height: 200px;    border-radius: 50%; // 设置百分比    border-radius: 100px; // 设置长度一半}
Copy after login

代码

border-radius 是缩写的格式,其实 border-radius 和 border 属性一样,还可以把各个角单独拆分出来,也就是以下四种写法

border-top-left-radius: length, length;border-top-right-radius: length, length;border-bottom-left-radius: length, length;border-bottom-right-radius: length, length;
Copy after login

第一个值是圆角水平半径,第二个值是垂直半径,如果第二个值省略,那么其等于第一个值,这时这个角就是一个四分之一的圆角,如果任意一个值为0,那么这个角就不是圆角

box-shadow

CSS3 原生支持了阴影效果

box-shadow:inset h-shadow v-shadow blur-radius spread-radius color
Copy after login

box-shadow 属性至多有6个参数设置:

  1. 阴影类型:此参数是一个可选值,如果不设值,其默认的投影方式是外阴影;如果取其唯一值inset,就是将外阴影变成内阴影

  2. h-shadow: 是指阴影水平偏移量其值可以是正负值可以取正负值,如果值为正值,则阴影在对象的右边,反之其值为负值时,阴影在对象的左边

  3. v-shadow: 是指阴影的垂直偏移量,其值也可以是正负值,如果为正值,阴影在对象的底部,反之其值为负值时,阴影在对象的顶部

  4. 阴影模糊距离:此参数是可选,,但其值只能是为正值,如果其值为0时,表示阴影不具有模糊效果,其值越大阴影的边缘就越模糊

  5. 阴影阴影的尺寸:此参数可选,其值可以是正负值,如果值为正,则整个阴影都延展扩大,反之值为负值是,则缩小

  6. 阴影颜色:此参数可选,如果不设定任何颜色时,浏览器会取默认色,但各浏览器默认色不一样

box-shadow 可以使用一个或多个投影,如果使用多个投影时必须需要用逗号,分开

box-shadow: 3px 3px 3px orange, 3px 3px 3px red;
Copy after login

box-sizing

这个属性多少和边框也相关,传统的盒模型width就是指内容区域宽度,和padding、border没有关系,但是这在布局上带来一定的困难

box-sizing 可以改变盒模型

  1. content-box:标准盒模型
  2. border-box:
    width = padding-left + padding-right + border-left-width + border-right-width + content width
    height = padding-top + padding-bottom + border-top-width + border-bottom-width + content height
div {    width: 100px; // width 包含了左右padding的宽度 + 左右border的宽度    height: 100px; // height 包含了上下padding的宽度 + 上下border的宽度    padding: 20px;    border: 1px solid #000;    box-sizing: border-box;}
Copy after login

代码

text-overflow

  1. clip:隐藏超出文本
  2. ellipsis:显示省略符号来代表被修剪的文本。

对于省略号效果还需要其它属性配合

p {    width: 100px;    overflow: hidden;    white-space: nowrap;    text-overflow: ellipsis;}
Copy after login

担心会有人提问上面的代码只能在第一行显示省略号,假如我想在第二行或者第三行显示怎么办呢?

代码

原理就是通过伪元素 ::after 来实现是不是非常的 easy。

过渡和动画

CSS3 的动画是浏览器原生支持的,好处就是流畅

transition(过渡)

在CSS3引入 transition 之前css没有时间轴,所有的状态变化都是瞬间完成。

transition 的作用在于,指定状态变化所需要的时间

div {    width: 100px;    height: 100px;    border: 1px solid #000;    background-color: #000;    transition: all 1s;}div:hover {    width: 200px;    height: 200px;}
Copy after login

代码

指定属性

我们还可以指定transition适用的属性

div {    transition: 1.5s width;}
Copy after login

完整代码

这样一来,只有height的变化需要1秒实现,其他变化(主要是width)依然瞬间实现。

在同一行transition语句中,可以分别指定多个属性

div {    transition: 1.5s width, 1s height;}
Copy after login

代码

delay

delay:中文翻译延迟,顾名思义就是延迟多长时间在执行状态变化。

需要注意的是 delay的参数是在时间的后面

div {    transition: 状态完成时间 height, 状态完成时间 延迟时间(delay) width;}
Copy after login
div {    transition: 2s height, 0.5s 2s width;}
Copy after login

完整代码

delay的真正意义在于,它指定了动画发生的顺序,使得多个不同的transition可以连在一起,形成复杂效果

transition-timing-function

transition的状态变化速度(又称timing function),默认不是匀速的,而是逐渐放慢,这叫做ease

div {    transition: 1s ease;}
Copy after login

除了ease以外,其他模式还包括

  • linear:匀速
  • ease-in:加速
  • ease-out:减速
  • ease-in-out:规定以慢速开始和结束的过渡效果
  • cubic-bezier函数:自定义速度模式 链接
  • 注意事项

    1. 目前,各大浏览器(包括IE 10)都已经支持无前缀的transition,所以transition已经可以很安全地不加浏览器前缀 transition兼容性
    2. transition需要明确知道,开始状态和结束状态的具体数值,才能计算出中间状态,什么none到block之类的是不行的
    3. transition是一次性的,不能重复发生,除非一再触发。

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1664
    14
    PHP Tutorial
    1269
    29
    C# Tutorial
    1248
    24
    Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

    The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

    HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

    HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

    The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

    The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

    HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

    HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

    See all articles