CSS hovering with images, text, and z-index - html

So I've been trying to recreate the hover disappear/re-appearing image effect found on oudolf.com with just CSS.
I've gotten this far: https://jsfiddle.net/cj5781ug/ but I can't figure out how to style the z-index so that div.text remains above everything else so I can hover from one text to another without having to leave the entire image.
Or, I was able to set it up so that other text (h3) will not be seen above the image but you won't be able to select other text until you leave the image. Seen here: https://jsfiddle.net/ogjh96rb/
I know Javascript will make my life a lot easier but I want to practice my CSS and try to do as much with CSS before I learn Javascript.

You didnt put the h3 in the text div
change it so that
<div class="text"></div>
<h3>Chicago</h3>
is
<div class="text">
<h3>Chicago</h3>
</div>
https://jsfiddle.net/9c9ye9sk/ I changed it just for chicago to show you
Also put all of the city names in the same z index because not all of the names are above the images for some.

If you want to know how that website is doing it, here it is:
The z-indexes aren't changing...the opacities are. Initially, the text and image are visible but the opacities are 0. The text you see is really an svg underneath with the same image and text knocked out. When you hover over the DIV containing those three things (text, img, svg), the text and image opacities are set to 1.
Here is a working example of that concept using part of your example markup.
The key piece to making the svg text align with the real text is the text x and y positioning. Example: <text x="168" y="217" id="knockout" fill="white">Chicago</text> I estimated it, you'll want to make it accurate.
https://jsfiddle.net/jbmy9s9m/4/
<div class="container">
<div class="words" id="p1">
<h3>Chicago</h3>
<img class="hover-pics" src="http://i.imgur.com/XV7wrRI.jpg" width="600" height="402" alt="picture 1"/>
<svg version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg" class="svgMask1" width="600" height="402" viewBox="0 0 600 402"><defs><mask id="maskID0"><text x="168" y="217" id="knockout" fill="white">Chicago</text></mask></defs><title>Chicago</title><desc>Chicago</desc>
<image style="mask:url(#maskID0);" width="600" height="402" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://i.imgur.com/XV7wrRI.jpg"></image></svg>
</div>
<div class="words" id="p2">
<h3>Cambridge</h3>
<img class="hover-pics" src="http://i.imgur.com/R1zVKKL.jpg" width="600" height="400" alt="picture 2"/>
<svg version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg" class="svgMask2" width="600" height="400" viewBox="0 0 600 400"><defs><mask id="maskID0"><text x="125" y="225" id="knockout" fill="white">Cambridge</text></mask></defs><title>Chicago</title><desc>Chicago</desc>
<image style="mask:url(#maskID0);" width="600" height="400" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://i.imgur.com/R1zVKKL.jpg"></image></svg>
</div>
</div><!--END OF CONTAINER-->

It's right here. (sample & no JS)
<!DOCTYPE html>
<html>
<head>
<style>
div {position: absolute;}
#container div {
background-color: lightblue;
border: 1px solid #333333;
width: 100px;
height: 100px;
opacity: 0.5;
}
div#myBox {
opacity: 1;
background-color: coral;
z-index: 1;
-webkit-animation: mymove 5s infinite linear; /* Chrome, Safari, Opera */
animation: mymove 5s infinite linear;
}
/* Chrome, Safari, Opera */
#-webkit-keyframes mymove {
50% {z-index: 5;}
}
/* Standard syntax */
#keyframes mymove {
50% {z-index: 5;}
}
</style>
</head>
<body style="position:absolute">
<p>The z-index property is <em>animatable</em> in CSS.</p>
<p><strong>Note:</strong> CSS Animations do not work in Internet Explorer 9 and earlier versions.</p>
<p>Gradually change the z-index property of "myBox" from 1, to 5, and back to 1:<p>
<div id="container">
<div id="myBox">myBox</div>
<div style="top:20px;left:20px;z-index:1;">z-index 1</div>
<div style="top:40px;left:40px;z-index:2;">z-index 2</div>
<div style="top:60px;left:60px;z-index:3;">z-index 3</div>
<div style="top:80px;left:80px;z-index:4;">z-index 4</div>
</div>
</body>
</html>
Live Demo / Source (via W3C).

Related

Problem rendering SVGs on safari with html5

I'll admit right off the bat i'm very new to SVG graphics in html. That said i'm attempting to upgrade my site images to SVG's where appropriate.
I started with this code:
<svg class="logo">
<image
class="logo"
xlink:href="https://cdn.badmonsterarts.com/main_logo.svg"
src="https://cdn.badmonsterarts.com/main_logo.png"
/>
</svg>
Which works fine in chrome and firefox, however when tested on Safari(Both mobile and desktop) it rendered as a blank rectangle. That said I did some research and tried some stack overflow answers which brings us to my current code:
<svg
class="logo"
viewBox="0 0 256 75"
xmlns="http://www.w3.org/2000/svg"
role="img"
>
<image
class="logo"
xlink:href="https://cdn.badmonsterarts.com/main_logo.svg"
src="https://cdn.badmonsterarts.com/main_logo.png"
/>
</svg>
The problem however is this still works in chrome and firefox, but I still can't get it to render in Safari.
Here's the CSS i'm using to size it, logo wrapper is a div surrounding the SVG as a warpper:
.logo-wrapper {
width: 256px;
height: 75px;
overflow: hidden;
.logo {
width: auto;
height: 100%;
}
}
I've also tried using <use ... /> instead of <image ... /> with no luck either, when I used <use ... /> it didn't even render in chrome. I'm hoping one of your brilliant minds can lead me in the right direction and save my sanity.
If it helps anyone debug this, there's a link to the site that the SVG is being used on(The logo in the top left on the nav bar).
https://www.badmonsterarts.com/
Thanks!
If you set the image width and height to 100% of the viewBox it ought to work (I took the viewBox values from the external SVG).
By only setting the viewBox you make the SVG responsive – why you can leave out the logo class
.logo-wrapper {
width: 256px;
height: 75px;
overflow: hidden;
}
<div class="logo-wrapper">
<svg viewBox="0 0 679 200">
<image width="100%" height="100%" xlink:href="https://cdn.badmonsterarts.com/main_logo.svg" />
</svg>
</div>

HTML & CSS working with images of different shapes

