terça-feira, 25 de novembro de 2014

10 Simple & Smart CSS Snippets

CSS is a underlying denunciation that gives websites a look. Although CSS is a candid denunciation and easy to learn, it can be formidable to strap in some cases. Nothing to fear, there are workarounds that we can find online, and here are though usually 10 accessible ones that we can use.



If we wish to hang prolonged text, autoadjust a breadth of your list columns, or emanate a elementary loading state though a use of Gifs, we have a snippets that will deliver, and more.



1. Vertical Align Anything


If we work with CSS, afterwards this will bug you: how do we align content or an component plumb of a container? Now, with a use of CSS3 Transforms, we can residence this problem some-more elegantly, like this:



.verticalcenter
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-o-transform: translateY(-50%);
transform: translateY(-50%);

Using this technique, all — from a singular line of text, a array of paragraphs, or a box — will align vertically. As distant as browser support is concerned, CSS3 Transform works in Chrome 4, Opera 10, Safari 3, Firefox 3, and Internet Explorer 9.


2. Stretch an Element To Full Window Height


In certain scenarios, we competence wish to widen an component to a full window height. Basic component resizing will usually resize adult to a enclosure distance so to make an component camber a tallness of a whole window height, we need to camber a top-most element: html and body.



html,
body
height: 100%;

Then request 100% tallness to any element, like so:



div
height: 100%;

3. Applying Different Styles Based on File Format


Sometimes we can have several links that we wish to make demeanour opposite from others, in sequence to make it easier to know where a couple is going. This dash next will supplement an idol before a couple content and use opposite icons or images for opposite kinds of sources, that in this instance is an outmost link.



a[href^="http://"]
padding-right: 20px;
background: url(external.gif) no-repeat core right;

/* emails */
a[href^="mailto:"]
padding-right: 20px;
background: url(email.png) no-repeat core right;


/* pdfs */
a[href$=".pdf"]
padding-right: 20px;
background: url(pdf.png) no-repeat core right;

Here’s what it looks like.



4. Cross-Browser Image Grayscale


Grayscale can broach a deeper tinge to your website creation it demeanour some-more grand and infrequently minimalistic. Here, we will be adding a grayscale filter to an picture regulating SVG. Here’s what we do to request grayscale:



svg xmlns="http://www.w3.org/2000/svg"
filter id="grayscale"
feColorMatrix type="matrix" values="0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0"/
/filter
/svg

And to broach this cross-browser, we use a filter skill this way:



img
filter: url(filters.svg#grayscale); /* Firefox 3.5+ */
filter: gray; /* IE6-9 */
-webkit-filter: grayscale(1); /* Google Chrome, Safari 6+ Opera 15+ */

5. Animating A Gradient Background


One of a many interesting underline in CSS is a ability to supplement animation effect. You can spur credentials color, opacity, size, though unfortuantely not for Gradient Color. Currently, we can’t spur a slope credentials however this dash might be of some help. It moves a credentials postion to make it demeanour as if it is animating.



button
background-image: linear-gradient(#5187c4, #1c2f45);
background-size: automobile 200%;
background-position: 0 100%;
transition: background-position 0.5s;

button:hover
background-position: 0 0;

Here’s a demo to uncover we what it does.



6. CSS Table Column Autowidth


Tables are a pain generally when it comes to adjusting a widths of columns. However, there is a by-pass we can use. Add white-space: nowrap in a td component to simply scold a content wrapping.



td
white-space: nowrap;


Check out a demo to review a result.



7. Showing Box Shadow Only on One or Two Sides


If we wish to have box shadows, try this pretence that can give we box shadows on possibly side of a box. To make this, initial conclude a box with a specific breadth and height. Give it a shade regulating :after pseudo component and play around to get a right positioning. This is a formula to make a bottom-only shadow:



.box-shadow
background-color:
width: 160px;
height: 90px;
margin-top: -45px;
margin-left: -80px;
position: absolute;
top: 50%;
left: 50%;

.box-shadow:after
content: "";
width: 150px;
height: 1px;
margin-top: 88px;
margin-left: -75px;
display: block;
position: absolute;
left: 50%;
z-index: -1;
-webkit-box-shadow: 0px 0px 8px 2px
-moz-box-shadow: 0px 0px 8px 2px
box-shadow: 0px 0px 8px 2px

This is a demo:



8. Wrapping Long Text Context


If we ever come opposite a word that is longer than a enclosure itself, this pretence will be useful to you. By default, a content will fill horizontally regardless of a breadth of a enclosure for example:



With elementary CSS formula we can make a content adjust a breadth of a container.



pre
white-space: pre-line;
word-wrap: break-word;

This is what it looks like now:



9. Making a Blurry Text


Want to spin content blurry? What we can do is make a tone transparent,t hen supplement text-shadow like this.



.blurry-text
color: transparent;
text-shadow: 0 0 5px rgba(0,0,0,0.5);

And voila, we got yourself some becloud text.



10. Animating Ellipsis Using CSS Animation


These snippets will assistance we make an animation called ellipsis, useful for creation elementary loading states instead of regulating a gif image.



.loading:after
overflow: hidden;
display: inline-block;
vertical-align: bottom;
animation: ellipsis 2s infinite;
content: "2026"; /* ascii formula for a ellipsis impression */

@keyframes ellipsis
from
width: 2px;

to
width: 15px;


Lets see a demo.



Do play with a snippets and examination with what some-more we can do with it.




10 Simple & Smart CSS Snippets

Nenhum comentário:

Postar um comentário