Keep placeholder of Input CSS only - css

I have an input field with a placeholder. On focus of it, I need a transition of placeholder and it should stay at a different position.
I cannot modify the HTML as it's getting generated from a plugin.
I have achieved the transition, but the placeholder disappears after it.
Need it by CSS-only.
Demo
.container {
margin-top: 80px;
}
input {
width: 500px;
font-size: 16px;
}
input::-webkit-input-placeholder {
-webkit-transition: all 0.3s ease, font-size 0.3s ease;
transition: all 0.3s ease, font-size 0.3s ease;
}
input:hover::-webkit-input-placeholder {
-webkit-transition: all 0.3s ease, font-size 0.3s ease;
transition: all 0.3s ease, font-size 0.3s ease;
}
input:focus::-webkit-input-placeholder {
-webkit-transform: translateY(-40px);
transform: translateY(-40px);
-webkit-transition: all 0.4s ease, font-size 0.4s ease;
font-size: 12px;
transition: all 0.4s ease, font-size 0.4s ease;
}
<h3>Todo: Make placeholder persistent</h3>
<div class="container">
<input type="text" placeholder="This placeholder should stay" />
</div>

Since we cannot change the HTML we will have to use a pseudo-element...and a new pseudo-class to target the .container when the input receives :focus
:focus-within
The :focus-within CSS pseudo-class represents an element that has received focus or contains an element that has received focus. In other words, it represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus
.container {
margin-top: 80px;
position: relative;
}
.container::before {
content: "This placeholder should stay";
position: absolute;
font-size: 16px;
color: grey;
top: 0;
opacity: 0;
}
.container:focus-within::before {
font-size: 12px;
top: -100%;
opacity: 1;
transition: all 0.4s ease;
}
input {
width: 500px;
font-size: 16px;
}
input:focus::-webkit-input-placeholder {
opacity: 0
}
<h3>Todo: Make placeholder persistent</h3>
<div class="container">
<input type="text" placeholder="This placeholder should stay" />
</div>

You need to achieve this effect with the inputs <label>. You can't achieve it with a placeholder as this is how placeholders work, you can't not hide them when there is focus.
If you position: absolute the <label> over the input then you can transform: translateY() it to achieve the same effect you have there.
Edit: To add this should provide you slightly better UX and accessibility by doing this with a label instead of relying on the placeholder as well.
This is also a very simple version of what you want to achieve: http://plnkr.co/edit/WCdSajNRyM4mj4GBXCHh?p=preview

Related

CSS Transition on page load

I want to replicate the effect of the that you see in the pictures here: http://www.akqa.com/work/
I thought this was the code necessary for it but it doesn't work. What is missing?
div {
opacity .4s,transform .4s
}
There are three things wrong here.
Firstly opacity .4s,transform .4s is not a valid CSS declaration.
The correct syntax looks like this:
div {
-webkit-transition: opacity .4s ease .4s;
transition: opacity .4s ease .4s;
}
Secondly, a transition rule implies that there are different values for the first and second instance (a point A and point B if you will). In the example below, you will notice that I have specified opacity:0; unless the div has a class .showing in which case it now has a rule that states opacity:1;
div {
opacity: 0;
-webkit-transition: opacity .4s ease .4s;
transition: opacity .4s ease .4s;
}
div.showing {
opacity: 1;
}
Lastly, you will also require something to change the state of the div to "let it know it needs to change it's opacity". We already told it in the CSS above that when it has a class .showing it's opacity is different.
A nice way to do this is to add a tiny jQuery script to give it the new class once the page has fully loaded.
jQuery(window).load(function(){
$('div').addClass('showing');
});
Are you focus on the text popup effect after mouse over the image? If yes, i did some trace from the html and css file.
<article class="work-item in-view" ...>
<picture>
<source></source>
<source></source>
<source></source>
<img></img>
<div class=content>
/* pop up text content*/
</div>
</picture>
</article>
.work-item {
background-color: #000;
cursor: pointer;
float: left;
overflow: hidden;
position: relative;
width: 100%
}
.work-item .content {
background-color: rgba(0,0,0,0);
bottom: 0;
color: #FFF;
height: 100%;
left: 0;
padding: 0 30px;
pointer-events: none;
position: absolute;
right: 0;
top: 0;
-webkit-transition: background-color .4s;
transition: background-color .4s;
width: 100%
}
I hope this findings may help you.
If the direction is correct, you can grep 'work-item' and 'content' from the css and follow the logic.

CSS3 Scale Flicker in Safari

I am experiencing an issue with CSS3 scaling and Safari (v10.0.1).
I have a selection of grid items with the following structure:
<div class="grid-inline col-12 bp1-col-6 bp3-col-3 index-grid-selector index">
<a href="">
<div class="index-grid-item">
<div class="index-grid-dummy"></div>
<img srcset=", 2x" title="" alt="">
</div>
</a>
</div>
CSS (LESS):
.index-grid-selector {
a {
.index-grid-item {
position: relative;
overflow: hidden;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backface-visibility: hidden;
-o-backface-visibility: hidden;
backface-visibility: hidden;
.index-grid-dummy {
padding-bottom: 100%;
}
img {
position: absolute;
top: 0;
left: 0;
-webkit-transition: all 0.25s ease-in-out;
-moz-transition: all 0.25s ease-in-out;
-ms-transition: all 0.25s ease-in-out;
-o-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
}
}
}
&:hover {
.index-grid-item {
img {
transform: scale(1.2);
-webkit-transition: all 0.25s ease-in-out;
-moz-transition: all 0.25s ease-in-out;
-ms-transition: all 0.25s ease-in-out;
-o-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
}
}
}
}
The image is positioned absolutely within the grid item container and then scaled on hover.
During the image scaling in Safari, the image enlarges slightly and appears to shift. Gaps (white lines) appear around the image. Once the animation is complete, the image sits correctly, until the hover function is removed.
I have set up a working demo here to showcase the issue.
http://www.testdomainone.co.uk/test.html
I have tried applying the folowing to the image and its parent, but the issue still occurs.
-webkit-transform-style: preserve-3d;
-webkit-transform:translate3d(0,0,0);
-webkit-transform: translateZ(0)
Does anyone know how this can be fixed?
This does not solve your specific issue using an img tag, though since you use fixed size for your images, why not use background-image
Updated with image-set to achieve the equivalent for background images as srcset does for img ... and since it is more or less only iOS Retina which use that high resolution, it will be a decent fallback for browser not supporting it
.index-grid-item {
position: relative;
overflow: hidden;
width: 200px;
height: 200px;
}
.index-grid-dummy {
padding-bottom: 100%;
background-size: 100%;
transition: transform 0.25s ease-in-out;
}
.index-grid-item:hover > div {
transform: scale(1.2);
transition: transform 0.25s ease-in-out;
}
<div class="index-grid-item">
<div class="index-grid-dummy" style="background-image: -webkit-image-set( url(http://www.testdomainone.co.uk/images/uploads/tours/39/jmc_5233-bigsur__thumb_large.jpg) 1x, url(http://www.testdomainone.co.uk/images/uploads/tours/39/jmc_5233-bigsur__thumb_large_2x.jpg) 2x );
background-image: url(http://www.testdomainone.co.uk/images/uploads/tours/39/jmc_5233-bigsur__thumb_large.jpg)"></div>
</div>
Side note, in above sample I use inline style to set the image source in the markup, as one does with the img tag, and of course this could be set in the CSS as well

anchor tag with absolutely positioned element causes the background image to move on hover in Chrome

I have an anchor tag with an image and an absolutely position span tag which appears on hover. The problem is that when the anchor tag is hovered over and the span is shown, the image in the anchor tag moves slightly in Google Chrome. All other browsers it works fine.
The HTML is:
<a href="{{link}}">
<span></span>
<img src="{{image}}" />
</a>
The CSS is:
#instafeed a {
width: 33.33333333333333%;
float: left;
position: relative;
}
#instafeed a > span {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
opacity: 0;
color: #FFF;
text-align: center;
background: rgba(0, 0, 0, 0.7);
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
#instafeed a:hover > span {
opacity: 1;
}
How can I stop the image from moving/resizing in Chrome?
You can see the problem here: http://dev.aaronpitts.ch/cj/ (the photos on the left hand side).
Many thanks

CSS hover transition cross-browser compatibility

I have a simple language select page with pure CSS animated transitions. I've made a jsFiddle here.
How it's supposed to behave is as follows:
User mouses over one of two (or more) language selectors.
That language selector transitions upward and comes to full opacity. The relevant language text (e.g., English, Español) appears as well.
The user either clicks on the link or mouses out, in which case the transition reverses.
In Chrome, it behaves as expected.
In Firefox, when I mouse over one image, both move up.
In Opera, it behaves mostly as expected, but the text jumps back down after moving up.
I'm trying to understand why this would happen in these browsers, and how I can fix it, if possible.
In the case that jsFiddle is down, the relevant code is:
HTML
<div id="container"><div id="cell">
<div class="langcell"><a href="en/index.html">
<img src="http://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/200px-Flag_of_the_United_States.svg.png" /><br/><p>English</p></a>
</div>
<div class="langcell"><a href="es/index.html">
<img src="http://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/200px-Flag_of_Spain.svg.png" /><br/><p>Español</p></a>
</div>
</div></div>
CSS
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#container {
width: 100%;
height: 100%;
display: table;
}
#cell {
display: table-cell; vertical-align: middle; text-align: center;
}
.langcell {
display: inline-block;
margin: auto 1em;
}
a {
position: relative;
top: 0;
-webkit-transition: top 0.25s;
-moz-transition: top 0.25s;
-o-transition: top 0.25s;
transition: top 0.25s;
color: black;
text-decoration: none;
}
a:hover {
top: -16pt;
}
a p {
font-size: 14pt;
font-weight: bold;
text-transform: uppercase;
font-family: Verdana, Geneva, sans-serif;
letter-spacing: 0.05em;
opacity: 0;
-webkit-transition: opacity 0.25s;
-moz-transition: opacity 0.25s;
-o-transition: opacity 0.25s;
transition: opacity 0.25s;
}
a:hover p {
opacity: 1;
}
a img {
opacity: 0.65;
-webkit-transition: opacity 0.25s;
-moz-transition: opacity 0.25s;
-o-transition: opacity 0.25s;
transition: opacity 0.25s;
}
a:hover img {
opacity: 1;
}
I got weird problems on firefox(v12) as well, where it was moving both elements up on hover. Later versions (19v), it seemed resolved.
I think there was something going on with your selectors and how mozilla interprets things versus webkit. See if this jsfiddle works for you.
All I really did was change a lot of the selectors of a to .langcell and it seem to work. I had to re-adjust a bit of css to achieve the same style, like the nested .langcell a selector. I have a suspicion that it may be due to a being inline by default while p is block and img is inline-block.
I won't lie and say I understand fully why that was happening to begin with, but just in general, giving styles to classes over elements is not just a preference, it is more efficient at render time as well.
CSS Selector Performance
Code:
.langcell {
display: inline-block;
margin: auto 1em;
position: relative;
top: 0;
-webkit-transition: top 0.25s;
-moz-transition: top 0.25s;
-o-transition: top 0.25s;
transition: top 0.25s;
}
.langcell a {
color: black;
text-decoration: none;
}
.langcell:hover {
top: -16pt;
}
.langcell p {
font-size: 14pt;
font-weight: bold;
text-transform: uppercase;
font-family: Verdana, Geneva, sans-serif;
letter-spacing: 0.05em;
opacity: 0;
-webkit-transition: opacity 0.25s;
-moz-transition: opacity 0.25s;
-o-transition: opacity 0.25s;
transition: opacity 0.25s;
}
.langcell:hover p {
opacity: 1;
}
.langcell img {
opacity: 0.65;
-webkit-transition: opacity 0.25s;
-moz-transition: opacity 0.25s;
-o-transition: opacity 0.25s;
transition: opacity 0.25s;
}
langcell:hover img {
opacity: 1;
}
CSS3 is pretty new. And many of the features are still not compatible in many browsers. Compatibility Chart
So it is kind of off-putting if your clients have a bit older browsers (even if they have a year old version), in which case CSS3 transition wont work.
Your safest bet to make the transition is to do it using javascript or some javascript library such as jQuery

Using CSS transitions to reveal more digits in a number

In HTML we have a number like 12.34 When someone hovers their mouse over it I'd like it to expand with some sort of transition to reveal additional significant digits, for example 12.345678.
Is this possible with just CSS and what would be the simplest way? (e.g. I probably don't want to use a fixed width div with overflow hidden, which then expands, because the width could be variable with numbers like 123.45, 1,234.56 etc.) So I'm thinking have two divs one rounded, one with all digits - but then we'd need some way to transition between them smoothly. Thanks!
I agree with Marc B - there are too many possible variations to determine the dot position to use just one field without resorting to using JS. However this is my solution, with two fields
http://jsfiddle.net/chrisdanek/mSjsj/1/
<span class="num">
<span class="abbr">123.45</span>
<span class="full">123.4567</span>
</span>
<span class="num">
<span class="abbr">123,345,567.45</span>
<span class="full">123,345,567.45000</span>
</span>
<span class="num">
<span class="abbr">123,345,567.455634434</span>
<span class="full">short one</span>
</span><!-- this one is not possible with numbers, but just to show how it works with shorter second number -->
And CSS
.num {
position: relative;
display: inline-block;
padding: 5px;
border: 1px solid red;
-webkit-transition: width 0.2s;
-moz-transition: width 0.2s;
-o-transition: width 0.2s;
transition: width 0.2s;
}
.abbr {
position: relative;
top: 0;
left: 0;
display: block;
opacity: 1;
z-index: 1;
-webkit-transition: opacity 0.2s;
-moz-transition: opacity 0.2s;
-o-transition: opacity 0.2s;
transition: opacity 0.2s;
}
.full {
top: 5px; left: 5px;
position: absolute;
display: block;
z-index: 0;
opacity: 0;
-webkit-transition: opacity 0.2s;
-moz-transition: opacity 0.2s;
-o-transition: opacity 0.2s;
transition: opacity 0.2s;
color: red;
}
.num:hover .abbr { position: absolute; top: 5px; left: 5px; opacity: 0; z-index: 0; }
.num:hover .full { position: relative; top: 0; left: 0; opacity: 1; z-index: 1; }
The best I could come up with is opacity change, as it doesn’t require setting width on the container. Without that it’s impossible to make a transition for width (you’ll notice transition code is added, but it’s not being executed). Perhaps someone else can come up with a workaround.
I couldn't get the opacity transitions to work cleanly on our side for some reason. But this is a much simpler version in case it helps someone: (in SASS)
.num {
display: inline-block;
.abbr { display: block; }
.full { display: none; }
&:hover {
.abbr { display: none; }
.full { display: block; }
}
}

Resources