As some of you guys may know allowing users to upload images can be a hassle and especially if you have to create some sort of list with them.
I have been looking all over the web and have been unable to find concrete answers to what you do in the case where you need to show a list of images of different shapes. Therefor i turn to you.
Say User 1 uploads the following image:
And User 2 uploads this image:
As you can see these two images are very different in both height and width.
Now lets say that you have 10 images of different sizes and wish to display them in a grid 4 by 4 (for this purpose i use ng-repeat to show a loop)
<div class="col-xs-4" ng-repeat="image in images">
<img alt="" ng-src="{{image}}">
</div>
if you do this, this will create a list that is uneven! and will look very "ugly" to say the least.
So my question is what do you do? Are there any tricks using CSS to make it fit any images of any size so that everything is aligned?
I hope my description of the problem was accurate enough for the sake of demonstration here is a fiddle that shows this issue as well.
In short how do i make sure they are all the same size without making one of the images look cramped and / or distorting the individual image?
fiddle
As mentioned in my comment, one option is to crop all the images to a suitable format, a square might be a good compromise. You can do this by wrapping your images in a container first, and positioning the image in relation to the container. Example:
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
.image-container {
width: 150px;
height: 150px;
overflow: hidden;
position: relative;
padding: 20px;
}
.image-container img {
width: 100%;
height: auto;
position: absolute;
margin: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-xs-12">
<div class="col-xs-4">
<div class="image-container"><img src="http://pngimg.com/upload/girls_PNG6448.png" width="100%" height="100%" class="image image-responsive"></div>
</div>
<div class="col-xs-4">
<div class="image-container">
<img class="image image-responsive" width="100%" height="100%" src="http://yeemei.mobile9.com/download/media/442/niceandsim_s8mhs1do.jpeg"></div>
</div>
<div class="col-xs-4">
<div class="image-container">
<img src="http://pngimg.com/upload/girls_PNG6448.png" width="100%" height="100%" class="image image-responsive"></div>
</div>
<div class="col-xs-4">
<div class="image-container">
<img class="image image-responsive" width="100%" height="100%" src="http://yeemei.mobile9.com/download/media/442/niceandsim_s8mhs1do.jpeg"></div>
</div>
</div>
</div>
Fiddle
You can also position the image in the container. For example if you wanted to center it you could add:
top: -100%;
bottom: -100%;
left: -100%;
right: -100%;
One solution is to provide the users with a cropper to your preferred ratio and allow them to select the part of the image to show.
An alternative is to use the images as background on a div with specific ratio and hope that it does not show irrelevant areas.
Here is a solution for the second case (with a - just for laughs - animation to show the whole of the image)
http://jsfiddle.net/mrccf3sv/
.image{
display:block;
background: url('') 50% 0% no-repeat;
background-size:cover;
border:1px solid #ccc;
animation:pan 10s linear infinite alternate;
}
.image:before{
content:'';
display:block;
padding-top:56.25%; /*ratio of 16:9*/
}
And see it responsive by using different bootstrap column count for each breakpoint.
http://jsfiddle.net/mrccf3sv/1
Scaling with CSS is incredibly bad practice. I mean, we all have to do it sometimes, but if you CAN scale server-side, better do that. Try PHP's imagick, if available.

Div inside SVG foreignObject loses its position in Mac Chrome

Div inside SVG foreignObject loses its position and not visible in MAC Chrome browser and mobile view.
I tried to run this HTML5 SVG code in MAC chrome (54.0.2840.98 (64-bit)); but the DIV inside the become invisible (or seems to be losing its position/jumping out from the SVG) when content in the DIV Overflows or scrollbar comes. However, it works perfectly in MAC Firefox and all browsers in Windows (except Mobile views).
Is it an issue regarding
viewport metadata?
div inside ForeignObject?
MAC chrome bug?
CSS?
How can we solve this?. Your help is much appreciated.
What I tried,
Test HTML file
https://www.dropbox.com/s/ygz6x0mu6sfhkes/testsvg.html?dl=0
Found a similar bug in Webkit forum
https://bugs.webkit.org/show_bug.cgi?id=23113
Head
<meta content="width=device-width, initial-scale=1" name="viewport" />
Body
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1600 1200">
<rect class="cls-a" x="568.13" y="103.99" width="409.76" height="350.53" />
<rect class="cls-b" x="602.86" y="159.55" width="340.31" height="350.28" rx="13.35" ry="13.35"/>
<foreignObject class="chat-outer" x="602.86" y="159.55" width="340.31" height="300.28" rx="13.35" ry="13.35">
<div xmlns="http://www.w3.org/1999/xhtml">
<div class="list-wrap" >
<div>
<div class="list-content">
<div class="list-row">
<p >Hi</p>
</div>
<div class="list-row">
<p >Hello</p>
</div>
<div class="list-row">
<p >how are you?</p>
</div>
</div>
</div>
</div>
</div>
</foreignObject>
</svg>
CSS
.list-content {
height: 280px;
padding: 0px 25px;
background: #ffccbc;
overflow: hidden;
overflow-y: auto;
}
We've just had a similar issue and managed to fix it by setting the overflow to visible on the foreignObject.

Controlling a CSS Animation with Buttons

I need to have scrolling images controlled by two arrows (scroll up and scroll down). I have a CSS "scrollUp" animation written as well as a "scrollDown" animation. How can I have these animations occur to the images when the arrows are clicked?
I think I need to have a "scrollUp" class and a "scrollDown" class applied to the images. but I am not sure how to make one arrow activate the "scrollUp" class animation and another activate the "scrollDown" animation.
I've seen people use links with href="#something" to activate an ID animation, but my images can't have multiple ID's so that will not work.
Any help would be greatly appreciated! If you know of any video tutorials that would be great too! I'm not opposed to using JavaScript but I'd prefer to use CSS.
EDIT: I basically need the following code to activate one image at a time, whenever a button is clicked:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>web browsers</title>
<style type="text/css">
body {
margin-left: 100px;
margin-top: 100px;
}
#header {
height: 256px;
width: 256px;
overflow: hidden;
border:1px solid gray;
}
.slider {
animation: myanimation 8s ease-in-out infinite alternate;
}
#keyframes myanimation
{
0% {transform:translateY(0px); }
25% {transform:translateY(-256px); }
50% {transform:translateY(-512px);}
75% {transform:translateY(-768px);}
100% {transform:translateY(-1024px);}
}
</style>
</head>
<body>
<div id="header">
<img class="slider" src="img1.png" width="256" height="256">
<img class="slider" src="img2.png" width="256" height="256">
<img class="slider" src="img3.png" width="256" height="256">
<img class="slider" src="img4.png" width="256" height="256" >
<img class="slider" src="img5.png" width="256" height="256">
</div>
</body>
</html>
It wouldn't be the best solution syntactically, but you could wrap the image scroller inside of an <input type="checkbox" id="scroll-up-control"> tag, then select against #scroll-up-control:checked to apply styles for scrolling up, with the default listed above for scrolling down.
I would use some simple javascript to just apply a class to the container with whether to scroll up or down though.
I was able to resolve this issue by changing the method I used to actually scroll the images. I decided to instead use an iFrame with a hidden scrollbar. Javascript allowed buttons to control the iFrame's scrolling. Thanks to all that helped out!

