效果图:
    效果图.png
    代码:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>CSS绘制各种特殊图形</title>
    8. <style type="text/css">
    9. * { margin: 0; padding: 0; }
    10. :root {
    11. --primaryText: #333;
    12. --primaryColor: #1890ff;
    13. --successColor: #52c41a;
    14. --warningColor: #faad14;
    15. --errorColor: #f5222d;
    16. }
    17. html {
    18. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB',
    19. 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif, 'Apple Color Emoji',
    20. 'Segoe UI Emoji', 'Segoe UI Symbol';;
    21. }
    22. ul { list-style: none; }
    23. .clearfix::after {
    24. content: '';
    25. display: table;
    26. clear: both;
    27. }
    28. .container {
    29. display: flex;
    30. flex-wrap: wrap;
    31. width: 1080px;
    32. margin: 100px auto;
    33. padding: 10px;
    34. box-shadow: 0 0 8px var(--primaryText);
    35. }
    36. .section {
    37. display: flex;
    38. flex-direction: column;
    39. align-items: center;
    40. justify-content: center;
    41. width: 180px;
    42. height: 180px;
    43. margin: 10px;
    44. border: 1px solid var(--primaryText);
    45. box-sizing: border-box;
    46. }
    47. .section .title {
    48. height: 32px;
    49. font-size: 18px;
    50. font-weight: 700;
    51. line-height: 32px;
    52. text-align: center;
    53. color: var(--primaryText);
    54. }
    55. .wrapper {
    56. flex: 1;
    57. display: flex;
    58. justify-content: center;
    59. align-items: center;
    60. }
    61. /* 三角形 */
    62. .triangle {
    63. width: 0;
    64. height: 0;
    65. border-top: 20px solid var(--primaryColor);
    66. border-left: 20px solid transparent;
    67. border-right: 20px solid transparent;
    68. }
    69. /* 弯尾箭头 */
    70. .curved-arrow {
    71. position: relative;
    72. width: 0;
    73. height: 0;
    74. border-top: 20px solid transparent;
    75. border-left: 20px solid var(--successColor);
    76. transform: rotate(45deg);
    77. }
    78. .curved-arrow::after {
    79. content: '';
    80. position: absolute;
    81. left: -10px;
    82. top: -25px;
    83. width: 30px;
    84. height: 12px;
    85. border-top: 6px solid var(--successColor);
    86. border-radius: 0 30px 0 0;
    87. transform: rotate(-45deg);
    88. }
    89. /* 梯形 */
    90. .trapezoid {
    91. position: relative;
    92. width: 60px;
    93. height: 40px;
    94. background-color: var(--warningColor);
    95. }
    96. .trapezoid::before {
    97. content: '';
    98. position: absolute;
    99. left: -10px;
    100. border-left: 10px solid transparent;
    101. border-bottom: 40px solid var(--warningColor);
    102. }
    103. .trapezoid::after {
    104. content: '';
    105. position: absolute;
    106. right: -10px;
    107. border-right: 10px solid transparent;
    108. border-bottom: 40px solid var(--warningColor);
    109. }
    110. /* 平行四边形 */
    111. .parallelogram {
    112. width: 60px;
    113. height: 40px;
    114. background-color: var(--errorColor);
    115. transform: skewX(25deg);
    116. }
    117. /* 六角星 */
    118. .hexagram {
    119. position: relative;
    120. width: 0;
    121. height: 0;
    122. border-top: 60px solid var(--errorColor);
    123. border-left: 30px solid transparent;
    124. border-right: 30px solid transparent;
    125. }
    126. .hexagram::after {
    127. content: '';
    128. position: absolute;
    129. left: -30px;
    130. top: -80px;
    131. width: 0;
    132. height: 0;
    133. border-bottom: 60px solid var(--errorColor);
    134. border-left: 30px solid transparent;
    135. border-right: 30px solid transparent;
    136. }
    137. /* 五角星 */
    138. .pertagram {
    139. position: relative;
    140. width: 0;
    141. height: 0;
    142. border-top: 45px solid var(--errorColor);
    143. border-left: 62px solid transparent;
    144. border-right: 62px solid transparent;
    145. }
    146. .pertagram::before {
    147. content: '';
    148. position: absolute;
    149. left: -20px;
    150. top: -85px;
    151. border-top: 62px solid transparent;
    152. border-bottom: 62px solid transparent;
    153. border-left: 45px solid var(--errorColor);
    154. transform: rotate(18deg)
    155. }
    156. .pertagram::after {
    157. content: '';
    158. position: absolute;
    159. right: -20px;
    160. top: -85px;
    161. border-top: 62px solid transparent;
    162. border-bottom: 62px solid transparent;
    163. border-right: 45px solid var(--errorColor);
    164. transform: rotate(-18deg)
    165. }
    166. /* 五边形 */
    167. .pentagon {
    168. position: relative;
    169. width: 54px;
    170. border-top: 50px solid var(--primaryColor);
    171. border-right: 18px solid transparent;
    172. border-left: 18px solid transparent;
    173. }
    174. .pentagon::after {
    175. content: '';
    176. position: absolute;
    177. left: -18px;
    178. top: -85px;
    179. width: 0;
    180. height: 0;
    181. border-right: 45px solid transparent;
    182. border-left: 45px solid transparent;
    183. border-bottom: 35px solid var(--primaryColor);
    184. }
    185. /* 六边形 */
    186. .hexagon {
    187. position: relative;
    188. width: 30px;
    189. height: 52px; /* Math.cos(30deg) * 30 * 2 */
    190. background-color: var(--warningColor);
    191. }
    192. .hexagon::before {
    193. content: '';
    194. display: block;
    195. position: absolute;
    196. left: 0;
    197. top: 0;
    198. width: 100%;
    199. height: 100%;
    200. background-color: var(--warningColor);
    201. transform-origin: center center;
    202. transform: rotate(60deg)
    203. }
    204. .hexagon::after {
    205. content: '';
    206. display: block;
    207. position: absolute;
    208. left: 0;
    209. top: 0;
    210. width: 100%;
    211. height: 100%;
    212. background-color: var(--warningColor);
    213. transform-origin: center center;
    214. transform: rotate(-60deg)
    215. }
    216. /* 八边形 */
    217. .octagon {
    218. position: relative;
    219. width: 100px;
    220. height: 100px;
    221. background-color: var(--successColor);
    222. }
    223. .octagon::before {
    224. content: '';
    225. position: absolute;
    226. top: 0;
    227. left: 0;
    228. width: 42px;
    229. height: 0;
    230. border-bottom: 29px solid var(--successColor);
    231. border-left: 29px solid #fff;
    232. border-right: 29px solid #fff;
    233. }
    234. .octagon::after {
    235. content: '';
    236. position: absolute;
    237. bottom: 0;
    238. left: 0;
    239. width: 42px;
    240. height: 0;
    241. border-top: 29px solid var(--successColor);
    242. border-left: 29px solid #fff;
    243. border-right: 29px solid #fff;
    244. }
    245. /* 心形 */
    246. .heart {
    247. position: relative;
    248. width: 100px;
    249. height: 80px;
    250. }
    251. .heart::before,
    252. .heart::after {
    253. content: '';
    254. position: absolute;
    255. width: 50px;
    256. height: 80px;
    257. border-radius: 50px 50px 0 0;
    258. background-color: var(--errorColor);
    259. }
    260. .heart::before {
    261. left: 0;
    262. top: 0;
    263. transform-origin: bottom right;
    264. transform: rotate(45deg);
    265. }
    266. .heart::after {
    267. right: 0;
    268. top: 0;
    269. transform-origin: bottom left;
    270. transform: rotate(-45deg);
    271. }
    272. /* 无限符号 */
    273. .infinity {
    274. position: relative;
    275. width: 100px;
    276. height: 50px;
    277. }
    278. .infinity::before,
    279. .infinity::after {
    280. content: '';
    281. position: absolute;
    282. width: 50px;
    283. height: 50px;
    284. border: 15px solid var(--warningColor);
    285. }
    286. .infinity::before {
    287. left: -36%;
    288. top: 0;
    289. border-radius: 50% 50% 0 50%;
    290. transform: rotate(-45deg);
    291. }
    292. .infinity::after {
    293. right: -36%;
    294. top: 0;
    295. border-radius: 50% 50% 50% 0;
    296. transform: rotate(45deg);
    297. }
    298. /* 菱形 */
    299. .diamond {
    300. width: 50px;
    301. height: 50px;
    302. background-color: var(--primaryColor);
    303. transform-origin: center center;
    304. transform: rotate(45deg);
    305. }
    306. /* 钻石 */
    307. .cut-diamond {
    308. position: relative;
    309. width: 20px;
    310. border-bottom: 15px solid var(--errorColor);
    311. border-left: 15px solid transparent;
    312. border-right: 15px solid transparent;
    313. }
    314. .cut-diamond::before {
    315. content: '';
    316. position: absolute;
    317. left: -15px;
    318. top: 15px;
    319. width: 0;
    320. height: 0;
    321. border-top: 30px solid var(--errorColor);
    322. border-left: 25px solid transparent;
    323. border-right: 25px solid transparent;
    324. }
    325. /* 鸡蛋 */
    326. .egg {
    327. width: 40px;
    328. height: 60px;
    329. border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
    330. background-color: var(--warningColor);
    331. }
    332. /* 吃豆人 */
    333. .pac-man {
    334. width: 0px;
    335. height: 0px;
    336. border-right: 30px solid transparent;
    337. border-top: 30px solid var(--primaryColor);
    338. border-left: 30px solid var(--primaryColor);
    339. border-bottom: 30px solid var(--primaryColor);
    340. border-top-left-radius: 50%;
    341. border-top-right-radius: 50%;
    342. border-bottom-left-radius: 50%;
    343. border-bottom-right-radius: 50%;
    344. }
    345. /* popover */
    346. .popover {
    347. position: relative;
    348. width: 100px;
    349. height: 40px;
    350. border-radius: 3px;
    351. background-color: var(--successColor);
    352. }
    353. .popover::after {
    354. content: '';
    355. position: absolute;
    356. left: 50%;
    357. bottom: -6px;
    358. margin-left: -6px;
    359. border-top: 6px solid var(--successColor);
    360. border-left: 6px solid transparent;
    361. border-right: 6px solid transparent;
    362. }
    363. /* 12边形 */
    364. .burst-12 {
    365. position: relative;
    366. width: 60px;
    367. height: 60px;
    368. background-color: var(--errorColor);
    369. }
    370. .burst-12::before,
    371. .burst-12::after {
    372. content: '';
    373. position: absolute;
    374. left: 0;
    375. top: 0;
    376. width: 100%;
    377. height: 100%;
    378. background-color: var(--errorColor);
    379. transform-origin: center center;
    380. }
    381. .burst-12::before {
    382. transform: rotate(30deg);
    383. }
    384. .burst-12::after {
    385. transform: rotate(60deg);
    386. }
    387. /* 太极图 */
    388. .tai-ji {
    389. position: relative;
    390. width: 96px;
    391. height: 48px;
    392. background-color: #fff;
    393. border-style: solid;
    394. border-width: 2px 2px 50px 2px;
    395. border-color: var(--primaryText);
    396. border-radius: 50%;
    397. }
    398. .tai-ji::before {
    399. content: "";
    400. position: absolute;
    401. top: 50%;
    402. left: 0;
    403. width: 12px;
    404. height: 12px;
    405. background-color: #fff;
    406. border-radius: 50%;
    407. border: 18px solid var(--primaryText);
    408. }
    409. .tai-ji::after {
    410. content: "";
    411. position: absolute;
    412. top: 50%;
    413. left: 50%;
    414. width: 12px;
    415. height: 12px;
    416. border: 18px solid #fff;
    417. border-radius: 50%;
    418. background-color: var(--primaryText);
    419. }
    420. /* 徽章丝带 */
    421. .badge-ribbon {
    422. position: relative;
    423. width: 60px;
    424. height: 60px;
    425. border-radius: 50%;
    426. background-color: var(--errorColor);
    427. }
    428. .badge-ribbon::before {
    429. content: '';
    430. position: absolute;
    431. left: 28px;
    432. bottom: -9px;
    433. margin-left: -20px;
    434. border-top: 20px solid var(--errorColor);
    435. border-left: 10px solid transparent;
    436. border-right: 10px solid transparent;
    437. transform: rotate(140deg);
    438. }
    439. .badge-ribbon::after {
    440. content: '';
    441. position: absolute;
    442. right: 28px;
    443. bottom: -9px;
    444. margin-right: -20px;
    445. border-top: 20px solid var(--errorColor);
    446. border-left: 10px solid transparent;
    447. border-right: 10px solid transparent;
    448. transform: rotate(-140deg);
    449. }
    450. /* 放大镜 */
    451. .magnifying-glass {
    452. position: relative;
    453. width: 30px;
    454. height: 30px;
    455. border: 4px solid var(--primaryText);
    456. border-radius: 50%;
    457. transform: rotate(-45deg);
    458. }
    459. .magnifying-glass::before {
    460. content: '';
    461. position: absolute;
    462. left: 50%;
    463. bottom: -11px;
    464. width: 4px;
    465. height: 8px;
    466. margin-left: -2px;
    467. background-color: var(--primaryText);
    468. }
    469. /* 圆锥 */
    470. .cone {
    471. width: 0;
    472. height: 0;
    473. border-top: 40px solid var(--warningColor);
    474. border-left: 28px solid transparent;
    475. border-right: 28px solid transparent;
    476. border-radius: 50%;
    477. }
    478. /* 月亮 */
    479. .moon {
    480. width: 60px;
    481. height: 60px;
    482. border-radius: 50%;
    483. box-shadow: 10px 10px 0 0 var(--primaryColor);
    484. }
    485. /* 标签 */
    486. .tag {
    487. position: relative;
    488. width: 60px;
    489. height: 30px;
    490. border-top-left-radius: 3px;
    491. border-bottom-left-radius: 3px;
    492. background-color: var(--successColor);
    493. }
    494. .tag::before {
    495. content: '';
    496. position: absolute;
    497. right: -20px;
    498. top: 0;
    499. border-left: 20px solid var(--successColor);
    500. border-top: 15px solid transparent;
    501. border-bottom: 15px solid transparent;
    502. }
    503. .tag::after {
    504. content: '';
    505. position: absolute;
    506. right: -8px;
    507. top: 50%;
    508. width: 6px;
    509. height: 6px;
    510. margin-top: -3px;
    511. border-radius: 50%;
    512. background-color: #fff;
    513. }
    514. /* 导航 */
    515. .nav .item {
    516. position: relative;
    517. float: left;
    518. width: 20px;
    519. height: 14px;
    520. line-height: 14px;
    521. font-size: 12px;
    522. text-align: center;
    523. color: #fff;
    524. background-color: var(--primaryColor);
    525. }
    526. .nav .item:first-child {
    527. margin-left: 0;
    528. }
    529. .nav .item:last-child {
    530. margin-right: 0;
    531. }
    532. .nav .item:not(:first-child)::before {
    533. content: '';
    534. position: absolute;
    535. left: -10px;
    536. top: 0;
    537. border-left: 10px solid transparent;
    538. border-top: 7px solid var(--primaryColor);
    539. border-bottom: 7px solid var(--primaryColor);
    540. }
    541. .nav .item::after {
    542. content: '';
    543. position: absolute;
    544. right: -10px;
    545. top: 0;
    546. border-left: 10px solid var(--primaryColor);
    547. border-top: 7px solid transparent;
    548. border-bottom: 7px solid transparent;
    549. }
    550. .nav .item:not(:first-child) {
    551. margin-left: 15px;
    552. }
    553. /* 优惠券 */
    554. .coupon {
    555. display: flex;
    556. width: 150px;
    557. height: 80px;
    558. background-color: var(--warningColor);
    559. }
    560. .coupon .coupon-main {
    561. flex: 1;
    562. display: flex;
    563. align-items: center;
    564. padding-left: 20px;
    565. position: relative;
    566. overflow: hidden;
    567. }
    568. .coupon .coupon-main em {
    569. position: relative;
    570. font-style: normal;
    571. color: var(--errorColor);
    572. font-size: 36px;
    573. font-weight: 700;
    574. }
    575. .coupon .coupon-main em::before {
    576. content: attr(data-symbol);
    577. position: absolute;
    578. left: -10px;
    579. top: 0;
    580. font-size: 12px;
    581. color: var(--primaryColor);
    582. }
    583. .coupon .coupon-main em::after {
    584. content: attr(data-desc);
    585. position: absolute;
    586. left: 100%;
    587. bottom: 0;
    588. font-size: 12px;
    589. white-space: nowrap;
    590. color: var(--primaryColor);
    591. }
    592. .coupon .coupon-main::before {
    593. content: '';
    594. position: absolute;
    595. left: -5px;
    596. top: -5px;
    597. height: calc(100% + 10px);
    598. border-left: 10px dotted #fff;
    599. }
    600. .coupon .coupon-vice {
    601. position: relative;
    602. width: 40px;
    603. text-align: center;
    604. line-height: 40px;
    605. font-size: 14px;
    606. color: #fff;
    607. background-color: var(--primaryColor);
    608. writing-mode: vertical-lr;
    609. overflow: hidden;
    610. }
    611. .coupon .coupon-vice::before {
    612. content: '';
    613. position: absolute;
    614. left: -3px;
    615. top: 0;
    616. height: 100%;
    617. border-right: 6px dotted var(--warningColor);
    618. }
    619. </style>
    620. </head>
    621. <body>
    622. <div class="container">
    623. <section class="section">
    624. <h2 class="title">三角形</h2>
    625. <div class="wrapper">
    626. <div class="triangle"></div>
    627. </div>
    628. </section>
    629. <section class="section">
    630. <h2 class="title">弯尾箭头</h2>
    631. <div class="wrapper">
    632. <div class="curved-arrow"></div>
    633. </div>
    634. </section>
    635. <section class="section">
    636. <h2 class="title">梯形</h2>
    637. <div class="wrapper">
    638. <div class="trapezoid"></div>
    639. </div>
    640. </section>
    641. <section class="section">
    642. <h2 class="title">平行四边形</h2>
    643. <div class="wrapper">
    644. <div class="parallelogram"></div>
    645. </div>
    646. </section>
    647. <section class="section">
    648. <h2 class="title">六角星</h2>
    649. <div class="wrapper">
    650. <div class="hexagram"></div>
    651. </div>
    652. </section>
    653. <section class="section">
    654. <h2 class="title">五角星</h2>
    655. <div class="wrapper">
    656. <div class="pertagram"></div>
    657. </div>
    658. </section>
    659. <section class="section">
    660. <h2 class="title">五边形</h2>
    661. <div class="wrapper">
    662. <div class="pentagon"></div>
    663. </div>
    664. </section>
    665. <section class="section">
    666. <h2 class="title">六边形</h2>
    667. <div class="wrapper">
    668. <div class="hexagon"></div>
    669. </div>
    670. </section>
    671. <section class="section">
    672. <h2 class="title">八边形</h2>
    673. <div class="wrapper">
    674. <div class="octagon"></div>
    675. </div>
    676. </section>
    677. <section class="section">
    678. <h2 class="title">心形</h2>
    679. <div class="wrapper">
    680. <div class="heart"></div>
    681. </div>
    682. </section>
    683. <section class="section">
    684. <h2 class="title">无限符号</h2>
    685. <div class="wrapper">
    686. <div class="infinity"></div>
    687. </div>
    688. </section>
    689. <section class="section">
    690. <h2 class="title">菱形</h2>
    691. <div class="wrapper">
    692. <div class="diamond"></div>
    693. </div>
    694. </section>
    695. <section class="section">
    696. <h2 class="title">钻石</h2>
    697. <div class="wrapper">
    698. <div class="cut-diamond"></div>
    699. </div>
    700. </section>
    701. <section class="section">
    702. <h2 class="title">鸡蛋</h2>
    703. <div class="wrapper">
    704. <div class="egg"></div>
    705. </div>
    706. </section>
    707. <section class="section">
    708. <h2 class="title">吃豆人</h2>
    709. <div class="wrapper">
    710. <div class="pac-man"></div>
    711. </div>
    712. </section>
    713. <section class="section">
    714. <h2 class="title">popover</h2>
    715. <div class="wrapper">
    716. <div class="popover"></div>
    717. </div>
    718. </section>
    719. <section class="section">
    720. <h2 class="title">12边形</h2>
    721. <div class="wrapper">
    722. <div class="burst-12"></div>
    723. </div>
    724. </section>
    725. <section class="section">
    726. <h2 class="title">太极图</h2>
    727. <div class="wrapper">
    728. <div class="tai-ji"></div>
    729. </div>
    730. </section>
    731. <section class="section">
    732. <h2 class="title">徽章丝带</h2>
    733. <div class="wrapper">
    734. <div class="badge-ribbon"></div>
    735. </div>
    736. </section>
    737. <section class="section">
    738. <h2 class="title">放大镜</h2>
    739. <div class="wrapper">
    740. <div class="magnifying-glass"></div>
    741. </div>
    742. </section>
    743. <section class="section">
    744. <h2 class="title">圆锥</h2>
    745. <div class="wrapper">
    746. <div class="cone"></div>
    747. </div>
    748. </section>
    749. <section class="section">
    750. <h2 class="title">月亮</h2>
    751. <div class="wrapper">
    752. <div class="moon"></div>
    753. </div>
    754. </section>
    755. <section class="section">
    756. <h2 class="title">标签</h2>
    757. <div class="wrapper">
    758. <div class="tag"></div>
    759. </div>
    760. </section>
    761. <section class="section">
    762. <h2 class="title">导航</h2>
    763. <div class="wrapper">
    764. <div class="nav">
    765. <ul class="list clearfix">
    766. <li class="item">1</li>
    767. <li class="item">2</li>
    768. <li class="item">3</li>
    769. </ul>
    770. </div>
    771. </div>
    772. </section>
    773. <section class="section">
    774. <h2 class="title">优惠券</h2>
    775. <div class="wrapper">
    776. <div class="coupon">
    777. <div class="coupon-main">
    778. <em data-symbol="¥" data-desc="优惠券">10</em>
    779. </div>
    780. <div class="coupon-vice">立即领取</div>
    781. </div>
    782. </div>
    783. </section>
    784. </div>
    785. </body>
    786. </html>

    实不相瞒,这上面大部分都是我抄的!