Link with 2 lines text and image - html

How do i create a link like this with HTML and CSS:
It does not respond as i hoped it would.
What i try to accomplish is create a navigation link that hold a image on the left, and 2 lines of text right next to the image and not below the image.
html:
<div id="nav">
<img src="http://www.werkenbijavl.nl/images/WerkenBijAVL/icon-email.png" heigh="24px" width="24px" alt=""> Line 1<br><span>Line 2</span>
</div>
css:
nav a {
background: grey;
padding: 10px;
text-align: center;
text-decoration: none;
line-height: 24px;
vertical-align: middle;
}
nav img {
padding-right: 10px;
}
nav span {
font-style: italic;
}

Inline-block should fix your issues: http://jsfiddle.net/sZnaR/
The HTML:
<a href="#">
<img src="blah" />
<span class="text">
Line 1<br/>
Line 2
</span>
</a>
The CSS:
a {
display: block;
text-decoration: none;
}
a img {
display: inline-block;
width: 30px;
height: 30px;
}
a span {
display: inline-block;
}
Your text elements render as inline elements, and because of that, wrap underneath the image.

You're better off putting the image as a background image on the link using CSS then using Padding to align the text to the image, making sure you've allowed enough padding for the text to clear the image on the left. Also, use the background position to position the image to the right or wherever if you need it there. It also makes the HTML a lot cleaner and easier to read.
edit if you need dynamic images for each link you can always use an inline style to specify the background-image url.

You can also do this by floating it to the left.
Like this
<span style="line-height:15px;float:left">Line 1<br>Line 2</span>
Working Fiddle

HTML:
<div id="nav">
<a href="#">
<img src="http://www.werkenbijavl.nl/images/WerkenBijAVL/icon-email.png" heigh="24px" width="24px">
<div>
Line 1<br>
Line 2
</div>
</a>
</div>
CSS:
#nav img, #nav div{
float: left
}
This will make "Line 1", "Line 2" - and whatever lines you put in their - be always to the right of the image.
If you want text to wrap around the image, change your CSS to be:
#nav img{
float: left
}

Related

Flexible way to vertically center top line of text box beside image

Seems quite easy, but I couldn't figure out the right css balance.
What I want, is this result(orange block is image):
This is easy, if the image has fixed height and text fixed properties (line-height etc.), but I need this to work, even if I insert it into different places on my website, it needs to automatically adjust to the size of image and text properties. So if image gets bigger, or text has different size / line-height, it still works. I might omit text variations, but different-sized images need to work.
So far, I only got display: flex with align-items: center, but that works only for one line, once it breaks to new line, it centers the whole text block, which is wrong. There's gotta be some css trick to achieve this, right?
EDIT:
After another trial, I managed to come up with something better, but the text still needs to be controlled (top value of text must be: line-height / -2)
.txticon {max-width: 250px; display: block; margin-bottom: 15px; display: flex;}
.txticon::after {content: ""; clear: both; display: table;}
.txticon img {float: left; margin-right: 10px;}
.txticon span {line-height: 20px; top: -10px; position: relative; overflow: hidden; transform: translateY(50%);}
<a class="txticon" href="#">
<img class="icon" src="https://via.placeholder.com/100x100" alt="">
<span>One line text</span>
</a>
<a class="txticon" href="#">
<img class="icon" src="https://via.placeholder.com/100x100" alt="">
<span>Multiple line text which is really really really long</span>
</a>
<div class="row">
<img />
<p>Text</p>
</div>
.row {
display: flex;
align-items: center;
}
This might work.
Would a margin-top on the text block work?
<div class="row">
<img />
<p class="text">Text</p>
</div>
.row {
display: flex;
align-items: flex-start;
}
.text {
margin-top: 10px; // replace with half of the image height
}

clickable <li> with background-image:

I have some images inside of an <li> that I am wanting to make a link. Only the bottom half of the image is clickable. I have tried different structures and looked into jquery solutions, but want to keep it pure css.
<div class="link">
<ul>
<li>
<a href="url">
<img src="image">
</a>
</li>
</ul>
</div>
.link {
white-space: nowrap;
text-align:center;
}
.link li {
max-width: 23.3%;
display: inline-block;
text-align: center;
vertical-align: middle;
}
.link img {
max-width:100%;
}
Add display:inline-block to the a link and make it wrap around the images, with height:100% and width:100%.
I found a deeper problem. My header was overlapping that area.

CSS - Margins on div

I am trying to align vertical some text in a div here:
HTML:
<div class="div1">
<div class="div11">
<img src="https://pbs.twimg.com/media/BrJSq87IMAAD3Zg.png" alt="" width="40px">
</div>
<div class="div12">
yo
</br>
yo
</div>
</div>
CSS:
.div1 {
height: 40px;
}
.div1 div {
display: inline-block;
}
.div12 {
padding-top: 5px;
}
JSFiddle:
http://jsfiddle.net/4mVPG/1/
The height of my divs is fixed so all I want to do is set up a padding-top on the last div (.div12) to move the text down a bit. However when I add a padding-top all the box are brought down.
Why is this happening and how can I fix this? Thanks
When you use inline-block, each block acts as if it where text that gets alined (by default) on the baseline. If you increase the height of the second block, the baseline goes down, and the first block as well.
You can use vertical-align: top to change this.
Just replace CSS with this:
.div1 {
height: 40px;
}
.div1 > div {
float: left;
}
.div12 {
padding: 0 0 0 3px;
}
The div1 div selector also applies to all descendants so using > only applies to first one. Also if container is height fixed i think it's better to use float.
JSFidle
Hope it helps!
You can use ".div1 div {display: inline-block;vertical-align: top;}" and there is no need for .div12 { margin-top: 30px; } since you want both aligned top and no extra space on top of "Text div"
Also, just for a better practice, avoid using "<br>" tag, there are many "Block" tag like, <div>, <p> etc. and if there are list of "links" use "<ul><li>" and you can control the margins and spaces with CSS.
Here is the updated code :
HTML
<div class="div1">
<div class="div11">
<img src="https://pbs.twimg.com/media/BrJSq87IMAAD3Zg.png" alt="" width="40px"/>
</div>
<div class="div12">
<ul>
<li>yo
</li>
<li> yo
</li>
</ul>
</div>
</div>
CSS
.div1 {
height: 40px;
}
.div1 div {
display: inline-block;
vertical-align: top;
}
.div12 ul,
.div12 li{
margin:0;
padding:0;
list-style:none;
}
Working Fiddle for the same: http://jsfiddle.net/PK3UU/
Hope this is helpful!!!
Is it what you want ? http://jsfiddle.net/4mVPG/4/ i cleaned up css a little bit
div {
height: 40px;
display: inline-block;
vertical-align:middle;
}
and for html
<div class="div11">
<img src="http://placekitten.com/40/40" alt="" width="40px" />
</div>
<div class="div12">
yo
<br />
yo
</div>

center images with text underneath in a nav

I want to make a nav bar using images but still have text under each image, yet I also want the text to be centered.
I have the following HTML
<div class="whatwedo-wrapper" id="whatwedo">
<div class="whatwedo">
<ul>
<li>
<img class="services" src="images/Brand-Online-Circle-Blue.png" width="200px"
onmouseover="this.src='images/Brand-Online-Circle-Grey.png'"
onmouseout="this.src='images/Brand-Online-Circle-Blue.png'">
<br>
Brand Online
</li>
<li>
<img class="services" src="images/Brand-Marketing-Circle-Blue.png" width="200px"
onmouseover="this.src='images/Brand-Marketing-Circle-Grey.png'"
onmouseout="this.src='images/Brand-Marketing-Circle-Blue.png'">
<br>
Brand Management
</li>
</ul>
</div>
</div>
and I have made what I have a jsfiddle here - http://jsfiddle.net/B7sdp/
The overall outcome I am looking for is for the images to be centered and the text to be underneath its image, also centred.
Thanks in advance!
You should add text-align:center property to your li and replace float:left with display:inline-block
.whatwedo li {
width: 200px;
display:inline-block;
list-style: none;
text-align:center;
}
See example here : http://jsfiddle.net/B7sdp/1/
You need to add text-align: center; to .whatwedo li
.whatwedo li {
width: 200px;
margin: 0 auto;
float: left;
text-align: center;
list-style: none;
}
JSFiddle

Inline elements with wrapping text, not floated though and vertical aligned

I have a set of links which are stored within an unordered list. Inside each list item is:
The link which surrounds everything inside
An image on the left
Some text next to the image
An arrow next to the text to continue
The text could be any length, meaning the overall link could be any height. More importantly, the width of the overall link must be 100% (it's for a mobile phone so could be portrait / landscape).
To ensure that I do not need to set widths I have tried to avoid unnecessary containers, simply setting the image to be vertical align. The problem comes in that if my text is too long it wraps but appears below the image (makes sense just hoped it wouldn't), see here: http://jsfiddle.net/mP2fr/9/ (you may need to resize your browser window to see the effects). My solution was to put the text inside a and set to inline-block, but these defaults a width of 100% pushing it below the image. I can set a width which will bring it back inline (and allow for multiple lines) but this involves setting the width, see here: http://jsfiddle.net/mP2fr/7/
HTML 1:
<ul>
<li>
<a href="#">
<img src="http://www.mtlettings.co.uk/images/uploads/properties/191-9163_IMG_thumb.JPG" width="400" />
hello world asd ad asdasdg
</a>
</li>
</ul>
CSS 1:
li {
background: red;
}
a {
padding-right: 50px;
display: block;
background: url('http://www.perfectgetaways.co.uk/admin/system/preview/img_thumb_29706_125x125') no-repeat right center;
}
a img {
display: inline-block;
vertical-align: middle;
}
HTML 2:
<ul>
<li>
<a href="#">
<img src="http://www.mtlettings.co.uk/images/uploads/properties/191-9163_IMG_thumb.JPG" width="400" />
<span>
hello world asd ad asdasdg
</span>
</a>
</li>
</ul>
CSS 2:
li {
background: red;
}
a {
padding-right: 50px;
display: block;
background: url('http://www.perfectgetaways.co.uk/admin/system/preview/img_thumb_29706_125x125') no-repeat right center;
}
a img {
display: inline-block;
vertical-align: middle;
}
a span {
display: inline-block;
vertical-align: middle;
width: 160px;
background: aqua;
}
Got a solution, inspired by one of Anagio's comments. Involves setting the img and span to display as table-cells and the outer a tag to a display as a table. This positions the stuff side by side and allows for the text in the span tag to push down over multiple lines. Not great IE support as it breaks below IE8 but for my particular scenario which is a mobile app I'm satisfied. Here's the final code: http://jsfiddle.net/chricholson/xW3qr/2/
HTML:
<ul>
<li>
<a href="#">
<img src="http://www.mtlettings.co.uk/images/uploads/properties/191-9163_IMG_thumb.JPG" />
<span>
hello world asd ad asdasdg asdf asdf asdf asdfasdf
</span>
</a>
</li>
</ul>
CSS:
li {
background: red url('http://www.perfectgetaways.co.uk/admin/system/preview/img_thumb_29706_125x125') no-repeat right center;
}
a {
display: table;
}
a img {
margin: 0;
padding: 0;
display: table-cell;
vertical-align: middle;
width: 300px;
}
a span {
padding-right: 60px;
display: table-cell;
vertical-align: middle;
}