treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Apply Background Color only on text

  • I'm changing the background of my headers, but there is this width problem that I've never met. For all the times that I've worked with CSS, I apply the background color and it only covers the part where there are text over. But this time, when I apply a background cover, it fills the entire row. I have no idea what to do now. The following is the CSS code for it:
    h1, h2, h3, h4, h5, h6 {
    margin: 4px 12px;
    padding: 2px;
    width: 115px;
    line-height: 1.1em;
    height: 1.2em;
    color: #fff;
    background: #0686c9;
    }

    I have included the text shadow and gradient code because it's kind of long. Please let me know if you need any further information.

    Angela
  • Edit: Remove the height from your css
  • Isn't it just because the header elements have a specific width of 115 pixels, so the background color would be applied to the entire element?
  • Maybe I'm confused on what @angelazou is having trouble with. I'm not really understanding what they're trying to do.
  • Maybe that headers are filling the entire row? Did you try adding a display:block; in your css?
  • display:block won't do any difference, since the H1-H6 tags already are block-level elements by default, plus they have a fixed width.

    I'm also a little confused what he is having trouble with to be honest...!

    But here's my guess....

    If the H1-H6 elements need to have a background color on the text only, don't give it a specific width of 115px, cause then the color will be applied over that full width. However if there's no width declared, it will be applied to the entire row, so you'll have to use float:left;.
  • Put display:inline-block; in your css for the headers.
  • @cnwtx That will cause issues in IE7, I would just float them left. Unless of course IE7 support isn't needed?
  • Floating isn't going to (without setting a width) solve this, is it?
  • Yeah it will. Floating automatically wraps the element at the width of the content. Here is an example: http://jsfiddle.net/sl1dr/3xUd7/
  • @angelazou To answer your question specifically, remove the fixed width and add float: left; and clear: left;, as shown in this example: http://jsfiddle.net/sl1dr/3xUd7/2/
  • Sigh... I hate IE.
  • Me too, especially when the customer only uses IE. I saw something interesting with IE9. I have 'height:' on because I needed to apply some text show in IE9. But one minute it was there, and the next refresh it's gone. I have no idea what's going on.

    I visited the jsFiddle link provided, I will try and see if there is any difference. What I'm saying that when I try to apply a background color (it fills the entire row). I had to limit it to 115px so it doesn't go over an entire row. But this is not a solution, because when I go to pages with post title (that are like 1 or 2 lines long), they are all stuck together because I limit the width to 115px.

    Thanks
    Angela
  • Can you post a link to the issue? There must be something else overriding your styles.
  • Well, I used float:left and only part of the problem is solved. Because I used float:left, all the elements follow after the header. I tried clear: left and clear:both, none of them works.

    PS: I'm doing a local installation. But I can get you a screenshot and the content of the CSS if you'd like.
  • If it's too complex to give us the whole context of the site, then maybe you can try wrapping each H1-H6 element in a div.

    Give H1-H6 a float:left; but NO width (and preferably no height either)
    Give the div that's wrapping the H1-H6 element a specific width (whatever you want to set as the maximum, in your case 115px) and the background color you want.

    http://jsfiddle.net/hzpqu/

    (I've set the maximum width to 150 though, just to make it more clear)

    If there are more lines though, the background color will expand to the longest line, and not go line-by-line.
  • Add display: table; to your headers.
  • This is the full code, I tried various combination of display (including table), but none of it really works.
    h1, h2, h3, h4, h5, h6 {
    margin: 4px 12px;
    padding: 2px 4px;
    float: left;
    display: block;
    clear: both;
    line-height: 1.1em;
    height: auto;
    filter: dropshadow(color=#666, offx=-1, offy=-1);
    color: #FFF;
    background: #0686C9;
    text-shadow: -1px -1px 1px #666;
    background: rgb(30,87,153);
    background: -moz-linear-gradient(top, rgba(30,87,153,1) 0%, rgba(41,137,216,1) 50%, rgba(32,124,202,1) 51%, rgba(125,185,232,1) 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(30,87,153,1)), color-stop(50%,rgba(41,137,216,1)), color-stop(51%,rgba(32,124,202,1)), color-stop(100%,rgba(125,185,232,1)));
    background: -webkit-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%);
    background: -o-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); background: -ms-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%);
    background: linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 );
    -webkit-border-radius: 2px;
    -webkit-border-top-right-radius: 8px;
    -webkit-border-bottom-left-radius: 8px;
    -moz-border-radius: 2px;
    -moz-border-radius-topright: 8px;
    -moz-border-radius-bottomleft: 8px;
    border-radius: 2px;
    border-top-right-radius: 8px;
    border-bottom-left-radius: 8px;
    }


    see a screenshot here
  • I'm not entirely sure what is wrong in the screenshot? Just that the rounded corners aren't showing up?
  • No, but aren't you seeing that all the content are coming after the title (instead of below the title)? That's the problem.
  • Oh, right.

    You need to give the first item below the header (the one that is currently to the right) clear: left;
  • But that means I have to search the entire theme for every instance coming after the header. Is there a shortcut? Perhaps a clear:right; for the headers? [Nope, clear: right; didn't work]
  • I assume that it is mainly paragraphs coming after headers? If this is the case you could use this:

    h1 + p {
    clear: left;
    }
    Shown here: http://jsfiddle.net/sl1dr/Fxja2/
  • Thanks for the tip, but unfortunately again, it only solves part of the problem. I am seeing some improvement, but not all of them. I'm using the following:

    h1 + p, h2 + p, h1 + a, h2 + a, h1 + div, h2 + div {
    clear: left;
    }


    But the code that I previously gave (for the Date too close thread) doesn't really apply even though it's immediately followed by a div. Is there a wildcard I can use? (And hopefully, it works with IE9, right?)
  • The wildcard selector (*) would cause you to have problems with other floats in your design, such as the date bit. The other option, as @Senff said, is to wrap every header in a div.
  • I did a little more digging and fixing, and I'm left with a final section, the text widgets. I use a lot of them, but I couldn't get them to bend.

    Initially, I used the following

    h2 + #text-1, h2 + #text-2, h2 + #text-3, h2 + #text-4, h2 + #text-5, h2 + #text-6, h2 + #text-7, h2 + #text-8, h2 + #text-9, h2 + #text-10, h2 + #text-11, h2 + #text-12, h3 + #text-1, h3 + #text-2, h3 + #text-3, h3 + #text-4, h3 + #text-5, h3 + #text-6, h3 + #text-7, h3 + #text-8, h3 + #text-9, h3 + #text-10, h3 + #text-11, h3 + #text-12  {
    clear: left;
    }


    Didn't work, then I tried this

    h2 + textwidget, h1 + textwidget, h3 + textwidget {
    clear: left;
    }


    Didn't work either. Here is a sample text widget content


    <div id="text-6" class="widget_text suf-widget"><h3>Title</h3> <div class="textwidget"><a href="http://www.google.com">Google</a><br />
    </div>
    </div><!-- widget end -->
  • hmm, this worked fine for me:

    h3 + .textwidget {
    clear: left;
    }
  • Haha, I forgot the ., so basically the page was looking for an element called textwidget. How sloppy of me!
  • HA! That is funny
  • Glad we were able to get it all sorted out!
  • Me too!