944,051 Members | Top Members by Rank

Ad:
Oct 16th, 2009
0

Probably very simple text centering quesiton

Expand Post »
Hello all,

I am in the process of teaching myself CSS to go along with my beginner-intermediate knowledge of HTML. I am just starting with CSS and have created a small test file to attempt to create a table with one row and one cell in that row. I then want to display white text that is centered in the one cell. Below is the current code that I have:

HTML and CSS Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4. <title>eBay Test Ad</title>
  5. <style type="text/css">
  6. table.toptable
  7. {
  8. background-color: #000;
  9. margin-left: auto;
  10. margin-right: auto;
  11. border-collapse: collapse;
  12. display: block;
  13. width: 900px;
  14. position: relative;
  15. }
  16. span.testtext
  17. {
  18. text-align: center;
  19. color: #fff;
  20. font-family: Arial, sans serif;
  21. font-weight: bold;
  22. font-size: 32px;
  23. }
  24.  
  25.  
  26. </style>
  27. </head>
  28. <body>
  29. <table class="toptable">
  30. <tr>
  31. <td>
  32. <span class="testtext">This is only a test...</span>
  33. </td>
  34. </tr>
  35. </table>
  36. </body>
  37. </html>

It seems to me that by having the statement:

text-align: center;

in the definition of .testtext that it should center this text. I sure this is a very simple fix and I'm just missing something cursory, but any help would be greatly appreciated.

Thanks,
D
Similar Threads
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009
Oct 16th, 2009
0
Re: Probably very simple text centering quesiton
Please check if this solves your issue:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>eBay Test Ad</title>
<style type="text/css">
table.toptable
{
background-color: #000;
margin-left: auto;
margin-right: auto;
border-collapse: collapse;
display: block;
width: 900px;
position: relative;
}
.testtext
{
text-align:center;
color: #fff;
font-family: Arial, sans serif;
font-weight: bold;
font-size: 32px;
width:900px;
}
</style>
</head>
<body>
<table class="toptable">
<tr>
    <td class="testtext">This is only a test...</td>
</tr>
</table>
</body>
</html>
Reputation Points: 11
Solved Threads: 9
Light Poster
webexpertzhere is offline Offline
27 posts
since Oct 2009
Oct 16th, 2009
0
Re: Probably very simple text centering quesiton
webexpertzhere,

Thank you very much! It works now. I now understand that the cell itself has to be specified to a certain width for this to work. One more question I have is:

I understand why this works the way you have corrected it, but it seems like the primary thing that was changed was the addition of the width attribute and placing all of the attributes on the cell. After getting it to work, I attempted to add a width attribute to the cell and keep the rest of the attributes on the span and it did not work. Why is it that all of the attributes need to be attributed to the cell and cannot be attributed to the span?

Thanks again for your help.

Regards,
D
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009
Oct 16th, 2009
0
Re: Probably very simple text centering quesiton
If you definitely want to put the text inside a separate tag within the cell, you can proceed to use the 'div' tag. It will solve your purpose.

According to my understanding a span tag is normally used only to format text and not position it. But I am not sure if I am 100% right about it.
Reputation Points: 11
Solved Threads: 9
Light Poster
webexpertzhere is offline Offline
27 posts
since Oct 2009
Oct 16th, 2009
0
Re: Probably very simple text centering quesiton
If you still want to proceed with the 'span' tag include this property also in the class 'testtext' which you are applying for the span :
display:block;

When we dont use this property a span tag spans upto the length of the text only. That is why you are unable to bring it to the center.

Please let me know if this solves your problem
Reputation Points: 11
Solved Threads: 9
Light Poster
webexpertzhere is offline Offline
27 posts
since Oct 2009
Oct 17th, 2009
0
Re: Probably very simple text centering quesiton
Yes, it does. Thank you very much for your help.

Regards,
D
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009
Oct 17th, 2009
0
Re: Probably very simple text centering quesiton
You are welcome and Thanks for updating!
Reputation Points: 11
Solved Threads: 9
Light Poster
webexpertzhere is offline Offline
27 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: How would I do this in CSS? Newby pulling hair out.
Next Thread in HTML and CSS Forum Timeline: On zoom out, content cracks





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC