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.
Related
I am building a website and I am using 2000px wide images. I want the images to be visible on wide screens (like mine - im using TV as monitor) but the smaller screens will only see the images size adequate to their size. (rest will be cropped). The problem is that the image must be centered no matter what size the viewport is. I have used text-align:center; to make the text responsive. I did read the similar topics but no matter what I do the image stays static and not centered. Please help.
HTML:
<body>
<div id="wrapper">
<div class="header">
<img src="images/design/header.png">
</div>
<div class="nav-bar">
Home
About
Portfolio
Gallery
Service
Contact
</div>
<div class="side-bar">
<h1>This is my side bar</h1>
</div>
<div class="content">
<h1>This is my content</h1>
</div>
<div class="footer">
<h1>This is my footer</h1>
</div>
</div>
</body>
CSS:
#wrapper {
max-width: 2000px;
margin: -8px;
overflow: hidden;
text-align: center;
}
One way is to use a responsive layout. Twitter Bootstrap provides the same by default.
Another way is by using #media tags. Refer http://www.w3schools.com/css/css_mediatypes.asp . Here, you will need to make a function like this: Get the screen size using mediacheck and then set up thresholds using #media.
Use the following css to make the image center. max-width:100% will fit the image if the container is smaller than the image width. the image width and height will be reduced to fit.
#wrapper > .header img
{
display: block;
height: auto;
max-width: 100%;
margin:0 auto; /* to make the image center of its container, if container is larger than image size */
}
Update
If you know the exact height of the image, then please see this fiddle. Resize fullscreen to see the image cropping.
HTML:
<div class="position-img-outer">
<div class="position-img-inner">
<img src="http://placehold.it/1000x300" class="position-img" alt="img" style=" height: 300px; " />
</div>
</div>
CSS:
.position-img-outer
{
height: 300px;
margin: 0 auto;
overflow: hidden;
}
.position-img-outer .position-img-inner
{
display: inline-block;
position: relative;
right: -50%;
}
.position-img-outer .position-img-inner .position-img
{
position: relative;
left: -50%;
}
If you know the width of the images, you can use the following:
#wrapper .header img{
position:relative;
width:2000px;
left:50%;
margin-left:-1000px;
}
UPDATE:
To avoid a horizontal Scrollbar, you can set the container element to width:100% and overflow:hidden
#wrapper .header {
position:relative;
width:100%;
overflow:hidden;
}
ATTENTION! PROBLEM SOLVED, SOLUTION PASTED BELOW INITIAL QUESTION
on the very top of web page Im working I have on to the left corner a flag (to change language) which I have in a div. On the right I have another div for another image (shop cart) but since I floated the right div, I still go to the right divs address when clicking on the left, like the right overrides the let one. Why? How can I solve this?
Also, I am doing this by using my html/css-files and editing them to fit wordpress for a customer.
CSS
#topmenu img {
margin-left: 25px;
float:left;
position:relative;
}
#cartmenu img {
position:relative;
margin-left: 542px;
}
header.php
<div id="container">
<div id="topmenu">
<img src="wp-content/themes/blank/images/icon_en_global.png" alt="English.png" width="42" height="30">
</div>
<div id="cartmenu">
<img src="wp-content/themes/blank/images/cart.png" alt="cart.png" height="" width="">
</div>
// SOLUTION Set width (in css) to both elements as well a float:left to both elements, then position with margins to get them where you want.
#topmenu img {
margin-left: 25px;
float:left;
position:relative;
width: 42px;
height:45;
margin-top:15px;
margin-bottom:-10px;
}
#cartmenu img {
position:relative;
margin-left: 520px;
float left;
width:350px;
height:40px;
margin-top:-20px;
}
Try to define a width for #topmenu img and giving the cartmenu a float:left; as well.
I have two divs in my page: leftpart and rightpart which has the following css:
.leftpart{
width:280px;
background:#0054a6;
color:#fff;
padding-top:40px;
height:100%;
min-height:637px;
box-shadow:3px 3px 10px #bebebe;
position:relative;
}
.rightpart{
width:75%;
padding-left:10px;
}
I want this sidebar(leftpart) till the end of my page(till the bottom). I've set the height to 100% but when I minimize the browser it shows the white space below the bar instead of showing blue background. I mean it does not take its height as 100%. How can I get that?
For a full length sidebar your best bet is probably the old faux columns method. You could do this in CSS but this is probably easier for you.
Put basically you want an image with your column background's in a thin long strip. You then add this as a background image to your parent div and it acts as pretend full height columns.
eg.
.container {
background: url(your_image) repeat-y left top;
}
<div class="container">
<div class="sidebar">SIDEBAR</div>
<div class="content">CONTENT</div>
</div>
You can read more about it here - http://www.alistapart.com/articles/fauxcolumns/
If you want to try this in CSS you could try the negative margins trick.
You set your container up with overflow set to hidden, then on each div add negative margin-bottom and equal positive padding-bottom.
#container { overflow: hidden; }
#container div { float: left; background: #ccc; width: 200px; margin-bottom: -2000px; padding-bottom: 2000px; }
#container .col2 { background: #eee; }
<div id="container">
<div>
SIDEBAR
</div>
<div class="col2">
CONTENT
</div>
</div>
So yet another question about vertically aligning an image within a div, but I think mine is different than the others I've found on here. I can't seem to find a solution that works for my situation.
I have a DIV that is 100% width (to it's container, which is floating left and has a set pixel width) and has a set pixel height. I have an image inside that I am positioning absolute to get it to the background of content within the DIV. The image is fluid with a width of 100%.
All works well, but I want to get the image to vertically align to the middle of the container and height is unknown.
Here is some sample code that shows what I'm trying to do:
<div class="container">
<div class="image-wrapper">
<img src="http://farm5.staticflickr.com/4111/4968056789_d872094672_o.jpg"
width="100%" />
</div>
<p>Some text</p>
</div>
And some sample CSS:
.container {
width:100%;
margin-top:10px;
height:100px;
overflow:hidden;
}
.image-wrapper {
position: relative;
}
.image-wrapper > img {
position: absolute;
z-index: -1;
}
p {
text-align: center;
padding-top: 10px;
color:#fff;
font-weight: bold;
}
But the flower should show up with it's center visible within the container div.
Any thoughts? I'm trying to avoid any Javascript sizing (the outer container, not shown in this sample, is already being sized). I'm not opposed to more DIVs, tables.. whatever you got!
A jsFiddle to demo this:
http://jsfiddle.net/JonMcL/sNz9h/
Why not go for the background-image property? That allows vertical centering...
http://jsfiddle.net/urrWS/
Assuming you want to only scale the image down and not stretch it beyond its native resolution this should do the trick. A little bit of jQuery is involved but it's minimal. Essentially, this adjusts the top-margin of the IMG on the window.resize event.
HTML
<div id="container">
<img id="image" src="image.jpg"> <!-- native size is 480x300 -->
</div>
CSS
#container {
margin: auto;
width: 80%;
height: 300px;
border: 1px solid gray;
}
#image {
display: block;
width: 100%;
max-width: 480px;
margin: auto;
}
jQuery
function adjustImage() {
$("#image").css('margin-top', ($("#container").height() - $("#image").height()) / 2);
}
$(window).load(function() {
adjustImage();
$(window).resize(function() {
adjustImage();
});
});
If I get what you need I would suggest setting the background image via css, then you can set the position correctly etc.
.container {
width:100%;
margin-top:10px;
background-image:url("http://farm5.staticflickr.com/4111/4968056789_d872094672_o.jpg");
background-repeat:no-repeat;
background-position:left center;
height:100px;
overflow:hidden;
}
http://jsfiddle.net/sNz9h/6/
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.