I am trying to vertically align to the middle both an image and some text within a list element but having no luck.
eg:
<ul> <li><img src="somepath" /> sometext </li> <li><img src="somepath2" /> sometext2 </li> </ul>
how can i do it? thanks
Inorder to make vertical-align: middle; work, you need to use display: table; for your ul element and display: table-cell; for li elements and than you can use vertical-align: middle; for li elements. You don't need to provide any explicit margins , paddings to make your text vertically middle.
We need to create a parent element that contain both image and text. After declaring the parent element as flexbox using display: flex; we can align the items to the center using align-items: center;. Example: This example uses flexbox to vertically align text next to an image using CSS.
Using the float property of CSS will allow you to place an image and text on the same line without breaking the line break. Or Alternatively, you should use the flexbox method of CSS that uses the flex property to make sure that images and lines are aligned in the same line.
Assuming your list items have a fixed height, you can use line-height
combined with vertical-align: middle
to do this.
Example:
ul li { display: block; height: 100px; line-height: 100px; } ul li img { vertical-align: middle; }
Working example here.
You should be able to use the CSS property "vertical-align" for the img tag. Example:
<style type="text/css"> img { vertical-align: middle; } </style> <ul> <li><img src="test.jpg" />test</li> </ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With