473,395 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

h1 background color

Hello,

I am using CSS to apply a background to a header:

<h1>Header</h1>

h1 {background-color: black;}

The problem is that the background color is applied not only to the
text but to the full lenght of the h1.
How to apply it only to the header text?

Thanks,
Miguel
Jun 27 '08 #1
11 28605
shapper wrote:
Hello,

I am using CSS to apply a background to a header:

<h1>Header</h1>

h1 {background-color: black;}

The problem is that the background color is applied not only to the
text but to the full lenght of the h1.
How to apply it only to the header text?

Thanks,
Miguel
<h1><span>Header</span></h1>
h1 span{background-color: black;}
Jun 27 '08 #2
shapper wrote:
Hello,

I am using CSS to apply a background to a header:

<h1>Header</h1>

h1 {background-color: black;}

The problem is that the background color is applied not only to the
text but to the full lenght of the h1.
How to apply it only to the header text?
A it is supposed to do by default. Her are your options:

1) Set the with explicitly

h1 { color: white; background-color: black; width: 3.25em; }

PROS: Does what you wish across browsers, sets the width and will be
proportional to font if you use ems.

CONS: Must be adjusted depending on the length of the content

2) Use float, will auto adjust width:

h1 { color: white; background-color: black; float: left; }

PROS: Does what you wish with most browsers

CONS: Changes flow of document, will need to clear float after the H1.
May interfere with layout if there are other floated elements on page.
IE is notorious for being 'twitchy' with floats

3) Change display like a table cell...

h1 { color: white; background-color: black; display: table-cell; }

PROS: Does exactly what you wish, but leaves IE out of the picture

CONS: Even though this would be the perfect solution, with IE's market
share it will be a hard case to make (even though from a designer's
perspective "life would be great" if IE would just go away...)

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Jun 27 '08 #3
Harris Kosmidhs wrote:
<h1><span>Header</span></h1>
h1 span{background-color: black;}
Good one! I miss that one. I would point out to OP those that it is bad
practice to set the background without setting the foreground. with the
default foreground color as black the above example will give you a
black box. Whereas if you do it properly

h1 span{ color: white; background-color: black; }

the problem would never arise.

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Jun 27 '08 #4
"Jonathan N. Little" <lw*****@central.netwrote in news:ee515$480c96f3
$4************@NAXS.COM:
shapper wrote:
>Hello,

I am using CSS to apply a background to a header:

<h1>Header</h1>

h1 {background-color: black;}

The problem is that the background color is applied not only to the
text but to the full lenght of the h1.
How to apply it only to the header text?
[snip]
3) Change display like a table cell...

h1 { color: white; background-color: black; display: table-cell; }

PROS: Does exactly what you wish, but leaves IE out of the picture
Conditional comment for IE.
CONS: Even though this would be the perfect solution, with IE's market
share it will be a hard case to make (even though from a designer's
perspective "life would be great" if IE would just go away...)
Block element displayed inline add zoom to trigger haslayout mayhaps
result in inline-block behavior for IE.

<!--[if IE]>
<style type="text/css">
h1 {
display:inline;
zoom:1;
}
</style>
<![endif]-->

--
BootNic Monday April 21, 2008 10:18 AM
Good communication is as stimulating as black coffee and just as
hard to sleep after.
*Anne Morrow Lindbergh*
Jun 27 '08 #5
BootNic wrote:
Block element displayed inline add zoom to trigger haslayout mayhaps
result in inline-block behavior for IE.

<!--[if IE]>
<style type="text/css">
h1 {
display:inline;
zoom:1;
}
</style>
<![endif]-->
A code fork is a code fork....I still stand by my comment...I do not
have a rosy memory of the browser-war 90's.

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Jun 27 '08 #6
On Apr 21, 4:09 pm, "Jonathan N. Little" <lws4...@central.netwrote:
BootNic wrote:
Block element displayed inline add zoom to trigger haslayout mayhaps
result in inline-block behavior for IE.
<!--[if IE]>
<style type="text/css">
h1 {
display:inline;
zoom:1;
}
</style>
<![endif]-->

A code fork is a code fork....I still stand by my comment...I do not
have a rosy memory of the browser-war 90's.

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIOhttp://www.LittleWorksStudio.com
Thank you all ...

A little while I tried the span tag inside the h1 tag but I was trying
to figure if there was another way.

I was trying to avoid adding one more tag.

I see using span tags inside other tags very often for solving a few
issues like in menus that uses lists. For example:

<ul>
<li>
<a id="singup" href="/signup/">
<span>
Sign-Up
</span>
</a>
</li>
<li>
<a id="tour" href="/tour/workflow">
<span>
Tour
</span>
</a>
</li>
</ul>

About the Foreground color I do set it up. I was just placing the code
directly related to my problem.

Thank You,
Miguel
Jun 27 '08 #7
shapper wrote:
I see using span tags inside other tags very often for solving a few
issues like in menus that uses lists. For example:

<ul>
<li>
<a id="singup" href="/signup/">
<span>
Sign-Up
</span>
</a>
</li>
<li>
<a id="tour" href="/tour/workflow">
<span>
Tour
</span>
</a>
</li>
</ul>
What issue does the addition of a span do for you here?

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Jun 27 '08 #8
shapper wrote:
About the Foreground color I do set it up. I was just placing the code
directly related to my problem.
...and therein lies the reason everyone asks for a URL, instead of code
fragments. When people just post bits of code, there is often other
necessary bits incorrect or unmentioned that could be the reason for the
problem.

Like your:

<ul>
<li>
<a id="singup" href="/signup/">
<span>
Sign-Up
</span>
</a>
....

1. You could assign CSS to: li a { } and not need a <span>
2. Do you *really* have id="singup" misspelled in your test page?

--
-bts
-Friends don't let friends drive Vista
Jun 27 '08 #9
Scripsit Jonathan N. Little:
h1 span{ color: white; background-color: black; }
Better still:

h1 span{ color: white; background: black; }

Just in case some odd style sheet sets a (possibly white) background
image for the element. Very unlikely, but why not take the precaution,
especially when it makes the rule shorter?

Moreover, adding something like
padding: 0 0.2em
would probably be a good idea. You don't want the heading text to extend
to the very edge of the background area but leave a little padding
there.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

Jun 27 '08 #10
On Apr 21, 6:26 pm, "Jonathan N. Little" <lws4...@central.netwrote:
shapper wrote:
I see using span tags inside other tags very often for solving a few
issues like in menus that uses lists. For example:
<ul>
<li>
<a id="singup" href="/signup/">
<span>
Sign-Up
</span>
</a>
</li>
<li>
<a id="tour" href="/tour/workflow">
<span>
Tour
</span>
</a>
</li>
</ul>

What issue does the addition of a span do for you here?

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIOhttp://www.LittleWorksStudio.com
For example the menu in the following web site:
http://www.springloops.com/

I have seen a lot of this lately.

Thanks,
Miguel
Jun 27 '08 #11
shapper wrote:
On Apr 21, 6:26 pm, "Jonathan N. Little" <lws4...@central.netwrote:
>shapper wrote:
>>I see using span tags inside other tags very often for solving a few
issues like in menus that uses lists. For example:
<ul>
<li>
<a id="singup" href="/signup/">
<span>
Sign-Up
</span>
</a>
</li>
<li>
<a id="tour" href="/tour/workflow">
<span>
Tour
</span>
</a>
</li>
</ul>
What issue does the addition of a span do for you here?
<snip signature>
For example the menu in the following web site:
http://www.springloops.com/

I have seen a lot of this lately.
Turn off JavaScript and reload the page. They have the spans because
there are using JavaScript to do and image replacement for the text on
the links...unless you are doing such JavaScript slight-of-hand the span
is not needed.
--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: erik | last post by:
Is it posssible to inherit the previous pages (parent pages) background image? Is there a script out there I could look at? Thanks
12
by: Stanimir Stamenkov | last post by:
Here are two cases regarding inline-level elements' line-height, padding and background, which I doesn't understand: <div style="background: black; color: white; line-height: 1.5">...
27
by: Kevin Yu | last post by:
When I declare on HTML page <LINK href="mycss.css" type="text/css" rel=stylesheet /> .... <BODY class=myclass> in mycss.css BODY { FONT-WEIGHT: bold; FONT-SIZE: 12px; FONT-FAMILY:...
3
by: MediaDesign | last post by:
so there's the problem: my text links have background images and when I put links around images on my page, they too get the background image treatment which I do not...I have tried several...
11
by: Konrad Den Ende | last post by:
I have a function returning a string but the problem is that the color of it is blue which suits me well for some pages but not for others. Is it possible to "feel" what the color of the background...
5
by: proximus | last post by:
Hi, I am trying to change the background of table TD's. The problem is that I have no access to the HTML code. SO I am trying to alter this using Javascript/DOM in an external .js file. I...
16
by: stevedude | last post by:
CSS newbie again. I have a problem trying to get coffee mug images within anchor tags to center with my link text for a vertical list menu. If I use the horizontal/vertical properties of...
19
by: david.karr | last post by:
If in my CSS I set the "background-color" property on the "body" element, it only covers the background of the elements defined in the body, up to the current width and height of the page. However,...
10
by: VividWeb | last post by:
Hi. I am relatively new to CSS and HTML but have a basic understanding of most things. One of my backgrounds is not positioning correctly in IE 7 or AOL. The background behind the content...
2
by: thephatp | last post by:
I'm having a problem with IE rendering correctly. I'm experimenting with using all div's in my pages now, and I'm not very familiar with the quirks of IE. I have created a sample page, and I'm...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.