Avatar of oxygen_728
oxygen_728

asked on 

HTML CSS - How do I make all table cells within a DIV center text?

What I have: A table with many cells.

What I want to do: I want to put a DIV around the table that causes all cells within the table to have "text-align: center"

I'm a CSS novice, but I think there's some easy way to do this, such as:

<css file>
.ClassID div td{
  text-align: center;
}

<html file>

<div class="ClassID">
   <table with many cells>
</div>

What am I missing?

Thanks
CSSHTML

Avatar of undefined
Last Comment
glcummins
Avatar of Matthew Kelly
Matthew Kelly
Flag of United States of America image

The div will center the table, not the cells.

To center the cells you could add style="text-align:center" to the
ASKER CERTIFIED SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of oxygen_728
oxygen_728

ASKER

glcummins,

But how do I implement that solution in my html?

Do I have to put a class="ClassID" on every one of my  tags?

Thanks
Avatar of Matthew Kelly
Matthew Kelly
Flag of United States of America image

Or this to do the div.
<html>
<head>
 <style type="text/css">
	.ClassID table{
	text-align: center;
	}
 </style>
</head>
 
<body>
<div>
 <table border=1 cellpadding=0 cellspacing=0 width="100%">
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>1</td><td>2</td><td>3</td></tr>
 </table>
</div>
<div class="ClassID">
 <table border=1 cellpadding=0 cellspacing=0 width="100%">
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>1</td><td>2</td><td>3</td></tr>
 </table>
</div>
</body>
</html>

Open in new window

Avatar of oxygen_728
oxygen_728

ASKER

Actually, I just got it to work by putting the div around the table with the appropriate class ID.

I guess everything inside the DIV inherits the DIV's class
SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
HTML
HTML

HTML (HyperText Markup Language) is the main markup language for creating web pages and other information to be displayed in a web browser, providing both the structure and content for what is sent from a web server through the use of tags. The current implementation of the HTML specification is HTML5.

62K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo