Skip to content

ใส่ CSS ให้ <details> กับ <summary> #3

@rootEnginear

Description

@rootEnginear
details {
  border: 1px gray solid;

  > summary {
    padding: 8px;
    cursor: pointer;

    list-style: none;

    &::-webkit-details-marker {
      display: none;
    }

    > span {
      display: flex;
      justify-content: space-between;

      width: 100%;

      &::after {
        content: "👇";
      }
    }
  }

  > div {
    padding: 8px;
    border-top: 1px gray solid;
  }

  &[open] > summary > span::after {
    transform: rotate(180deg);
  }
}

Structure:

<details>
  <summary><span>What is Details & Summary?</span></summary>
  <div>
    The &lt;details&gt; HTML element creates a disclosure widget in which information is visible only when the widget is toggled into an "open" state. A summary or label must be provided using the &lt;summary&gt; element.
  </div>
</details>

วิธีลบ ▼ ของเดิมที่มากับ <details>

summary {
  list-style: none;

  &::-webkit-details-marker {
    display: none;
  }
}

ทำไมต้อง nest <span> ข้างใน <summary>

  1. Safari (ในเวอร์ชันนี้) ทำ <summary> เป็น flex ไม่ได้ แก้ไขแล้วใน Safari Technology Preview
  2. <summary> ใส่ได้แค่ Phrasing content หรือ Heading content เท่านั้น (MDN ของ <summary>)

จาก 2 ข้อข้างบน ทำให้ไม่สามารถจับ <summary> flex โดยตรงได้ และไม่สามารถยัด <div> ลงไปใน <summary> ได้

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions