Chinaunix首页 | 论坛 | 博客
  • 博客访问: 138854
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-21 14:51
个人简介

天不设牢,二人自在心中舍牢。顺天而行者,心中无牢。

文章分类

全部博文(48)

文章存档

2013年(48)

我的朋友

分类: Html/Css

2013-05-25 18:55:40

原文:

1. CSS Resets

01 html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
02   margin: 0;
03   padding: 0;
04   border: 0;
05   font-size: 100%;
06   font: inherit;
07   vertical-align: baseline;
08   outline: none;
09   -webkit-box-sizing: border-box;
10   -moz-box-sizing: border-box;
11   box-sizing: border-box;
12 }
13 html { height: 101%; }
14 body { font-size: 62.5%; line-height: 1; font-family: Arial, Tahoma, sans-serif; }
15  
16 article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; }
17 ol, ul { list-style: none; }
18  
19 blockquote, q { quotes: none; }
20 blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; }
21 strong { font-weight: bold; }
22  
23 table { border-collapse: collapse; border-spacing: 0; }
24 img { border: 0; max-width: 100%; }
25  
26 p { font-size: 1.2em; line-height: 1.0em; color: #333; }

Basic CSS browser resets are some of the most common snippets you’ll find online. This is a customized snippet by myself which is based off . I have included a bit for responsive images and set all core elements to border-box, keeping margins and padding measurements aligned properly.

2. Classic CSS Clearfix

1 .clearfix:after { content: "."; display: block; clear: both; visibility: hidden; line-height: 0; height: 0; }
2 .clearfix { display: inline-block; }
3   
4 html[xmlns] .clearfix { display: block; }
5 * html .clearfix { height: 1%; }

This clearfix code has been around the Web for years circulating amongst savvy web developers. You should apply this class onto a container which holds floating elements. This will ensure any content which comes afterwards will not float but instead be pushed down and cleared.

3. 2011 Updated Clearfix

1 .clearfix:before, .container:after { content: ""; display: table; }
2 .clearfix:after { clear: both; }
3  
4 /* IE 6/7 */
5 .clearfix { zoom: 1; }

From what I can tell there isn’t a major difference in rendering between this newer version and the classic version. Both of these classes will effectively clear your floats, and they should work in all modern browsers and even legacy Internet Explorer 6-8.

4. Cross-Browser Transparency

1 .transparent {
2     filter: alpha(opacity=50); /* internet explorer */
3     -khtml-opacity: 0.5;      /* khtml, old safari */
4     -moz-opacity: 0.5;       /* mozilla, netscape */
5     opacity: 0.5;           /* fx, safari, opera */
6 }

Some of the newer CSS3 properties have pampered us into thinking they may be applied everywhere. Unfortunately opacity is one such example where CSS still requires some minor updates. Appending the filter property should handle any older versions of IE with grace.

5. CSS Blockquote Template

01 blockquote {
02     background: #f9f9f9;
03     border-left: 10px solid #ccc;
04     margin: 1.5em 10px;
05     padding: .5em 10px;
06     quotes: "\201C""\201D""\2018""\2019";
07 }
08 blockquote:before {
09     color: #ccc;
10     content: open-quote;
11     font-size: 4em;
12     line-height: .1em;
13     margin-right: .25em;
14     vertical-align: -.4em;
15 }
16 blockquote p {
17     display: inline;
18 }

Not everybody needs to use blockquotes inside their website. But I feel these are an excellent HTML element for separating quoted or repeated content within blogs or webpages. This basic chunk of CSS offers a default style for your blockquotes so they don’t appear as drab and bland.

6. Individual Rounded Corners

01 #container {
02     -webkit-border-radius: 4px 3px 6px 10px;
03     -moz-border-radius: 4px 3px 6px 10px;
04     -o-border-radius: 4px 3px 6px 10px;
05     border-radius: 4px 3px 6px 10px;
06 }
07  
08 /* alternative syntax broken into each line */
09 #container {
10     -webkit-border-top-left-radius: 4px;
11     -webkit-border-top-right-radius: 3px;
12     -webkit-border-bottom-right-radius: 6px;
13     -webkit-border-bottom-left-radius: 10px;
14      
15     -moz-border-radius-topleft: 4px;
16     -moz-border-radius-topright: 3px;
17     -moz-border-radius-bottomright: 6px;
18     -moz-border-radius-bottomleft: 10px;
19 }

Most developers are familiar with the CSS3 rounded corners syntax. But how would you go about setting different values for each of the corners? Save this code snippet and you should never run into the problem again! I’ve included both a condensed version and a longer base with each corner radius broken down into a different property.

7. General Media Queries

01 /* Smartphones (portrait and landscape) ----------- */
02 @media only screen
03 and (min-device-width : 320px) and (max-device-width : 480px) {
04   /* Styles */
05 }
06  
07 /* Smartphones (landscape) ----------- */
08 @media only screen and (min-width : 321px) {
09   /* Styles */
10 }
11  
12 /* Smartphones (portrait) ----------- */
13 @media only screen and (max-width : 320px) {
14   /* Styles */
15 }
16  
17 /* iPads (portrait and landscape) ----------- */
18 @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {
19   /* Styles */
20 }
21  
22 /* iPads (landscape) ----------- */
23 @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
24   /* Styles */
25 }
26  
27 /* iPads (portrait) ----------- */
28 @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
29   /* Styles */
30 }
31  
32 /* Desktops and laptops ----------- */
33 @media only screen and (min-width : 1224px) {
34   /* Styles */
35 }
36  
37 /* Large screens ----------- */
38 @media only screen and (min-width : 1824px) {
39   /* Styles */
40 }
41  
42 /* iPhone 4 ----------- */
43 @media only screen and (-webkit-min-device-pixel-ratio:1.5), only screen and (min-device-pixel-ratio:1.5) {
44   /* Styles */
45 }

Code Source

This is an excellent template which you can find on CSS-Tricks for other bits and pieces of media queries. However I’ve copied their example in full which includes tons of real mobile devices. These codes will even target retina-based devices using min-device-pixel-ratio.

8. Modern Font Stacks

01 /* Times New Roman-based serif */
02 font-family: Cambria, "Hoefler Text", Utopia, "Liberation Serif", "Nimbus Roman No9 L Regular", Times, "Times New Roman", serif;
03  
04 /* A modern Georgia-based serif */
05 font-family: Constantia, "Lucida Bright", Lucidabright, "Lucida Serif", Lucida, "DejaVu Serif," "Bitstream Vera Serif", "Liberation Serif", Georgia, serif;
06  
07 /*A more traditional Garamond-based serif */
08 font-family: "Palatino Linotype", Palatino, Palladio, "URW Palladio L", "Book Antiqua", Baskerville, "Bookman Old Style", "Bitstream Charter", "Nimbus Roman No9 L", Garamond, "Apple Garamond", "ITC Garamond Narrow", "New Century Schoolbook", "Century Schoolbook", "Century Schoolbook L", Georgia, serif;
09  
10 /*The Helvetica/Arial-based sans serif */
11 font-family: Frutiger, "Frutiger Linotype", Univers, Calibri, "Gill Sans", "Gill Sans MT", "Myriad Pro", Myriad, "DejaVu Sans Condensed", "Liberation Sans", "Nimbus Sans L", Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif;
12  
13 /*The Verdana-based sans serif */
14 font-family: Corbel, "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", "Bitstream Vera Sans", "Liberation Sans", Verdana, "Verdana Ref", sans-serif;
15  
16 /*The Trebuchet-based sans serif */
17 font-family: "Segoe UI", Candara, "Bitstream Vera Sans", "DejaVu Sans", "Bitstream Vera Sans", "Trebuchet MS", Verdana, "Verdana Ref", sans-serif;
18  
19 /*The heavier “Impact” sans serif */
20 font-family: Impact, Haettenschweiler, "Franklin Gothic Bold", Charcoal, "Helvetica Inserat", "Bitstream Vera Sans Bold", "Arial Black", sans-serif;
21  
22 /*The monospace */
23 font-family: Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier, monospace;

It is difficult brainstorming your own CSS font stacks for designing new webpages. I hope this snippet may alleviate some torture and give you a few templates for getting started. If you want to find more examples check out which is one of my favorite resources.

9. Custom Text Selection

