- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
Mastering CSS Vertical-Align for Web Development
Enhance your web design skills with this comprehensive guide.
Vertical Alignment in CSS
The vertical-align property in CSS controls the vertical positioning of elements within a line box. This includes aligning images with text and ensuring elements are properly positioned relative to each other. By using different vertical-align values, you can achieve various alignment effects, enhancing the overall appearance and readability of your web content.
Syntax
vertical-align: value;Values
- Keyword Values:
baselinesubsupertext-toptext-bottommiddletopbottom
- Length Values:
10em4px
- Percentage Values:
20%
- Global Values:
inheritinitialrevertrevert-layerunset
Examples
To align an element to the baseline of its parent:
vertical-align: baseline;To align an element to the top of its parent’s font:
vertical-align: text-top;To raise an element by 10 pixels above the baseline:
vertical-align: 10px;To position an element 20% above the baseline:
vertical-align: 20%;Values for Inline Elements
Parent-Relative Values
baseline: Aligns the baseline of the element with the baseline of its parent.sub: Aligns the baseline of the element with the subscript-baseline of its parent.super: Aligns the baseline of the element with the superscript-baseline of its parent.text-top: Aligns the top of the element with the top of the parent element’s font.text-bottom: Aligns the bottom of the element with the bottom of the parent element’s font.middle: Aligns the middle of the element with the baseline plus half the x-height of the parent.<length>: Aligns the baseline of the element to the given length above the baseline of its parent.<percentage>: Aligns the baseline of the element to the given percentage above the baseline of its parent, with the value being a percentage of theline-heightproperty.
Line-Relative Values
top: Aligns the top of the element and its descendants with the top of the entire line.bottom: Aligns the bottom of the element and its descendants with the bottom of the entire line.
Values for Table Cells
top: Aligns the content to the top of the table cell.bottom: Aligns the content to the bottom of the table cell.middle: Aligns the content to the middle of the table cell.baseline: Aligns the content to the baseline of the table cell.<length>: Aligns the content to the given length above the baseline of the table cell.<percentage>: Aligns the content to the given percentage above the baseline of the table cell, with the value being a percentage of theline-heightproperty.
Examples of vertical-align in Table Cells
To align the content to the top of a table cell:
td { vertical-align: top;}To align the content to the middle of a table cell:
td { vertical-align: middle;}To align the content to the bottom of a table cell:
td { vertical-align: bottom;}Example Usage
To create a table where cells are aligned differently:
HTML:
<table> <tr> <td class="baseline">Baseline</td> <td class="top">Top</td> <td class="middle">Middle</td> <td class="bottom">Bottom</td> </tr></table>CSS:
table { width: 100%; border-collapse: collapse;}
td { border: 1px solid black; padding: 10px;}
.baseline { vertical-align: baseline;}
.top { vertical-align: top;}
.middle { vertical-align: middle;}
.bottom { vertical-align: bottom;}Basic Example
HTML:
<div> An <img src="frame_image.svg" alt="link" width="32" height="32" /> image with a default alignment.</div><div> An <img class="top" src="frame_image.svg" alt="link" width="32" height="32" /> image with a text-top alignment.</div><div> An <img class="bottom" src="frame_image.svg" alt="link" width="32" height="32" /> image with a text-bottom alignment.</div><div> An <img class="middle" src="frame_image.svg" alt="link" width="32" height="32" /> image with a middle alignment.</div>CSS:
img.top { vertical-align: text-top;}img.bottom { vertical-align: text-bottom;}img.middle { vertical-align: middle;}Example: Vertical Alignment in a Line Box
HTML:
<p> top: <img style="vertical-align: top" src="star.png" alt="star"/> middle: <img style="vertical-align: middle" src="star.png" alt="star"/> bottom: <img style="vertical-align: bottom" src="star.png" alt="star"/> super: <img style="vertical-align: super" src="star.png" alt="star"/> sub: <img style="vertical-align: sub" src="star.png" alt="star"/></p>
<p> text-top: <img style="vertical-align: text-top" src="star.png" alt="star"/> text-bottom: <img style="vertical-align: text-bottom" src="star.png" alt="star"/> 0.2em: <img style="vertical-align: 0.2em" src="star.png" alt="star"/> -1em: <img style="vertical-align: -1em" src="star.png" alt="star"/> 20%: <img style="vertical-align: 20%" src="star.png" alt="star"/> -100%: <img style="vertical-align: -100%" src="star.png" alt="star"/></p>CSS:
img { margin-right: 0.5em;}
p { height: 3em; padding: 0 0.5em; font-family: monospace; text-decoration: underline overline; margin-left: auto; margin-right: auto; width: 80%;}Example: Vertical Alignment in a Table Cell
HTML:
<table> <tr class="bottom"> <td class="baseline">baseline</td> <td class="top">top</td> <td class="middle">middle</td> <td>bottom</td> <td>Row's style</td> <td> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse pretium felis eu sem mattis vulputate. </td> </tr></table>CSS:
.baseline { vertical-align: baseline;}
.top { vertical-align: top;}
.middle { vertical-align: middle;}
.bottom { vertical-align: bottom;}Browser Compatibility
The vertical-align property is widely supported across all major web browsers, ensuring that you can reliably use it to control the vertical alignment of elements in your web designs.
Browser Compatibility Table
| Browser | Version | Release Date |
|---|---|---|
| Chrome | 1.0 | December 2008 |
| Edge | 12.0 | July 2015 |
| Firefox | 1.0 | November 2004 |
| Internet Explorer | 4.0 | September 1997 |
| Safari | 1.0 | June 2003 |
| Opera | 4.0 | June 2000 |
Practical Tips
- Testing: Always test your web designs across multiple browsers to ensure consistent appearance and functionality.
- Polyfills and Fallbacks: For older browsers that may have limited support for certain CSS properties, consider using polyfills or fallbacks to maintain compatibility.
- Documentation: Refer to the official CSS specifications and browser documentation for the most up-to-date information on compatibility and any known issues.
See Also
For further exploration and understanding of related CSS properties and concepts, you may find the following resources helpful:
- [Typical Use Cases of Flexbox]WebsiteUrl: Learn about the various applications of Flexbox, including how to center items within a container.
- [
line-height]WebsiteUrl: Understand how theline-heightproperty affects the vertical spacing of text and how it interacts withvertical-align. - [
text-align]WebsiteUrl: Explore thetext-alignproperty for horizontal text alignment, which complementsvertical-alignfor comprehensive text layout control. - [
margin]WebsiteUrl: Learn about themarginproperty and how it can be used to create space around elements, enhancing layout and readability. - [Understanding
vertical-align]WebsiteUrl: Delve into an in-depth explanation of thevertical-alignproperty, including its nuances and practical examples. - [Vertical-Align: All You Need To Know]WebsiteUrl: A comprehensive guide on the
vertical-alignproperty, covering its various uses and best practices. - [HTML Reference Guide]WebsiteUrl: Access a comprehensive reference for HTML elements, which can help you understand how to structure your web content effectively.
สร้างเว็บไซต์ 1 เว็บ ต้องใช้งบเท่าไหร่? เจาะลึกทุกองค์ประกอบ website development cost อยากสร้างเว็บไซต์แต่ไม่มั่นใจในเรื่องของงบประมาณ อ่านสรุปเจาะลึกตั้งแต่ดีไซน์, ฟังก์ชัน และการดูแล พร้อมตัวอย่างงบจริงจาก Till it’s done ที่แผนชัด งบไม่บานปลายแน่นอน
Next.js สอน 14 ขั้นตอนเบื้องต้น: สร้างโปรเจกต์แรกใน 30 นาที เริ่มต้นกับ Next.js ใน 14 ขั้นตอนเพียงแค่ 30 นาที พร้อม SSR/SSG และ API Routes ด้วยตัวอย่างโค้ดง่าย ๆ อ่านต่อเพื่อสร้างโปรเจ็กต์แรกได้ทันทีที่นี่
วิธีสมัคร Apple Developer Account เพื่อนำแอปขึ้น App Store ทีละขั้นตอน อยากปล่อยแอปบน App Store ระดับโลก มาอ่านคู่มือสมัคร Apple Developer Account พร้อมเคล็ดลับ TestFlight และวิธีอัปโหลดที่ง่ายในบทความเดียวนี้ได้เลย
TypeScript Interface คืออะไร? อธิบายพร้อมวิธีใช้และข้อแตกต่างจาก Type เรียนรู้วิธีใช้ TypeScript Interface เพื่อสร้างโครงสร้างข้อมูลที่ปลอดภัยและเข้าใจง่าย พร้อมเปรียบเทียบข้อดีข้อแตกต่างกับ Type ที่คุณต้องรู้ ถูกรวมเอาไว้ในบทความนี้แล้ว
Material-UI (MUI) คืออะไร อยากสร้าง UI สวยงามและเป็นมืออาชีพในเวลาอันรวดเร็วใช่ไหม มาทำความรู้จักกับ Material-UI (MUI) ที่ช่วยให้คุณพัฒนาแอปพลิเคชันบน React ได้ง่ายและดูดีในทุกอุปกรณ์
เปรียบเทียบ 3 วิธีติดตั้ง install node js บน Ubuntu: NVM vs NodeSource vs Official Repo แบบไหนดีที่สุด? เรียนรู้วิธีติดตั้ง Node.js บน Ubuntu ด้วย NVM, NodeSource หรือ Official Repo เลือกวิธีที่เหมาะกับความต้องการของคุณ พร้อมเปรียบเทียบ เพื่อการพัฒนาที่มีประสิทธิภาพ! พูดคุยกับซีอีโอ
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.