CSS Shapes: สี่เหลี่ยม, วงกลม, รูปทรงอื่นๆ ด้วย Border-radius
รูปทรงพื้นฐานสร้างได้จาก width, height, background, border-radius รูปทรงซับซ้อนใช้ clip-path, transform, ::before/::after
โค้ดตัวอย่างพื้นฐาน
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
div { display: inline-block; margin: 20px; }
.square { width: 100px; height: 100px; background: rgb(23, 153, 121); border: 3px solid darkblue; }
.rectangle { width: 200px; height: 100px; background: rgb(255, 232, 21); border: 3px solid rgb(42, 0, 70); }
.circle { width: 100px; height: 100px; background: rgb(0, 255, 13); border: 3px solid rgb(27, 0, 78); border-radius: 50%; }
</style>
</head>
<body>
<h1>ตัวอย่างรูปทรง CSS</h1>
<div class="square"></div> <div class="rectangle"></div> <div class="circle"></div>
</body>
</html>
รูปทรงเพิ่มเติมด้วย Border-radius
- Oval:
width: 200px; height: 100px; border-radius: 50%; - Pill/Rounded bar:
border-radius: 50px;(ครึ่งความสูง) - Leaf:
border-radius: 0 100% 0 100%; - Triangle:
width: 0; height: 0; border: 50px solid transparent; border-bottom-color: red;
Clip-path: รูปทรงซับซ้อน
.triangle { clip-path: polygon(50% 0%, 0% 100%, 100% 100%); }
.hexagon { clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%); }
.star { clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%); }
เครื่องมือช่วย
- Clippy (CSS clip-path maker) – วาด clip-path แบบ GUI
- CSS Tricks Shapes – รวมรูปทรงพร้อมโค้ด