SVG scaled differently on different browsers

I need to have my svg within div's as follows:
<div style="height:100px; width: 300px; border:1px solid red;" >
<div style="width: 100%; height: 100%; display: table;">
<div style="display:table-row; height:100%">
<div style="position: relative; vertical-align: middle; height:100%;">
<div style="vertical-align: middle; position: relative; margin: 0px auto; height:100%;">
<svg viewBox="0 0 485 255" id="damageCanvas" style="vertical-align: middle;" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" version="1.1" fill="rgba(124,240,10,0.5)" height="100%" width="100%">
<g transform="translate(1,1)">
<rect stroke="black" stroke-width="1.5" width="99.5%" height="99.5%" fill-opacity="0" style="padding: 2px 2px 2px 2px"></rect>
</g>
</svg>
</div>
</div>
</div>
</div>
</div>
The problem is that the following SVG is rendered in different browsers in different ways.
Chrome:
Firefox:
IE 9:
I want the image to be scaled as in chrome version. How can i achieve this?
Fiddle: http://jsfiddle.net/Sq5bL/5/
You can save your SVG in a file and use it as a resource, via <img> or <embed>. I've used both and it scales nicely. <img> stops you from accessing the SVG, so if you need access to the SVG then I would recommend using <embed>.
In my case I did:
<embed id="gaugeSpeed" class="gaugeImage" width="200" height="200" type="image/svg+xml" src="assets/gauges/speed.svg">
and it scaled it nicely. Same for when I use SVG with the <img> tag. I've tested on Transformer Prime and Nexus 7 running both ICS and Jellybean, works fine.
Should work fine with your parent div as well, as it's just treated like a normal img or object.
Well, since you would like to keep your SVG... your problem is caused by the use of the
display: table
on the second div. If you change that to a table-cell or remove it then your problem is resolved.
This is a JSFiddle that shows it working with table-cell