คุณสมบัติ CSS overflow
ควบคุมอะไร เกิดขึ้นกับเนื้อหาที่ใหญ่เกินกว่าจะเข้ากับพื้นที่ได้
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
#overflowTest {
background: #4CAF50;
color: white;
padding: 15px;
width: 50%;
height: 100px;
overflow: scroll;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<h2>CSS Overflow</h2>
<p>The overflow property controls what happens to content that is too big to fit into an area.</p>
<div id="overflowTest">This text is really long and the height of its container is only 100 pixels. Therefore, a scrollbar is added to help the reader to scroll the content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem.</div>
</body>
</html>
คุณสมบัติ overflow
ระบุว่าจะตัดหรือไม่ เนื้อหาหรือ เพื่อเพิ่มแถบเลื่อนเมื่อเนื้อหาขององค์ประกอบมีขนาดใหญ่เกินกว่าจะพอดีกับที่ระบุ พื้นที่.
คุณสมบัติ overflow
มีค่าต่อไปนี้:
visible
- ค่าเริ่มต้น. โอเวอร์โฟลว์ไม่ได้ถูกตัดออก เนื้อหาแสดงผลนอกกล่องขององค์ประกอบ
hidden
- โอเวอร์โฟลว์ถูกตัดออก และเนื้อหาที่เหลือจะมองไม่เห็น
scroll
- โอเวอร์โฟลว์ถูกตัดออก และเพิ่มแถบเลื่อนเพื่อดูเนื้อหาที่เหลือ
auto
- คล้ายกับ scroll
แต่จะเพิ่มแถบเลื่อนเมื่อจำเป็นเท่านั้น
หมายเหตุ: คุณสมบัติ overflow
ใช้งานได้กับองค์ประกอบบล็อกที่มีความสูงที่ระบุเท่านั้น
หมายเหตุ: ใน OS X Lion (บน Mac) แถบเลื่อนจะถูกซ่อนไว้ตามค่าเริ่มต้น และจะแสดงเฉพาะเมื่อมีการใช้งานเท่านั้น (แม้ว่าจะมีการตั้งค่า "overflow:scroll") ไว้ก็ตาม
overflow: มองเห็นได้
ตามค่าเริ่มต้น การโอเวอร์โฟลว์จะ มองเห็น
ซึ่งหมายความว่าจะไม่ถูกตัดและแสดงผลนอกกล่องขององค์ประกอบ:
div {
width: 200px;
height:
65px;
background-color: coral;
overflow: visible;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: coral;
width: 200px;
height: 65px;
border: 1px solid;
overflow: visible;
}
</style>
</head>
<body>
<h2>Overflow: visible</h2>
<p>By default, the overflow is visible, meaning that it is not clipped and it renders outside the element's box:</p>
<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>
</body>
</html>
overflow: ซ่อนอยู่
ด้วยค่า hidden
โอเวอร์โฟลว์จะถูกตัดออก และเนื้อหาส่วนที่เหลือจะถูกซ่อน:
div {
overflow: hidden;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: coral;
width: 200px;
height: 65px;
border: 1px solid black;
overflow: hidden;
}
</style>
</head>
<body>
<h2>Overflow: hidden</h2>
<p>With the hidden value, the overflow is clipped, and the rest of the content is hidden:</p>
<p>Try to remove the overflow property to understand how it works.</p>
<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>
</body>
</html>
overflow: scroll
การตั้งค่าเป็น scroll
ส่วนโอเวอร์โฟลว์จะถูกตัดออก และเพิ่มแถบเลื่อนเพื่อเลื่อนภายในกล่อง โปรดทราบว่าการดำเนินการนี้จะเพิ่มแถบเลื่อนทั้งแนวนอนและแนวตั้ง (แม้ว่าคุณจะไม่ต้องการก็ตาม):
div {
overflow: scroll;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: coral;
width: 200px;
height: 100px;
border: 1px solid black;
overflow: scroll;
}
</style>
</head>
<body>
<h2>Overflow: scroll</h2>
<p>Setting the overflow value to scroll, the overflow is clipped and a scrollbar is added to scroll inside the box. Note that this will add a scrollbar both horizontally and vertically (even if you do not need it):</p>
<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>
</body>
</html>
overflow: auto
ค่า auto
คล้ายกับ scroll
แต่จะเพิ่มแถบเลื่อนเมื่อจำเป็นเท่านั้น:
div {
overflow: auto;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: coral;
width: 200px;
height: 65px;
border: 1px solid black;
overflow: auto;
}
</style>
</head>
<body>
<h2>Overflow: auto</h2>
<p>The auto value is similar to scroll, only it add scrollbars when necessary:</p>
<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>
</body>
</html>
overflow-x
และ overflow-y
overflow-x
และ overflow-y
คุณสมบัติระบุ ไม่ว่าจะเปลี่ยนเนื้อหาล้นเพียงแนวนอนหรือแนวตั้ง (หรือ ทั้งคู่):
overflow-x
ระบุสิ่งที่ต้องทำกับขอบซ้าย/ขวาของ เนื้อหา
overflow-y
ระบุสิ่งที่ต้องทำกับขอบบน/ล่างของ เนื้อหา.
div {
overflow-x: hidden; /* Hide horizontal scrollbar
*/
overflow-y: scroll; /* Add vertical scrollbar */
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: coral;
width: 200px;
height: 65px;
border: 1px solid black;
overflow-x: hidden;
overflow-y: scroll;
}
</style>
</head>
<body>
<h2>Overflow-x and overflow-y</h2>
<p>You can also change the overflow of content horizontally or vertically.</p>
<p>overflow-x specifies what to do with the left/right edges of the content.</p>
<p>overflow-y specifies what to do with the top/bottom edges of the content.</p>
<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>
</body>
</html>
ระบุสิ่งที่จะเกิดขึ้นหากเนื้อหาล้นกล่องขององค์ประกอบ
ระบุว่าเบราว์เซอร์สามารถแบ่งบรรทัดด้วยคำยาวๆ ได้หรือไม่ มันล้นภาชนะของมัน
ระบุสิ่งที่ต้องทำกับขอบซ้าย/ขวาของเนื้อหา หากล้นพื้นที่เนื้อหาขององค์ประกอบ
ระบุสิ่งที่ต้องทำกับขอบด้านบน/ล่างของเนื้อหา หากล้นพื้นที่เนื้อหาขององค์ประกอบ