1 ::selection { background: #e2eae2; }
2 ::-moz-selection { background: #e2eae2; }
3 ::-webkit-selection { background: #e2eae2; }

Some newer web browsers will allow you to define the highlight color on your webpage. This is set to light blue by default, but you can setup any color value which tickles your fancy. This snippet includes the typical ::selection target along with vendor prefixes for Webkit and Mozilla.

10. Hiding H1 Text for Logo

1 h1 {
2     text-indent: -9999px;
3     margin: 0 auto;
4     width: 320px;
5     height: 85px;
6     background: transparent url("images/logo.png") no-repeat scroll;
7 }

I first noticed this technique being implemented on the old . You can setup an H1 tag which also has your website’s name in plaintext for SEO purposes. But using CSS we can move this text so it isn’t visible, and replace it with a custom logo image.

11. Polaroid Image Border

01 img.polaroid {
02     background:#000; /*Change this to a background image or remove*/
03     border:solid #fff;
04     border-width:6px 6px 20px 6px;
05     box-shadow:1px 1px 5px #333; /* Standard blur at 5px. Increase for more depth */
06     -webkit-box-shadow:1px 1px 5px #333;
07     -moz-box-shadow:1px 1px 5px #333;
08     height:200px; /*Set to height of your image or desired div*/
09     width:200px; /*Set to width of your image or desired div*/
10 }

Applying this basic snippet will allow you to implement .polaroid classes onto your images. This will create the old photo-style effect with a large white border and some slight box shadows. You’ll want to update the width/height values to match that of your image dimensions and website layout.

12. Anchor Link Pseudo Classes

1 a:link { color: blue; }
2 a:visited { color: purple; }
3 a:hover { color: red; }
4 a:active { color: yellow; }

Code Source

Most CSS developers know about the anchor link styles and :hover effects. But I wanted to include this small code snippet as a reference for newcomers. These are the four default states for an anchor link, and also a few other HTML elements. Keep this handy until you can memorize some of the more obscure ones.

13. Fancy CSS3 Pull-Quotes

01 .has-pullquote:before {
02     /* Reset metrics. */
03     padding: 0;
04     border: none;
05      
06     /* Content */
07     content: attr(data-pullquote);
08      
09     /* Pull out to the right, modular scale based margins. */
10     float: right;
11     width: 320px;
12     margin: 12px -140px 24px 36px;
13      
14     /* Baseline correction */
15     position: relative;
16     top: 5px;
17      
18     /* Typography (30px line-height equals 25% incremental leading) */
19     font-size: 23px;
20     line-height: 30px;
21 }
22  
23 .pullquote-adelle:before {
24     font-family: "adelle-1", "adelle-2";
25     font-weight: 100;
26     top: 10px !important;
27 }
28  
29 .pullquote-helvetica:before {
30     font-family: "Helvetica Neue", Arial, sans-serif;
31     font-weight: bold;
32     top: 7px !important;
33 }
34  
35 .pullquote-facit:before {
36     font-family: "facitweb-1", "facitweb-2", Helvetica, Arial, sans-serif;
37     font-weight: bold;
38     top: 7px !important;
39 }

Pull-quotes are different from blockquotes in that they appear off to the side of your blog or news article. These often reference quoted text from the article, and so they appear slightly different than blockquotes. This default class has some basic properties along with 3 unique font families to choose from.

14. Fullscreen Backgrounds with CSS3

1 html {
2     background: url('images/bg.jpg') no-repeat center center fixed;
3     -webkit-background-size: cover;
4     -moz-background-size: cover;
5     -o-background-size: cover;
6     background-size: cover;
7 }

Code Source

I should note that this code will not work properly in older browsers which do not support CSS3 syntax. However if you’re looking for a quick solution and don’t care about legacy support, this is the best chunk of code you’ll find! Great for adding big photographs into the background of your website while keeping them resizable and fixed as you scroll.

15. Vertically Centered Content

1 .container {
2     min-height: 6.5em;
3     display: table-cell;
4     vertical-align: middle;
5 }

Using the margin: 0 auto technique it is very easy to embed content into the horizontal center of your page. However vertical content is a lot harder, especially considering scrollbars and other methods. But this is a pure CSS solution which should work flawlessly without JavaScript.

16. Force Vertical Scrollbars

1 html { height: 101% }

When your page content doesn’t fill the entire height of your browser window then you don’t end up getting any scrollbars. However resizing will force them to appear and append an extra 10-15 pixels to the width of your window, pushing over your webpage content. This snippet will ensure your HTML element is always just a little bit higher than the browser which forces scrollbars to stay in place at all times.

17. CSS3 Gradients Template

1 #colorbox {
2     background: #629721;
3     background-image: -webkit-gradient(linear, left top, left bottom, from(#83b842), to(#629721));
4     background-image: -webkit-linear-gradient(top, #83b842, #629721);
5     background-image: -moz-linear-gradient(top, #83b842, #629721);
6     background-image: -ms-linear-gradient(top, #83b842, #629721);
7     background-image: -o-linear-gradient(top, #83b842, #629721);
8     background-image: linear-gradient(top, #83b842, #629721);
9 }

CSS3 gradients are another wondrous part of the newer specifications. Many of the vendor prefixes are difficult to memorize, so this code snippet should save you a bit of time on each project.

18. @font-face Template

01 @font-face {
02     font-family: 'MyWebFont';
03     src: url('webfont.eot'); /* IE9 Compat Modes */
04     src: url('webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
05     url('webfont.woff') format('woff'), /* Modern Browsers */
06     url('webfont.ttf')  format('truetype'), /* Safari, Android, iOS */
07     url('webfont.svg#svgFontName') format('svg'); /* Legacy iOS */
08 }
09      
10 body {
11     font-family: 'MyWebFont', Arial, sans-serif;
12 }

Here is another bit of CSS3 code which isn’t the easiest to memorize. Using @font-face you may embed your own TTF/OTF/SVG/WOFF files into your website and generate custom font families. Use this template as a base example for your own projects in the future.

19. Stitched CSS3 Elements

01 p {
02     position:relative;
03     z-index:1;
04     padding: 10px;
05     margin: 10px;
06     font-size: 21px;
07     line-height: 1.3em;
08     color: #fff;
09     background: #ff0030;
10     -webkit-box-shadow: 0 0 0 4px #ff0030, 2px 1px 4px 4px rgba(10,10,0,.5);
11     -moz-box-shadow: 0 0 0 4px #ff0030, 2px 1px 4px 4px rgba(10,10,0,.5);
12     box-shadow: 0 0 0 4px #ff0030, 2px 1px 6px 4px rgba(10,10,0,.5);
13     -webkit-border-radius: 3px;
14     -moz-border-radius: 3px;
15     border-radius: 3px;
16 }
17  
18 p:before {
19     content: "";
20     position: absolute;
21     z-index: -1;
22     top: 3px;
23     bottom: 3px;
24     left :3px;
25     right: 3px;
26     border: 2px dashed #fff;
27 }
28  
29 p a {
30     color: #fff;
31     text-decoration:none;
32 }
33  
34 p a:hover, p a:focus, p a:active {
35     text-decoration:underline;
36 }

20. CSS3 Zebra Stripes

1 tbody tr:nth-child(odd) {
2     background-color: #ccc;
3 }

Possibly the best item to include zebra stripes is within a table of data. It can be difficult when users are scanning 40 or 50 rows to determine exactly which cell is lined up to which row. By adding zebra stripes on default we can update odd rows with varying background colors.

21. Fancy Ampersand

1 .amp {
2     font-family: Baskerville, 'Goudy Old Style', Palatino, 'Book Antiqua', serif;
3     font-style: italic;
4     font-weight: normal;
5 }

This class would be applied to one span element wrapped around your ampersand character in page content. It will apply some classic serif fonts and use italics to enhance the ampersand symbol. Try it out on a demo webpage and see how you like the design.

22. Drop-Cap Paragraphs

1 p:first-letter{
2     display: block;
3     margin: 5px 0 0 5px;
4     float: left;
5     color: #ff3366;
6     font-size: 5.4em;
7     font-family: Georgia, Times New Roman, serif;
8 }

Typically you’ll notice dropped capitals appear in printed mediums, such as newspapers and books. However this can also be a neat effect in webpages or blogs where there is enough extra room in the layout. This CSS rule is targeting all paragraphs but you may limit this based on a single class or ID.

23. Inner CSS3 Box Shadow

1 #mydiv {
2     -moz-box-shadow: inset 2px 0 4px #000;
3     -webkit-box-shadow: inset 2px 0 4px #000;
4     box-shadow: inset 2px 0 4px #000;
5 }

The box shadow property has offered immense changes into how we build websites. You can portray box shadows on nearly any element, and they all generally look great. This piece of code will force inner shadows which is a lot harder to design around, but in the right cases it looks pristine.

24. Outer CSS3 Box Shadow

1 #mydiv {
2     -webkit-box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.52);
3     -moz-box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.52);
4     box-shadow: 0 2px 2px -2px rgba(0, 0, 0, 0.52);
5 }

In relation to the inner CSS3 shadows I also want to present an outer shadow code snippet. Note the 3rd number in our syntax represents blur distance while the 4th number represents the spread. You can learn more about these values from .

25. Triangular List Bullets

01 ul {
02     margin: 0.75em 0;
03     padding: 0 1em;
04     list-style: none;
05 }
06 li:before {
07     content: "";
08     border-color: transparent #111;
09     border-style: solid;
10     border-width: 0.35em 0 0.35em 0.45em;
11     display: block;
12     height: 0;
13     width: 0;
14     left: -1em;
15     top: 0.9em;
16     position: relative;
17 }

Believe it or not it is actually possible to generate triangle-shaped bullets solely in CSS3. This is a really cool technique which does look awesome in respected browsers. The only potential issue is a major lack of support for fallback methods.

26. Centered Layout Fixed Width

1 #page-wrap {
2     width: 800px;
3     margin: 0 auto;
4 }

I know earlier it was mentioned how to setup horizontal positioning. I want to jump back in with this quick snippet for horizontal positioning, which is perfect to be used on fixed-width layouts.

27. CSS3 Column Text

1 #columns-3 {
2     text-align: justify;
3     -moz-column-count: 3;
4     -moz-column-gap: 12px;
5     -moz-column-rule: 1px solid #c4c8cc;
6     -webkit-column-count: 3;
7     -webkit-column-gap: 12px;
8     -webkit-column-rule: 1px solid #c4c8cc;
9 }

Code Source

CSS3 columns would be nice to see in website layouts, but the reality is how we can split up text based on column styles. Use this snippet to place any number of columns inline with your paragraphs, where text will split evenly based on your column number.

28. CSS Fixed Footer

01 #footer {
02     position: fixed;
03     left: 0px;
04     bottom: 0px;
05     height: 30px;
06     width: 100%;
07     background: #444;
08 }
09   
10 /* IE 6 */
11 * html #footer {
12     position: absolute;
13     top: expression((0-(footer.offsetHeight)+(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)+(ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop))+'px');
14 }

This is actually a lot more useful than it sounds, but appending a fixed footer into your website is quite simple. These footers will scroll with the user and may contain helpful information about your site or unique contact details. Ideally this would only be implemented in cases where it truly adds value to the user interface.

29. Transparent PNG Fix for IE6

01 .bg {
02     width:200px;
03     height:100px;
04     background: url(/folder/yourimage.png) no-repeat;
05     _background:none;
06     _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/folder/yourimage.png',sizingMethod='crop');
07 }
08  
09  
10 /* 1px gif method */
11 img, .png {
12     position: relative;
13     behavior: expression((this.runtimeStyle.behavior="none")&&(this.pngSet?this.pngSet=true:(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none",
14        this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='image')",
15        this.src = "images/transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''),
16        this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "', sizingMethod='crop')",
17        this.runtimeStyle.backgroundImage = "none")),this.pngSet=true));
18 }

Using transparent images inside websites has become a very common practice. This started with gif images, but has evolved into alpha-transparent PNGs. Unfortunately older legacy versions of Internet Explorer have never supported the transparency. Adding this brief CSS snippet should clear up the problem.

30. Cross-Browser Minimum Height

1 #container {
2     min-height: 550px;
3     height: auto !important;
4     height: 550px;
5 }

Developers who have needed to work with min-height know all about the shady support. Many newer browsers can handle this perfectly, however Internet Explorer and older versions of Firefox do have trouble. This set of codes should provide a fix to any related bugs.

31. CSS3 Glowing Inputs

01 input[type=text], textarea {
02     -webkit-transition: all 0.30s ease-in-out;
03     -moz-transition: all 0.30s ease-in-out;
04     -ms-transition: all 0.30s ease-in-out;
05     -o-transition: all 0.30s ease-in-out;
06     outline: none;
07     padding: 3px 0px 3px 3px;
08     margin: 5px 1px 3px 0px;
09     border: 1px solid #ddd;
10 }
11   
12 input[type=text]:focus, textarea:focus {
13     box-shadow: 0 0 5px rgba(81, 203, 238, 1);
14     padding: 3px 0px 3px 3px;
15     margin: 5px 1px 3px 0px;
16     border: 1px solid rgba(81, 203, 238, 1);
17 }

