Help us understand the problem. What is going on with this article?

テーブル内で長い文字列を決まった長さで改行させる方法

More than 1 year has passed since last update.

テーブルの中に長い文字列が入る可能性があり、
テーブルセルの決まった長さで折り返し(改行)させたいときの対処法です

①テーブルセル幅を固定する設定

<table ~~~ style="table-layout:fixed;width:100%;">

これで幅が固定できます

②テーブルセル幅を設定

固定する設定ができていても、その長さが決まってないといけない、ってわけです
colgroupタグとcolタグでやってもらいましょう

<table ~~~ style="table-layout:fixed;width:100%;">
    <colgroup>
        <col style="width:33%;">
        <col style="width:33%;">
        <col style="width:33%;">
    </colgroup>
    <tbody>
        <tr><td>hey</td><td>hey</td><td>hey</td></tr>
        <tr><td>hey</td><td>hey</td><td>hey</td></tr>
        <tr><td>hey</td><td>hey</td><td>hey</td></tr>
    </tbody>
</table>

BootstrapなんかのCSSライブラリを使ってる場合はすでにできている可能性がありますね
こんな風に↓

<th class="col-xs-2">aaaaa</th>
<th class="col-xs-3">bbbbb</th>
<th class="col-xs-3">ccccc</th>
<th class="col-xs-2">ddddd</th>
<th class="col-xs-2">eeeee</th>

Bootstrapといえばグリッドシステムつかいますよね

③改行させる設定

さていよいよ本命の作業

<td ~~~ style="word-wrap:break-word;"></td>

これだけ。

以上テーブルでうまく改行させる方法でした。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした