ในบทนี้ คุณจะได้เรียนรู้เกี่ยวกับคุณสมบัติต่อไปนี้:
text-overflow
word-wrap
word-break
writing-mode
คุณสมบัติ CSS text-overflow
ระบุว่าเนื้อหาล้นที่ไม่ ที่แสดงควรส่งสัญญาณไปยังผู้ใช้
มันสามารถตัดได้:
This is some long text that will not fit in the box
หรือสามารถแสดงเป็นจุดไข่ปลา (...):
This is some long text that will not fit in the box
รหัส CSS เป็นดังนี้:
p.test1 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: clip;
}
p.test2 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: ellipsis;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
p.test1 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: clip;
}
p.test2 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<h1>The text-overflow Property</h1>
<p>The following two paragraphs contains a long text that will not fit in the box.</p>
<h2>text-overflow: clip:</h2>
<p class="test1">This is some long text that will not fit in the box</p>
<h2>text-overflow: ellipsis:</h2>
<p class="test2">This is some long text that will not fit in the box</p>
</body>
</html>
ตัวอย่างต่อไปนี้แสดงวิธีที่คุณสามารถแสดงเนื้อหาที่ล้นออกมาเมื่อวางเมาส์เหนือองค์ประกอบ:
div.test:hover {
overflow: visible;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
div.test {
white-space: nowrap;
width: 200px;
overflow: hidden;
border: 1px solid #000000;
}
div.test:hover {
overflow: visible;
}
</style>
</head>
<body>
<p>Hover over the two divs below, to see the entire text.</p>
<div class="test" style="text-overflow:ellipsis;">This is some long text that will not fit in the box</div>
<br>
<div class="test" style="text-overflow:clip;">This is some long text that will not fit in the box</div>
</body>
</html>
คุณสมบัติ word-wrap
ของ CSS ช่วยให้สามารถตัดคำยาวๆ และตัดไปบรรทัดถัดไปได้
หากคำยาวเกินไปที่จะพอดีกับพื้นที่ คำนั้นจะขยายออกไปด้านนอก:
This paragraph contains a very long word: thisisaveryveryveryveryveryverylongword. The long word will break and wrap to the next line.
คุณสมบัติ word-wrap ช่วยให้คุณสามารถบังคับตัดข้อความได้ แม้ว่าจะหมายถึงการแยกข้อความตรงกลางคำก็ตาม:
This paragraph contains a very long word: thisisaveryveryveryveryveryverylongword. The long word will break and wrap to the next line.
รหัส CSS เป็นดังนี้:
ปล่อยให้คำยาวๆ สามารถแยกออกและตัดไปบรรทัดถัดไป:
p {
word-wrap: break-word;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
p.test {
width: 11em;
border: 1px solid #000000;
word-wrap: break-word;
}
</style>
</head>
<body>
<h1>The word-wrap Property</h1>
<p class="test">This paragraph contains a very long word: thisisaveryveryveryveryveryverylongword. The long word will break and wrap to the next line.</p>
</body>
</html>
คุณสมบัติ CSS word-break
ระบุกฎการแบ่งบรรทัด
This paragraph contains some text. This line will-break-at-hyphens.
This paragraph contains some text. The lines will break at any character.
รหัส CSS เป็นดังนี้:
p.test1 {
word-break:
keep-all;
}
p.test2 {
word-break:
break-all;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
p.test1 {
width: 140px;
border: 1px solid #000000;
word-break: keep-all;
}
p.test2 {
width: 140px;
border: 1px solid #000000;
word-break: break-all;
}
</style>
</head>
<body>
<h1>The word-break Property</h1>
<p class="test1">This paragraph contains some text. This line will-break-at-hyphens.</p>
<p class="test2">This paragraph contains some text. The lines will break at any character.</p>
</body>
</html>
คุณสมบัติ CSS writing-mode
ระบุ ไม่ว่าบรรทัดข้อความจะวางในแนวนอนหรือแนวตั้ง
ข้อความบางส่วนที่มีองค์ประกอบ span พร้อมด้วยโหมดการเขียนแนวตั้ง rl
Some text with a span element with a vertical-rl writing-mode.
ตัวอย่างต่อไปนี้แสดงโหมดการเขียนที่แตกต่างกัน:
p.test1 {
writing-mode: horizontal-tb;
}
span.test2 {
writing-mode: vertical-rl;
}
p.test2 {
writing-mode:
vertical-rl;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
p.test1 {
writing-mode: horizontal-tb;
}
span.test2 {
writing-mode: vertical-rl;
}
p.test2 {
writing-mode: vertical-rl;
}
</style>
</head>
<body>
<h1>The writing-mode Property</h1>
<p class="test1">Some text with default writing-mode.</p>
<p>Some text with a span element with a <span class="test2">vertical-rl</span> writing-mode.</p>
<p class="test2">Some text with writing-mode: vertical-rl.</p>
</body>
</html>
ตารางต่อไปนี้แสดงรายการคุณสมบัติเอฟเฟกต์ข้อความ CSS:
ระบุว่าข้อความชิดขอบควรจัดแนวและเว้นระยะห่างอย่างไร
ระบุว่าควรส่งสัญญาณเนื้อหาล้นที่ไม่แสดงไปยังผู้ใช้อย่างไร
ระบุกฎการแบ่งบรรทัดสำหรับสคริปต์ที่ไม่ใช่ CJK
ทำให้สามารถตัดคำยาวๆ แล้วไปขึ้นบรรทัดถัดไปได้
ระบุว่าบรรทัดของข้อความถูกจัดวางในแนวนอนหรือแนวตั้ง