I got a weird behaviour on IE8 for this
HTML:
<a class='main'>
<img src='http://annawrites.com/blog/wp-content/uploads/2011/04/color-explosion.jpg' />
<div class='layer'> </div>
</a>
CSS:
.main { display: block; position: relative; width: 100px; height: 100px; }
.main img { width: 100px; height: 100px; /*display: none;*/ }
.layer { position: absolute; top: 0px; left: 0px; width: 100%; height: 50%; cursor: crosshair; }
JSFiddle: http://jsfiddle.net/HLua8/2/ (or open this in IE8)
On IE8, .layer (ie. the crosshair cursor) is only on the top left corner (it minimizes itself to the content, which is 3 x )
I noticed it works fine when I set .main img to display:none but I need the image
Could someone help me to make .layer shown on IE8 as big as it is supposed to be? (ie. 100% width 50% height, just like on other browsers)
After much fiddling, the best solution I could come up with for you, is to create a blank/transparent image and use this as the background image for your layer.
add to your css:
.layer {background-image:url(blank.png);}
.layer:hover{cursor: crosshair;}
an updated version of your document would look as follows:
css:
#container {
margin:40px;
position: relative;
width: 100px;
height: 100px;
}
.main img {
max-width: 100%;
}
.layer {
position: absolute;
top: 0px;left: 0px;
width: 100%;height: 50%;
background-image:url(img/blank.png);
}
.layer:hover{cursor:crosshair;}
html:
<div id="container">
<a class='main' href="test.html"><img src='http://annawrites.com/blog/wp-content/uploads/2011/04/color-explosion.jpg' /></a>
<div class='layer'></div>
</div>
Related
I'm building a website with HTML and CSS. I'm trying to make 4 images contained in a white rectangle, and when those images get clicked it takes you to another part of the page.
Unfortunately, only the last of the 4 images actually takes you to the other part of the page. The other 3 can't even be clicked on. If I delete the last image and the div class it's contained in, the "new" last picture will now work even though it did not previously work before.
I'm pretty sure this is a CSS problem, as when I commented out the CSS code of the rectangle div all the images were working links (though they were jumbled up as the CSS code for their parent class was commented out).
This is the CSS code of the rectangle class and the images contained in it:
#rectangle {
background: white;
width: 1000px;
height: 500px;
position: absolute;
left: 500px;
top: 200px;
text-align: center;
}
#rectangle h1 {
font-size: 50px;
position: relative;
top: 10px;
}
#rectangle .hardware {
position: relative;
top: 50px;
right: 340px;
}
#rectangle .software {
position: relative;
bottom: 200px;
right: 100px;
}
#rectangle .config {
position: relative;
bottom: 450px;
left: 120px;
}
#rectangle .code {
position: relative;
bottom: 700px;
left: 350px;
}
<div id="rectangle">
<h1>Welcome to the docs.</h1>
<h3>Learn how to build your own Olympia</h3>
<div class="hardware">
<img src="../assets/img/hardware.png" height="200px" width="200px" />
<p><b>Hardware</b></p>
</div>
<div class="software">
<img src="../assets/img/software.png" height="200px" width="200px" />
<p><b>Software</b></p>
</div>
<div class="config">
<img src="../assets/img/gear.png" height="200px" width="200px" />
<p><b>Config</b></p>
</div>
<div class="code">
<img src="../assets/img/code.png" height="200px" width="200px" />
<p><b>Code</b></p>
</div>
</div>
Any advice? Thank you!
change
position:relative;
with
display:inline-block;
like this:
#rectangle .software {
/* position: relative;*/
display: inline-block;
bottom: 200px;
right: 100px;
}
#rectangle .config {
/* position: relative;*/
display: inline-block;
bottom: 450px;
left: 120px;
}
#rectangle .code {
/* position: relative;*/
display: inline-block;
bottom: 700px;
left: 350px;
}
Just remove text-align: center; property from #rectangle
This happens because the last div covers the others. Therefore when you hover the other boxes, you are actually hovering the div of the last box.
Usually, using the dev tools in google chrome you can see what's happening actually.
Right click -> Inspect
Is it possible to get a similar effect like background-size: contain with an img tag?
I want to display product pictures inside a fixed width/height div container, that have various dimensions (square, portrait, landscape). The pictures should
Always display the whole image, nothing should be cropped
Be aligned horizontally and vertically centered
This is, what background-size: contain does. Unfortunately I have to use an img tag (vor various reasons, going from the framework I use to SEO stuff).
In this plnkr you can see the problems and how it should look like (using background-size) http://plnkr.co/edit/k9Nv4ELoZgYCaQfVuSDQ?p=preview
Looks good
Should be centered vertically
Is cropped, but should display 100% of its height
CSS:
.product {
background-color: green;
border: 1px solid blue;
width: 200px;
height: 200px;
overflow: hidden;
margin-bottom: 20px;
}
.product img {
width: 100%;
}
Note: Somehow I can't insert HTML tags, it displays the images not the
source code. Please have a look into the plnkr.
EDIT:
This should work with CSS only, no JS.
Support for all modern browsers, including IE 10
You can do it like this by setting position: relative to the .product and position: absolute with other formatting to the .product img. Please check the fiddle below.
.product-css {
background-color: red;
border: 1px solid blue;
width: 200px;
height: 200px;
background-repeat: no-repeat;
background-size: contain;
background-position: center;
margin-bottom: 20px;
}
.product {
background-color: green;
border: 1px solid blue;
width: 200px;
height: 200px;
overflow: hidden;
margin-bottom: 20px;
position: relative;
}
.product img {
max-width: 100%;
max-height: 100%;
position: absolute;
top: -100%;
bottom: -100%;
left: -100%;
right: -100%;
margin: auto;
}
<h2>With img tag</h2> 1.
<div class="product">
<img src="http://keentype.com/post-images/wineBottles/vine-bottles-post.jpg">
</div>
2.
<div class="product">
<img src="https://0.s3.envato.com/files/149175458/wine_bottle_mockup_05.jpg">
</div>
3.
<div class="product">
<img src="http://www.designer-daily.com/wp-content/uploads/2009/02/boxhead-wine.jpg">
</div>
<hr>
<h2>Desired behaviour (with background-image)</h2>
<div class="product-css" style="background-image: url(http://keentype.com/post-images/wineBottles/vine-bottles-post.jpg)">
</div>
<div class="product-css" style="background-image: url(https://0.s3.envato.com/files/149175458/wine_bottle_mockup_05.jpg)">
</div>
<div class="product-css" style="background-image: url(http://www.designer-daily.com/wp-content/uploads/2009/02/boxhead-wine.jpg)">
</div>
Try to add this css:
.product img {
max-width: 200px;
max-height:200px;
width:auto;
height:auto;
margin: 0 auto;
display: block;
position: relative;
top: 50%;
transform: translateY(-50%);
}
I wanna make overlay background image to image. the code is here.
How can I put background image over img element?
my html is;
<div id="sidebar">
<div class="editor-select">
<img src="http://i.imgur.com/mh3noQH.jpg" alt="img"/>
</div>
</div>
my css is;
#sidebar{
width: 344px;
background: url("http://i.imgur.com/BQc7nYP.png") no-repeat top right !important;
float: left;
height: 500px;
margin-top: 45px;
padding: 8px;
position: relative;
z-index: 1000;
}
.editor-select{
width: 350px;
height: 360px;
position: relative;
}
.editor-select>img{
width: 320px;
height: 160px;
z-index : -1;
}
You can't. Your img element is contained within the #sidebar element, which effectively makes the img element on top of the #sidebar element. You cannot position a parent element on top of its child.
Pure CSS Solution
What you can do, however, is use a pseudo-element (:before or :after), positioned on top of your img element (which I've offset by 6px on all 4 sides to allow the image to overlap properly):
.editor-select:after {
content: '';
position: absolute;
top: -6px;
left: -6px;
height: 172px;
width: 332px;
background: url("http://i.imgur.com/BQc7nYP.png") no-repeat top right
}
CodePen Demo.
You can't have a background for a given element above the element contents .... You'll need to make some adjustments to your markup.
I forked your pen: http://codepen.io/anon/pen/ipmDv
This is your new HTML:
<div id="sidebar">
<div class="overlay"></div>
<img src="http://i.imgur.com/mh3noQH.jpg" alt="resim"/>
</div>
And your new CSS:
#sidebar{
width: 344px;
float: left;
height: 500px;
margin-top: 45px;
padding: 8px;
position: relative;
z-index: 1000;
}
.overlay{
width: 116px;
height: 116px;
position: absolute;
z-index:1000;
background: url("http://i.imgur.com/BQc7nYP.png") no-repeat top right !important;
top:0px;
right:25px;
}
I need an image to be resized to fit in inside a div. This div must, necessarely, no matter what, be an position: absolute; div. Apart from the image have 100% from its greatest dimension, it should be centered in the other way.
I could resize to fit it, but can't center. I tried to make it inline and use vertical-align, but it didn't work.
Since code worth more than words, check my fiddle example.
This is the code from the jsfiddle:
CSS:
.relative {
width: 400px;
height: 400px;
position: relative;
<!-- Next is not important, only to display better -->
display: block;
background-color: green;
border: 3px solid yellow;
margin-bottom: 10px;
}
.absolute {
position: absolute;
top: 20px;
bottom: 20px;
left: 20px;
right: 20px;
background-color: red;
}
img {
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
}
HTML:
<div class="relative">
<div class="absolute">
<img src="http://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg"/>
</div>
</div>
<div class="relative">
<div class="absolute">
<img src="http://us.123rf.com/400wm/400/400/pashok/pashok1101/pashok110100126/8578310-vertical-shot-of-cute-red-cat.jpg"/>
</div>
</div>
you may put the image to background instead of an img tag.
<div class="absolute">
<img src="//upload.wikimedia.org/wikipedia/commons/5/52/Spacer.gif">
</div>
.absolute {
background-image: url(http://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
however, if you can set a fixed height for the div, you can use this:
.absolute { line-height:360px; }
.absolute img { vertical-align:middle; }
Only for semi-new browsers:
img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Absolutely position all the things!
transform still needs browser prefixes I hear. -webkit- works for me.
http://jsfiddle.net/rudiedirkx/G9Z7U/1/
Maybe I did not understand the question…
.absolute {
position: absolute;
top: 20px;
bottom: 20px;
left: 20px;
right: 20px;
background-color: red;
line-height:350px; //new
}
img {
position:relative;
display:inline-block; // new
vertical-align:middle; // new
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
}
<div id="subpageMain">
<div id="subpageHeaderImageSection">
<div id="subpageHeaderLeft">
</div>
</div>
</div>
#subpageHeaderImageSection {
position: relative;
top: 0;
left: 0;
width: 100%;
height: 400px;
}
#subpageHeaderLeft {
position: absolute;
left: 0;
top: 0;
float: left;
width: 100%;
height: 100%;
background: url('../theImages/subpageHeaderImage.png') no-repeat;
background-size: 100% 100%;
}
#subpageMain {
position: relative;
margin-left: 295px;
margin-right: 10px;
padding-bottom: 43px;
top: 50px;
}
This is what I see:
Why does IE8 take the background image and stretch it too much that the right content doesn't show and goes out of the screen?
How do I fix it?
IE8 does not support the CSS background-size attribute (see the compatibility table at MDN). To support IE8, you'll need to use an <img> element instead and set max-width: 100%; max-height: 100%;.
IE8 can't stretch the background out of the div. However, it looks like the div with id #subpageHeaderLeft is stretched to the right due to the fact that it has width:100%