Vertically align image with text within <li> - html

I'm trying to vertically align text with an image (or vice-versa?). Here's some code:
<div class="row">
<div class="col-md-3">col-md-3
<ul>
<li><img src="http://placehold.it/60x60"><p>Text Text Text</p></li>
<li><img src="http://placehold.it/60x60"><p>Text</p></li>
<li><img src="http://placehold.it/60x60"><p>Text Text Text Text</p></li>
<li><img src="http://placehold.it/60x60"><p>Text Text</p></li>
<li><img src="http://placehold.it/60x60"><p>Text Text Text Text Text</p></li>
<li><img src="http://placehold.it/60x60"><p>Text</p></li>
<li><img src="http://placehold.it/60x60"><p>Text Text</p></li>
</ul>
{# 3 more columns like this #}
</div>
also CSS:
ul {
list-style-type: none;
padding: 0;
margin: 0;
}
div ul li {
display: table-row;
}
img {
float: left;
margin: 0 0 10px 0;
padding: 2px;
vertical-align: middle;
}
also, might be important all images are the same fixed size, let's say 60x60 like in example and I can not use it as a background.
How can I align it? Thanks.
Update: as were pointed out, I'm looking for text to be in the middle of the right edge of the picture, thanks.

My solution works with one line of text as well as multiple lines.
Working Fiddle Tested on: IE10, IE9, IE8, Chrome, FF, Safari
HTML: same as you posted
I'm assuming you meant middle alignment. if not: use top | bottom instead of middle
CSS
*
{
padding: 0;
margin: 0;
}
ul
{
list-style-type: none;
}
div ul li
{
margin: 5px;
}
img
{
vertical-align: middle; /* | top | bottom */
}
div ul li p
{
display: inline-block;
vertical-align: middle; /* | top | bottom */
}

Just remove the <p> tag and the float:left from img and you got it.
Demo at http://jsfiddle.net/BA2Lc/

I am not a big fan of using those table display options, had some bad cross-browser experiences with them.
Seems to me you could use line-height here. Something like this:
ul {
list-style-type: none;
padding: 0;
margin: 0;
}
div ul li {
line-height: 60px;
}
img {
float: left;
margin: 0 0 10px 0;
padding: 2px;
}
and a fiddle: http://jsfiddle.net/qjSpj/1/

You can modify the <p> style from the default display to inline:
p {display: inline}

You can use the vertical-align: top; declaration on the li element. But you will need to use a clearfix since you're floating the image.

I just added vertical-align:middle/top/bottom; to the images and worked.

Related

Justify images CSS [duplicate]

This question already has answers here:
Fluid width with equally spaced DIVs
(7 answers)
Closed 8 years ago.
I want to justify 3 images, I have never done this before so I have no clue how to do it.
Now I Googled a bit and found the property "justify", but I saw it only works for Text ( correct me if i'm wrong. )
But I tried the following.
HTML
<ul>
<li><img class="uspIconOntwerp" src="images/ontwerp-icon.png" /><div class="uspText">Ontwerp</div></li>
<li><img class="uspIconRealisatie" src="images/realisatie-icon.png" /><div class="uspText">Realisatie</div></li>
<li><img class="uspIconPrijs" src="images/prijs-icon.png" /><div class="uspText">Betaalbare prijs</div></li>
</ul>
And my css
ul
{
text-align: justify;
}
But this doesn't work (ofcourse).
Does anyone have a clue how to do this?
To make justify property works as do in the alignment of texts you will need to make the li items inline-block elements. Try this:
ul {
text-align: justify;
}
ul > li {
display: inline-block;
}
ul:after {
content: '';
display: inline-block;
width: 100%;
}
* {
margin: 0;
padding: 0
}
ul {
background: red;
padding: 40px;
box-sizing: border-box;
text-align: justify;
}
ul > li {
display: inline-block;
}
ul:after {
content: '';
display: inline-block;
width: 100%;
}
<ul id="Grid">
<li>
<img class="uspIconOntwerp" src="images/ontwerp-icon.png" />
<div class="uspText">Ontwerp</div>
</li>
<li>
<img class="uspIconRealisatie" src="images/realisatie-icon.png" />
<div class="uspText">Realisatie</div>
</li>
<li>
<img class="uspIconPrijs" src="images/prijs-icon.png" />
<div class="uspText">Betaalbare prijs</div>
</li>
</ul>
Note that you will need that after pseudo-element in order to make that line of items work as an entire one not the last on some justified content.

How to correctly use text overflow in bootstrap

I'm trying to hide the overflow text of li and my li looks like this
<li class="food_item">
<a href="#" class="food_name" title="test">
testtesttesttesttesttesttesttesttest
</a>
<span>(12)</span>
</li>
<li class="food_item">
a short one
<span>(12)</span>
</li>
and my css
.food_category>.food_item {
width: 25%;
float: left;
line-height: 30px;
}
.food_category .food_name {
font-size: 14px;
margin: 0px 4px 0px 0px;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
width: 80%;
display: inline-block;
}
food_category is the class name of the ul tag, now the effect is below:
the span and the a tag are not in the same line, I assume it's about the inline-block property of the a, but if I remove that, the text-overflow will not work and the overflow text will not be hidden. I'm stuck here, can anybody help me? how can I make the a and span show in the same line while keeping overflow text hidden?
Update
this is the jsfiddle link, btw,I didn't set the css of span. What I want is to make the span text right behind the a tag like this testest... (12).Thx!
In regards to your update, you need to set the anchor tag and span tag to be vertically aligned at the top of the list element. Add the following to your CSS:
.food_item a,
.food_item span {
vertical-align: top;
}
This produces the desired behavior.
DEMO
You can do something like:
.food_item span {
display: inline-block;
float:left;
}
and add float:left; to .food_item .food_name
.food_item .food_name {
font-size: 14px;
margin: 0px 4px 0px 0px;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
width: 60%;
display: inline-block;
float:left;
}
FIDDLE
You may need to update the margin/padding for the spacing of the span.
I would also recommend adding something like clearfix on each li element to prevent float issues:
<li class="food_item clearfix">
...
</li>
<li class="food_item clearfix">
...
</li>
.clearfix {
*zoom: 1;
}
.clearfix:before,
.clearfix:after {
display: table;
line-height: 0;
content: "";
}
.clearfix:after {
clear: both;
}

Align image and text li in horizontal ul

I have a ul with an image and a piece of text:
<ul id="botoes">
<li><img src="image.png"></li>
<li>Perguntas</li>
</ul>
I'm trying to get both items aligned horizontally, since it is an horizontal list. Therefore I'm using:
#botoes li{
display: inline;
list-style-type: none;
padding-right: 20px;
vertical-align: top;
}
this would solved the problem
#botoes li{
display: inline-block;
list-style-type: none;
padding-right: 20px;
vertical-align: middle;
}
Use This CSS this will work hear is an example
#botoes li{
display: inline-block; /* changed */
list-style-type: none;
padding-right: 20px;
vertical-align: middle; /* changed */
}
simply use line-height for your text.
Here is a working Fiddle
(change the alignment value to: top | bottom | middle to your needed use)
Just add that to your css
#botoes a
{
display: inline;
vertical-align: top;
}
just put
style="display:inline-block;"
in tag a or tag p
example
<ul>
<li>
<img height="69" width="94" src="images/guest.png"/>
<a href="#" style="display:inline-block;">
<h4>KM Plan 2016</h4>
</a>
</li>
</ul>
try yourself with JSFilddle