I really enjoy this basic custom CSS3 class because of how it overwrites the default browser behavior. Users of Chrome & Safari know about annoying input outlines in forms. Adding these properties into your stylesheet will setup a whole new design for basic input elements.

32. Style Links Based on Filetype

01 /* external links */
02 a[href^="http://"] {
03     padding-right: 13px;
04     background: url('external.gif') no-repeat center right;
05 }
06   
07 /* emails */
08 a[href^="mailto:"] {
09     padding-right: 20px;
10     background: url('email.png') no-repeat center right;
11 }
12   
13 /* pdfs */
14 a[href$=".pdf"] {
15     padding-right: 18px;
16     background: url('acrobat.png') no-repeat center right;
17 }

Code Source

Quite the obscure bit of CSS but I love the creativity! You can determine the file type of your links using CSS selectors and implement icons as background images. These may include the various protocols (HTTP, FTP, IRC, mailto) or simply the file types themselves (mp3, avi, pdf).

33. Force Code Wraps

1 pre {
2     white-space: pre-wrap;       /* css-3 */
3     white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
4     white-space: -pre-wrap;      /* Opera 4-6 */
5     white-space: -o-pre-wrap;    /* Opera 7 */
6     word-wrap: break-word;       /* Internet Explorer 5.5+ */
7 }

The typical pre tags are used in layouts to display large chunks of code. This is preformatted text like you would find inside Notepad or Textedit, except you’ll often notice long lines produce horizontal scrollbars. This block of CSS will force all pre tags to wrap code instead of breaking outside the container.

34. Force Hand Cursor over Clickable Items

1 a[href], input[type='submit'], input[type='image'], label[for], select, button, .pointer {
2     cursor: pointer;
3 }

Code Source

There are lots of default clickable HTML elements which do not always display the hand pointer icon. Using this set of CSS selectors you may force the pointer over a number of key elements, along with any other objects using the class .pointer.

35. Webpage Top Box Shadow

