Automatically translated.View original post

content #3

# Web Site Writing Basics

# appscript

# google

1.The HTML Structure

First, build your HTML file infrastructure, consisting of two main parts: the ON / OFF button and the Modal box itself.

HTML

< div class = "toggle-container" >

< input type = "checkbox" id = "modal-toggle" class = "modal-toggle" >

< label for = "modal-toggle" class = "toggle-button" >

Open Modal

< / label >

< div class = "modal" >

< div class = "modal-content" >

< label for = "modal-toggle" class = "close-button" >

& times;

< / label >

< h2 > This is your Modal. < / h2 >

< p > You can put any content here...< / p >

< / div >

< / div >

< / div >

• < input type = "checkbox" >: This is the key to this technique. We will use the Checkbox checked or unchecked state as the Modal Display Controller.

• < label >: Used to connect to input using the attribute for which a value matches the id of input. Clicking < label > will cause the Checkbox to be checked or unchecked.

• < div class = "modal" >: This is the Modal box that we will hide at first.

2. CSS Management (The CSS Magic)

In the CSS section, we will use a special option (pseudo-classes) and a sibling combinator option to control the display of the Modal.

CSS

/ * Hide the real Checkbox * /

.modal-toggle {

Display: none;

}

/ * Set Modal default to hide * /

.modal {

Visibility: hidden;

Opacity: 0;

Transition: visibility 0s, opacity 0.5s; / * cause Fade-in / out effect * /

Position: fixed;

Top: 0;

Left: 0;

width: 100%;

Height: 100%;

Background-color: rgba (0, 0, 0, 0.5); / * Opaque background * /

Display: flex;

Justify-content: center;

align-items: center;

}

/ * When the Checkbox is selected (checked), display Modal * /

.modal-toggle: check ~ .modal {

Visibility: visible;

Opacity: 1;

}

/ * Decoration other parts as needed * /

.modal-content {

Background-color:# fff;

padding: 20px;

Border-radius: 8px;

width: 80%;

Max-width: 500px;

Position: relative;

Transform: translateY (-20px);

Transition: transform 0.5s ease-out;

}

.modal-toggle: check ~ .modal-content {

Transform: translateY (0);

}

. Close-button {

Position: absolute;

Top: 10px;

Right: 15px;

font-size: 24px;

Cursor: pointer;

Color:# aaa;

}

.toggle-button {

Cursor: pointer;

padding: 10px 20px;

Background-color: # 007bff;

Color: white;

Border-radius: 5xx;

Text-decoration: none;

Display: inline-block;

}

• .modal-toggle: check: This is the heart of the entire mechanism when the Checkbox is clicked until it is checked.

• ~ (General Sibling Combinator): This option selects the .modal element next to the .modal-toggle: checked element at the same level.

• visibility: visible; and opacity: 1;: When the above conditions are true, we change these properties to make the hidden Modal show up.

Working summary

When the user clicks on the "Open Modal" button, which is actually < label >, < input type = "checkbox" id = "modal-toggle" > changes the state to checked. When < input > is checked, the CSS Selector .modal-toggle: checked ~ .modal causes the previously hidden Modal to appear. When you click the "×" button, which is < label > that points back to < input > again, < input > changes the state back to unchecked, and the Modal hides down again.

2025/9/23 Edited to

... Read moreถ้าคุณกำลังหา “modal web” ที่ทำงานได้แบบเบาๆ ไม่ต้องเขียน JavaScript วิธี checkbox + label เป็นทริคที่ใช้ได้จริง โดยหลักคือให้ <input type="checkbox"> เป็นตัวเก็บสถานะเปิด/ปิด แล้วใช้ CSS selector :checked ไปสั่งให้กล่อง modal โผล่ขึ้นมา ทริคที่ผมเจอบ่อยว่าคนพลาดคือ “ตำแหน่งของโครงสร้าง HTML” เพราะ selector แบบ .modal-toggle:checked ~ .modal จะทำงานก็ต่อเมื่อ .modal อยู่ “ถัดจาก” input และอยู่ระดับเดียวกัน (เป็น sibling กัน) ถ้าเผลอเอา .modal ไปอยู่คนละชั้น เช่นถูกครอบด้วย div อีกชั้น selector จะไม่ติด วิธีเช็กง่ายๆ คือเปิด DevTools แล้วดูว่ากล่อง .modal อยู่หลัง input จริงไหม อีกจุดที่ควรปรับเพื่อให้ใช้งานดีขึ้นคือเรื่องการคลิกพื้นที่มืดเพื่อปิด modal (overlay close) ผมมักทำให้พื้นหลังทึบแสงเป็น label ที่ชี้กลับไปที่ checkbox เหมือนปุ่มปิด เช่นทำโครงสร้างเป็น <label for="modal-toggle" class="modal"> แล้วค่อยมี <div class="modal-content"> อยู่ข้างใน จากนั้นต้องกันไม่ให้คลิกในกล่องแล้วปิดโดยไม่ตั้งใจ ด้วยการใส่ pointer-events หรือหยุดการคลิกที่ content (ถ้าไม่ใช้ JS วิธีง่ายคือทำให้ modal-content ไม่ใช่ label) เรื่องเอฟเฟกต์แนะนำให้ใช้คู่กัน 2 อย่าง: opacity + transform จะให้ความรู้สึกลื่นกว่าใช้ opacity อย่างเดียว เช่นตอนเปิดให้ .modal-content เลื่อนขึ้นนิดๆ (translateY) แล้วค่อยกลับเป็น 0 พร้อม transition ส่วน visibility ช่วยกันการกดคลิกโดนของด้านหลังตอน modal ซ่อนอยู่ ตัวอย่างสั้นๆ ที่ผมใช้บ่อย: - ซ่อน checkbox: display:none; - modal overlay: position:fixed; inset:0; background:rgba(0,0,0,.5); display:flex; align-items:center; justify-content:center; - สถานะปิด: opacity:0; visibility:hidden; - สถานะเปิด: .modal-toggle:checked ~ .modal {opacity:1; visibility:visible;} สุดท้ายถ้าตั้งใจทำให้รองรับการใช้งานจริงมากขึ้น (UX) ผมแนะนำเพิ่มรายละเอียดเล็กๆ เช่น 1) ตั้ง max-width ให้กล่อง เช่น 500px และใส่ padding ให้พอดี 2) ถ้าเนื้อหายาวให้กล่องเลื่อนเอง: max-height: 80vh; overflow:auto; 3) ใส่ focus style ให้ปุ่มเปิด/ปิดเพื่อคนใช้คีย์บอร์ด (ถึงจะไม่ใช้ JS ก็ทำได้บางส่วน) โดยรวมวิธีนี้เหมาะกับ modal ง่ายๆ เช่น กล่องแจ้งเตือน, แสดงรายละเอียดสินค้า, หรือ popup ฟอร์มสั้นๆ บนเว็บ เพราะโค้ดน้อย โหลดไว และคอนเซ็ปต์ตรงกับการค้นหาแนว “modal web” ที่อยากได้ตัวอย่างพร้อมใช้งานครับ