Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vertical align img and text within li

Tags:

html

css

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

like image 654
raklos Avatar asked Feb 16 '11 20:02

raklos


People also ask

How do I vertically align text in a Li?

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.

How do I align text and vertical image in HTML?

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.

How do I align text and image in one line?

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.


2 Answers

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.

like image 70
Aron Rotteveel Avatar answered Sep 21 '22 23:09

Aron Rotteveel


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> 
like image 38
Ben Jakuben Avatar answered Sep 22 '22 23:09

Ben Jakuben