【问题标题】:How do I add a Google One style multi-color border to a circular image?如何将 Google One 样式的多色边框添加到圆形图像?
【发布时间】:2019-11-12 07:48:09
【问题描述】:

我正在为我所在大学的 Google DSC 俱乐部创建一个网站,我想在网站上的圆形作品集图片中添加 Google One style border

我想知道如何使用 CSS 来实现这一点,但预渲染的 Photoshop 或 Illustrator 技巧也可以。

我尝试修改this answer中给出的一些代码,但无法达到完美的效果。

.test {
  margin: 25px 0;
  width: 200px;
  height: 200px;
  border-radius: 50%;
  border: 12px solid transparent;
  background-size: 100% 100%, 50% 50%, 50% 50%, 50% 50%, 50% 50%;
  background-repeat: no-repeat;
  background-image: linear-gradient(white, white), 
                    linear-gradient(30deg, #ea4335 36%, #4285f4 30%),
                    linear-gradient(120deg, #4285f4 36%, #34a853 30%),
                    linear-gradient(300deg, #fbbc04 36%, #ea4335 30%),
                    linear-gradient(210deg, #34a853 36%, #fbbc04 30%);
  background-position: center center, left top, right top, left bottom, right bottom;
  background-origin: content-box, border-box, border-box, border-box, border-box;
  background-clip: content-box, border-box, border-box, border-box, border-box;
  transform: rotate(90deg);
}
<div class="test"></div>

如您所见,边框颜色未正确对齐。

有什么方法可以使用更简单的方法来完成此任务吗?

谢谢!

【问题讨论】:

  • 你的意思是他们没有正确对齐?你能显示一张图片吗?从sn-p看起来不错
  • 嘿 Waldir,我的意思是我在帖子中链接的原始 Google One 边框没有正确对齐。边框的颜色与 Google 'G' 徽标中的颜色一致。红色和蓝色大,绿色中等,黄色最小。但每当我尝试改变颜色的角度或长度时,它就会变得一团糟。我真的不想这么具体,但现在我已经向团队提出了建议,他们想要这样。
  • 是的,就是这样。非常感谢!

标签: css svg photoshop adobe-illustrator


【解决方案1】:

带有面具的小svg解决方案

<svg viewbox="-5 -5 10 10" height="90vh">
  <defs>
    <mask id="mask1">
      <circle r="4.5" stroke="white" fill="black" stroke-width="0.5"></circle> 
    </mask>
    <mask id="mask2">
      <circle r="4"fill="white"></circle> 
    </mask>
  </defs>
  <g mask="url(#mask1)" transform="rotate(-45)">
    <rect width="7" height="7" fill="#fabb04" x="-7" y="-6"></rect>
    <rect width="7" height="7" fill="#34a852" x="-5" y="-1"></rect>
    <rect width="7" height="7" fill="#ea4335" x="-2" y="-5"></rect>
    <rect width="7" height="7" fill="#4285f4" x="0"  y="-1"></rect>
  </g>
  <image x="-4" y="-4" width="8" height="8" mask="url(#mask2)"
    xlink:href="https://i.stack.imgur.com/3q67w.gif"></image>
</svg>

【讨论】:

    【解决方案2】:

    SVG 选项

    要创建圆形的多色扇区,请使用stroke-dasharray

    行和空格的长度计算:

    对于半径R = 100px圆周=2 * 3.1415 * 100 = 628.3 px

    为了得到等于圆四分之一的直线,我们计算属性stroke-dasharray

    628.3 / 4 = 157.075 空间将是圆长度的 3/4 = 471px

    但由于蓝色、红色和绿色扇区的长度略大于四分之一圆,我们添加了这个差异。 stroke-dasharray = "183.255 445.045"

    stroke-dashoffset = "78.54" 将扇区的起点移动圆周的 1/8

    一个部门的例子

    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"
    	     width="250" height="250" viewBox="0 0 250 250" > 
    		 
    <circle cx="125" cy="125" r="100" fill="none" stroke="#d3d3d3" stroke-width="8" />		 
     		 
    <circle id="blue" cx="125" cy="125" r="100" fill="none" stroke="#4285F4" stroke-width="8" stroke-dasharray="183.255 445.045" stroke-dashoffset="78.54" /> 
     
     </svg>	 

    我们还为其他颜色扇区设置属性。

    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"
    	     width="250" height="250" viewBox="0 0 250 250" >  
    
    		 
    <circle id="blue" cx="125" cy="125" r="100" fill="none" stroke="#4285F4" stroke-width="8" stroke-dasharray="183.255 445.045" stroke-dashoffset="78.54" /> 
    
      <circle id="red" cx="125" cy="125" r="100" fill="none" stroke="#EA4335" stroke-width="8" stroke-dasharray="203.255 425.045" stroke-dashoffset="281.9" /> 
    
      <circle id="gold" cx="125" cy="125" r="100" fill="none" stroke="#FABB04" stroke-width="8" stroke-dasharray="78.54 549.76" stroke-dashoffset="340	" /> 
     
       <circle id="green" cx="125" cy="125" r="100" fill="none" stroke="#34A852" stroke-width="8" stroke-dasharray="183.255 445.045" stroke-dashoffset="525.225	" /> 
     
     </svg>	 

    添加图像并使用蒙版将其剪切以适合圆圈

    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"
    	     width="40%" height="40%" viewBox="0 0 250 250" >  
    
     <defs> 
      <mask id="msk1"> 
        <rect width="100%" height="100%" fill="black" />
       <circle cx="125" cy="125" r="100" fill="white" stroke-width="20" stroke="black"  />
      </mask>
    </defs>
    
     <image xlink:href="https://i.stack.imgur.com/UsGg5.jpg" x="0" y="23" width="100%" height="100%" mask="url(#msk1)" />	
    		 
    <circle id="blue" cx="125" cy="125" r="100" fill="none" stroke="#4285F4" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="78.54" /> 
    
      <circle id="red" cx="125" cy="125" r="100" fill="none" stroke="#EA4335" stroke-width="8" stroke-dasharray="203.3 425.1" stroke-dashoffset="281.9" /> 
    
      <circle id="gold" cx="125" cy="125" r="100" fill="none" stroke="#FABB04" stroke-width="8" stroke-dasharray="80 549.7" stroke-dashoffset="345	" /> 
     
       <circle id="green" cx="125" cy="125" r="100" fill="none" stroke="#34A852" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="525.2	" /> 
     
     </svg>	 

    该解决方案是自适应的,并且在所有现代浏览器中都可以正常工作,包括IE11Edge

    更新

    OP 没有问,但也许这个补充对他或其他人有用。

    SVG+CSS 动画

    为了给您的应用程序增添趣味,我添加了动画选项。

    #1。笔画围绕图像旋转的动画

    • 用组标签包裹所有形成多色线的圆圈 &lt;g&gt;

    • fill ="none" 替换为 fill ="transparent" 当您将鼠标悬停在整个圆圈上时动画会起作用

    .student {
    background: rgb(238,174,202);
    background: linear-gradient(90deg, rgba(238,174,202,1) 0%, rgba(148,208,233,1) 100%);
    }
    .container {
    display: inline-block;
    width:25%;
    }
    #gr1 {
    transform-origin:125px 125px;
      -webkit-transition: -webkit-transform 1s ease-in-out;
              transition:         transform 1s ease-in-out;
    }
    
    #gr1:hover {
      -webkit-transform: rotate(720deg);
              transform: rotate(720deg);
    }
    <div class="student"> 
     <div class="container">
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"
    	      viewBox="0 0 250 250" >  
    
     <defs> 
      <mask id="msk1"> 
        <rect width="100%" height="100%" fill="black" />
       <circle cx="125" cy="125" r="100" fill="white" stroke-width="20" stroke="black"  />
      </mask>
    </defs>
    
     <image id="img" xlink:href="https://i.stack.imgur.com/IzNqO.jpg" x="0" y="24" width="100%" height="100%" mask="url(#msk1)" />	
    
    <g id="gr1"> 
    <circle id="blue" cx="125" cy="125" r="100" fill="transparent" stroke="#4285F4" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="78.54" /> 
    
      <circle id="red" cx="125" cy="125" r="100" fill="none" stroke="#EA4335" stroke-width="8" stroke-dasharray="203.3 425.1" stroke-dashoffset="281.9" /> 
    
      <circle id="gold" cx="125" cy="125" r="100" fill="none" stroke="#FABB04" stroke-width="8" stroke-dasharray="80 549.7" stroke-dashoffset="345	" /> 
     
       <circle id="green" cx="125" cy="125" r="100" fill="none" stroke="#34A852" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="525.2	" /> 
     </g>
     </svg>	
    </div>  
     
    <div class="container">
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"
    	      viewBox="0 0 250 250" >  
    
     <defs> 
      <mask id="msk1"> 
        <rect width="100%" height="100%" fill="black" />
       <circle cx="125" cy="125" r="100" fill="white" stroke-width="20" stroke="black"  />
      </mask>
    </defs>
    
     <image id="img" xlink:href="https://i.stack.imgur.com/d4AlZ.jpg" x="0" y="25" width="100%" height="100%" mask="url(#msk1)" />	
    <g id="gr1">		 
    <circle id="blue" cx="125" cy="125" r="100" fill="transparent" stroke="#4285F4" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="78.54" /> 
    
      <circle id="red" cx="125" cy="125" r="100" fill="none" stroke="#EA4335" stroke-width="8" stroke-dasharray="203.3 425.1" stroke-dashoffset="281.9" /> 
    
      <circle id="gold" cx="125" cy="125" r="100" fill="none" stroke="#FABB04" stroke-width="8" stroke-dasharray="80 549.7" stroke-dashoffset="345	" /> 
     
       <circle id="green" cx="125" cy="125" r="100" fill="none" stroke="#34A852" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="525.2	" /> 
    </g> 
     </svg>	 
     </div> 
     <div class="container">
     <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"
    	      viewBox="0 0 250 250" >  
    
     <defs> 
      <mask id="msk1"> 
        <rect width="100%" height="100%" fill="black" />
       <circle cx="125" cy="125" r="100" fill="white" stroke-width="20" stroke="black"  />
      </mask>
    </defs>
    
     <image id="img" xlink:href="https://i.stack.imgur.com/YG6VN.png" x="0" y="25" width="100%" height="100%" mask="url(#msk1)" />	
    <g id="gr1">	 
    <circle id="blue" cx="125" cy="125" r="100" fill="transparent" stroke="#4285F4" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="78.54" /> 
    
      <circle id="red" cx="125" cy="125" r="100" fill="none" stroke="#EA4335" stroke-width="8" stroke-dasharray="203.3 425.1" stroke-dashoffset="281.9" /> 
    
      <circle id="gold" cx="125" cy="125" r="100" fill="none" stroke="#FABB04" stroke-width="8" stroke-dasharray="80 549.7" stroke-dashoffset="345	" /> 
     
       <circle id="green" cx="125" cy="125" r="100" fill="none" stroke="#34A852" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="525.2	" /> 
    </g> 
       </svg>	 
      </div> 
     </div>

    2.#悬停时图片旋转动画

    CSS规则用于实现图片的旋转

    #img {
    transform-origin:125px 125px;
      -webkit-transition: -webkit-transform 1s ease-in-out;
              transition:         transform 1s ease-in-out;
    }
    
    #img:hover {
      -webkit-transform: rotate(360deg);
              transform: rotate(360deg);
    }
    
    

    .student {
    
    background: rgb(238,174,202);
    background: linear-gradient(90deg, rgba(238,174,202,1) 0%, rgba(148,208,233,1) 100%);
    }
    .container {
    display: inline-block;
    width:25%;
    }
    #img {
    transform-origin:125px 125px;
      -webkit-transition: -webkit-transform 1s ease-in-out;
              transition:         transform 1s ease-in-out;
    }
    
    #img:hover {
      -webkit-transform: rotate(360deg);
              transform: rotate(360deg);
    }
    <div class="student"> 
     <div class="container">
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"
    	      viewBox="0 0 250 250" >  
    
     <defs> 
      <mask id="msk1"> 
        <rect width="100%" height="100%" fill="black" />
       <circle cx="125" cy="125" r="100" fill="white" stroke-width="20" stroke="black"  />
      </mask>
    </defs>
    
     <image id="img" xlink:href="https://i.stack.imgur.com/IzNqO.jpg" x="0" y="24" width="100%" height="100%" mask="url(#msk1)" />	
    		 
    <circle id="blue" cx="125" cy="125" r="100" fill="none" stroke="#4285F4" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="78.54" /> 
    
      <circle id="red" cx="125" cy="125" r="100" fill="none" stroke="#EA4335" stroke-width="8" stroke-dasharray="203.3 425.1" stroke-dashoffset="281.9" /> 
    
      <circle id="gold" cx="125" cy="125" r="100" fill="none" stroke="#FABB04" stroke-width="8" stroke-dasharray="80 549.7" stroke-dashoffset="345	" /> 
     
       <circle id="green" cx="125" cy="125" r="100" fill="none" stroke="#34A852" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="525.2	" /> 
     
     </svg>	
    </div>  
     
    <div class="container">
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"
    	      viewBox="0 0 250 250" >  
    
     <defs> 
      <mask id="msk1"> 
        <rect width="100%" height="100%" fill="black" />
       <circle cx="125" cy="125" r="100" fill="white" stroke-width="20" stroke="black"  />
      </mask>
    </defs>
    
     <image id="img" xlink:href="https://i.stack.imgur.com/d4AlZ.jpg" x="0" y="25" width="100%" height="100%" mask="url(#msk1)" />	
    		 
    <circle id="blue" cx="125" cy="125" r="100" fill="none" stroke="#4285F4" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="78.54" /> 
    
      <circle id="red" cx="125" cy="125" r="100" fill="none" stroke="#EA4335" stroke-width="8" stroke-dasharray="203.3 425.1" stroke-dashoffset="281.9" /> 
    
      <circle id="gold" cx="125" cy="125" r="100" fill="none" stroke="#FABB04" stroke-width="8" stroke-dasharray="80 549.7" stroke-dashoffset="345	" /> 
     
       <circle id="green" cx="125" cy="125" r="100" fill="none" stroke="#34A852" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="525.2	" /> 
     
     </svg>	 
     </div> 
     <div class="container">
     <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"
    	      viewBox="0 0 250 250" >  
    
     <defs> 
      <mask id="msk1"> 
        <rect width="100%" height="100%" fill="black" />
       <circle cx="125" cy="125" r="100" fill="white" stroke-width="20" stroke="black"  />
      </mask>
    </defs>
    
     <image id="img" xlink:href="https://i.stack.imgur.com/YG6VN.png" x="0" y="25" width="100%" height="100%" mask="url(#msk1)" />	
    		 
    <circle id="blue" cx="125" cy="125" r="100" fill="none" stroke="#4285F4" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="78.54" /> 
    
      <circle id="red" cx="125" cy="125" r="100" fill="none" stroke="#EA4335" stroke-width="8" stroke-dasharray="203.3 425.1" stroke-dashoffset="281.9" /> 
    
      <circle id="gold" cx="125" cy="125" r="100" fill="none" stroke="#FABB04" stroke-width="8" stroke-dasharray="80 549.7" stroke-dashoffset="345	" /> 
     
       <circle id="green" cx="125" cy="125" r="100" fill="none" stroke="#34A852" stroke-width="8" stroke-dasharray="183.2 445.1" stroke-dashoffset="525.2	" /> 
     
       </svg>	 
      </div> 
     </div>

    【讨论】:

    • 非常感谢!这是完美的!
    • @Varun Gupta 感谢您提出一个有趣的问题!
    • @Alexandr_TT 你能检查一下这个问题吗stackoverflow.com/q/66025363/9004424
    • @Husna 我看了你的竞争性问题。你想让我试着回答你的问题吗?
    • @Alexandr_TT 是的,我想让你试试。因为我几天前发布了我的问题,但我没有得到任何答案。
    猜你喜欢
    • 1970-01-01
    • 2015-01-29
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2012-03-21
    相关资源
    最近更新 更多