How do you change Background Color size around text?

jpb6891

Member
Messages
33
Reaction score
0
Points
6
How do you change background color size that is around the text? I know it involves something with Width and Height. I tried to do it and It did not work.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" /><title>index</title>
</head>
<body style="background-color: rgb(0, 153, 0); color: rgb(0, 0, 0);" alink="#ee0000" link="#0000ee" vlink="#551a8b">
<h1>Logo Goes Here</h1>
<p style="background-color: white;" home="">Home <a href="about.html">About Us</a> <a href="products.html">Products</a> <a href="contact.html">Contact Us</a></p>
<p>Text</p>
</body></html>

I am refering to the p style background color. It displays white text across the entire row of the page. I tried using google and found nothing. I want to get it so it only displays until the end of contact us.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Wrap the text in a 'span' and apply the style there, not in the 'p'
 

gflash76

New Member
Messages
25
Reaction score
0
Points
1
Have you tried linking it into a CSS file?? You'll have much better control over how to design your text and display images.
 

grimfusi

New Member
Messages
2
Reaction score
0
Points
0
You can do that a few ways, but it's all going to be with CSS. You have two options, though. You can apply in-line CSS; meaning all your CSS remains IN your HTML file, or you can apply an attached CSS stylesheet, which is an external file your website externally references.

Inline CSS is incorporated into HTML tags with the "style" declaration like this:
<span style="CSS GOES HERE;">TEXT HERE</span>

External stylesheets work like this:
HTML:
<span class="ThisIsASpan">TEXT HERE</span>
External CSS file:
.ThisIsASpan {
CSS GOES HERE;
}

Now that you have a rough idea of how CSS works, there are quite a few ways you can increase the space around your text. Placing it in a SPAN works, but it'd work better if you set the span to display as a block or inline-block, then you can specifically set the width or height as a pixel size or percentage. You can also put your text inside of a DIV. DIVs are already formatted as blocks unless specified otherwise, so you wouldn't need to declare a display type (but that depends on what kind of content surrounds the DIV).

I could go over margin and padding, but if we're just talking about text content and you'll never be adding image content to the SPAN or DIV, line-height is probably more preferable. Say you set your span or div to 50px height and your font-size is 22px. Normally the text would appear in the upper-left corner of the div and wouldn't be vertically centered. If you set your line-height to the height of your div or span, the text will become vertically centered without the necessity of accounting for extra padding or margin aside from the set width and height of your span or div.

Example 1:
<span style="display:block;width:400px;height:75px;text-size:22px;line-height:75px;text-align:center;">This is your text</span>

Example 2:
<div style="width:400px;height:75px;text-size:22px;line-height:75px;text-align:center;">This is your text</div>

Example 3:
HTML:
<span class="title">This is your text</span>
CSS:
.title {
display:block;
width:400px;
height:75px;
text-size:22px;
line-height:75px;
text-align:center;
}

Get it?
 

rajdeep01

Banned
Messages
66
Reaction score
0
Points
0
.team-member-teaser-name
{
color: #4fb4d0;
background: #135364;
margin-top: 5px;
margin-bottom: 5px;
padding-left: 5px;
font-size: 10px;
}
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
As long as this thread has been necro'd anyway, it's time to stop the madness.

You have A elements. Style them. Better yet, get all of that junk out of the paragraph it's living in right now -- it is most assuredly not a paragraph -- and put it into a list. And there is no good excuse for using inline styles on an (X)HTML page (although there may be -- I remain unconvinced, but it is a remote possibility -- an excuse for inline styles in text data stored elsewhere). For a single, stand-alone page (and for testing/development) putting the styling information into a STYLE element on the page is acceptable. For most purposes, though, a separate CSS file is a much, much better idea (it makes site-wide updates easier and the user only has to download it once, saving both you and your users bandwidth and time).

The menu is a list of links, so it should be a list. Specifically, it should be an unordered list (a UL element), with the ARIA landmark role of menu. The items in the list should have the ARIA role of menuitem. That makes the page much more accessible to users with disabilities -- and it makes it more accessible to search engines. (This, people, is where SEO starts.) You can style the list to appear as a menu bar rather than the default bullet list. There are many examples out there on the web (some actually correct); I will update this thread later with an example of how this page ought to look.

Speaking of accessibility (and usability), it's a really bad idea to change the link colours in body text. It's okay if your navigation areas (menus, footers) look different, but please leave the blue/dark magenta values alone unless your page background is something that would make the links difficult to see.

There is also no good reason to use XHTML anymore. It's a failed experiment, not "the wave of the future". XML isn't a document markup language, it's a data container, and free text doesn't fit the format. (Most sites/pages using XHTML would actually fail to display at all if the browsers really played by the rules. It's only Postel's Principle that keeps most of the web working.) Use HTML 5 unless you need to support older browsers; if you need to support older browsers (IE7 and below), HTML 4.01 Strict is the right doctype.

HTML:
<!DOCTYPE html>
<head>
   <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" />
   <title>index</title>
   <style>
      body {
         background-color: rgb(0, 153, 0);
         color: rgb(0, 0, 0);
         }
      #nav {
         list-style: none;
         padding: 0;
         text-indent: 0;
         margin: 1em 0;
         }
      #nav li {
         float: left;
         }
      #nav a, #nav span {
         display: block;
         padding: 0.5em 1em;
         background-color: white;
         text-decoration: none;
         }
      #nav:after {
         /* This is a "clearfix" for the menu. */
         /* It inserts an unfloated block-level element */
         /* after the last list item */
         display: block;
         clear: both;
         content: "";
         }
   </style>
</head>
<body>
   <h1>Logo Goes Here</h1>
   <!-- the main navigation menu has been transformed into a list -->
   <ul id="nav" role="menu">
      <li class="nav-entry" role="menuitem"><span class="nav-nolink">Home</span></li>
      <li class="nav-entry" role="menuitem"><a href="about.html">About Us</a></li>
      <li class="nav-entry" role="menuitem"><a href="products.html">Products</a></li>
      <li class="nav-entry" role="menuitem"><a href="contact.html">Contact Us</a></li>
   </ul>
   <p>Text</p>
</body>
</html>

That's just a basic means of creating the menu-like structure on a stand-alone page. Ideally, you would want to identify the major structural parts of your page (the header, the main navigator, the footer, etc.) and mark them up properly. HTML 5 includes proper sectioning elements, HEADER, NAV and FOOTER, but older browsers won't know what they're for. You can make them act properly using JavaScript; there are examples out there on the web like Modernizr. (The hipster web devs call it an "HTML5 shiv", which is wrong on so, so many levels. A "shiv" is a knife, usually homemade or illicit, used as a weapon rather than as a tool. This is a "shim"; something used to make parts fit together properly.)

The important part here is that your list of links is now a list of links, and it has been identified as a menu for screen readers and search engines. And you now have styling "hooks" to take control of all of them at thee same time. You would ordinarily want to use the :link and :visited pseudo-selectors to make menu items the same colour whether they've been visited or not (menus are special; all other links should appear different after they've been visited) and the :hover and :active pseudo-selectors to give the user some indication that they can click or have clicked on the menu items (touch pads don't do "hover").
 
Last edited:
Top