Submit Your Article Forum Rules

Results 1 to 4 of 4

Thread: CSS styling for HR

  1. #1

    CSS styling for HR

    Is it possible to have more than one background image styled for an HR tag?

    Currently, I have some code written like this (pseudo-code):

    Code:
    <div align="center">
      <img src="line.gif" style="height:1px; width:43%; vertical-align:middle;" />
      <img src="middle.gif" style="vertical-align:middle;" />
      <img src="line.gif" style="height:1px; width:43%; vertical-align:middle;" />
    </div>
    This displays three images pasted next to one another... the middle image is sandwhiched between two horizontal lines (gif images) which extend/contract based on the window size.

    Is it possible to style this to do the same thing using an HR tag? This would be easier than doing an SSI INCLUDE call for this snippet of code.

  2. #2
    Junior Member
    Join Date
    May 2007
    Posts
    6

    Thumbs up Re: CSS styling for HR

    Quote Originally Posted by ArthurKay View Post
    Is it possible to have more than one background image styled for an HR tag?
    Quite simply, No.

    The css attribute "background-image" is included for the (x)html element "hr" in CSS 2.x, but browser support is not consistent. For example, IE and Opera (and some versions of Safari) will render a border around the hr, even if you set "border:none;" in the css.

    I'm also not convinced that the code you posted would actually span the entire width of the viewport, since theoretically it only would do so when the width of the middle image equals 14% or more of the total viewport.

    There is, however, a good way of achieving this effect with simple css. I will assume that, for stylistic reasons, the images are not just plain lines (in which case you'd be much better suited using divs with css borders for the outer two lines).

    Code:
    css:
    
    #line {
    	width:100%;  /* this is assumed if not specified, so you can omit this line if you want */
    	height:1px;  /* set the height of the line container */
        vertical-align:middle;  /* this will cascade to the subordinate divs */
        margin:0;  /* necessary for < IE7 */
    }
    #line_left {
        width:49%;  /* we must account for IE's crappy box model */
        background:url('line.gif') repeat-x;
        float:left;
        z-index:1;
    }
    #line_right {
        width:49%;  /* we must account for IE's crappy box model */
        height:1px;
        background:url('line.gif') repeat-x;
        float:right;
        z-index:1;
    }
    #line_clearfloat {
        clear:both;
    }
    #line_middle {
        width:40px;  /* replace this value with the width of the actual image */
    	height:1px;
    	background:url('middle.gif') no-repeat;
        margin:auto;  /* this will center the image */
    	margin-top:-1px;  /* this will place it on top of the lines above */
    	_margin-top:-38px; /* hack for < IE7 */
        z-index:2;  /* this tells browsers to display this image on top of the other images */
    }
    Code:
    (x)html:
    
    <div id="line">
        <div id="line_left">&nbsp;</div>
        <div id="line_right">&nbsp;</div>
        <div id="line_clearfloat">&nbsp;</div>
    </div>
    <div id="line_middle">&nbsp;</div>
    Since you were using the (x)html attribute "align," I will assume you're not interested in validating as XHTML Strict. If you are, however, you'll need to omit the _margin-top hack for IE, as the underscore hack will not pass css validation. If you'd like to achieve validation, delete the _margin-top line in the css and replace <div id="line_middle">&nbsp;</div> in the (x)html with the following:

    Code:
    (x)html:
    
    <!--[if lte IE 6]><div style="margin-top:-38px;"><![endif]-->
        <div id="line_middle">&nbsp;</div>
    <!--[if lte IE 6]></div<![endif]-->
    If you'd like to use this multiple times on the same page, you can leave the css as it is and replace the attribute "id" with "class" in the (x)html.

    Hope this is helpful.

  3. #3

    Re: CSS styling for HR

    That's actually seems like more work than what I currently have.

    Basically, a client of mine wants the ease of using an HR tag (which they can use as many times as they please) that gives the output of the code I originally posted.

    I think I'm basically stuck creating a new image.. one that combines the three I'm currently using, and stick that as the background of an HR tag.

  4. #4
    WebProWorld MVP
    Join Date
    Aug 2003
    Posts
    1,039

    Re: CSS styling for HR

    As your main driving force seems to be making it easy for the client, then you can place hr tags within the page and use JavaScript to find and replace them with more complex HTML/CSS. The JavaScript is just run after the page loads.

    This makes it easy to edit the content as they only have to deal with hr tags and maybe an optional class to select the style, but allows you to style it anyway you want.

    The following JS placed at the end of the page just before </body> will replace all hr tags with <div class="myhr"></div>, you can obviously expand this to insert much more complex markup and so forth. I've only tested on Firefox and IE7:
    Code:
    <script type="text/javascript">
    //<![CDATA[
    		
    var els = document.getElementsByTagName('hr');
    for(var i=0; i < els.length; i++) {
    	var t = document.createElement("div");
    	t.className = 'myhr';
    	els[i].parentNode.replaceChild(t, els[i]);
    }
    
    //]]>
    </script>
    I use a variation on this to do the black box on my home page.

Similar Threads

  1. CSS: <label> ignores width styling
    By hughes in forum Graphics & Design Discussion Forum
    Replies: 4
    Last Post: 10-27-2009, 07:12 PM
  2. Inline Styling For Hyperlinks
    By kruser in forum Graphics & Design Discussion Forum
    Replies: 3
    Last Post: 09-02-2008, 08:12 PM
  3. Styling <ul> as <select>with CSS
    By thindenim in forum Graphics & Design Discussion Forum
    Replies: 2
    Last Post: 06-05-2007, 03:45 PM
  4. Styling Content with and without CSS
    By Dcrux in forum Content Discussion Forum
    Replies: 19
    Last Post: 02-07-2006, 05:30 PM
  5. Super Styling with a Dedicated CSS Editor
    By WPW_Feedbot in forum Graphics & Design Discussion Forum
    Replies: 0
    Last Post: 06-21-2005, 07:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •