How to make a liquid, centered, image with a caption overlay? - css

I'm trying with CSS to display an image centered with a caption overlay, and a liquid capability when the browser window is too small (shrink to fit).
My current best attempt looks like the following HTML (using Google's logo as sample image)
<div align="center">
<div class="container">
<img src="http://www.google.fr/images/logos/ps_logo2.png" class="picture" />
<h3 class="caption">Image Caption</h3>
</div>
</div>
with the following CSS
.container {
position : relative;
max-width : 364px;
}
.picture {
border-radius:0.5em;
box-shadow: 0px 0px 0.5em #000000;
max-width:364px;
}
.caption {
position:absolute;
padding:0.25em;
top:1em; left:0; right:0;
color:black;
background-color:rgba(0,0,0,0.2);
text-align:center;
}
However, if it behaves centered as I want it to be for large browser windows, it doesn't shrink for small browser windows...
Also I don't need IE support, a WebKit-specific (iPhone/Android) would be enough, and I would like to avoid JavaScript if possible.
JSFiddle ready-to play with version http://jsfiddle.net/kWH3C/1/

Just set the max-width to the container instead of the image and tell the image to be width:100%
http://jsfiddle.net/Madmartigan/kWH3C/5/
<div class="container">
<img src="http://www.google.fr/images/logos/ps_logo2.png" class="picture" />
<h3 class="caption">Image Caption</h3>
</div>
.container {
position : relative;
max-width : 364px;
margin:0 auto;
}
.picture {
border-radius:0.5em;
box-shadow: 0px 0px 0.5em #000000;
width:100%;
}
.caption {
position:absolute;
padding:0.25em;
top:1em; left:0; right:0;
color:black;
background-color:rgba(0,0,0,0.2);
text-align:center;
}
You don't need the outer div, and align="center" is deprecated in HTML5, use CSS for alignment and positioning.

Related

How to build a responsive layered image with content below?

I've tested creating a layered image using a png with transparency over a jpg and using absolute positioning for the top image, however that upsets the positioning of content that should appear below the image. So then I tried without absolute positioning as described by Overlay Divs Without Absolute Position. This works for wider screens where the image width is 100%, but on smaller screens when the image width is scaled down the top image rides up because of the fixed negative top margin.
Is there anyway to achieve a responsive layered image with content positioned correctly below without having to set the negative top margin using media queries? I've created a test at
https://jsfiddle.net/6v0Ls54o/3/
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#productimage {
background: #fff;
max-width:486px;
text-align:left;
padding:0 20px 0 20px;
margin:0;
height:auto;
}
#layeredImage{
margin:20px 0 40px 0;
width:100%;
height:auto;
float:left
}
#layeredImage img{
width:100%;
height:auto;
}
#topImage{
margin-top:-300px;
float:left;
position:relative;
overflow:visible;
z-index:1
}
#belowImage{
float:left
}
.clear{
clear:both;
font-size:0;
line-height:0;
height:0;
overflow:hidden;
margin:0
}
<div id="productimage">
<div class="titleWrapper">
<h1>How to build a responsive layered image?</h1>
</div>
<div id="layeredImage">
<img src="https://picsum.photos/486/300" alt="" width="486" height="300" id="bottomImage" />
<img src="https://picsum.photos/486/300" alt="Top Image" title="Top Image" width="486" height="300" id="topImage" />
<div class="clear"></div>
</div>
<div id="belowImage"><h2>Some info to appear below the image:</h2>
<p>
Is there a way to build a responsive layered image where content can appear below the layered image?
</p></div>
<div class="clear"></div>
</div>
If I understand it correct, you want to overlap 2 images on top of each other?
I would use position: relative on #layeredImage and position: absolute on both img like this:
#layeredImage {
position: relative;
}
img {
position: absolute;
left: 0
right: 0
}
Using this code, both images will overlap withi their parent div.

vertical align middle not working

I want the text in front of the image to be in the middle of it!
My problem is the vertical align middle is not working...
what is wrong?
<div class="comments">
<div class="pull-left lh-fix">
<img class=foto src="/$foto" class="imgborder">
</div>
<div class="comment-text pull-left">
<span class="pull-left color strong">anna:</span> dododod
</div>
</div>
.pull-left { float: left; }
.lh-fix { line-height: 0 !important; }
.comments {
position:relative;
display:block;
overflow:auto;
padding-left:15px;
padding-top:8px;
padding-bottom:8px;
border:1px solid #000;
}
.comment-text {
margin-left: 8px;
color: #333;
vertical-align:middle; //not working?
line-height:normal;
width: 85%;
text-align:left;
}
.foto{
width:50px;
height:50px;
float:left;
}
https://jsfiddle.net/a0bhv4n1/
Vertical-align works on inline elements. You are applying it to the class .comment-text which is for a div element. A div is a block style element which of course means that it will take up the entire space that it is allowed to, thus you cannot center something that already takes up the whole space. Inline elements only take up the space they need to based on the content in them and you can simply add display:inline-block to .comment-text to allow vertical-align:middle to work. More information can be found at MDN's article on vertical-align

Incorrect container height based on the image inside it

As you can see in the attached pic, the .grid-item div doesn't have the exact height of the content inside it (in my case the <img>), but it seems to add a few pixel on the bottom.
How can I tell the .grid-item div to use the exact height of the image inside it?
Ideally if it's possible I'd like to keep absolute and relative positioning of the divs like it is in the example below.
HTML structure:
<section>
<div class="grid-item">
<div class="item-content">
<div class="caption">
<!-- .... -->
</div>
<img src="http://placehold.it/700x400/" alt="" />
</div>
</div>
</section>
CSS:
*{
box-sizing:border-box;
margin:0;
}
body{
font: 16px/1.5em sans-serif;
}
img{
width:100%;
height:auto;
position:relative;
}
section{
max-width:960px;
margin:0 auto;
}
section .grid-item{
width:50%;
float:left;
padding:20px;
}
section .grid-item .item-content{
position:relative;
width:100%;
overflow:hidden;
}
section .grid-item .item-content .caption{
position:absolute;
width:100%;
height:100%;
background:#333;
transform:translateX(-50%);
z-index:2;
}
-
FULL CODE:
http://codepen.io/anon/pen/zrydZX?editors=1100
Add display:block to your img rule: http://codepen.io/anon/pen/qbLXxd?editors=1100
Images are by default inline allowing you to vertically align them in blocks of text. The key disadvantages are that this then results in them also being affected by line height, font size, kerning, etc.

Issue with Div Centering

I have a div with the following CSS:
.glyphicon-arrow-right {
font-size:25px;
margin-top:22.66%;
margin-left:5%;
color: #414141;
}
set inside a div with CSS
.forwardbutton {
width:60px;
height:60px;
border-radius:50%;
margin: 0 auto;
border: 1.75px solid #414141;
opacity: .3;
text-decoration:none;
}
When I resize on a computer screen, the arrow sits directly in the center. However, when I view the div on other devices (tablet, netbook/ phone) the arrow is not centered, with a height noticeable higher than it should be.
I was wondering if anyone could identify the issue. Thanks in advance.
EDIT: HTML
<a href="work.html">
<div class="forwardbutton">
<div class="front glyphicon glyphicon-arrow-right"></div>
</div>
</a>

Hide page content behind transparent header

I have a fixed navbar using curved ribbon images that have transparent bits above and below the actual ribbon and I have a scaling full size background (so I can't make a navbar with a matching background at the top). I would like the page content to disappear behind the ribbon, halfway through the navbar as the user is scrolling.
It's the same problem as these two questions and the answers (which are good) aren't working for me.
Hide scrollable content behind transparent fixed position divs when scrolling the page?
Hide Scrolling Content Under Transparent Header
This is what I don't want:
http://imageshack.us/photo/my-images/213/badnr.jpg/
This is kind of what I want but without the scrollbars:
http://imageshack.us/photo/my-images/534/scrolled.jpg/
Thanks in advance for any help, it's greatly appreciated, this site has and will continue to teach me a lot.
The css z-index attribute should do the trick to place any element in front of or behind another element. Like so:
<style type="text/css">
body, html {
margin:0 auto;
padding:0;
font-family:Verdana, Geneva, sans-serif;
font-size:12px;
}
/* Header Styling */
#header {
color:#FFF;
background: url(/images/header-back.png) repeat-x;
position:fixed;
top:0;
left:0;
width:100%;
height:50px;
z-index:1;
}
#headerWrap {
width:1024px;
margin:0 auto;
height:50px;
}
/* Sub Header */
#subHeader {
position:fixed;
top:50px;
margin:0 auto;
z-index:1;
}
#subHeaderWrap {
height:30px;
width:830px;
border-bottom:thin solid #333;
background: url(../images/subheader.png) repeat-x;
}
/* Contaier */
#container {
margin:0 auto;
width:1024px;
min-height:600px;
}
#containerWrap {
margin-top:50px;
}
/* Menu */
#sidebar {
float:left;
width:140px;
min-height:600px;
}
#content {
border-left:#333333 solid thin;
border-right:#333333 solid thin;
border-bottom:#333333 solid thin;
float:left;
width:830px;
min-height:600px;
padding-top:30px;
margin-bottom:10px;
}
#contentWrap {
width:830px;
margin:0 auto;
padding-bottom:10px;
}
</style>
<div id="container">
<div id="header" style="z-index:1;"/* Places div on top */">
This is transparent.
</div>
<div id="containerWrap">
<div id="sidebar">
Menu Items Here
</div>
<div id="content">
<div id="contentWrap">
<div id="subHeader" style="z-index:1;"/* Places div on top */">
<div id="subHeaderWrap">
This div is transparent too, but is now on top.
</div>
</div>
Anything here is scrollable and will scroll under the divs above with z-index:1;
</div>
</div>
I have found the solution you're looking for.
You're going to use a little Jquery and some CSS. I will assume you're loading the latest version of Jquery in your footer.
The header will be fixed, the elements inside it will be absolute. We will not focus on elements inside the header because that really doesn't matter for this, but if you were to put a menu and logo in the header you would make them absolute.
HTML Div with class header assigned or if you prefer you could just create a <header></header> element, whichever. But for this example we will use a class.
<div class="header">...Your Header Elements In this...</div>
CSS
body {background: url('../img/page-background.jpg') no-repeat top center fixed; background-size: cover;}
.header {position: fixed; top: 0; left: 0; width: 100%; height: 100px; background: transparent;}
JS - I use a seperate JS file and then load this after I've loaded Jquery in the footer.
$(window).scroll(function() {
"use strict";
var windowYmax = 1;
var scrolledY = $(window).scrollTop();
if (scrolledY > windowYmax) {
$('.header').addClass("hide-content");
} else {
$('.header').removeClass("hide-content");
}
});
Add this CSS for new class assigned:
.hide-content {background: transparent url('../img/page-background.jpg') no-repeat top center fixed; background-size: cover;}
Here is a JSfiddle: The Fiddle
I was not able to get the JS to work in JSfiddle for some reason, maybe someone can fix that issue, but I don't really have the time to mess with JSfiddle much, but wanted to provide an example to the end result. So I just added the class that gets assigned by the JS to the div in the HTML and you can see the result in the preview pane.

Resources