代码之家  ›  专栏  ›  技术社区  ›  rahulkesharwani

使用CSS合并节标记的边框颜色

  •  8
  • rahulkesharwani  · 技术社区  · 7 年前

    我创建了一个中心略微旋转的剖面边框。此外,我给每一面的部分不同的颜色。下面是我的基本代码:

    body{
        background-color: #594451;
        color: #fff;
    }
    
    .boxed{
        position: absolute;
        margin:auto;
        /* border: 3pc solid rgba(255, 255, 255 ,1); */
        border-left: solid rgba(127, 203, 170, 1) 3pc;
        border-right: solid rgba(186, 179, 173, 1) 3pc;
        border-bottom: solid rgba(0, 171, 238, 1) 3pc;
        border-top: solid rgba(229, 99, 57, 1) 3pc;
        height: 20pc;
        width: 20pc;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -o-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
        
    }
    <h1>Welcome to Rahul's Lab</h1>
        <section class="boxed">
            <!-- <p>This will contain the box properly</p> -->
        </section>

    我能实现彼此的边界重叠吗?是否可以只使用CSS,或者我必须为此创建单独的section/div? HTML & CSS Design and build websites

    参考:HTML&由Jon Duckett设计和构建网站。

    4 回复  |  直到 7 年前
        1
  •  5
  •   Takit Isy Surendra    7 年前

    边界重叠是不可能的

    但是,这里有一个只使用CSS创建效果的解决方案,使用:

    • 倍数 linear-gradient 让我们来创建 background
    • background-blend-mode 混合角落里的颜色

    ⋠⋠â‹

    更新的代码片段:
    使用背景速记语法,就像Temani在这里做的那样 https://stackoverflow.com/a/50526694/5061000

    .boxed {
      position: absolute;
      margin: 5pc auto; /* Modified to better fit in tiny snippet */
      --border-pc: 10%; /* You could even use a CSS variable to store the width if you want to be able to modify it easily. Or the colors. Or both. */
      background:
        /* Shorthand syntax used below: image position/sizeX sizeY */
        linear-gradient(rgba(229, 099, 057, 1), rgba(229, 099, 057, 1)) top   /100% var(--border-pc), /* Orange with CSS var */
        linear-gradient(rgba(000, 171, 238, 1), rgba(000, 171, 238, 1)) bottom/100% var(--border-pc), /* Blue */
        linear-gradient(rgba(127, 203, 170, 1), rgba(127, 203, 170, 1)) left  /var(--border-pc) 100%, /* Green */
        linear-gradient(rgba(186, 179, 173, 1), rgba(186, 179, 173, 1)) right /var(--border-pc) 100%; /* Gray */
      background-blend-mode: multiply;
      background-repeat: no-repeat;
      height: 20pc;
      width: 20pc;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      transform: rotate(45deg);
    }
    <section class="boxed">
      <!-- <p>This will contain the box properly</p> -->
    </section>

    ⋠⋠â‹

    旧式片段:
    是的,它起作用了,但我更喜欢第一个!

    .boxed {
      position: absolute;
      margin: 5pc auto; /* Modified to better fit in tiny snippet */
      --border-pc: 10%; /* You could even use a CSS variable to store the width if you want to be able to modify it easily. Or the colors. Or both. */
      /* I tried to indent it to better see the code: */
      background-image:
        linear-gradient(to top, /* Blue */
          rgba(0, 171, 238, 1) 0%,
          rgba(0, 171, 238, 1) var(--border-pc), /* example use of the CSS var */
          transparent var(--border-pc) ),
        linear-gradient(to right, /* Green */
          rgba(127, 203, 170, 1) 0%,
          rgba(127, 203, 170, 1) 10%,
          transparent 10% ),
        linear-gradient(to bottom, /* Orange */
          rgba(229, 99, 57, 1) 0%,
          rgba(229, 99, 57, 1) 10%, 
          transparent 10% ),
        linear-gradient(to left, /* Gray */
          rgba(186, 179, 173, 1) 0%,
          rgba(186, 179, 173, 1) 10%,
          transparent 10% );
      background-blend-mode: multiply; /* Added to mix the colors */
      height: 20pc;
      width: 20pc;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      transform: rotate(45deg);
    }
    <节class=“装箱”>
    <!——&书信电报;p>这将正确地容纳盒子</p>-->
    </第节>

    (请注意,如果不使用 背景混合模式 你可以通过按顺序选择哪些渐变与其他渐变重叠!)

    希望有帮助。

        2
  •  3
  •   Maharkus zok    7 年前

    实际上不可能重叠边界,因为它们在div中总是以特定角度相交。不过,仍然可以通过制作单独的块来创建效果。财产 mix-blend-mode 允许图像提供的叠加效果。

    记住 混合模式 不支持 IE/Edge .

    以下是如何做到这一点:

    body {
      margin: 30px
    }
    
    .block1 {
      background: rgba(127, 203, 170, 1);
    }
    
    .block2 {
      background: rgba(0, 171, 238, 1);
      transform: rotate(90deg);
      transform-origin: 20px 0;
      mix-blend-mode: color-burn;
    }
    
    .block3 {
      background: rgba(186, 179, 173, 1);
      transform: translateY(140px);
      mix-blend-mode: color-burn;
    }
    
    .block4 {
      background: rgba(229, 99, 57, 1);
      transform: translateY(30px) translateX(90px) rotate(90deg);
      mix-blend-mode: darken;
    }
    
    .block {
      height: 20px;
      width: 200px;
    }
    <div class="block1 block"></div>
    <div class="block2 block"></div>
    <div class="block3 block"></div>
    <div class="block4 block"></div>
        3
  •  2
  •   Temani Afif    7 年前

    这是另一个梯度解,不需要 mix-blend-mode 。其想法是使用渐变制作每个角,以便轻松控制颜色并模拟重叠:

    .box {
      margin:50px;
      height:200px;
      width:200px;
      box-sizing:border-box;
      
      background:
        /*The 4 corners*/
        linear-gradient(#905c3f,#905c3f) top left/20px 20px,
        linear-gradient(#b04d32,#b04d32) top right/20px 20px,
        linear-gradient(#02a9ae,#02a9ae) bottom left/20px 20px,
        linear-gradient(#0085aa,#0085aa) bottom right/20px 20px,
        
        /*The 4 borders*/
        linear-gradient(#e6663a,#e6663a) top/100% 20px,
        linear-gradient(#01adf0,#01adf0) bottom/100% 20px,
        linear-gradient(#7fcaac,#7fcaac) left/20px 100%,
        linear-gradient(#bab1ae,#bab1ae) right/20px 100%;  
       background-repeat:no-repeat;
       
       transform:rotate(45deg);
    }
    <div class="box">
    
    </div>
        4
  •  0
  •   Serge Inácio    7 年前

    我找到的唯一方法是以下方法,可能有更好的选择:

    body{
        background-color: #594451;
        color: #fff;
    }
    
    .boxed1, .boxed2, .boxed3, .boxed4 {
        position: absolute;
        margin:auto;
        /* border: 3pc solid rgba(255, 255, 255 ,1); */
        box-sizing: border-box;
        height: 20pc;
        width: 20pc;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -o-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
        
    }
    
    .boxed1 {
        border-top: solid rgba(229, 99, 57, 1) 3pc;
    }
    
    .boxed2 {
        border-right: solid rgba(186, 179, 173, 1) 3pc;
    }
    
    .boxed3 {
        border-bottom: solid rgba(0, 171, 238, 1) 3pc;
    }
    
    .boxed4 {
        border-left: solid rgba(127, 203, 170, 1) 3pc;
    }
    <h1>Welcome to Rahul's Lab</h1>
        <section class="boxed1">
            <!-- <p>This will contain the box properly</p> -->
        </section>
        <section class="boxed2">
            <!-- <p>This will contain the box properly</p> -->
        </section>
        <section class="boxed3">
            <!-- <p>This will contain the box properly</p> -->
        </section>
        <section class="boxed4">
            <!-- <p>This will contain the box properly</p> -->
        </section>

    希望这有帮助。