I’m trying to get images to align right within list elements as follows:
The images are squares (equal height and width) and I want them to fit within the height of the list element to the right.
My html code is pretty simple at the moment (the following is an example):
<ul>
<li>
<img src="https://upload.wikimedia.org/wikipedia/commons/d/dd/Square_-_black_simple.svg">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</li>
</ul>
What would be the best way to do this?
2
Answers
1 possible answer, perhaps not suitable for you is to use a grid on each li.
the second column width will define the img width.
the img is in a div, this div is grid align center and justify center too. Now the img can have a size 100% it’s relative to something.
To achieve that, you need to use
<tr>
and<td>
. This is a example of how to do that:You can use a table and I set a line with two columns. The second column is the image, and I use
background-image
so that the image will fit in the column. I also usebackground-position
andbackground-size
to center and contain the image, so that no matter what is the size of the image, it will show it properly.