Getting rid of width attribute and using something else to line up both texts

痞子三分冷 提交于 2021-02-11 15:38:59

问题


Is it possible to align these texts with each other automatically?

I want to align the pink word with group At the top so they are both matching at the same level

#title {
  float: left;
  margin-left: 15px;
  padding-left: 15px;
  padding-top: 5px;
  font-size: 0.5vw;
  font-family: 'Armata';
  text-shadow: 0 0 80px rgba(255, 255, 255, 0.5);
  text-align: center;
}
<!-- This the table texts at the top -->

<table id="topTable" width="100%">
  <tr class="heading">
    <th width='1%'>ID</th>
    <th width='15%'>Name</th>
    <th width='5%'>Money</th>
    <th width='25%'>Job</th>
    <th width='15%'>Group</th>
    <th width='5%'>WP</th>
    <th width='5%'>Ping</th>
    <th width='1%'>Country</th>
  </tr>
</table>

<!-- This the pink table i want to align with the one at the top -->

<tr class="playersTable">
  <th width="5%">' ..playerID.. '</th>
  <th width="25%">' .. sanitize(name) .. '</th>
  <th width="15%">' .. (money) .. '</th>
  <th width="15%">' .. job .. '</th>
  <th width="15%">' .. group .. '</th>
  <th width="5%">' .. wantedLevel .. '</th>
  <th width="5%">' .. ping .. '</th>
  <th width="15%">' .. country .. '</th>
</tr>

I want to get rid of this width stuff and hopefully do it automatically, sorry if anything is not clear.


回答1:


i would get rid of the % widths and try to combine the tables into one. use css to set width of each column if necessary and align the columns to your liking. doing so would give you consistent horizontal text alignment on all columns and control over how you want to style each row or column accordingly.

.table {
  width: 100%;
  
}

.table tr {
  text-align: left;
}

.table th {
  /* style however you like for the header row here */
  font-weight: bold;
}


/* use n-th child value select which column you'd like to style differently from default */
.table td:nth-child(5), .table th:nth-child(5) {
  text-align: right;
}
<table class="table">
  <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Money</th>
    <th>Job</th>
    <th>Group</th>
    <th>WP</th>
    <th>Ping</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>' ..playerID.. '</td>
    <td>' .. sanitize(name) .. '</td>
    <td>' .. (money) .. '</td>
    <td>' .. job .. '</td>
    <td>' .. group .. '</td>
    <td>' .. wantedLevel .. '</td>
    <td>' .. ping .. '</td>
    <td>' .. country .. '</td>
  </tr>
</table>


来源:https://stackoverflow.com/questions/61297130/getting-rid-of-width-attribute-and-using-something-else-to-line-up-both-texts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!