01 body:before {
02     content: "";
03     position: fixed;
04     top: -10px;
05     left: 0;
06     width: 100%;
07     height: 10px;
08  
09     -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
10     -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
11     box-shadow: 0px 0px 10px rgba(0,0,0,.8);
12     z-index: 100;
13 }

Developers may not find a great use for this other than some pleasing aesthetics. But I really enjoy this effect and it’s definitely something unique! Simply append this CSS code targeting your body element to display a dark drop shadow fading down from the top of your webpage.

36. CSS3 Speech Bubble

01 .chat-bubble {
02     background-color: #ededed;
03     border: 2px solid #666;
04     font-size: 35px;
05     line-height: 1.3em;
06     margin: 10px auto;
07     padding: 10px;
08     position: relative;
09     text-align: center;
10     width: 300px;
11     -moz-border-radius: 20px;
12     -webkit-border-radius: 20px;
13     -moz-box-shadow: 0 0 5px #888;
14     -webkit-box-shadow: 0 0 5px #888;
15     font-family: 'Bangers', arial, serif;
16 }
17 .chat-bubble-arrow-border {
18     border-color: #666 transparent transparent transparent;
19     border-style: solid;
20     border-width: 20px;
21     height: 0;
22     width: 0;
23     position: absolute;
24     bottom: -42px;
25     left: 30px;
26 }
27 .chat-bubble-arrow {
28     border-color: #ededed transparent transparent transparent;
29     border-style: solid;
30     border-width: 20px;
31     height: 0;
32     width: 0;
33     position: absolute;
34     bottom: -39px;
35     left: 30px;
36 }

Numerous user interface purposes come to mind when discussing speech bubbles. These could be handy in discussion comments, or creating bulletin boards, or displaying quoted text. Simply add the following classes into your stylesheet and you can find related HTML codes from .

37. Default H1-H5 Headers

01 h1,h2,h3,h4,h5{
02     color: #005a9c;
03 }
04 h1{
05     font-size: 2.6em;
06     line-height: 2.45em;
07 }
08 h2{
09     font-size: 2.1em;
10     line-height: 1.9em;
11 }
12 h3{
13     font-size: 1.8em;
14     line-height: 1.65em;
15 }
16 h4{
17     font-size: 1.65em;
18     line-height: 1.4em;
19 }
20 h5{
21     font-size: 1.4em;
22     line-height: 1.25em;
23 }

I have offered lots of common syntax including browser CSS resets and a few HTML element resets. This template includes default styles for all major heading elements ranging from H1-H5. You may also consider adding H6 but I have never seen a website using all six nested headers.

38. Pure CSS Background Noise

1 body {
2     background-image: url(data:image/png;base64,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);
3     background-color: #0094d0;
4 }

Designers have seen this effect added into websites for a long time, although they generally use repeating tile images with alpha-transparency. However we can embed Base64 code into CSS to generate brand new images. This is the case as in the snippet above which generates a small noise texture above the body background, or you can create a customized noise background over at .

39. Continued List Ordering

01 ol.chapters {
02     list-style: none;
03     margin-left: 0;
04 }
05  
06 ol.chapters > li:before {
07     content: counter(chapter) ". ";
08     counter-increment: chapter;
09     font-weight: bold;
10     float: left;
11     width: 40px;
12 }
13  
14 ol.chapters li {
15     clear: left;
16 }
17  
18 ol.start {
19     counter-reset: chapter;
20 }
21  
22 ol.continue {
23     counter-reset: chapter 11;
24 }

I feel this may not be an extremely popular snippet, but it does have its market among developers. There may be situations where you’ll need to continue a list of items but split into two separate UL elements. Check out the code above for an awesome CSS-only fix.

40. CSS Tooltip Hovers

01 a {
02     border-bottom:1px solid #bbb;
03     color:#666;
04     display:inline-block;
05     position:relative;
06     text-decoration:none;
07 }
08 a:hover,
09 a:focus {
10     color:#36c;
11 }
12 a:active {
13     top:1px;
14 }
15   
16 /* Tooltip styling */
17 a[data-tooltip]:after {
18     border-top: 8px solid #222;
19     border-top: 8px solid hsla(0,0%,0%,.85);
20     border-left: 8px solid transparent;
21     border-right: 8px solid transparent;
22     content: "";
23     display: none;
24     height: 0;
25     width: 0;
26     left: 25%;
27     position: absolute;
28 }
29 a[data-tooltip]:before {
30     background: #222;
31     background: hsla(0,0%,0%,.85);
32     color: #f6f6f6;
33     content: attr(data-tooltip);
34     display: none;
35     font-family: sans-serif;
36     font-size: 14px;
37     height: 32px;
38     left: 0;
39     line-height: 32px;
40     padding: 0 15px;
41     position: absolute;
42     text-shadow: 0 1px 1px hsla(0,0%,0%,1);
43     white-space: nowrap;
44     -webkit-border-radius: 5px;
45     -moz-border-radius: 5px;
46     -o-border-radius: 5px;
47     border-radius: 5px;
48 }
49 a[data-tooltip]:hover:after {
50     display: block;
51     top: -9px;
52 }
53 a[data-tooltip]:hover:before {
54     display: block;
55     top: -41px;
56 }
57 a[data-tooltip]:active:after {
58     top: -10px;
59 }
60 a[data-tooltip]:active:before {
61     top: -42px;
62 }

