CSS 製作鏤空三角形的例子

來源:互聯網
上載者:User


@kejunz 的 <!DOCTYPE html>

 代碼如下 複製代碼

<html>
<head>

<meta charset="utf-8" />
<title>Untitled</title>
<style>
html, body {
  height: 100%;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #333;
  font-size: 24px;
}

.traingle {
  position: relative;
  width: 5px;
  height: 5em;
  background: #ffae62;
  border-radius: 2px;
}

.traingle:before {
  content: "";
  position: absolute;
  width: calc(5em - 5px);
  height: 5px;
  left: 2px;
  top: 0;
  background: #ffae62;
  border-radius: 2px;
  transform: rotate(30deg);
  transform-origin: 0 0;
}

.traingle:after {
  content: "";
  position: absolute;
  width: calc(5em - 5px);
  height: 5px;
  bottom: 0;
  left: 2px;
  border-radius: 2px;
  background: #ffae62;
  transform: rotate(-30deg);
  transform-origin: 0 0;
}</style>
<script src="http://dabblet.com/code/prefixfree.min.js"></script>
<script>
if (parent === window) {
 document.addEventListener('DOMContentLoaded', function() {
  // alert('Hello world!'); });
}
</script>
</head>
<body><div class="traingle" /></body>
</html>

@小水坑裡的小魚 的 DEMO

 代碼如下 複製代碼
     body {
  background: #000;
}
#line,
#line:before,
#line:after {
  position: absolute;
  width: 10px;
  height: 300px;
  background: #fff;
  border-radius: 5px;
}
#line:before,
#line:after {
  content: '';
}
#line:before {
  -webkit-transform-origin: top center;
  -webkit-transform: rotate(-60deg) translate(-3px);
}
#line:after {
  -webkit-transform-origin: bottom center;
  -webkit-transform: rotate(60deg) translate(-3px);
}
           

       


@kejunz 和 @小水坑裡的小魚 兩位大大的實現方式差不多,都是利用虛擬元素通過絕對位置和旋轉來實現。


@點頭豬 的 DEMO,妹子的方法和前面兩位大大有一些區別,只了一個虛擬元素來絕對位置,然後通過倒影重疊來形成三角形。

 代碼如下 複製代碼

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta id="viewport" name="viewport" content="target-densitydpi=medium-dpi, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>Demo</title>
<style>
html, body, div, span, h1, h2, h3, h4, h5, h6, p, blockquote, pre, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, footer, header, section, summary, time, mark { margin:0; padding:0; }
ul, li { list-style:none; }
body { width: 320px; font-size:16px; font-family:helvetica; -webkit-user-select:none; -webkit-text-size-adjust:none; }
.wrap { padding: 10px; }
.tri, .tri:after { width: 20px; background: #000; }
.tri { position: relative; height: 100px; border-radius: 10px 10px 0 0; -webkit-box-reflect: below -9px; }
.tri:after { position: absolute; content: ""; height: 200px; border-radius: 10px; -webkit-transform: translate(-9px) rotate(-60deg); -webkit-transform-origin: top center; }
*/</style>
</head>
<body>
 <div class="wrap">
  <div class="tri"></div>
 </div>
</body>
</html>

@一絲yisi 給出的 DEMO 也是兩個虛擬元素,通過絕對位置和旋轉來實現。不過增加了動畫,讓我們更加清楚的瞭解製作三角形的過程。(本人為了減少頁面的代碼量,把其他瀏覽器的首碼都給去掉了,只支援webkit核心瀏覽器。)

 代碼如下 複製代碼

.demo{cursor:pointer;width:100px;}
.corner-rounded,.corner-rounded:after,.corner-rounded:before{width:12px;height:100px;border-radius:999px;background-color:#FFAC5F}
.corner-rounded{position:relative}
.corner-rounded:after,.corner-rounded:before{content:"";position:absolute;top:0;left:0;-webkit-transition:all 1.2s ease-in-out;-webkit-transform-origin:50% 6px}
.corner-rounded:after{-webkit-transform:rotate(-60deg)}
.corner-rounded:before{-webkit-transform-origin:50% -webkit-calc(100% - 6px);-webkit-transform:rotate(60deg)}
.demo:hover .corner-rounded:after{background-color:#88c;-webkit-transform:none}
.demo:hover .corner-rounded:before{background-color:#66E070;-webkit-transform:none}


<div class="demo" ><div class="corner-rounded" ></div></div>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.