css 如何用Tailwind制作三角形?

hfsqlsce  于 2023-04-01  发布在  其他
关注(0)|答案(4)|浏览(903)
<div class="">
                <div class="w-16 h-16 border-b-30 border-l-30 border-solid border-black">
                    <div class="h-16 w-16 border-t-30 border-r-30 bg-transparent"></div>
                </div>
</div>

如何用tailwindCss制作一个三角形而不用插件??

zpqajqem

zpqajqem1#

您可以尝试使用transform:

<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<div class="w-16 overflow-hidden inline-block">
 <div class=" h-11 w-11 bg-black rotate-45 transform origin-bottom-left"></div>
</div>

<div class="w-16 overflow-hidden inline-block">
 <div class=" h-11 w-11 bg-black -rotate-45 transform origin-top-left"></div>
</div>

<div class="w-11  overflow-hidden inline-block">
 <div class=" h-16  bg-black -rotate-45 transform origin-top-right"></div>
</div>

<div class="w-11  overflow-hidden inline-block">
 <div class=" h-16  bg-black rotate-45 transform origin-top-left"></div>
</div>

<div class="w-11  overflow-hidden inline-block">
 <div class=" h-16  bg-black -rotate-45 transform origin-bottom-right"></div>
</div>

<div class="w-11  overflow-hidden inline-block">
 <div class=" h-16  bg-black rotate-45 transform origin-bottom-left"></div>
</div>

<div class="w-11  overflow-hidden inline-block">
 <div class=" h-16  bg-black -rotate-45 transform origin-top-left"></div>
</div>

<div class="w-11  overflow-hidden inline-block">
 <div class=" h-16  bg-black rotate-45 transform origin-top-right"></div>
</div>
w6lpcovy

w6lpcovy2#

您也可以尝试使用边框
tailwind playground

<!-- down -->
<div class="border-solid border-t-black border-t-8 border-x-transparent border-x-8 border-b-0"></div>
<!-- up -->
<div class="border-solid border-b-black border-b-8 border-x-transparent border-x-8 border-t-0"></div>
<!-- left -->
<div class="border-solid border-r-black border-r-8 border-y-transparent border-y-8 border-l-0"></div>
<!-- right -->
<div class="border-solid border-l-black border-l-8 border-y-transparent border-y-8 border-r-0"></div>
nbysray5

nbysray53#

也许你想使用三角形作为工具提示。(这样的搜索让我来到了这里):

<div class="group relative mt-4 ml-4 inline-block">
  <button type="button" class="rounded-md border border-neutral-600 px-1">...</button>

  <!-- container for triangle and the menu ↓-->
  <div class="invisible absolute left-0 -mt-[2px] flex flex-col group-focus-within:visible group-active:visible">
     <div class="ml-2 -mb-[1px] inline-block overflow-hidden"> <!-- ← triangle container -->
        <!-- triangle ↓ -->
      <div class="h-3 w-3 origin-bottom-left rotate-45 transform border border-neutral-500 bg-neutral-100"></div>
    </div>

    <!-- menu ↓ -->
    <div class="flex min-w-max flex-col rounded-md border border-neutral-500 bg-neutral-100 px-2 py-1">
      <div class="cursor-pointer hover:underline">Do amazing stuff</div>
      <div class="cursor-pointer hover:underline">Go back</div>
    </div>
  </div>
</div>

Demo on play.tailwindcss.com
依据:

sycxhyv7

sycxhyv74#

通过伪元素(小三角形):

<script src="https://cdn.tailwindcss.com"></script>
<div>small triangle:</div>
<div class="flex gap-x-1">
  <div class="before:content-['▴']"></div>
  <div class="before:content-['▾']"></div>
  <div class="before:content-['◂']"></div>
  <div class="before:content-['▸']"></div>
</div>

按伪元素(正三角形):

<script src="https://cdn.tailwindcss.com"></script>

<div>normal triangle:</div>
<div class="flex gap-x-1">
  <div class="before:content-['▲']"></div>
  <div class="before:content-['▼']"></div>
  <div class="before:content-['◀']"></div>
  <div class="before:content-['▶']"></div>
</div>

按边境:

<script src="https://cdn.tailwindcss.com"></script>
<div>triangle by border</div>
<div class="flex gap-x-3">
  <div class="w-0 h-0 border-8 border-solid border-transparent border-b-black"></div>
  <div class="w-0 h-0 border-8 border-solid border-transparent border-t-black"></div>
  <div class="w-0 h-0 border-8 border-solid border-transparent border-r-black"></div>
  <div class="w-0 h-0 border-8 border-solid border-transparent border-l-black"></div>
</div>

按边框(简化):

<script src="https://cdn.tailwindcss.com"></script>

<div>triangle by border (simplify)</div>
<style type="text/tailwindcss">
  @layer components { .triangle { @apply w-0 h-0 border-8 border-solid border-transparent } }
</style>
<div class="flex gap-x-3">
  <div class="triangle border-b-black"></div>
  <div class="triangle border-t-black"></div>
  <div class="triangle border-r-black"></div>
  <div class="triangle border-l-black"></div>
</div>

相关问题