CSS is a wonderful language for presenting web pages. It’s not too difficult to learn, though like most things, it does have a learning curve. Where many people seem to get tripped up is in specific solutions to specific problems. Fortunately there’s a good chance that someone before you has already had the same problem and even better has found a solution.
Here are 47 of those solutions. 47 CSS Tips, Tricks, and Techniques to add to your CSS toolbox. Some you may be familiar with while others may be new to you. The tips below span from beginner to more advanced CSS code. Hopefully you’ll find a few techniques that will be both new and useful to you and perhaps some will find their way into your next project.
For those, who don’t know what is CSS? And what it can do? Than CSS (Cascading Style Sheets) is a style sheet language used to describe the presentation semantics (that is, the look and formatting) of a document written in a markup language. Its most common application is to style web pages written in HTML and XHTML, but the language can be applied to any kind of XML document, including SVG and XUL.
Please follow the link below for detail introduction.
CSS Tips, Tricks, and Techniques for Your Website
If you’re working with WordPress then of course, the WordPress Codex is always the best place to learn about WordPress and its tweaks with CSS. However, if you are into web designing business than there are thousands of resources available for CSS tips. But unfortunately, it’s too much for a simple user. This the only reason we compiled this fairly comprehensive list of the 47 Exceptional CSS Tips, Tricks, and Techniques to unleash the power of your favorite mark-up language.Most of these tips what follows will work cross-browser. Where there are differences between browsers alternate solutions have been provided if they exist. With that said let’s dive in.
01. Horizontal Centering
To horizontally center block level elements in CSS you need three things. You need to explicitly set a width on the element, set the left and right margins to auto, and include a proper doctype to keep older versions of IE from going into quirks mode.- div#page {width: 960px; margin: 0 auto}
02. Vertical Centering Text with Line-Height
If you want to vertically center text within a containing block who’s height is fixed, simply set the line-height of the text to be the same as the height of the containing blockThe HTML:
- <div id="container">some text here</div>
- div#container {height: 35px; line-height: 35px}
03. Vertical Centering Block Level Elements
Positioning can be used to vertically center block level elements.The HTML:
- <div id="content">your content here</div>
- div#content {position: absolute; top: 50%; height: 500px; margin-top: -250px}
This same trick works for horizontal centering as well. Change top to left and margin-top to margin-left and set the negative margin to be half of the width of the element.
- div#content {position: absolute; top: 50%; left:50%; width:800px; height: 500px; margin-left: -400px; margin-top: -250px}
04. Fluid Images
To create fluid images set a max-width on the images to be 100%- img {max-width: 100%}
- img {width: 100%}
05. 3D Buttons with CSS Only
3D CSS buttons are easy to create. The trick is to give your elements borders with different colors. Lighter where the light source shines and darker where it doesn’t.- div#button {background: #888; border: 1px solid; border-color: #999 #777 #777 #999 }
06. CSS Font Shorthand
When using shorthand on the font property you need to specify each property in the following order- body { font: font-style font-variant font-weight font-size line-height font-family; }
07. Setting Multiple Classes on an HTML Element
Easy and easy to forget the proper syntax. If you want to set multiple classes on an html element it should look like:
- <div class="class-1 class-2 class-3">
- </div>
- class-2 {color: blue}
- class-3 {color: green}
- class-1 {color: red}
08. Rounded Corners
When CSS3 is fully supported across browsers rounded corners will be as simple as:
- .element {border-radius: 5px}
In Safari, Chrome, and other webkit browsers we use
-webkit-border-radius
and in Firefox and other Mozilla based browsers we use -moz-border-radius
.- .element {
- border-radius: 5px
- -webkit-border-radius: 5px
- -moz-border-radius: 5px
- }
- .element {
- border-top-left-radius: 5px
- -webkit-border-top-left-radius: 5px
- -moz-border-radius-top-left
- }
- $(".element").corner("5px");
09. Link Style Order
When setting CSS on the different link states, the link states need to be set in a particular order- a:link
- a:visited
- a:hover
- a:active
10. Clearing and Containing Floats
There are two basic methods to clearing CSS floats. The first is to use the clear property- <div id="wrapper">
- <div id="header"></div>
- <div id="content"></div>
- <div id="sidebar"></div>
- <div id="footer"></div>
- </div>
- #content {float: left}
- #sidebar {float: left}
- #footer {clear: both}
If on the other hand your html was:
- <div id="header">
- <img id="logo" src="" alt="">
- <p id="tagline">
- </p></div>
div
(<div class="clear"></div>
) and then clear the empty div as above or you can use the overflow property on the header div to contain the floated elements- div#header {overflow: hidden}
11. Conditional Comments
Conditional comments are an ideal way to target IE browsers only, since IE is often the browser that won’t cooperate. The basic form of a conditional comments is:
- <!--[if IE]>
- <link rel="stylesheet" type="text/css" href="ie-stylesheet.css" />
- < ![endif]-->
- <!--[if IE 6]> - targets IE6 only -->
- <!--[if gt IE 6]> - targets IE7 and above -->
- <!--[if lt IE 6]> - targets IE5.5 and below -->
- <!--[if gte IE 6]> - targets IE6 and above -->
- <!--[if lte IE 6]> - targets IE6 and below -->
12. HTML Hack for IE
Another way to target IE specifically is to use the HTML * hack. Internet Explorer allows you to use something other than the html element as the root of your document. By placing an * in front of html in your CSS (*html
) you can target IE only as other browsers will ignore the declaration.- div#content {width: 580px}
- * html body div#content {width: 600px}
The above works for IE6 and below. When not in standards mode, but in quirks mode this will work on IE7 as well. You can also target IE7 (in quirks mode) specifically with
- *+html body div#content {width: 620px}
13. CSS Specificity
When two or more CSS selectors are sending conflicting instructions to a single html element, a choice must be made as to which styles to apply. This is done through CSS specificity calculations and is expressed in the form of (a.b,c,d)- Element, Pseudo Element: d = 1 – (0,0,0,1)
- Class, Pseudo class, Attribute: c = 1 – (0,0,1,0)
- Id: b = 1 – (0,1,0,0)
- Inline Style: a = 1 – (1,0,0,0)
- p: 1 element – (0,0,0,1)
- div: 1 element – (0,0,0,1)
- #sidebar: 1 id – (0,1,0,0)
- div#sidebar: 1 element, 1 id – (0,1,0,1)
- div#sidebar p: 2 elements, 1 id – (0,1,0,2)
- div#sidebar p.bio: 2 elements, 1 class, 1 id – (0,1,1,2)
14. min-height fix for IE
Sadly IE still doesn’t comply withmin-height
. However it does treat the height property as though it was min-height
. Knowing that, we can sort of get min-height
working in Internet Explorer.- .element {
- min-height: 500px
- height: auto !important
- height: 500px
- }
!important
will override the height declaration below in all browsers, except IE.Another way to target IE only is to use _height. Only IE6 will treat
_height
as height. All other browsers will ignore it. You have to specify _height
after any height declaration as IE will use whichever comes last.- .element {
- min-height: 500px
- _height: 500px
- }
15. Base Font-Size
Using fluid measurements like'em' or %
on your fonts is a great way to help create more fluid designs. It can be a pain though to calculate ‘em’s for every different element. A simple trick is to set your base font to be the equivalent of 10px. Since the default font-size of browsers is 16px you can set a base font-size
with the following:- body {font-size:62.5%}
- 10/16 = 62.5%
- h1 {font-size: 2.4 em}
16. 100% Height
100% height is one of those things CSS doesn’t do so easily. When you specify an element to have a height of 100%, the 100% refers to the containing element’s height. The containing element would then need to be 100% the height of its containing element and so on. The trick is to set the height of the outermost elements to be 100%- html, body {height: 100%}
min-height
on the content’s container- #content {min-height: 100%}
min-height
to IE.17. CSS Drop Caps
Drop Caps can easily be styled by taking advantage of the CSS pseudo-element:first-letter
.- p:first-letter {
- display: block;
- float: left;
- margin: 5px 5px 0 0;
- color: red
- font-size: 1.4em;
- background: #ddd;
- font-family: Helvetica;
- }
:first-letter
notation.18. Remove Dotted Outline on Links
Easy with the CSS outline property- a {outline: none} or a {outline: 0}
19. Text-transform
Through the CSS text-transform property you can make sure certain blocks of text are either uppercase, lowercase, or only the first letter of each word is uppercase- p {text-transform: uppercase}
- p {text-transform: lowercase}
- p {text-transform: capitalize}
20. Font Variant
Another trick with type is to use the font-variant property to create small caps- p {font-variant: small-caps}
21. Removing the Border from Image Links
By default any image that is wrapped in a link will have a border around the image (similar to the way text is underlined). Removing the border is simple- a image {border: none} or a image {border: 0}
22. Using a CSS Reset for Cross-Browser Compatibility
One of the issues in cross-browser web development is that different browsers use different default values for various CSS properties. By explicitly setting a property like margin to be 0 for certain html elements we can assure that the margin will be 0 on that element in all browsers.
The best way to do that is to use a CSS reset file like the ones developed by Eric Meyer or Yahoo or you can develop your own.
Using a CSS reset ensure that all browsers are on the same page so to speak.
23. Setting Padding on Background Images
Since background images don’t create a new CSS box you can’t directly set padding on them. What you want to do is use the background-position property on the background image to create the same effect.- background-position {top-value left-value}
Both can also take values in % or px or any other measurement. So to create 5px of padding around a background image you would use:
- {background-position: 5px 5px}
24. Stretching a Background Image
To create a background image that can expand and contract with it’s containing element you want to create an image larger than needed, large enough to accommodate the largest possible size of the containing element. Then use the background-position property to center the background image- {background-position: 50% 50%}
25. Wrap Links Around a Background Image
Again since a background image doesn’t create a new CSS box you can’t directly wrap a link around it. You can either wrap the link around the containing element or wrap the link around all the content inside the containing element.- <a href="">
- <div id="bkgd-image">
- your content here
- </div>
- </a>
- <div><a href="">
- your content here
- </a></div>
26. Background Images as List Bullets
Sometimes it’s nice to be able to use an image as a bullet instead of one of the supported
list-style-types
.- ul {list-style: none}
- ul li {
- background-image: url("path-to-your-image");
- background-repeat: none;
- background-position: 0 0.5em;
- }
27. Swap Background Images on Hover
There are two very similar ways to achieve this. The key in both is to use the:hover pseudo-class
to change either the url of the image or the position of an image sprite- .element {background-image: url("path-to-an-image")}
- .element:hover {background-image: url("path-to-a-different-image")}
- .element {background-position: top-value left-value}
- .element:hover {background-position: different-top-value different-left-value}
28. Visibility or Display?
On the surface both the CSS visibility and display properties seem to do the same thing, hide or show an element on the page. Beneath the surface they work differently.{visibility: hidden}
– The element holds the space, but isn’t seen{display: none}
– The element does not hold space. Other elements collapse to fill the space
display: none
, unless your goal is to leave an empty open space on your page.29. Cross-Browser Transparency
Not all browsers currently support the CSS3 opacity property. However we can still make transparency work across browsers.- .element {
- filter:alpha(opacity=50);
- -moz-opacity:0.5;
- -khtml-opacity: 0.5;
- opacity: 0.5;
- }
opacity is the CSS standard and will work in current Webkit and Mozilla browsers as well as Opera
-moz-opacity is
for older versions of Mozilla Browsers-khtml-opacity
is for older versions of Safari and any browser using the khtml rendering enginefilter:alpha(opacity=50)
is for Internet Explorer
30. Target IE7 (and below) and IE6 (specifically) without Conditional Comments
We all know how difficult IE can sometimes be when it comes to CSS. However instead of cursing IE under your breath or out loud you can easily write IE specific code that other browsers will ignore. My own preference is for conditional comments, but here’s a quick trick you can use in your main CSS file.
- .element {
- background: red; /* modern browsers */
- *background: green; /* IE 7 and below */
- _background: blue; /* IE6 exclusively */
- }
31. nth-child
The nth-child CSS pseudo-selectors allow you to target the 3rd or 7th or nth element in a list. Another use would be to style odd and even rows in a table differently. The alternative is to add a class specifically to the list-item you want to style differently, but that’s not very flexible. The nth-child syntax looks like this:- ul li:nth-child(3) {
- background: blue
- }
- ul li:nth-child(3n+3) {
- background: blue
- }
Unfortunately no current version of IE supports it. However there is a way to simulate the 1st bit of code above for IE7 and IE8.
- ul > li:nth-child(3) is the same as ul > *:first-child + * + *
Another and perhaps more practical solution currently is to use jQuery, which supports all CSS3 selectors.
32. Basic 2-column CSS layout (fixed width, centered, header and footer, sidebar on right)
The above is a pretty common website layout and it’s rather easy to code.The HTML:
- <div id="wrapper">
- <div id="header">
- </div>
- <div id="content">
- </div>
- <div id="sidebar">
- </div>
- <div id="footer">
- </div>
- </div>
- #wrapper {width:960px; margin:0 auto}
- #content {float:left; width:600px}
- #sidebar {float:left; width:360px}
- #footer {clear: both}
33. Basic 3-column CSS layout (fixed width, centered, header and footer, sidebars on right and left of the content)
Expanding on the above 2-column layout is this common 3-column layout.The HTML:
- <div id="wrapper">
- <div id="header">
- </div>
- <div id="primary">
- </div>
- <div id="content">
- </div>
- <div id="secondary">
- </div>
- <div id="footer">
- </div>
- </div>
- #wrapper {width:960px; margin:0 auto}
- #primary {float:left; width:230px}
- #content {float:left; width:500px}
- #secondary {float:left; width:230px}
- #footer {clear: both}
34. CSS Triangles and Other Shapes
CSS borders come together at an angle at any corner. This isn’t obvious when setting a border that’s the same color on all sides or if yourborder-width
is only a few px. Making the width much larger and setting different border-colors on different sides lets you see this clearly. Taking advantage of these angles we can use the border-property to create some interesting shapes.Creating a triangle is a matter of setting the border-color on 3 of the 4 sides to transparent. You also want to set the width and height of the element to be 0 in order for all 4 corners to meet at a point. You can set the border opposite the triangle to 0 as well, but the adjacent borders need to maintain a width or the entire element with borders will collapse to a single point.
The HTML:
- <div class="triangle"></div>
- .triangle {
- border-color: transparent transparent green transparent;
- border-style: solid;
- border-width: 0px 300px 300px 300px;
- height: 0px;
- width: 0px;
- }
35. Prevent a Line Break
Sometimes the text in a link or heading will break where you don’t want it to. A simple way to prevent this is:- h1 { whitewhite-space:nowrap; }
36. Class vs. Id
Use an Id for elements that are used once and only once on a pageThe HTML:
- <div id="content"></div>
- #content {background: #fff}
The HTML:
- <p class="large-text">
- </p>
- .large-text {font-size: 1.4em}
37. Replace Text with an Image
Better typography through more font choices is here, but there are still limitations to using it in practice. Sometimes the easiest solution is to use an image. However you also want the original text to be there for search engines and screen readers. A simple way to have both text and image is to use thetext-indent
property.- h1 {
- text-indent:-9999px;
- background:url("h1-image.jpg") no-repeat;
- width:200px;
- height:50px;
- }
38. Style the Element that has Focus in a Form
A nice usability tip is to let people filling out a form know which input currently has focus. You can do this easily using the:focus pseudo-selector
- input:focus { border: 2px solid green; }
39. Understanding !important
!important is a way to override CSS specificity. In general it’s not the best solution since the more you use it, the more you end up needing to use it again. It can make your CSS unmaintainable in a hurry. It can be useful due to a quirk in IE. When you have something like the following- h1 {
- color: red !important;
- color: blue
- }
40. Simple Debugging in CSS
Sometimes your CSS doesn’t seem to be working like you expect and you want to isolate one element in your html to see what space it occupied. An easy way to do this is to give the element a border temporarily- .element {border:1px solid red}
41. Create a CSS Frame for Images
Many images look nice a frame. You typically frame pictures before hanging them on a wall so why not add a frame to images on your website. Just like what you can see how every article image here at instantShift is framed using modified verion of this very same trick.
The trick is to wrap your images in a div and add a simple class.
The HTML:
- <div class="frame"><img src="" alt="" height="" width=""></div>
- .frame {
- border: 2px ridge #000;
- padding: 10px;
- background-color: #fff;
- }
- .frame img {
- border: 2px solid #ooo;
- padding: 1px;
- margin: 10px;
- }
Once set up all you’ll need to do to add your frame to any image is wrap it with a div and assign the class. You can even create several different styles of frames and then pick and choose which to use for each image.
42. Mobile Specific Stylesheets
As more and more people are accessing the web via smart phones and other mobile devices we need to make sure our sites display well on different platforms. Fortunately html allows you to serve different stylesheets for mobile devices.- <link type="text/css" rel="stylesheet" href="handheldstyle.css" media="handheld">
43. Letterpress Type Through CSS
The basic idea is to make use of the CSS3 property for text-shadow to create the letterpress effect.- .element {
- color: #222;
- text-shadow: 0px 2px 3px #555;
- }
- text-shadow: x-offset y-offset blur color;
44. Setting Page Breaks For Print
Some people will want to print your pages and you may want your pages to print in a more readable form. For example you may have a table of data on a page and you’d prefer the entire table be printed on one page as opposed half the table ending up at the bottom of one page and the other half at the top of the next. CSS offers a way to force a page break in some places and prohibit in it others.First you’ll need to direct your styles to printed media through @media print
- <style type="text/css">
- @media print
- table {page-break-inside: avoid}
- </style>
page-break-before
(auto, always, avoid, left, right, inherit)page-break-after
(auto, always, avoid, left, right, inherit)page-break-inside
(auto, avoid, inherit)45. Creating Circles With Border-Radius
The CSS2 border-radius property can be used to create circles in all browsers that support the property. The trick is to set the height and width of the element so they’re the same and set the border-radius of the element to be half that value.- .cirlce {
- width: 300px;
- height: 300px;
- background-color: red;
- border-radius: 150px;
- -moz-border-radius: 150px;
- -webkit-border-radius: 150px;
- }
-moz-border-radius
is for Mozilla based browsers like Firefox-webkit-border-radius
is for webkit based browsers like Safari and Chrome
46. CSS Tooltips
You can create a lightweight CSS cross browser tooltip easily with a few lines of code.The HTML:
- This is the <a class="tooltip" href="#">Tooltip Link<span>This will be the text that shows up in the tooltip</span></a> You can place any text you want here.
- a.tooltip {position: relative}
- a.tooltip span {display:none; padding:5px; width:200px;}
- a:hover {background:#fff;} /*background-color is a must for IE6*/
- a.tooltip:hover span{display:inline; position:absolute;}
display: none
until you hover over the link. When you hover over the link the display is changed to show inline and given a position of absolute. position: relative is necessary on the link in order to ensure the tooltip is positioned in relation to the link and not another containing element.47. Creating a Fixed Header
Instead of letting your entire page scroll you might want to hold the header with your logo and navigation fixed in place and only let the content below scroll.The HTML:
- <div id="header">header </div>
- <div id="content">content </div>
- #header {
- position:fixed;
- }
position: fixed
on the header. Other ideas you can try are creating a scrollable table with a fixed header within your page or keeping both header and footer fixed while allowing your content to scroll between them.
0 التعليقات:
Post a Comment