CSS3 border-image介紹_html/css_WEB-ITnose
Jun 21, 2016 am 08:49 AM
CSS3里border邊框?qū)傩宰隽舜蠓鶖U(kuò)展,其中border-image這個(gè)屬性(顧名思義,就是可以給邊框添加圖片)理解和使用起來(lái)有點(diǎn)難度。本篇就介紹一下border-image。
先從基本的語(yǔ)法開始入手,例如border-image: url(border.png) 26 repeat stretch;??梢钥闯龇殖扇齻€(gè)部分:url,剪裁位置,顯示方式
首先是url,可以使用絕對(duì)或相對(duì)路徑來(lái)加載邊框圖片,比較簡(jiǎn)單就不啰嗦了。
其次是剪裁位置,使用TRBL原則(top-right-bottom-left),上例中26就表示上右下左都剪裁26px。你可以設(shè)成26 52,就表示上下剪裁26px,左右剪裁52px。你也可以設(shè)成10 15 20 25表示上10px,右15px,下20px,左25px位置開始剪裁。單位不指定的話,默認(rèn)是px像素,你也可以設(shè)成%百分比。比如一張50px * 50px的圖片,你設(shè)成20%,就表示上右下左都剪裁10px。
經(jīng)過(guò)上右下左剪裁后,好好一張圖片就成了9宮格的樣子。(9宮格大家都知道的吧?錘子手機(jī)界面就是9宮格)。一圖勝千言
剪裁前該圖片的尺寸是78px * 78px
經(jīng)過(guò)border-image: url(border.png) 26;剪裁后成了9宮格
9宮格分為9個(gè)部分(這不是廢話么…):上右,上中,上左,中右,中中,中左,下右,下中,下左。經(jīng)剪裁后,9宮格最中心的部分,即中中,不會(huì)被顯示出來(lái),會(huì)被丟棄。能顯示的就是9宮格周圍那一圈。其中四個(gè)角(圖中紅色菱形部分)就分別固定在border的四個(gè)角。即上右部分就顯示在border的右上角,同理上左,下右,下左部分就分別顯示在border的左上角,右下角,左下角。剩下4個(gè)部分上中,中右,中左,下中(圖中藍(lán)色菱形部分)的顯示效果需要結(jié)合顯示方式。
顯示方式分stretch拉伸,repeat重復(fù),round平鋪。默認(rèn)值是stretch拉伸。你需要對(duì)9宮格的上中和下中設(shè)置水平顯示方式,對(duì)中右和中左設(shè)置垂直顯示方式。
stretch拉伸最容易理解。但round平鋪和repeat重復(fù)比較搞,有什么區(qū)別呢?round會(huì)改變9宮格部分的原始尺寸,而repeat重復(fù)會(huì)保留9宮格部分的原始尺寸,然后居中開始往兩邊無(wú)腦重復(fù)。
比如水平平鋪(9宮格里上中,下中部分平鋪),垂直拉伸(9宮格里中右,中左部分拉伸),所謂一圖勝千言:
.border-image { width: 120px; height: 80px; border: 26px solid; border-image: url(border.png) 26 round stretch;}<div class="border-image"></div>
垂直stretch拉伸效果一目了然。水平round平鋪會(huì)改變9宮格部分的原始尺寸,例中div長(zhǎng)度是120px,9宮格裁剪后一個(gè)藍(lán)色菱形是26px,因此可以顯示4.6個(gè)藍(lán)色菱形,四舍五入會(huì)顯示5個(gè)藍(lán)色菱形,每個(gè)菱形被縮小成了24px。
那我們?cè)囋噷⑺礁某蓃epeat重復(fù),看看有什么差別,所謂一圖勝千言:
.border-image { width: 120px; height: 80px; border: 26px solid; border-image: url(border.png) 26 repeat stretch;}<div class="border-image"></div>
repeat重復(fù)會(huì)保留9宮格部分的原始尺寸(每個(gè)藍(lán)色菱形仍舊26px),然后居中開始往兩邊無(wú)腦重復(fù),最終顯示4.6個(gè)藍(lán)色菱形
水平和垂直如果是同樣值,不必重復(fù)敲代碼:border-image: url(border.png) 26 round;即可。等價(jià)于border-image: url(border.png) 26 round round;。因?yàn)轱@示方式的默認(rèn)值是stretch拉伸。所以border-image: url(border.png) 26;等價(jià)于border-image: url(border.png) 26 stretch stretch;
用法介紹完了,有什么用呢?這就得發(fā)揮想象力了。比如一些漂亮的頁(yè)面效果。原始png圖:
用border-image給div的邊框加上該圖片后,該div的效果:
最后根據(jù)Can I Use顯示該屬性在IE上有兼容性問(wèn)題,只有IE11才開始支持。(對(duì)于IE,或曰對(duì)于微軟我早已無(wú)力吐槽)
其他瀏覽器最新版,直接用border-image沒(méi)問(wèn)題。但舊版需要加上前綴,參考MDN通常這樣寫:
-moz-border-image:url("border.png") 30 30 repeat; /* Old Firefox */ -webkit-border-image:url("border.png") 30 30 repeat; /* Safari */ -o-border-image:url("border.png") 30 30 repeat; /* Opera */ border-image:url("border.png") 30 30 repeat;

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

