1. Advanced CSS Attribute Selector Demo

    March 14, 2010 by J.P.

    Recently a back end developer asked me with there was a way to use a wild card in CSS that would allow him to generate ID’s on the fly. The solution I came up with was to use an attribute selector.

    When using attribute selectors in CSS the name before the “|” is the attribute targeted. In this case “id” is used but we could have just as easily used “rel”, “input”, “value” etc.

    The text after the “|” is what the attribute must start with to be read. The example uses “section”. Each “id” in the html that starts with “section” will be styled. Some id’s use more specific code in certain div’s to give them unique colors showing how the cascading works in the CSS file.

    Here is the solution:

    View the Demo

    CSS Code
    [id|="section"]{
    border: 10px solid #999;
    margin:10px 0;
    background:black;
    width: 200px;
    height: 200px;
    }
    [id|="section-item"]{
    background:green;
    }
    #section-1{
    background:red;
    }
    #section-2{
    background:blue;
    }
    #section-3{
    background:yellow;
    }


  2. Font CSS Substitution Methods

    September 2, 2009 by J.P.

    font-image

    Many new methods of font substitution are coming of the wood work and old methods are now getting a second look.

    @font-face method

    The @font-face method was added to the CSS2 spec but then not recommended in CSS2.1. However with CSS3 it’s back and supported by most popular modern browsers. Read about @font-face at W3.org.

    This method requires the font being loaded on the server and will cause a slight flickering of the font rendering while it is stored in the cache. The plus side of this method is it’s easy to implement and requires no JavaScript.

    Here is an example of @font-face being used. Click Here

    Sample CSS code used (Note: font is stored on the server)

    @font-face {
    font-family: 'BrushScript';
    src: url('BrushScript.ttf') format("truetypefont");
    }
    /* for IE */
    @font-face {
    font-family: 'BrushScript';
    src: url(BrushScript.ttf);
    }
    body {
    font-family: 'BrushScript', helvetica; /*degrades to a standard web font if unavailable */
    font-size:1em;
    }

    Cufon JavaScript Method

    This currently my personal favorite method. No font is stored on the server and there is no flicker of the font while loading. The wizard also provides many options and settings so you can customize the needs for your site.

    Easy to add

    Cufon provides a simple method for converting fonts on your local machine into a js file that you include in the header along with the Cufon script. A simple replace call is used to substitute the font. Read more about Cufon here

    Example of the Cufon Method »

    Example Header Code

    <script src="cufon-yui.js" type="text/javascript"></script> <script src="AGaramondPro_400-AGaramondPro_700-AGaramondPro_italic_400.font.js" type="text/javascript"></script>
    <script type="text/javascript">Cufon.replace('h1');Cufon.replace('p');</script>

    Background Image method

    This method is good for replacing a few headers and best method to use with the logo of your site. Detailed px to px cross browser look with the design is needed while the code remains semantic. This method has been implemented in this version of my site since for major section H1 headers and the logo of the site.

    XHTML Example Code

    <h3 id="Portfolio">Portfolio</h3>

    CSS Example Code

    h3#Portfolio
    {
    background:url('/portfolio/2009/gfx/title_portfolio-trans.png') no-repeat;
    text-indent: -9999px;
    width:229px;
    height:61px;
    }

    This will work with all browsers. A list will be added shortly of the other methods and their support.


  3. CSS Attribute Selectors

    August 20, 2009 by J.P.

    computer_controls

    CSS Attribute selectors are a great way to target specific elements in your page and apply styles to them with out have go back and opening up the template file and changing code.

    I find them extremely useful for creating form css.

    There are 6 different types of attribute selectors

    • [att=value]
      The attribute has to have the exact value specified.
    • [att~=value]
      The attribute’s value needs to be a whitespace separated list of words (for example, class=”title featured home”), and one of the words is exactly the specified value.
    • [att|=value]
      The attribute’s value is exactly “value” or starts with the word “value” and is immediately followed by “-”, so it would be “value-”.
    • [att^=value]
      The attribute’s value starts with the specified value.
    • [att$=value]
      The attribute’s value ends with the specified value.
    • [att*=value]
      The attribute’s value contains the specified value.

    Here is an example page where I have formatted elements with attribute selectors only.

    Sample CSS Used

    <style type="text/css" media="screen">
    p[tile='para1'] {
    color:red;
    }
    input[type="text"] {
    width: 200px;
    background-color: #DDD;
    border:0px;
    display:block;
    padding:3px;
    }
    input[type="submit"] {
    width: 200px;
    background-color: #000;
    border:0px;
    display:block;
    padding:5px;
    color:#fff;
    margin-top:5px;
    }
    a[href$='.jpg']{
    background:green;
    color:#fff;
    }
    </style>



Older Posts »