Table of Contents
border-image简介
为什么图片可以作为边框?
border-image-source
border-image-slice
border-image-width
border-image-repeat
border-image-outset
后续
Home Web Front-end HTML Tutorial 详解css3之border-image_html/css_WEB-ITnose

详解css3之border-image_html/css_WEB-ITnose

Jun 21, 2016 am 09:03 AM

border-image简介

css3中新增了一个属性border-image,这个属性允许开发者使用图片来定义边框,扩充了 CSS2 中仅有的几个预定义边框样式(border-style)。
border-image 是一个简写属性,分别设定了以下几个属性。

  • border-image-source

  • border-image-slice

  • border-image-width

  • border-image-outset

  • border-image-repeat

  • 为什么图片可以作为边框?

    在开始学习border-image之前我有一个疑问,图片是怎么应用到容器的边框上的?

    看下图

    简单的说就是把图片的中间部分去掉,从而形成一个类似边框的框架。

    那么如何使用被划分出来的八格素材来生成一个边框呢?

    左上右上左下右下的四格素材分别作为边框的四个角,而上下左右的四格素材分别经过不同形式的扩展,形成边框的四条边。上图很好的说明了图片形成边框的处理形式。

    border-image-source

    border-image-source属性用来指定边框所需素材的路径,语法可以参照backgound-image。
    需要注意的是,如果只设置了border-image-source属性而其他属性使用缺省值,则边框素材不会被划分九宫格,而是将整个素材按照边框宽度缩放至合适尺寸后安放在边框四角

    <!doctype html><html><head><meta charset="utf-8"><title>边框图片</title><style>#border_image {    width:100px;    height:100px;    border:15px solid #ccc;    border-image:url("http://img.mukewang.com/52e21fea000127e802100210.jpg");    background:#ccc;    }</style></head><body><div id="border_image"></div></body></html>
    Copy after login

    如下图

    border-image-slice

    border-image-slice属性用来设置边框素材的切割尺寸,如下图,依次是上横切割线,右竖切割线,下横切割线,左竖切割线。数值分别代表从上、右、下、左边缘向素材中心延伸的像素/百分比数
    注意:切割的数值只接受像素和百分比两个单位,并且像素单位必须省略,即只接收数值或者百分比的形式。例如,border-image-slice: 10 10 30 10代表图片素材按照下图的样式被切割:

    border-image-width

    border-image-width用来设置边框素材的宽度,当同时设置了border-image-width和border-width属性时,那么边框的实际宽度由border-width属性决定。此时,如果border-image-width属性小于border-width属性,边框图片会沿边框的外侧分布而内侧留空形成 padding 的效果;如果border-image-width属性大于border-width属性,边框图片会仍会沿边框的外侧分布而内测溢出,如下图

    border-image-repeat

    border-image-repeat属性用来设置上下左右四边(即2、4、5、7四个素材块)的重复形式,分别有stretch repeat round space四个取值

  • stretch:指定用拉伸方式来填充边框背景图。

  • repeat:指定用平铺方式来填充边框背景图。当图片碰到边界时,如果超过则被截断。

  • round:指定用平铺方式来填充边框背景图。图片会根据边框的尺寸动态调整图片的大小直至正好可以铺满整个边框。

  • space:指定用平铺方式来填充边框背景图。图片会根据边框的尺寸动态调整图片的之间的间距直至正好可以铺满整个边框。

  • border-image-outset

    border-image-outset属性会产生使 border-image 相对于原始位置向外侧推移的效果,设置border-image-outset属性会使 border-image 溢出,但不会影响整个盒模型的尺寸,因此在设置此属性时要特别注意防止元素间的相互干扰。

    如下图

    后续

    border-image是css3属性所以存在兼容性问题,需要在属性前面设置-webkit

    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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    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)

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

    The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

    What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    See all articles