To use HTML button elements to achieve clickable buttons, you must first master its basic usage and common precautions. 1. Create buttons with tags and define behaviors through type attributes (such as button, submit, reset), which is submitted by default; 2. Add interactive functions through JavaScript, which can be written inline or bind event listeners through ID to improve maintenance; 3. Use CSS to customize styles, including background color, border, rounded corners and hover/active status effects to enhance user experience; 4. Pay attention to common problems: make sure that the disabled attribute is not enabled, JS events are correctly bound, layout occlusion, and use the help of developer tools to troubleshoot exceptions. Master this

Metadata in HTMLhead is crucial for SEO, social sharing, and browser behavior. 1. Set the page title and description, use and keep it concise and unique; 2. Add OpenGraph and Twitter card information to optimize social sharing effects, pay attention to the image size and use debugging tools to test; 3. Define the character set and viewport settings to ensure multi-language support is adapted to the mobile terminal; 4. Optional tags such as author copyright, robots control and canonical prevent duplicate content should also be configured reasonably.

TolearnHTMLin2025,chooseatutorialthatbalanceshands-onpracticewithmodernstandardsandintegratesCSSandJavaScriptbasics.1.Prioritizehands-onlearningwithstep-by-stepprojectslikebuildingapersonalprofileorbloglayout.2.EnsureitcoversmodernHTMLelementssuchas,

How to make HTML mail templates with good compatibility? First, you need to build a structure with tables to avoid using div flex or grid layout; secondly, all styles must be inlined and cannot rely on external CSS; then the picture should be added with alt description and use a public URL, and the buttons should be simulated with a table or td with background color; finally, you must test and adjust the details on multiple clients.

Using HTML sums allows for intuitive and semantic clarity to add caption text to images or media. 1. Used to wrap independent media content, such as pictures, videos or code blocks; 2. It is placed as its explanatory text, and can be located above or below the media; 3. They not only improve the clarity of the page structure, but also enhance accessibility and SEO effect; 4. When using it, you should pay attention to avoid abuse, and apply to content that needs to be emphasized and accompanied by description, rather than ordinary decorative pictures; 5. The alt attribute that cannot be ignored, which is different from figcaption; 6. The figcaption is flexible and can be placed at the top or bottom of the figure as needed. Using these two tags correctly helps to build semantic and easy to understand web content.

class, id, style, data-, and title are the most commonly used global attributes in HTML. class is used to specify one or more class names to facilitate style setting and JavaScript operations; id provides unique identifiers for elements, suitable for anchor jumps and JavaScript control; style allows for inline styles to be added, suitable for temporary debugging but not recommended for large-scale use; data-properties are used to store custom data, which is convenient for front-end and back-end interaction; title is used to add mouseover prompts, but its style and behavior are limited by the browser. Reasonable selection of these attributes can improve development efficiency and user experience.

When there is no backend server, HTML form submission can still be processed through front-end technology or third-party services. Specific methods include: 1. Use JavaScript to intercept form submissions to achieve input verification and user feedback, but the data will not be persisted; 2. Use third-party serverless form services such as Formspree to collect data and provide email notification and redirection functions; 3. Use localStorage to store temporary client data, which is suitable for saving user preferences or managing single-page application status, but is not suitable for long-term storage of sensitive information.

Native lazy loading is a built-in browser function that enables lazy loading of pictures by adding loading="lazy" attribute to the tag. 1. It does not require JavaScript or third-party libraries, and is used directly in HTML; 2. It is suitable for pictures that are not displayed on the first screen below the page, picture gallery scrolling add-ons and large picture resources; 3. It is not suitable for pictures with first screen or display:none; 4. When using it, a suitable placeholder should be set to avoid layout jitter; 5. It should optimize responsive image loading in combination with srcset and sizes attributes; 6. Compatibility issues need to be considered. Some old browsers do not support it. They can be used through feature detection and combined with JavaScript solutions.
