Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

สร้างเครื่องหมายถูก (Checkmark/Tick) ด้วย CSS


เราสามารถสร้างเครื่องหมายถูกแบบกำหนดเองได้ด้วย CSS ตัวอย่างต่อไปนี้แสดงเทคนิคต่างๆ

ตัวอย่างที่ 1: ใช้ pseudo-elements ::before และ ::after

<!DOCTYPE html> <html> <style> div {    margin: 2%;    position: relative;    width: 40px;    height: 40px;    box-shadow: inset 0 0 12px lightblue; } div::before {    content: "";    position: absolute;    width: 8px;    top: 50%;    height: 50%;    border-radius: 2px;    background-color: rgb(123,45,20);    transform: translateX(12px) rotate(-45deg);    transform-origin: left bottom;    z-index: +1; } div::after {    content: "";    position: absolute;    bottom: 0;    height: 8px;    width: 100%;    border-radius: 2px;    background-color: rgb(200,52,120);    transform: translateX(12px) rotate(-45deg);    transform-origin: left bottom; } </style> <body> Custom mark! <div></div> </body> </html>

ตัวอย่างที่ 2: ใช้ border และ transform (วิธีง่ายกว่า)

<!DOCTYPE html> <html> <style> body {    padding: 2%;    text-align: center; } div {    display: inline-block;    height: 100px;    width: 35px;    border-bottom: 10px solid chartreuse;    border-right: 10px solid chartreuse;    transform: rotate(45deg); } </style> <body> <div></div> </body> </html>

วิธีที่ 2 นิยมใช้มากกว่าเพราะโค้ดสั้น กำหนดได้ง่าย และปรับขนาดสีได้โดยเปลี่ยนค่า border เท่านั้น