changing the line-height of an individual li tag

I want to have the phone number and email address vertically align with the little icons next to them. I'm trying to change their line-height, but that changes the line height of all the li's in that area. I think that is because they are inline. Here is the site and the css.
LINK: www.matthewtbrown.com/newsite
HTML:
<ul class="contact">
<li><img src="http://s7.postimg.org/64ux9a1if/email.png"></li>
<li class="contacttext">mbrown74#rocketmail.com</li>
<li><img src="http://s7.postimg.org/g0w08x7af/phone.png"></li>
<li class="contacttext">978-761-1205</li>
</ul>
CSS:
.contact {
color: #fff;
text-align: center;
float:none;
}
.contact > li {
display: inline;
}
.contacttext {
font-size: 19px;
padding-left: 5px;
}
That's where vertical-align comes into play which aligns inline elements to each other:
In your case the following should work:
.contacttext{
vertical-align:text-top;
}
Also note, that if you want to have your li contain the img properly, it needs a display-type other than the default inline - inline-block might be suited most.
Try something like this:
li {
display: inline;
vertical-align: top;
line-height: 25px;
}
after this i have this result:
try this
<ul class="contact">
<li class="contacttext">
<img src="http://s7.postimg.org/64ux9a1if/email.png">mbrown74#rocketmail.com
</li>
<li class="contacttext">
<img src="http://s7.postimg.org/g0w08x7af/phone.png">978-761-1205
</li>
I just kept the icons and the text in the same li's
I got it. I did:
.contacttext {
font-size: 19px;
padding-left: 5px;
display:inline-block;
vertical-align:middle;
}
You could add margin-bottom to your <img> to solve this issue.
li img {
margin-bottom: 3px;
}

Vertially align image and link on a single line

How do I align the following so that the image and the link are vertically aligned? Also how do I increase the spacing between consecutive lines?
<li><img src="c.png" alt="Centern | Karta"/> Centern </li>
<li><img src="fi.png" alt="Feministiskt initiativ | Karta"/>Feministiskt initiativ </li>
You can put the images as background.
HTML
<li><a class='centern' href="centern.php" title="Centern | Karta">Centern</a> </li>
CSS
a.centern {
background: url('c.png') no-repeat;
height:20px; //height of the link
width:100px; //width of the link
padding-left:40px //give some space for the background image
}
Not entirly sure what you mean by alignment. I set up a fiddle here: http://jsfiddle.net/GX5k6/
/* this takes care of the alignment of the text and images */
li a {
display: block;
line-height: 32px;
float: left;
padding-left: 5px; /* spacing between image and text */
}
li img {
display: block;
float: left;
}
li {
clear: both;
overflow: hidden;
}
/* spacing between lines */
li {
margin-bottom: 5px;
}
Depending on wether you consider the images styling or content and if you want them to show up when the page is printed, you should indeed consider making them background images as suggested by #petar