Vertical-align Property in Css with Programming Examples
Description
Vertical-align Property in Css with Programming Examples- Do you know how to specify the vertical position of text, images, etc. on the screen with CSS? It is useful to remember how to use it, as it is directly related to the readability of text and the visibility of images! So this time you will learn
- What is the vertical-align property for aligning the vertical position with CSS?
- Using the vertical-align property
- Examples of when the vertical-align property cannot be used and how to deal with it
- What is the line-height property that sets the top and bottom margins of characters?
We will thoroughly explain everything from the basics to applied methods!
What is the vertical-align property?
First, a brief description of the vertical-align property. The vertical-align property is a property for specifying the position of the vertical axis of text.
In addition to top alignment, center alignment, and bottom alignment, there are multiple setting methods. It is a property that is directly related to readability and usability, so it is recommended to remember how to use it!
Using the vertical-align property
Next, I will explain how to use the vertical-align property:
vertical-align: set value
List of setting values:
No | value | meaning | Example of use |
1 | top | top of line | top |
2 | middle | middle of the line | middle |
3 | bottom | bottom of line | bottom |
Four | text-top | top of font | text-top |
Five | text-bottom | bottom of font | text-bottom |
6 | baseline | font reference line | baseline |
7 | number + % | The position of the specified number ・Above the reference line if positive , below the reference line if negative | 20% , -30% |
8 | number + unit | The position of the specified number ・Above the reference line if positive , below the reference line if negative | 10px , -0.2em |
However, if it is only this, the position displayed for each value is a little difficult to understand. Also, I think that many people find it difficult to understand the difference between top and text-top, and the difference between baseline and middle.So I’ve prepared a diagram to help you understand. Here’s a picture that clearly explains the vertical-align values:
While confirming the above, actually try using the vertical-align property in CSS, and only when the intended result is not obtained, directly specify the value with “number + %” or “number + unit” and set it to the appropriate position we recommend the flow!
How to set the vertical-align property in CSS Programming Examples:
Next, based on the sample code, I will explain how to write concretely!
Example1:
index.html Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html><!DOCTYPE html> <html lang = "en" > <head> <meta charset = "UTF-8" /> <title> vertical-align example </title> <link rel = "stylesheet" href = "main.css" /> </head> <body> <h2> Sample code </h2> <div class = "main-contents" > <span class = "font-samurai" >Programming Digest</span> <span class = "vertical-align-top" > top </span> <span class = "vertical-align-middle" > middle </span> <span class = "vertical-align-bottom" > bottom </span> <span class = "vertical-align-text-top" > text-top </span> <span class = "vertical-align-baseline" > baseline </span> <span class = "vertical-align-text-bottom" > text-bottom </span> <span class = "vertical-align-30per" > 30% </span> <span class = "vertical-align-20px" > 20px </span> </div> </body> </html> |
Sample main.css code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /* underline the entire text */ span { text-decoration : underline ; } /* Add vertical lines to div tags with text */ .main-contents { border-top : solid 1px black ; border-bottom : solid 1px black ; } /* Specify the font size of "Programming Digest" */ .font-samurai { font-size : 50px ; } /********** Set vertical-align below **********/ .vertical-align-top { vertical-align : top ; font-size : 25px ; background-color : orange } .vertical-align-middle { vertical-align : middle ; font-size : 25px ; background-color : aqua } .vertical-align-bottom { vertical-align : bottom ; font-size : 25px ; background-color : orange } .vertical-align-text-top { vertical-align : text-top ; font-size : 25px ; background-color : aqua } .vertical-align-baseline { vertical-align : baseline ; font-size : 25px ; background-color : orange } .vertical-align-text-bottom { vertical-align : text-bottom ; font-size : 25px ; background-color : aqua } .vertical-align-30per { vertical-align : 30 %; font-size : 25px ; background-color : orange } .vertical-align-20px { vertical-align : 20px ; font-size : 25px ; background-color : aqua } |
output:
If the font size is the same, there is no difference in text-top, text-bottom, baseline, etc., so the font size of the “Programming Digest” and the characters applied to each setting value are different. In this way, you can easily specify the position of the vertical axis of the character.
Before you get used to it, it is recommended to use properties such as “background-color” to check the background color and adjust the position as shown in the sample.
Example2:
Examples of when the vertical-align property cannot be used and how to deal with it
So far, we have explained how to use the vertical-align property, but there are cases where the vertical-align property does not work as it is. That’s when I accidentally set the vertical-align property on a block-level element tag. I prepared a sample in which the previous sample was replaced with a div tag of a block-level element.
Sample index.html Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!DOCTYPE html><!DOCTYPE html> <html lang = "en" > <head> <meta charset = "UTF-8" /> <title> vertical-align example </title> <link rel = "stylesheet" href = "main.css" /> </head> <body> <h2> Sample Code </h2> <div class = "main-contents" > <div class = "font-samurai" >Programming Digest</div> <div class = "vertical-align-top" > top </div> <div class = "vertical-align-middle" > middle </div> <div class = "vertical-align-bottom" > bottom </div> <div class = "vertical-align-text-top" > text-top </div> <div class = "vertical-align-baseline" > baseline </div> <div class = "vertical-align-text-bottom" > text-bottom </div> <div class = "vertical-align-30per" > 30% </div> <div class = "vertical-align-20px" > 20px </div> <div class = "clear-both" ></div> </div> </body> </html> |
Sample main.css Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | /* Add vertical lines to div tags with text */ .main-contents { border-top : solid 1px black ; border-bottom : solid 1px black ; display : table-cell ; } /* Specify the font size of "Programming Digest" */ .font-samurai { font-size : 50px ; float : left ; /* Added for tiling */ } /********** Set vertical-align below **********/ .vertical-align-top { vertical-align : top ; font-size : 25px ; background-color : orange ; float : left ; /* Added for tiling */ } .vertical-align-middle { vertical-align : middle ; font-size : 25px ; background-color : aqua ; float : left ; /* Added for tiling */ } .vertical-align-bottom { vertical-align : bottom ; font-size : 25px ; background-color : orange ; float : left ; /* Added for tiling */ } .vertical-align-text-top { vertical-align : text-top ; font-size : 25px ; background-color : aqua ; float : left ; /* Added for tiling */ } .vertical-align-baseline { vertical-align : baseline ; font-size : 25px ; background-color : orange ; float : left ; /* Added for tiling */ } .vertical-align-text-bottom { vertical-align : text-bottom ; font-size : 25px ; background-color : aqua ; float : left ; /* Added for tiling */ } .vertical-align-30per { vertical-align : 30 %; font-size : 25px ; background-color : orange ; float : left ; /* Added for tiling */ } .vertical-align-20px { vertical-align : 20px ; font-size : 25px ; background-color : aqua ; float : left ; /* Added for tiling */ } .clear-both { clear : both ; } |
output:
As you can see, the vertical-align property does not work correctly with div tags.It may be difficult until you get used to it, but when you set the vertical-align property and it doesn’t work, isn’t it a block element? It may be a good idea to keep it in the corner of your head to check.
How to align left/center/right?
So far, I’ve explained the vertical-align property for setting the position of the vertical axis, but when adjusting the position of characters, there are many times when you want to align them with the horizontal axis! Therefore, it is recommended to remember not only the vertical-align property that changes the position of the vertical axis but also the text-align property that changes the position of the horizontal axis! Including the text-align property, the methods for left alignment, center alignment, and right alignment are thoroughly summarized below, so if you are interested, please take a look!
Summary
This time, I explained how to use the vertical-align property to set the vertical axis position of characters. As I mentioned at the beginning, the readability of the text is directly related to the usability of the app. As the saying goes, “God is in the details”, it’s important not to cut corners because it’s the details. It’s easy to use, so please give it a try!