คุณสมบัติ overflow ใน CSS มีประโยชน์เมื่อผู้ใช้ต้องการแสดงเนื้อหาที่ใหญ่กว่าในภาชนะที่เล็กกว่าโดยไม่ต้องปรับขนาดเนื้อหา คุณสมบัตินี้อนุญาตให้ผู้ใช้ตัดเนื้อหา แสดงแถบเลื่อนเพื่อดูเนื้อหาที่ถูกตัด หรือแสดงเนื้อหาไว้นอกภาชนะ จึงได้ชื่อว่า overflow
ไวยากรณ์
ไวยากรณ์สำหรับคุณสมบัติ CSS Overflow ดังนี้:
Selector { overflow: /*value*/ }
ค่าต่อไปนี้สำหรับคุณสมบัติ CSS Overflow:
| ลำดับ | ค่าและคำอธิบาย |
|---|---|
| 1 | visible เป็นค่าเริ่มต้น เนื้อหาไม่ถูกตัดและแสดงไว้นอกกล่องขององค์ประกอบ |
| 2 | hidden ตัดเนื้อหาที่ล้นออกจากกล่องขององค์ประกอบ เนื้อหาที่ถูกตัดไม่สามารถมองเห็นได้ |
| 3 | scroll ตัดเนื้อหาที่ล้นออกจากกล่องขององค์ประกอบ เนื้อหาที่ถูกตัดมองเห็นได้เพราะมีแถบเลื่อนแสดงพร้อมกับเนื้อหา |
| 4 | auto จะแสดงแถบเลื่อนอัตโนมัติเพื่อดูเนื้อหาที่ล้น |
ลองดูตัวอย่างของคุณสมบัติ CSS overflow:
ตัวอย่าง
<!DOCTYPE html> <html> <head> <title>CSS Overflow</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } #containerDiv { margin: 0 auto; height: 100px; width: 100px; overflow: auto; } </style> </head> <body> <form> <fieldset> <legend>CSS Overflow</legend> <div id="containerDiv"> <img id="image" src="https://www.tutorialspoint.com/hadoop/images/hadoop-mini-logo.jpg"> </div> <input type="button" onclick="fitHeight()" value="Remove Scrollbars"> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var imgSelect = document.getElementById("image"); var containerDiv = document.getElementById("containerDiv"); function fitHeight() { containerDiv.style.height = imgSelect.height+'px'; containerDiv.style.width = imgSelect.width+'px'; containerDiv.style.overflow = 'hidden'; } </script> </body> </html>
ผลลัพธ์
ก่อนคลิกปุ่มใดๆ:

หลังคลิกปุ่ม 'Remove Scrollbars':

ลองดูอีกตัวอย่างสำหรับคุณสมบัติ CSS overflow:
ตัวอย่างเพิ่มเติม
<!DOCTYPE html> <html> <head> <title>CSS Overflow</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } #containerDiv { margin: 0 auto; height: 110px; overflow: scroll; } </style></head> <body> <form> <fieldset> <legend>CSS Overflow</legend> <div id="containerDiv"> This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.</div> <input type="button" onclick="add()" value="Remove Scrollbars"> </fieldset> </form> <script> function add() { document.querySelector('#containerDiv').style.overflow = "hidden"; } </script> </body> </html>
ผลลัพธ์
ก่อนคลิกปุ่ม 'Remove Scrollbars':

หลังคลิกปุ่ม 'Remove Scrollbars':
