Posts Mentioning RSS Toggle Comment Threads | Keyboard Shortcuts

  • Jay Kwong 2:14 am on July 8, 2010 Permalink
    Tags: , :before   

    Multiple Backgrounds and Borders with CSS 2.1 – Nicolas Gallagher — Blog & Ephemera 

    Support: Firefox 3.5+, Safari 4+, Chrome 4+, Opera 10+, IE8+.

    How does it work?

    Essentially, you create pseudo-elements using CSS (:before and :after) and treat them similarly to how you would treat HTML elements nested within your target element. But they have distinct benefits — beyond semantics — over the use of nested HTML elements.

    To provide multiple backgrounds and/or borders, the pseudo-elements are pushed behind the content layer and pinned to the desired points of the HTML element using absolute positioning.

    The pseudo-elements contain no true content and are absolutely positioned. This means that they can be stretched to sit over any area of the “parent” element without affecting its content. This can be done using any combination of values for the top, right, bottom, left, width, and height properties and is the key to their flexibility.

    What effects can be achieved?

    Using just one element you can create parallax effects, multiple background colours, multiple background images, clipped background images, image replacement, expandable boxes using images for borders, fluid faux columns, images existing outside the box, the appearance of multiple borders, and other popular effects that usually require images and/or the use of presentational HTML. It is also possible to include 2 extra presentational images as generated content.

    The Multiple Backgrounds with CSS 2.1 and Multiple Borders with CSS 2.1 demo pages show how several popular examples of these effects can be achieved with this technique.

    Most structural elements will contain child elements. Therefore, more often than not, you will be able to gain a further 2 pseudo-elements to use in the presentation by generating them from the first child (and even last-child) element of the parent element. In addition, you can use style changes on :hover to produce complex interaction effects.

    Example code: multiple background images

    Using this technique it is possible to reproduce multiple-background parallax effects like those found on the Silverback site using just one HTML element.

    The element gets its own background image and any desired padding. By relatively positioning the element it acts as the reference point when absolutely positioning the pseudo-elements. The positive z-index will allow for the correct z-axis positioning of the pseudo-elements.

    #silverback {
       position:relative;
       z-index:1;
       min-width:200px;
       min-height:200px;
       padding:120px 200px 50px;
       background:#d3ff99 url(vines-back.png) -10% 0 repeat-x;
    }

    Both pseudo-elements are absolutely positioned and pinned to each side of the element. The z-index value of -1 moves the pseudo-elements behind the content layer. This way the pseudo-elements sit on top of the element’s background and border but all the content is still selectable or clickable.

    #silverback:before,
    #silverback:after {
       position:absolute;
       z-index:-1;
       top:0;
       left:0;
       right:0;
       bottom:0;
       padding-top:100px;
    }

    Each pseudo-element then has a repeated background-image set. This is all that is needed to reproduce the parallax effect.

    The content property lets you add an image as generated content. With two pseudo-elements you can add 2 further images to an element. They can be crudely positioned within the pseudo-element box by varying other properties such as text-align and padding.

    #silverback:before {
       content:url(gorilla-1.png);
       padding-left:3%;
       text-align:left;
       background:transparent url(vines-mid.png) 300% 0 repeat-x;
    }
    
    #silverback:after {
       content:url(gorilla-2.png);
       padding-right:3%;
       text-align:right;
       background:transparent url(vines-front.png) 70% 0 repeat-x;
    }

    The finished product is part of the Multiple Backgrounds with CSS 2.1 demo.

    Example code: fluid faux columns

    Another application is creating equal height fluid columns without images or extra nested containers.

    The HTML base is very simple. I’ve used specific classes on each child div rather than relying on CSS 2.1 selectors that IE6 does not support. If you don’t require IE6 support you don’t actually need the classes.

    <div id="faux">
       <div class="main">[content]</div>
       <div class="supp1">[content]</div>
       <div class="supp2">[content]</div>
    </div>

    The percentage-width container is once again relatively positioned and a positive z-index is set. Applying overflow:hidden gets the element to wrap its floated children and will hide the overflowing pseudo-elements. The background colour will provide the colour for one of the columns.

    #faux {
       position:relative;
       z-index:1;
       width:80%;
       margin:0 auto;
       overflow:hidden;
       background:#ffaf00;
    }

    By using relative positioning on the child div‘s you can also control the order of the columns independently of their source order.

    #faux div {
       position:relative;
       float:left;
       width:30%;
    }
    
    #faux .main {left:35%}
    #faux .supp1 {left:-28.5%}
    #faux .supp2 {left:8.5%}

    The other two full-height columns are produced by creating, sizing, and positioning pseudo-elements with backgrounds. These backgrounds can be (repeating) images if the design requires.

    #faux:before,
    #faux:after {
       content:"";
       position:absolute;
       z-index:-1;
       top:0;
       left:33.333%;
       width:100%;
       height:100%;
       background:#f9b6ff;
    }
    
    #faux:after {
       left:66.667%;
       background:#79daff;
    }

    The finished product is part of the Multiple Backgrounds with CSS 2.1 demo.

    Example code: multiple borders

    Multiple borders are produced in much the same way. Using them can avoid the need for images to produce simple effects.

    An element must be relatively positioned and have sufficient padding to contain the width of the extra border you will be creating with pseudo-elements.

    #borders {
       position:relative;
       z-index:1;
       padding:30px;
       border:5px solid #f00;
       background:#ff9600;
    }

    The pseudo-elements are positioned at specific distances away from the edge of the element’s box, moved behind the content layer with the negative z-index, and given the border and background values you want.

    #borders:before {
       content:"";
       position:absolute;
       z-index:-1;
       top:5px;
       left:5px;
       right:5px;
       bottom:5px;
       border:5px solid #ffea00;
       background:#4aa929;
    }
    #borders:after {
       content:"";
       position:absolute;
       z-index:-1;
       top:15px;
       left:15px;
       right:15px;
       bottom:15px;
       border:5px solid #00b4ff;
       background:#fff;
    }

    That’s all there is to it. The finished product is part of the Multiple Borders with CSS 2.1 demo.

    Progressive enhancement and legacy browsers

    IE6 and IE7 have no support for CSS 2.1 pseudo-elements and will ignore all :before and :after declarations. They get none of the enhancements but are left with the basic usable experience.

    A warning about Firefox 3.0

    Firefox 3.0 supports CSS 2.1 pseudo-elements but does not support their positioning. Due to this partial support some effects look pretty broken, while others are completely unaffected, and there is often no graceful fallback for Firefox 3.0 when using this technique. Sometimes an improved appearance can be achieved by adding display:block to the pseudo-element styles.

    Before using pseudo-elements in a way that requires their positioning, consider the importance of Firefox 3.0 support and the percentage of your users currently using the browser.

    Enhancing with CSS3

    All the applications included in this article could be further enhanced to take advantage of present-day CSS3 implementations.

    Using border-radius, rgba, and transforms, and CSS3 multiple background images in tandem with pseudo-elements can produce even more complex presentations that I hope to include in a future article. Currently there is no browser support for the use of CSS3 transitions or animations on pseudo-elements.

    In the future: CSS3 pseudo-elements

    The proposed extensions to pseudo-elements in the CSS3 Generated and Replaced Content Module include the addition of nested pseudo-elements (::before::before), multiple pseudo-elements (::after(2)), wrapping pseudo-elements (::outside), and the ability to insert pseudo-elements into later parts of the document (::alternate).

    These changes would provide a near limitless number, and arrangement, of pseudo-elements for all sorts of complex effects and presentations using just one element.

     
  • Jay Kwong 1:55 am on July 8, 2010 Permalink
    Tags: , , css3   

    Fancy Inset CSS Image Borders | Evan Byrne’ Writings 

    Before

    After



    The HTML Markup

    In a perfect world we would only use a single img tag with a class attribute. This is not a perfect world. Because the img tag does not support the CSS pseudo-element :after, we have to wrap our image in a span.

    <span class="fancy">< img src="dingo.jpg" /></span>

    The CSS

    The first step is to apply a simple reset to our span and img tags.

    span,img{padding:0;margin:0;border:0;}
    

    Next, we style the wrapper element.

    .fancy{
    position:relative;
    display:inline-block;
    font-size:0;
    line-height:0;
    }
    
    .fancy:after{
    position:absolute;
    top:1px;
    left:1px;
    bottom:1px;
    right:1px;
    border:1px solid rgba(255,255,255,0.5);
    outline:1px solid rgba(0,0,0,0.2);
    content:&amp;quot; &amp;quot;;
    }
    
     
  • Jay Kwong 7:40 am on June 28, 2010 Permalink
    Tags: , footer   

    keep footers at the bottom of the page 

    The HTML div structure

    <div id="container">
       <div id="header"></div>
       <div id="body"></div>
       <div id="footer"></div>
    </div>
    

    There are only four divs required for this to work. The first is a container div that surrounds everything. Inside that are three more divs; a header, a body and a footer. That’s it, all the magic happens in the CSS.

    
    html, body { margin:0; padding:0; height:100%; }
    
    #container { min-height:100%; position:relative; }
    
    #header { background:#ff0; padding:10px; }
    
    #body { padding:10px; padding-bottom:60px;   /* Height of the footer */ }
    
    #footer { position:absolute; bottom:0; width:100%; height:60px;   /* Height of the footer */
    background:#6cf; }
    

    And one simple CSS rule for IE 6 and IE 5.5:

    #container { height:100%; }
    

    via Get down! How to keep footers at the bottom of the page.

     
  • Jay Kwong 7:06 am on June 28, 2010 Permalink | Reply
    Tags: , , ,   

    CSS3 Gradient Buttons 

    button states

    What Is So Cool About These Buttons?

    • Pure CSS: no image or Javascript is used.
    • The gradient is cross-browser supported (IE, Firefox 3.6, Chrome, and Safari).
    • Flexible and scalable: button size and rounded corners can be adjusted by changing the font size and padding values.
    • It has three button states: normal, hover, and active.
    • It can be applied to any HTML element: a, input, button, span, div, p, h3, etc.
    • Fallback: if CSS3 is not supported, it will display a regular button (no gradient and shadow).

    Preview

    The image below shows how the button will display in different browsers.

    preview

    Button States

    • normal state = gradient with border and shadow styles.
    • hover = darker gradient
    • active = gradient is reversed, 1px down, and darker font color as well.

    button states

    General Styles For The Button

    The following code is the general styles for the .button class. I use em value in the padding and border-radius property to make it scalable base on the font-size. To adjust the rounded corners and button size, simply change the border-radius, font-size and padding values. For example: I can make a smaller button by decreasing the font-size and padding values (see demo).

    For more details on border-radius, text-shadow, and box-shadow, read my article The Basics of CSS3.

    .button {
    	display: inline-block;
    	outline: none;
    	cursor: pointer;
    	text-align: center;
    	text-decoration: none;
    	font: 14px/100% Arial, Helvetica, sans-serif;
    	padding: .5em 2em .55em;
    	text-shadow: 0 1px 1px rgba(0,0,0,.3);
    	-webkit-border-radius: .5em;
    	-moz-border-radius: .5em;
    	border-radius: .5em;
    	-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2);
    	-moz-box-shadow: 0 1px 2px rgba(0,0,0,.2);
    	box-shadow: 0 1px 2px rgba(0,0,0,.2);
    }
    .button:hover {
    	text-decoration: none;
    }
    .button:active {
    	position: relative;
    	top: 1px;
    }
    

    button general styles

    Color Gradient Styles

    The code below is the CSS styling for the orange button. The first background line is a fallback for the non-CSS3 browsers, the second line is for Webkit browsers, the third line is for Firefox, and the last line is a gradient filter that is only read by Internet Explorer.

    For more details on CSS gradient, read my article Cross-Browser CSS Gradient.

    .orange {
    	color: #fef4e9;
    	border: solid 1px #da7c0c;
    	background: #f78d1d;
    	background: -webkit-gradient(linear, left top, left bottom, from(#faa51a), to(#f47a20));
    	background: -moz-linear-gradient(top,  #faa51a,  #f47a20);
    	filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#faa51a', endColorstr='#f47a20');
    }
    .orange:hover {
    	background: #f47c20;
    	background: -webkit-gradient(linear, left top, left bottom, from(#f88e11), to(#f06015));
    	background: -moz-linear-gradient(top,  #f88e11,  #f06015);
    	filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#f88e11', endColorstr='#f06015');
    }
    .orange:active {
    	color: #fcd3a5;
    	background: -webkit-gradient(linear, left top, left bottom, from(#f47a20), to(#faa51a));
    	background: -moz-linear-gradient(top,  #f47a20,  #faa51a);
    	filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#f47a20', endColorstr='#faa51a');
    }
    

    button general styles

    How To Use My Buttons?

    Lets say you like the blue button and want to use it on your page:

    • First, copy the .button and .blue CSS (view demo source code).
    • Then, add to the HTML element where you want the button to be (eg. <a href="#">Button</a>). The CSS classes can be applied to any element such as link, p, span, div, input, button, etc.

    applying

     
  • Jay Kwong 7:49 am on February 23, 2010 Permalink | Reply
    Tags: caption,   

    wp-caption 

    .wp-caption{ background: #ffffff; text-align:center!important; border:1px solid #D0D0D0; width:570px!important; padding:3px;-moz-border-radius: 5px;
    -webkit-border-radius: 5px; }
    .wp-caption img{ max-width:100%; text-align:center; padding:0px!important; margin:0 auto}
    .wp-caption .wp-caption-text{ padding:3px!important; margin:0!important; font-size:11px!important; color: #666}
    
     
  • Jay Kwong 6:48 am on February 9, 2010 Permalink | Reply
    Tags: ,   

    Inner shadows in CSS3 « Devthought 

    -webkit-box-shadow (in Webkit nightly) and -moz-box-shadow support inner shadows with the inset keyword. Both also support multiple shadow declarations separated by commas.

    div.box {
    -webkit-box-shadow: 0 3px 3px rgba(0,0,0,0.20), rgba(0,0,0,0.12) 0px 0px 10px inset;
    -moz-box-shadow: 0 3px 3px rgba(0,0,0,0.20), rgba(0,0,0,0.12) 0px 0px 10px inset;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border: 1px solid #fff;
    padding: 6px;
    width: 200px;
    background: #fff;
    }
    

    via Inner shadows in CSS3 « Devthought.

     
  • Jay Kwong 6:54 am on January 22, 2010 Permalink | Reply
    Tags: , , , text-shadow   

    Buttons with CSS3 and RGBA 

    
    .awesome{
    background: #222 url(/images/alert-overlay.png) repeat-x;
    display: inline-block;
    padding: 5px 10px 6px;
    color: #fff;
    text-decoration: none;
    font-weight: bold;
    line-height: 1;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-box-shadow: 0 1px 3px #999;
    -webkit-box-shadow: 0 1px 3px #999;
    text-shadow: 0 -1px 1px #222;
    border-bottom: 1px solid #222;
    position: relative;
    cursor: pointer;
    }
    
      /* Sizes ---------- */
     .small.awesome {
     font-size: 11px;
     }
    .medium.awesome {
     font-size: 13px;
     }
    .large.awesome {
    font-size: 14px;
     padding: 8px 14px 9px;
    }
    
     /* Colors ---------- */
     .blue.awesome {
     background-color: #2daebf;
    }
     .red.awesome {
     background-color: #e33100;
     }
     .magenta.awesome {
     background-color: #a9014b;
     }
     .orange.awesome {
     background-color: #ff5c00;
     }
    .yellow.awesome {
     background-color: #ffb515;
     }
    

    via ZURB – Super Awesome Buttons with CSS3 and RGBA.

     
  • Jay Kwong 6:45 am on January 22, 2010 Permalink | Reply
    Tags: , -webkit-gradient, -webkit-transform, -webkit-transition   

    [CSS] The Fancy Modal 

    The Fancy Modal

    That modal is okay — it gets the job done and once you know it, the pieces can be put together in less than 5 minutes. We wanted to do a little more for Notable, so we cooked up a fancier style using a few other CSS tricks like -webkit-gradient and -webkit-transform.

    Modal-2 in Stronger, Better, Faster Design with CSS3

    Modal-2 in Stronger, Better, Faster Design with CSS3

    This modal is a little flashier and more akin to a desktop app in terms of interaction: it slides down from the top and gets a little more chroming than the simple modal. We accomplish this through a couple of cool tricks.

    
    div#fancyModal {
    display: block; width: 560px;
    position: absolute; top: -310px; left: 170px;
    padding: 90px 20px 20px 20px;
    border: solid 1px #999;
    background: -webkit-gradient(linear, left top, left bottom, from(rgb(255,255,255)), to(rgb(230,230,230)));
    -webkit-box-shadow: 0px 3px 6px rgba(0,0,0,0.25);
    -webkit-border-bottom-left-radius: 6px;
    -webkit-border-bottom-right-radius: 6px;
    -webkit-transition: -webkit-transform 0.25s ease-out;
    -webkit-transform: translateY(-570px);
    -moz-box-shadow: 0px 3px 6px rgba(0,0,0,0.25);
    -moz-border-radius: 0 0 6px 6px;
    -moz-transform: translateY(-570px);
    }
    

    via Stronger, Better, Faster Design with CSS3 – Smashing Magazine.

     
  • Jay Kwong 6:32 am on January 22, 2010 Permalink | Reply
    Tags: -moz-border-image, , ,   

    border-image 

    div.border {
    border: 5px solid #feb515;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    -moz-border-image: url(border-image.png) 5 5 5 5 stretch;
    -webkit-border-image: url(border-image.png) 5 5 5 5 stretch;
    }
    

    via ZURB – Behind the Scenes: Building the New Visual Annotations.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel