您的位置:

详解CSS border-image属性

一、border-image简介

CSS边框是一个重要的元素,可以通过它来给网站增加美观度和品质感。而在CSS中,可以通过border-image属性来设置元素的边框图片和显示方式。

border-image属性分为九部分,分别是:border-image-source(图片地址)、border-image-slice(图片切片)、border-image-width(图片宽度)、border-image-outset(图片边距)、border-image-repeat(图片重复方式)和四个缩写属性。

二、border-image源

border-image-source属性用于定义边框图片的来源,支持所有的URL值、图片资源和线性渐变。

    .border-image-source {
      border-image-source: url(border.png);
    }

或者使用线性渐变作为边框图片源:

    .border-image-source {
      border-image-source: linear-gradient(to bottom right, #4CAF50, #FF5722);
    }

三、图片切片

border-image-slice属性定义将边框图片分为九个区域以及如何调整这些区域的大小。语法如下:

    .border-image-slice {
      border-image-slice: number|[number]%;
    }

可以使用px为单位设置切片大小,同时可以通过设置百分比来调整边框图像大小:

    .border-image-slice {
      border-image-slice: 30;
      border-image-slice: 30% 20% 10% 5%;
    }

四、图片宽度

border-image-width属性定义边框图片的宽度,也可以设置百分比来调整大小并应用到每个切片。语法如下:

    .border-image-width {
      border-image-width: number|[number]%;
    }

可以分别给每个方向设置边框图片宽度:

    .border-image-width {
      border-image-width: 30;
      border-image-width: 30% 20% 10% 5%;
      /*top, right, bottom, left*/
      border-image-width: 30% 20% 10%;
      /*top, right and left, bottom*/
      border-image-width: 30% 20%;
      /*top and bottom, right and left*/
    }

五、边距

border-image-outset属性定义边框图片的超出边框线的距离,使用负值会将边框图片缩小以适应边框大小。

    .border-image-outset {
      border-image-outset: number|[number]%;
    }

不过多重叠的仍然会保持原来的形状,推荐给不同的边使用不同的值以避免这种情况。

六、重复方式

border-image-repeat属性定义如何重复边框图像。语法如下:

    .border-image-repeat {
      border-image-repeat: stretch|repeat|round;
    }

默认情况下,边框图像是在边框区域内重复,使用“stretch”值会拉伸图像以适应边缘区域。而“repeat”将按图片大小重复。

"round"会拉伸图像以适合边缘区域,并尝试在重复之前进行四舍五入缩小,以使重复之后的宽度恰好适合边缘区域。

七、CSS border-image缩写属性

除了以上的独立属性外,CSS 还提供了一种缩写属性,可以将所有单独属性合在一起。

    .border-image {
      border-image: url(border.png) 30 round;
    }

可以通过缩写属性的方式一次设置所有的属性值。

八、CSS border-radius对圆角的设置

CSS border-radius属性是用于设置元素的圆角的属性。使用border-radius属性与border-image属性可以轻松的达到各种符合设计要求的效果。

使用border-radius 设置圆角:

    .border-radius {
      border-radius: 10px;
    }

也可以多边形圆角效果:

    .border-radius {
      border-top-left-radius: 20px;
      border-top-right-radius: 10px;
      border-bottom-left-radius: 30px;
      border-bottom-right-radius: 40px;
    }

九、小结

CSS border-image是一种非常实用的属性,使用它可以轻松的设置元素的边框图片、边框图案和切片等效果。除了border-image属性,border-radius属性也是一个非常实用的元素设置上圆角效果的属性。

详解CSS border-image属性

2023-05-20
CSS Border 属性详解

2023-05-12
CSS Image Border

2023-05-12
CSS Image Border

2023-05-12
CSS border color属性详解

2023-05-12
CSS Border Image

2023-05-12
CSS Border Image: 创造独特的边框设计

2023-05-12
CSS Border Bottom常用属性详解

2023-05-19
深入探究border-image属性

2023-05-21
深度解析border-image

2023-05-23
CSS border-spacing 属性

2023-05-12
CSS属性列表及用法详解

2023-05-12
CSS Border Images

2023-05-12
HTML Border属性详解

2023-05-19
CSS border-color属性

2023-05-12
CSS文档:完整的CSS属性参考手册

2023-05-12
CSS Border Images

2023-05-12
提升网页外边框美观度: 轻松使用border-image属性

2023-05-17
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)