1

I have a simple HTML Table:

table,
th,
td {
  border: 1px solid black;
}
<h1>The table width attribute</h1>

<table width="400">
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
</table>

when i enter into lots of data (for example: instead of 100$ i enter 1000000000000000$) it keeps spreading. i tried to limit it's width to like 10px and overflow-y auto and didnt work...

any idea how i can limit the amout of chars inside and auto enter?

Thanks in advance.

1
  • would you mind accepting my answer if you think it worked for you? Commented Mar 16, 2020 at 6:38

2 Answers 2

2

You can use something like this.

The width you gave to your table was 400 so if you can do that then you should limit it.

there is CSS property called overflow-wrap this will help you to bring the extra content in that block.

td {
  max-width:20px;
 overflow-wrap: break-word;
}
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
}
</style>
</head>
<body>

<h1>The table width attribute</h1>

<table width="400">
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$8000000000000000000000000000000000000000000000000000000000000</td>
  </tr>
</table>
</body>
</html>

OR

Another option is to give a selector class to td and provide css property to only that class, that should also work for you.

Sign up to request clarification or add additional context in comments.

1 Comment

@Manjuboyz- Thanks a lot! seems like that max-width did the trick :)
0

You can use text-overflow: ellipsis; or refer to w3schools

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.