There are lots of open source jQuery-based tooltips which you can implement on your websites. But CSS-based tooltips are very rare, and this is one of my favorite snippets. Just copy this over into your stylesheet and using the new HTML5 data-attributes you can setup the tooltip text via data-tooltip.

41. Dark Grey Rounded Buttons

01 .graybtn {
02     -moz-box-shadow:inset 0px 1px 0px 0px #ffffff;
03     -webkit-box-shadow:inset 0px 1px 0px 0px #ffffff;
04     box-shadow:inset 0px 1px 0px 0px #ffffff;
05     background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ffffff), color-stop(1, #d1d1d1) );
06     background:-moz-linear-gradient( center top, #ffffff 5%, #d1d1d1 100% );
07     filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#d1d1d1');
08     background-color:#ffffff;
09     -moz-border-radius:6px;
10     -webkit-border-radius:6px;
11     border-radius:6px;
12     border:1px solid #dcdcdc;
13     display:inline-block;
14     color:#777777;
15     font-family:arial;
16     font-size:15px;
17     font-weight:bold;
18     padding:6px 24px;
19     text-decoration:none;
20     text-shadow:1px 1px 0px #ffffff;
21 }
22 .graybtn:hover {
23     background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #d1d1d1), color-stop(1, #ffffff) );
24     background:-moz-linear-gradient( center top, #d1d1d1 5%, #ffffff 100% );
25     filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#d1d1d1', endColorstr='#ffffff');
26     background-color:#d1d1d1;
27 }
28 .graybtn:active {
29     position:relative;
30     top:1px;
31 }

As another helpful template for web developers I have included this simplistic CSS3 buttons class. I am using the class name .graybtn which is appropriate for the colors, but this isn’t to say you couldn’t change the styles to match your own website. Check out the hex values inside a color wheel to match similar hues in different color ranges.

42. Display URLS in a Printed Webpage

1 @media print   { 
2   a:after { 
3     content: " [" attr(href) "] "; 
4   } 
5 }

If you run a news website or resource with lots of print material, this is possibly one of the greatest snippets you’ll ever find. Anchor links in your webpage will look and display exactly as normal. However when printed your users will be able to see the link text along with the full hyperlinked URL. This is handy when visitors need to access a webpage you’ve linked but cannot see the URL in a typical printed document.

43. Disable Mobile Webkit Highlights

1 body {
2     -webkit-touch-callout: none;
3     -webkit-user-select: none;
4     -khtml-user-select: none;
5     -moz-user-select: none;
6     -ms-user-select: none;
7     user-select: none;
8 }

Depending on your experience working in mobile this snippet may not appear very helpful. But when accessing mobile websites in Safari and other Webkit-based engines, you’ll notice a grey box surrounds elements as you tap them. Just append these styles into your website and it should remove all native mobile browser highlights.

44. CSS3 Polka-Dot Pattern

1 body {
2     background: radial-gradient(circle, white 10%, transparent 10%),
3     radial-gradient(circle, white 10%, black 10%) 50px 50px;
4     background-size: 100px 100px;
5 }

I was a bit taken back when initially finding this snippet online. But it is a really interesting method for generating CSS3-only BG patterns on the fly. I have targeted the body element as default but you could apply this onto any container div in your webpage.

45. CSS3 Checkered Pattern

1 body {
2     background-color: white;
3     background-image: linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black),
4     linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black);
5     background-size: 100px 100px;
6     background-position: 0 0, 50px 50px;
7 }

Similar to the polka-dots above we can also create a full seamless checkerboard pattern. This method requires a bit more syntax to get working, but it looks flawless in all CSS3-supported browsers. Also you can change the color values from white and black to match that of your own website color scheme.

46. Github Fork Ribbon

01 .ribbon {
02     background-color: #a00;
03     overflow: hidden;
04     /* top left corner */
05     position: absolute;
06     left: -3em;
07     top: 2.5em;
08     /* 45 deg ccw rotation */
09     -moz-transform: rotate(-45deg);
10     -webkit-transform: rotate(-45deg);
11     /* shadow */
12     -moz-box-shadow: 0 0 1em #888;
13     -webkit-box-shadow: 0 0 1em #888;
14 }
15 .ribbon a {
16     border: 1px solid #faa;
17     color: #fff;
18     display: block;
19     font: bold 81.25% 'Helvetiva Neue', Helvetica, Arial, sans-serif;
20     margin: 0.05em 0 0.075em 0;
21     padding: 0.5em 3.5em;
22     text-align: center;
23     text-decoration: none;
24     /* shadow */
25     text-shadow: 0 0 0.5em #444;
26 }

As a big user on Github this basic code snippet blew my mind. You can quickly generate Github corner ribbons in your webpage using CSS3 transform properties. This is perfect for open source plugins or code packs which have a popular following on Github. Also great for hosted HTML/CSS/JS demos if you have an active Github repo.

47. Condensed CSS Font Properties

1 p {
2   font: italic small-caps bold 1.2em/1.0em Arial, Tahoma, Helvetica;
3 }

The main reason web developers don’t always use this condensed font property is because not every setting is needed. But having an understanding of this shorthand may save you a lot of time and space in your stylesheets. Keep this snippet handy just in case you ever want to shorten the formatting of your font styles.

48. Paper Page Curl Effect

01 ul.box {
02     position: relative;
03     z-index: 1; /* prevent shadows falling behind containers with backgrounds */
04     overflow: hidden;
05     list-style: none;
06     margin: 0;
07     padding: 0;
08 }
09  
10 ul.box li {
11     position: relative;
12     float: left;
13     width: 250px;
14     height: 150px;
15     padding: 0;
16     border: 1px solid #efefef;
17     margin: 0 30px 30px 0;
18     background: #fff;
19     -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset;
20     -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset;
21     box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset;
22 }
23  
24 ul.box li:before,
25 ul.box li:after {
26     content: '';
27     z-index: -1;
28     position: absolute;
29     left: 10px;
30     bottom: 10px;
31     width: 70%;
32     max-width: 300px; /* avoid rotation causing ugly appearance at large container widths */
33     max-height: 100px;
34     height: 55%;
35     -webkit-box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
36     -moz-box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
37     box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
38     -webkit-transform: skew(-15deg) rotate(-6deg);
39     -moz-transform: skew(-15deg) rotate(-6deg);
40     -ms-transform: skew(-15deg) rotate(-6deg);
41     -o-transform: skew(-15deg) rotate(-6deg);
42     transform: skew(-15deg) rotate(-6deg);
43 }
44  
45 ul.box li:after {
46     left: auto;
47     right: 10px;
48     -webkit-transform: skew(15deg) rotate(6deg);
49     -moz-transform: skew(15deg) rotate(6deg);
50     -ms-transform: skew(15deg) rotate(6deg);
51     -o-transform: skew(15deg) rotate(6deg);
52     transform: skew(15deg) rotate(6deg);
53 }

This page curl effect can be applied to almost any container which holds website content. Immediately I thought about image media and quoted text, but really this could be anything at all. Check out the snippet’s for a better grasp of how these page curls function.

49. Glowing Anchor Links

01 a {
02     color: #00e;
03 }
04 a:visited {
05     color: #551a8b;
06 }
07 a:hover {
08     color: #06e;
09 }
10 a:focus {
11     outline: thin dotted;
12 }
13 a:hover, a:active {
14     outline: 0;
15 }
16 a, a:visited, a:active {
17     text-decoration: none;
18     color: #fff;
19     -webkit-transition: all .3s ease-in-out;
20 }
21 a:hover, .glow {
22     color: #ff0;
23     text-shadow: 0 0 10px #ff0;
24 }

CSS3 text shadows offer a unique method of styling your webpage typography. And more specifically this snippet is an excellent resource for custom creative links with glowing hover effects. I doubt this effect can be pulled off elegantly in the majority of websites, but if you have the patience to get it looking nice you are sure to impress visitors.

50. Featured CSS3 Display Banner

01 .featureBanner {
02     position: relative;
03     margin: 20px
04 }
05 .featureBanner:before {
06     content: "Featured";
07     position: absolute;
08     top: 5px;
09     left: -8px;
10     padding-right: 10px;
11     color: #232323;
12     font-weight: bold;
13     height: 0px;
14     border: 15px solid #ffa200;
15     border-right-color: transparent;
16     line-height: 0px;
17     box-shadow: -0px 5px 5px -5px #000;
18     z-index: 1;
19 }
20  
21 .featureBanner:after {
22     content: "";
23     position: absolute;
24     top: 35px;
25     left: -8px;
26     border: 4px solid #89540c;
27     border-left-color: transparent;
28     border-bottom-color: transparent;
29 }

Generally you would need to setup a background image to duplicate this effect in other browsers. But in CSS3-supported engines we can generate dynamic banners which hang off the edge of your content wrappers, all without images! These may look good attached onto e-commerce products, image thumbnails, video previews, or blog articles, to list just a few ideas.

阅读(1522) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~