What the dot before a value means in CSS? - css

I was looking at the Angular 2 tutorial and this detail in the CSS caught my attention.
What does the dot in ".1em" means or do?
.heroes li:hover {
color: #607D8B;
background-color: #DDD;
left: .1em;
}

It's short-hand for 0.1em, ie. one tenth of an em. In other words, you are not limited to whole numbers (integers).
This isn't quite as useless as it seems. CSS minimizers are becoming common and would reduce this code to
.heroes li:hover {color:#607D8B;background-color:#DDD;left:.1em;}
The leading zero before .1 is just another byte that can go away.

it mean 0.1em but some people wrote it like .1em, it is short cut, like you used in Maths.

EM is a value relative to the font-size of the element.
.1em is 10% of 1em
With no css:
1em == 16px
If the font size changes, 1em = new font-size value.

The dot actually means 0. and CSS put the number after the dot as a decimal of 1. E.g. .1 = 0.1 or .3s = 0.3s
So, the two blocks below act the same:
div {
transition: all .3s;
}
div {
transition: all 0.3s;
}
Which results 300 milliseconds or 0.3 second.

Related

CSS Transition: Opacity vs changing Background-Color?

I am curious if there is an industry standard for transitioning colour since there are multiple ways to accomplish the desired transition effect.
There are multiple ways to make a particular colour brighter or darker to indicate to the user that the element is intractable.
For example, if you have a p tag and you want to add a hover over effect you could:
HTML
<div><p> P tag text </p></div>
CSS v1 using opacity
div {
background-color:black;
}
p {
color: white;
opacity: 0.8;
transition: opacity 0.2s ease-in-out;
}
p:hover {
opacity: 1;
}
CSS v2 using colour
div {
background-color:black;
}
p {
color: #ccc;
transition: color 0.2s ease-in-out;
}
p:hover {
color: #fff;
}
As far I'm aware both methods provide similar results however, I am still curious whether or not there is a correct way to do this kind of thing.
The main thing you should realize is that opacity affects an entire element and all its descendants, whereas color and background-color do not.
In your simple example, white text with reduced opacity on a solid black background looks gray, so the visual effect is basically the same as changing the color from gray to pure white.
But in any more complicated example – say, the background of your paragraph's parent div is an image rather than a solid color, or you're using opacity on an element that contains other elements and not merely on text – what you're gonna end up with is things that truly look see-through. That also may mean that text becomes harder to read.
So the answer is less about there being any particular industry standard and more about choosing the right tool for the job. If you just want to make some text a lighter color, transition color itself. If you want to make things see-through, use opacity.
That's the theory, anyway, but in real life sometimes it's not that clean cut. Maybe a designer gives you a mockup with text that's color: #C44242; opacity: 0.87 on top of a solid-colored background that's background-color: #B48927. You could compute what the opaque version of that text color would be, but it may not be worth your time to do so. The world won't end if you just stick with opacity.

CSS color transition triggers multiple animations when loading the website

This is my first question in this forum so if it's not well explained, feel free to ask me for more details.
I have a color transition in all the links on my navbar, that triggers when you hover your mouse over them. This work wells, the problem is that when the website loads, all those elements began to resize or move to their initial positions.
CSS
nav{
height: 80px;
width: 100%;
background-color: rgba(250,250,250,1);
font-size: 13px;
border-bottom: 1px solid #D8D8D8;
color: #6E6E6E;
position: fixed;
margin-top: -80px;
}
nav a{
padding: 20px 20px;
line-height: 80px;
-webkit-transition: all 0.8s;
transition: all 0.8s;
}
nav a:hover{
color:#00BFFF;
}
UPDATE
I have tried to make a JSFiddle with the problem, but even when the CSS and HTML is exactly the same its seem to work correctly on the demo
I have changed the transition property from all to color. This has solved the problem partially, since now the elements don't move when the page loads, but the problem now is that all links that include this color transition, when the website loads, show an initial blue color (inexistent in my CSS) taking the transition time to change to the correct color. This initial blue color is similar to the visited links standard color (but I have also used the selector a:visited without positive result.
This only happens on Firefox.
As due to my low reputation I can't post images, I have taken the blue initial tone: RGB (6,6,231)
You only need animate the color:
-webkit-transition: color 0.8s;
transition: color 0.8s;
note that I change all for color only.
note 2 you can do
transition: color 0.8s, height 0.2s ease-out, opacity 1s linear 0.5s;
Try adding script tag at footer of the html page.This worked for me.
<script> </script>

CSS3 Change text opacity without affecting stroke

Have a unique issue here. I'm trying to set some text to completely opaque, while still keeping a visible stroke on the text. I'd like to be able to see through the text as the background is dynamic, so I can't simply set it to the color of its backround.
Here's what I've got, but opacity is overwriting the stroke:
#special {
opacity: 0;
-webkit-text-stroke-width: 1px;
-webkit-text-stroke-color: black;
}
Help appreciated!
The opacity affects all visual properties of the object.
But, what about this?
#special {
-webkit-text-stroke-width: 1px;
-webkit-text-stroke-color: black;
color: transparent;
}
JsFiddle: http://jsfiddle.net/fdLecLan/2/
About the opacity, the W3schools says:
The opacity-level describes the transparency-level, where 1 is not transparant at all, 0.5 is 50% see-through, and 0 is completely transparent.
So setting it to 0 will make it totally transparent, but is important to remember that it stills stay in the View port, and stills interact with user (So, if it's a button or link, it'll still be clickeable...)

Font-Weight CSS Transition in Chrome

Trying to get font-weight to gracefully transition from '400' to '600' using CSS but it doesn't appear to be working in Chrome. Is this a known bug or am I doing something wrong?
Check the Fiddle here for an example
The problem is that font weights, when represented numerically, must be a multiple of 100. To animate between 400 and 600, the font would change from 400 to 500 to 600 (3 'frames', if you like) and wouldn't look very smooth. An animation wouldn't increment the weight by 1 each time (400, 401, 402...) it would increment the weight by 100 (400, 500, 600). If your animation lasted 2 seconds, after 1 second the weight would suddenly become 500, and after 2 seconds the weight would suddenly become 600; there are no in-between variations.
A further problem with what you're attempting here is that the font you're using (or JSFiddle's default, at least) doesn't have anything different for font-weight:500, meaning it defaults to 400:
<p style="font-weight:400;">a - 400, normal</p>
<p style="font-weight:500;">a - 500 (no support, defaults to 400)</p>
<p style="font-weight:600;">a - 600 (bold)</p>
<p style="font-weight:650;">a - 650 (not valid, defaults to 400)</p>
http://jsfiddle.net/r4gDh/6/
Numeric font weights for fonts that provide more than just normal and bold. If the exact weight given is unavailable, then 600-900 use the closest available darker weight (or, if there is none, the closest available lighter weight), and 100-500 use the closest available lighter weight (or, if there is none, the closest available darker weight). This means that for fonts that provide only normal and bold, 100-500 are normal, and 600-900 are bold.
https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
This basically means that you can't smoothly animate font-weight. Even if you had support for all weights between 100 and 900 the change wouldn't be pleasant and there would be a dramatic change between 500 and 600 (where lighter weight meets darker weight).
I came here to find out the answer myself for how to transition font weight, and was disappointed when I read the approved answer above saying that it can't be done (or at least not very well).
With font-weight animation unavailable, I decided to try another effect, which actually gives you a font-weight effect... which I didn't even think would work for this type of transition.
Here is how to make the weight grow:
.weightGrow:hover {
text-shadow:
-1px -1px 0 #2DD785,
1px -1px 0 #2DD785,
-1px 1px 0 #2DD785,
1px 1px 0 #2DD785;
-webkit-transition: all .5s;
-moz-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
Perfectly smooth and exactly what I was looking for when I first arrived on this page. Hope it helps someone.
Font-weight animation is currently not supported in Chrome and IE-10 based on numerous tests. This may change in the future.
Fonts are not simple vector image collections (that's why svg fonts never took off). Opentype fonts include all kinds of magic to clamp fonts to the pixel grid, which makes it unrealistic to expect a font family to ever include every possible weight value.
Because fonts are not simple vector image collections they will also never resize linearly – there will be bumps to take the pixel grid into account.
This Google whitepaper explains why linear resizing does not work for text on current screens
https://docs.google.com/document/d/1wpzgGMqXgit6FBVaO76epnnFC_rQPdVKswrDQWyqO1M/edit
which is why no browser will attempt it and they will all rely on pre-computed font weights.
That may change in a decade when retina displays are the lowest common denominator but current font tech targets current screens.
This Microsoft whitepaper documents some standard font weight values
http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-components-postattachments/00-02-24-90-36/WPF-Font-Selection-Model.pdf
(but just because they are documented does not mean the number of fonts that actually include all of them is not quite small)
You can probably achieve the effect you want with an svg font and some javascript. The svg font will be crap for text use though.
I seem to have obtained a "font-weight" transition effect accidentally, by doing a quick color transition (light gray to dark blue) at the same time.
Although there is no direct way to get the font-weight to smoothly transition, I have found a way to do this indirectly (although with some limitations).
In essence you can simply use one of the pseudo elements in order to make a mirror copy of the element whom font you want to transition to bold, where the font-weight on the pseudo element is bold and the opacity is 0. And on hover simply transition the pseudo element's opacity and that does the trick with a very smooth transition.
You can see a demo here:
http://jsfiddle.net/r4gDh/45/
HTML:
<div class="element" data-text="text will turn to bold on hover">
text will turn to bold on hover
</div>
In the HTML you can already see one of the limitations, because we are using pseudo elements we need to pass the contents of the element as an attribute as well, so the content is duplicated.
CSS:
.element,
.element::before {
background: red;
font-weight:normal;
font-size:40px;
text-align:center;
display: inline-block;
padding: 0px 30px 0px 30px;
position: relative;
top: 0;
left: 0;
}
.element::before {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
padding: 0;
content: attr(data-text);
opacity: 0;
font-weight: bold;
transition: all 1s linear;
}
.element:hover::before {
opacity: 1;
}
The second limitation comes from the nature of the technique, namely because you are simply overlaying the pseudo element over the original, then the element needs to have a solid background or this will not work, because the original element will remain visible in the background.
One thing you should really keep an eye on is that the pseudo element and the original element have the same dimensions, to this end, in the demo, I've removed the padding on the pseudo element, so you may need to do something similar.
As you can see, this way of getting the font-weight transition is not without it's problems and far from ideal, but this is the best I can currently think of and in limited cases like for buttons and what not this is quite useful and if done correctly will look decent even in legacy browsers.
Smooth font-weight transitions and animations are possible using variable fonts which are not limited to increments of 100. More info on variable fonts here: https://web.dev/variable-fonts/
Try this snippet with eg. https://fonts.google.com/specimen/Raleway
#font-face {
font-family: 'Raleway Flex';
src: url('Raleway-VariableFont_wght.ttf');
font-weight: 100 900;
}
h1 {
font-family: 'Raleway Flex', sans-serif;
animation: example 1s linear 0s infinite alternate both;
}
#keyframes example {
from {
font-weight: 100;
}
to {
font-weight: 900;
}
}
<h1>HELLO</h1>
<p>I look way smoother if you use a variable font.</p>
I've tweaked #Idra's fiddle to make the normal to bold transition a bit smoother. Here's the fiddle: http://jsfiddle.net/y7rLwx95/
Changes... Since the text width will get wider when going to bold, I've added an initial "stretch" transition by increasing the letter spacing:
.element:hover {
letter-spacing: 0.9px;
transition: all .3s linear;
}
Then delayed the fading in of the bold ::before element:
.element:hover::before {
opacity: 1;
transition-delay: .2s
}
Also some additional tweaks here:
.element::before {
transition: all .3s linear; /* replace */
letter-spacing: 0; /* additional */
}
The transition timing is just whatever feels right to me. The original idea #Idra posted is significant to me. I accept that fact that the widths should be different between normal and bold, as that's the intent of different font weights. So really the challenge, IMHO, is to figure out how to go between the 2 looks in a smooth, no jarring way. This seems to be the best solution so far I've found.

What's the point of visibility in transition?

Please advise if i'm confused but is there any point of having a transition on visibility? Opacity creates a neat effect, sure. But the change none to block will be from nada to full immediately. Maybe we can spread its occurrence sometime in a time interval but the transition will happen all at once. Or am i mistaken?
Here's the code i'm creating. Should i keep the last three lines in the first style?
div.contentItem{
border: 2px solid #00bb00;
border-radius: 20px;
background-color: Beige;
padding: 10px;
-webkit-transition: visibility 3.0s, opacity 3.0s;
-moz-transition: visibility 3.0s, opacity 3.0s;
-o-transition: visibility 3.0s, opacity 3.0s;
}
div.contentItemHidden{
opacity: 0;
}
div.contentItemVisible{
opacity: 1;
}
Before, i had block styles in the two last clauses but that actually damaged the transition of the opacity (probably due to the fact that display: none causes the elements not to be opacitable at all).
div.contentItemHidden{
display: none;
opacity: 0;
}
div.contentItemVisible{
display: block;
opacity: 1;
}
Yes, there is a point to having a transition on visibility. If you just use opacity on an element, that element will still be there and will block clicks and hover effects on whatever is below it. With visibility: hidden, the element will no longer be visible (similar to opacity: 0), but cannot be clicked.
Here is a link that helps to explain in more detail why using visibility and opacity together may be necessary, and how to do so correctly: http://www.greywyvern.com/?post=337
As a side note, I noticed that you mention visibility in your question, but have display in your code. I'd like to note that there is a difference between visibility and display. In particular, elements visibility: hidden are not visible but still take up space. Elements with display: none are not visible but do not take up space.

Resources