Align text in a table header

后端 未结 8 2148
生来不讨喜
生来不讨喜 2021-02-06 20:37

It seems align is not working for the th element. Here is my HTML:

相关标签:
8条回答
  • 2021-02-06 20:46

    Try:

    text-align: center;
    

    You may be familiar with the HTML align attribute (which has been discontinued as of HTML 5). The align attribute could be used with tags such as

    <table>, <td>, and <img> 
    

    to specify the alignment of these elements. This attribute allowed you to align elements horizontally. HTML also has/had a valign attribute for aligning elements vertically. This has also been discontinued from HTML5.

    These attributes were discontinued in favor of using CSS to set the alignment of HTML elements.

    There isn't actually a CSS align or CSS valign property. Instead, CSS has the text-align which applies to inline content of block-level elements, and vertical-align property which applies to inline level and table cells.

    0 讨论(0)
  • 2021-02-06 20:47

    In HTML5, the easiest, and fastest, way to center your <th>THcontent</th> is to add a colspan like this:

    <th colspan="3">Thcontent</th> 
    

    This will work if your table is three columns. So if you have a four-column table, add a colspan of 4, etc.

    You can manage the location furthermore in the CSS file while you have put your colspan in HTML like I said.

    th { 
        text-align: center; /* Or right or left */
    }
    
    0 讨论(0)
  • 2021-02-06 20:48

    Try to use text-align in style attribute to align center.

    <th class="not_mapped_style" style="text-align:center">DisplayName</th>
    
    0 讨论(0)
  • 2021-02-06 20:54

    Your code works, but it uses deprecated methods to do so. You should use the CSS text-align property to do this rather than the align property. Even so, it must be your browser or something else affecting it. Try this demo in Chrome (I had to disable normalize.css to get it to render).

    • Source: http://jsfiddle.net/EDSWL/
    • Demo: http://jsfiddle.net/EDSWL/show
    0 讨论(0)
  • 2021-02-06 20:59

    HTML:

    <tr>
        <th>Language</th>
        <th>Skill Level</th>
        <th>&nbsp;</th>
    </tr>
    

    CSS:

    tr, th {
        padding: 10px;
        text-align: center;
    }
    
    0 讨论(0)
  • 2021-02-06 21:04

    For me none of the above worked. I think it is because I have two levels of header and a fixed width on level 1. So I couldn't align the text inside the corresponding columns on level 2.

    +---------------------------+
    |           lvl 1           |
    +---------------------------+
    | lvl 2 col a | lvl 2 col b |
    +---------------------------+
    

    I had to use the combination of width:auto and text:align-center :

    <th style="width:auto;text-align:center">lvl 2 col a</th>
    <th style="width:auto;text-align:center">lvl 2 col b</th>
    
    0 讨论(0)
提交回复
热议问题