How to write a caption under an image? - css

I have two images that need to kept inline; I want to write a caption under each image.
<center>
<a href="http://example.com/hello">
<img src="hello.png" width="100px" height="100px">
</a>
<a href="http://example.com/hi">
<img src="hi.png" width="100px" height="100px">
</a>
</center>
How can I implement?

Figure and Figcaption tags:
<figure>
<img src='image.jpg' alt='missing' />
<figcaption>Caption goes here</figcaption>
</figure>
Gotta love HTML5.
See sample
#container {
text-align: center;
}
a, figure {
display: inline-block;
}
figcaption {
margin: 10px 0 0 0;
font-variant: small-caps;
font-family: Arial;
font-weight: bold;
color: #bb3333;
}
figure {
padding: 5px;
}
img:hover {
transform: scale(1.1);
-ms-transform: scale(1.1);
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
-o-transform: scale(1.1);
}
img {
transition: transform 0.2s;
-webkit-transition: -webkit-transform 0.2s;
-moz-transition: -moz-transform 0.2s;
-o-transition: -o-transform 0.2s;
}
<div id="container">
<a href="#">
<figure>
<img src="http://lorempixel.com/100/100/nature/1/" width="100px" height="100px" />
<figcaption>First image</figcaption>
</figure>
</a>
<a href="#">
<figure>
<img src="http://lorempixel.com/100/100/nature/2/" width="100px" height="100px" />
<figcaption>Second image</figcaption>
</figure>
</a>
</div>

CSS
#images{
text-align:center;
margin:50px auto;
}
#images a{
margin:0px 20px;
display:inline-block;
text-decoration:none;
color:black;
}
HTML
<div id="images">
<a href="http://xyz.com/hello">
<img src="hello.png" width="100px" height="100px">
<div class="caption">Caption 1</div>
</a>
<a href="http://xyz.com/hi">
<img src="hi.png" width="100px" height="100px">
<div class="caption">Caption 2</div>
</a>
</div>​
​A fiddle is here.

For responsive images. You can add the picture and source tags within the figure tag.
<figure>
<picture>
<source media="(min-width: 750px)" srcset="images/image_2x.jpg"/>
<source media="(min-width: 500px)" srcset="images/image.jpg" />
<img src="images.jpg" alt="An image">
</picture>
<figcaption>Caption goes here</figcaption>
</figure>

Put the image — let's say it's width is 140px — inside of a link:
<a><img src='image link' style='width: 140px'></a>
Next, put the caption in a and give it a width less than your image, while centering it:
<a>
<img src='image link' style='width: 140px'>
<div style='width: 130px; text-align: center;'>I just love to visit this most beautiful place in all the world.</div>
</a>
Next, in the link tag, style the link so that it no longer looks like a link. You can give it any color you want, but just remove any text decoration your links may carry.
<a style='text-decoration: none; color: orange;'>
<img src='image link' style='width: 140px'>
<div style='width: 130px; text-align: center;'>I just love to visit this most beautiful place in all the world.</div>
</a>
I wrapped the image with it's caption in a link so that no text could push the caption out of the way: The caption is tied to the picture by the link. Here's an example: http://www.alphaeducational.com/p/okay.html

<div style="margin: 0 auto; text-align: center; overflow: hidden;">
<div style="float: left;">
<img src="hello.png" width="100px" height="100px">
caption 1
</div>
<div style="float: left;">
<img src="hi.png" width="100px" height="100px">
caption 2
</div>
</div>

CSS is your friend; there is no need for the center tag (not to mention it is quite depreciated) nor the excessive non-breaking spaces. Here is a simple example:
CSS
.images {
text-align:center;
}
.images img {
width:100px;
height:100px;
}
.images div {
width:100px;
text-align:center;
}
.images div span {
display:block;
}
.margin_right {
margin-right:50px;
}
.float {
float:left;
}
.clear {
clear:both;
height:0;
width:0;
}
HTML
<div class="images">
<div class="float margin_right">
<img src="hello.png" width="100px" height="100px" />
<span>This is some text</span>
</div>
<div class="float">
<img src="hi.png" width="100px" height="100px" />
<span>And some more text</span>
</div>
<span class="clear"></span>
</div>

To be more semantically correct and answer the OPs orginal question about aligning them side by side I would use this:
HTML
<div class="items">
<figure>
<img src="hello.png" width="100px" height="100px">
<figcaption>Caption 1</figcaption>
</figure>
<figure>
<img src="hi.png" width="100px" height="100px">
<figcaption>Caption 2</figcaption>
</figure></div>
CSS
.items{
text-align:center;
margin:50px auto;}
.items figure{
margin:0px 20px;
display:inline-block;
text-decoration:none;
color:black;}
https://jsfiddle.net/c7borg/jLzc6h72/3/

The <figcaption> tag in HTML5 allows you to enter text to your image for example:
<figcaption>
Your text here
</figcaption>.
You can then use CSS to position the text where it should be on the image.

<table>
<tr><td><img ...><td><img ...>
<tr><td>caption1<td>caption2
</table>
Style as desired.

Related

CSS effect two elements

I'm trying to affect two elements, the event-thumbnail img:hover will effect also .event-date .
when I'm trying to do something like .event-date+.event-date
its effect only on the .event-date.
.event-list .event-thumbnail img{
display: block;
transition: 0.8s;
}
.event-list .event-thumbnail img:hover +.event-date {
transform: scale(1.1,1.1);
}
.event-thumbnail .event-date{
position: absolute;
left: 0;
top: 0;
background:#07A2DD;
color:#FFF;
width: 60px;
text-align: center;
padding: 10px 0;
line-height: 1;
font-weight: 600;
}
<div id="latest-events">
<h1>events</h1>
<div class="event-list clearfix">
<figure class="event-thumbnail">
<a href="http://sd.esy.es/event2/">
<img src="http://sd.esy.es/wp-content/uploads/2016/10/1-135x100.png" alt="2">
<div class="event-date">
<span class="event-date-day">10</span>
<span class="event-date-month">oc</span>
</div>
</a>
</figure>
<div class="event-detail">
<h4 class="event-title">
2
</h4>
<div class="event-excerpt">
text
</div>
</div>
</div>
<div class="event-list clearfix">
<figure class="event-thumbnail">
<a href="http://sd.esy.es/event3/">
<img src="http://sd.esy.es/wp-content/uploads/2016/10/1-135x100.png" alt=" 3">
<div class="event-date">
<span class="event-date-day">10</span>
<span class="event-date-month">oc</span>
</div>
</a>
</figure>
<div class="event-detail">
<h4 class="event-title">
3
</h4>
<div class="event-excerpt">
text text text text text text text text text text text text text text text text
</div>
</div>
</div>
<div class="event-list clearfix">
<figure class="event-thumbnail">
<a href="http://sd.esy.es/event1/">
<img src="http://sd.esy.es/wp-content/uploads/2016/10/1-135x100.png" alt=" 1">
<div class="event-date">
<span class="event-date-day">10</span>
<span class="event-date-month">feb</span>
</div>
</a>
</figure>
<div class="event-detail">
<h4 class="event-title">
1
</h4>
<div class="event-excerpt">
some text </div>
</div>
</div>
</div>
Try this :
.event-thumbnail img,
.event-thumbnail img + .event-date {
transition: 0.8s;
}
.event-thumbnail img:hover,
.event-thumbnail img:hover + .event-date {
transform: scale(1.1, 1.1);
}
The same properties can be applied to multiple selectors at once by separating the selectors with a comma, as in the following:
selectorA,
selectorB,
selectorC {
property: value;
property: value;
property: value;
}
It should work fine, see my example.
div:hover + p {
background: #000;
color: #fff;
...
}
Maybe the transform: scale(1.1,1.1); doesn't work? Try to set primitive styles to make sure your selector works right. For example background: red;.

css div tag formatting errors appear

the first problem is that there is a gap between my banner and my nav bar even though I don't have anything seperating them
the next problem I have is that my nav bar should extend blue until the end of the page (or 970px in my case )but instead it only has blue for each li element
and thirdly the div tags don't seem to be doing their job right since when I expand my page I get the next div tag moving up to the right of this one.
my css code
#button {
padding: 3px;
}
#button li {
display: inline;
}
#button li a {
font-family: Arial;
font-size:14px;
text-decoration: none;
float:left;
padding: 10px;
background-color: #4169E1;
color: #fff;
}
#button li a:hover {
background-color: #E30800;
margin-top:-2px;
padding-bottom:12px;
}
and here is my html
<body leftmargin="50px" rightmargin="50px">
<img src="banner.jpg" width="970" height="120" />
<div>
<ul id="button">
<li>Home</li>
<li>Contact </li>
<li>Resumé</li>
<li>Help</li>
</ul>
</div>
<div>
<table width="970" cellpadding="5px">
<tr height="270">
<td width="700"><div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider"> <img src="demo/images/nemo.jpg" alt="" /> <img src="demo/images/up.jpg" alt="" title="#htmlcaption" /> <img src="demo/images/toystory.jpg" alt="" title="This is an example of a caption" /> <img src="demo/images/walle.jpg" alt="" /> </div>
</div>
<div id="htmlcaption" class="nivo-html-caption"> <strong>This</strong> is an example of a <em>HTML</em> caption with a link. </div></td>
<td width = "270"><img src="demo/images/nahum-pachenik.jpg"></td>
</tr>
<table>
</div>
<hr color="#4169E1"/>
<script type="text/javascript">
$(window).load(function() {
$('#slider').nivoSlider();
});
</script>
</body>
jsFiddle: http://jsfiddle.net/Hjc2e/
html,ul{
/*clear any default margin or padding, research css reset*/
/*this is how you get the <ul> to line up flush with your image*/
margin:0;
padding:0;
}
body{
/*width 100% for content and you can control you width with the left and right margins of body*/
margin-left:50px;
margin-right:50px;
}
#button li {
background-color: #4169E1;
float:left;
list-style-type:none;
text-align:center;
/*Must calculate width of menu based on width of list items in menu, 4 items thus 25%*/
width:25%;
height:40px;
/* so that text lines up vertically center padding-top is always half the height*/
padding-top:20px;
}
#button li a {
font-family: Arial;
font-size:14px;
text-decoration: none;
color: #fff;
}
#button li a:hover {
background-color: #E30800;
margin-top:-2px;
padding-bottom:5px;
}
<!--no need for div wrapper and it was adding gap between banner image and menu-->
<ul id="button">
<li>Home</li>
<li>Contact </li>
<li>Resumé</li>
<li>Help</li>
</ul>
<div>
<table width="970" cellpadding="5px">
<tr height="270">
<td width="700"><div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider"> <img src="demo/images/nemo.jpg" alt="" /> <img src="demo/images/up.jpg" alt="" title="#htmlcaption" /> <img src="demo/images/toystory.jpg" alt="" title="This is an example of a caption" /> <img src="demo/images/walle.jpg" alt="" /> </div>
</div>
<div id="htmlcaption" class="nivo-html-caption"> <strong>This</strong> is an example of a <em>HTML</em> caption with a link. </div></td>
<td width = "270"><img src="demo/images/nahum-pachenik.jpg"></td>
</tr>
<table>
</div>
<hr color="#4169E1"/>
<script type="text/javascript">
$(window).load(function() {
$('#slider').nivoSlider();
});
</script>
</body>

100% width footer in 960 grid

I have a footer and am wanting it to be 100% in width using the 960 grid system, all is fine I can it to work within a div tag using an id. but the grid floats all the way to the left of the page while the whole site using the grid is centered on the page. I have tried prefix_2 to give a push over to the right but it doesn't line up correctly.
Here is my code to start with.
<div id="footer" class="container_12"><img class="prefix_6" id="abs" align="right" src="#img" width="500" height="258" />
<br /><br />
<div class="grid_2"><p>Home</p><p>Services</p><p>Plans</p></div>
<div class="grid_2"><p>Pricing</p><p>Design</p><p>Logos</p></div>
<div class="grid_2 suffix_6"><p>Call Tool Free:</p><p>1-800-495-5933</p><p>Contact Us</p></div>
<div class="grid_6"><img src="#img" width="16" height="16" />Follow me on Twitter <img src="#img" width="16" height="16" />Become a Fan on Facebook</div>
</div>
css:
#footer {
background-color: #f0e9d8;
font-family: Verdana, Geneva, sans-serif;
font-size: 14px;
color: #6e2500;
font-weight: bold;
height: 250px;
}
#abs {
position: absolute;
clip: rect(auto,auto,500px,auto);
}
If I understand you correctly, you want that footer would be 100% width? That's a easy fix.
You just need an another wrapper around your footer code. I would do it like this:
<div id="footer">
<div class="container_12">
<img class="prefix_6" id="abs" align="right" src="#img" width="500" height="258" />
<br /><br />
<div class="grid_2"><p>Home</p><p>Services</p><p>Plans</p></div>
<div class="grid_2"><p>Pricing</p><p>Design</p><p>Logos</p></div>
<div class="grid_2 suffix_6"><p>Call Tool Free:</p><p>1-800-495-5933</p><p>Contact Us</p></div>
<div class="grid_6"><img src="#img" width="16" height="16" />Follow me on Twitter <img src="#img" width="16" height="16" />Become a Fan on Facebook</div>
</div>
</div>
And the css for that would be:
#footer {
background-color: #f0e9d8;
font-family: Verdana, Geneva, sans-serif;
font-size: 14px;
color: #6e2500;
font-weight: bold;
height: 250px;
width: 100%;
position: relative;
}
#abs {
position: absolute;
clip: rect(auto,auto,500px,auto);
}
If you want the footer's width to be 100% just remove it from the 960 Grid System.
<html>
<body>
<header>
Header
</header>
<section class="container_12">
<!--960 grid-->
</section>
<footer class="footer-main">
<!--img-->
</footer>
</body>
</html>
css
.footer-main {
width:100%;
}

Issue with cropped images for an image gallery

I have an image gallery. The thumbnails are 240px in width, but the heights vary. I tried cropping the images so that they are all 150 pixels high.
At the moment I've got this:
HTML
<div data-category="category1">
<a href="image1.jpg" rel="lightbox[on]" title="">
<img src="image1.jpg" width="" height="" />
</a></div>
<div data-category="category1">
<a href="image2.jpg" rel="lightbox[on]" title="">
<img src="image2.jpg" width="" height="" />
</a></div>
<div data-category="category1">
<a href="image3.jpg" rel="lightbox[on]" title="">
<img src="image3.jpg" width="" height="" />
</a></div>
<div data-category="category2">
<a href="image4.jpg" rel="lightbox[on]" title="">
<img src="image4.jpg" width="" height="" />
</a></div>
<div data-category="category2">
<a href="image5.jpg" rel="lightbox[on]" title="">
<img src="image5.jpg" width="" height="" />
</a></div>
<div data-category="category2">
<a href="image6.jpg" rel="lightbox[on]" title="">
<img src="image6.jpg" width="" height="" />
</a></div>
CSS
.gallery {
position:relative;
}
.gallery > div {
position:absolute;
}
.gallery > div > a > img {
position:absolute;
top: 0;
left: 0;
clip: rect(0px,240px,150px,0px);
overflow: hidden;
border:none;
}
This works with cropping the images to 150px, but only the images on the top row are aligned. The images on the other rows are not aligned because they are being placed directly below the original heights of the images on the row above. (The cropping doesn't get rid of the rest of the image. The rest of the image is not visible, but it's still there,). What do I need to add to my CSS to sort this out?
Surely your code is a bit more complex of what you paste here, but please take in consideration this base css:
.gallery { position: relative; }
.gallery > div { float: left; }
.gallery > div > a { max-height: 150px; width: 240px; overflow: hidden; display: inline-block; }
.gallery > div > a > img { border: none; }
Here a working example:
http://jsfiddle.net/3W7Xt/
Regards

Hover image on top of another image

I'm trying to add some hover effect on some images I got.
<div id="horizontal_list">
<div class="grid_3 first overlay">
<a href="#">
<img border="0" alt="first" src="path">
</a>
</div>
<div class="grid_3 overlay">
<a href="#">
<img border="0" alt="first" src="path">
</a>
</div>
</div>
When I hover the div with the overlay class I want another image to hover on top of the imagetag..
I've got the following css:
#horizontal_list{
margin-top: 18px;
height: 330px;
}
.grid_3{
display: inline;
float: left;
margin-left: 10px;
margin-right: 10px;
}
.first{
margin-left: 0px !important;
}
.last{
margin-right: 0px !important;
}
.overlay{
height: 330px;
}
.overlay:hover{
background: url('path') no-repeat;
}
I'm not sure what you mean by 'imagetag' but here is what I think you mean:
What you can do, is adding the second image in your html:
<div id="horizontal_list">
<div class="grid_3 first overlay">
<a href="#">
<img class="first_img" border="0" alt="first" src="path">
<img class="sec_img" border="0" alt="second" src="path">
</a>
</div>
<div class="grid_3 overlay">
<a href="#">
<img class="first_img" border="0" alt="first" src="path">
<img class="sec_img" border="0" alt="second" src="path">
</a>
</div>
</div>
And in your CSS:
.overlay {position: relative;}
.overlay:hover a img.sec_img {display: none;}
.overlay:hover a img.sec_img {
display: block;
position: absolute;
background: url(path) no-repeat;
top: 0;
left: 0;
width: [imgwidth]px;
height: [imgheight]px;
}
Be sure to change [imgwidth] and [imgheight] into the correct dimensions.
HTML
<div class="hoverholder">
<a href="#" class="picone">
<img src="#" alt="" />
</a>
<a href="#" class="pictwo">
<img src="#" alt="" />
</a>
</div>
CSS
.pictwo {display:none;}
.hoverholder:hover .picone{
display:none;
}
.hoverholder:hover .pictwo{
display:block;
}

Resources