简体   繁体   English

如何在 HTML/CSS 中居中文本

[英]How do I center text in HTML/CSS

Does anyone know why my top row is not center?有谁知道为什么我的顶行不是中心? I have attached the picture of my table for reference.我附上了我的桌子的图片以供参考。 The Product Sales headings are not center.产品销售标题不在中心。 In my html code you only need to pay attention to header-cell div class as that is where I wrote code for the top row.在我的 html 代码中,您只需要注意 header-cell div class 因为那是我为第一行编写代码的地方。 I tried all the centering techniques I know in the CSS file but none seem to work.我在 CSS 文件中尝试了所有我知道的居中技术,但似乎都没有奏效。 Any help would be appreciated!任何帮助,将不胜感激!

在此处输入图像描述

 .single-cell { margin-left: 0px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px; text-align: center; }.header-cell { margin-left: 3px; margin-right: 3px; margin-top: 3px; margin-bottom: 3px; text-align: center; align-items: center; justify-content: center; } table, th, td { border: 1px solid lightgray; border-collapse: collapse; text-align: center; vertical-align: middle; }
 <table> <tr> <div class="header-cell"> <tr> <b>Month</b> </tr> </div> <th *ngFor="let p of products"> <div class="header-cell"> <tr> <b>{{ p.salesLabel }}</b> </tr> </div> </th> </tr> <tr></tr> <tr> <td>1</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>2</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>3</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>4</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>5</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>6</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>7</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>8</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>9</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>10</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>11</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> <tr> <td>12</td> <th *ngFor="let p of products"> <div class="single-cell"> <tr> <input style="border: none" type="text" name="cogs-label" /> </tr> </div> </th> </tr> </table>

Your table structure is very wrong structured.您的表结构结构非常错误。 F.ex.前任a tr is not a child of a div, but either thead, tbody, tfoot or the table it self. tr 不是 div 的子项,而是thead、tbody、tfoot 或它自己的表。 Don't mix td and th.不要混合 td 和 th。 th is for thead and td is for tbody and tfoot. th 用于 thead,td 用于 tbody 和 tfoot。

 table { border-collapse: collapse; width: 100%; } table, th, td { border: 1px solid lightgray; } th, td { text-align: center; vertical-align: middle; }
 <table> <thead> <tr> <th>Month</th> <th>Product 1 Sales</th> <th>Product 2 Sales</th> <th>Product 3 Sales</th> </tr> </thead> <tbody> <tr> <td>1</td> <td></td> <td></td> <td></td> </tr> <tr> <td>2</td> <td></td> <td></td> <td></td> </tr> <tr> <td>3</td> <td></td> <td></td> <td></td> </tr> <tr> <td>4</td> <td></td> <td></td> <td></td> </tr> <tr> <td>5</td> <td></td> <td></td> <td></td> </tr> <tr> <td>6</td> <td></td> <td></td> <td></td> </tr> <tr> <td>7</td> <td></td> <td></td> <td></td> </tr> <tr> <td>8</td> <td></td> <td></td> <td></td> </tr> <tr> <td>9</td> <td></td> <td></td> <td></td> </tr> <tr> <td>10</td> <td></td> <td></td> <td></td> </tr> <tr> <td>11</td> <td></td> <td></td> <td></td> </tr> <tr> <td>12</td> <td></td> <td></td> <td></td> </tr> </tbody> </table>

How to create a table如何创建表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM