简体   繁体   English

使 div 边框的一部分透明 html

[英]make part of div border transparent html

Can I make part (from x1 to x2) of div border transparent?我可以使 div 边框的一部分(从 x1 到 x2)透明吗?

If not what approach can you advice?如果不是,您可以建议什么方法?

My idea [very bad] is to draw border in canvas element and place it (canvas body is trasparent) over div element.我的想法 [非常糟糕] 是在画布元素中绘制边框并将其放置在 div 元素上(画布主体是透明的)。

在此处输入图片说明

Since DIVs have only 4 elements (top, bottom, left, right) you can't make part of a border transparent AFAIK.由于 DIV 只有 4 个元素(顶部、底部、左侧、右侧),因此您无法将边框的一部分设为透明 AFAIK。

However, you could create elements that would overlay your div and use relative positioning to build a border to your taste.但是,您可以创建覆盖 div 的元素并使用相对定位来构建符合您口味的边框。 For example:例如:

<style>
 .multiborder{
        border:1px solid black;
        border-top:none;
        width:500px;
        height:100px;
        position:relative;
    }
    .top-border-1{
        border-top:2px solid red;
        width:100px;
        position:absolute;
        top:0px;
        right:0px;
    }
    .top-border-2{
        border-top:3px double blue;
        width:300px;
        position:absolute;
        top:0px;
        left:0px;
    }
</style>
<div class="multiborder">
    <div class="top-border-1"></div>
    <div class="top-border-2"></div>
</div>

You can see the result at http://jsfiddle.net/Bekqu/3/ .你可以在http://jsfiddle.net/Bekqu/3/看到结果。

Here are two possible ways to do this:这里有两种可能的方法来做到这一点:

Required HTML will remain the same in both methods and is as follows:两种方法中所需的HTML将保持不变,如下所示:

HTML: HTML:

<div class="box"></div>

Method #01:方法#01:

  1. Draw the top, right and left borders with border css property.使用border css 属性绘制顶部、右侧和左侧边框。
  2. Draw the bottom transparent border with linear-gradient css property.使用linear-gradient css 属性绘制底部透明边框。

CSS: CSS:

.box {
  /* Following css will create bottom border */
  background: linear-gradient(to right, #000 30%, transparent 30%, transparent 70%, black 70%) no-repeat;
  background-size: 100% 8px;
  background-position: 0 100%;

  /* Following css will create top, left and right borders */
  border: solid #000;
  border-width: 8px 8px 0;

  width: 100px;
  height: 50px;
}

 html, body { height: 100%; } body { background: linear-gradient(to top, #ff5a00 0, #ffae00 100%); margin: 0; } .box { background: linear-gradient(to right, #000 30%, transparent 30%, transparent 70%, black 70%) no-repeat; background-size: 100% 8px; background-position: 0 100%; border: solid #000; border-width: 8px 8px 0; margin: 20px 15px; width: 100px; height: 50px; }
 <div class="box"></div>


Method #02:方法#02:

  1. Draw the top, right and left borders with border css property.使用border css 属性绘制顶部、右侧和左侧边框。
  2. Draw the bottom borders with :before and :after pseudo elements.使用:before:after伪元素绘制底部边框。

CSS: CSS:

.box {
  /* Following css will create top, left and right borders */
  border: solid black;
  border-width: 8px 8px 0;

  position: relative;
  overflow: hidden;
  width: 100px;
  height: 50px;
}

/* Following css will create bottom border */
.box:before,
.box:after {
  position: absolute;
  background: #000;
  content: '';
  height: 8px;
  width: 30%;
  bottom: 0;
  left: 0;
}

.box:after {
  left: auto;
  right: 0;
}

 html, body { height: 100%; } body { background: linear-gradient(to top, #ff5a00 0, #ffae00 100%); margin: 0; } .box { border: solid black; border-width: 8px 8px 0; position: relative; overflow: hidden; margin: 15px 10px; width: 100px; height: 50px; } .box:before, .box:after { position: absolute; background: #000; content: ''; height: 8px; width: 30%; bottom: 0; left: 0; } .box:after { left: auto; right: 0; }
 <div class="box"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM