How does css realize the beyond-hidden effect of table content

05-25-2023

This article mainly explains how css can realize the beyond-hidden effect of table content. Interested friends may wish to take a look. The method described in this article is simple, quick and practical. Next, let the editor take you to learn how css can realize the beyond-hidden effect of table content!


In web design, tables are commonly used layout tools. However, when the content of the table exceeds the width of the container, it can cause page layout artifacts. In order to solve this problem, we can use the overflow property in css to realize the hidden content of the table.

In css, the overflow attribute has four possible values: visible (default value, allowing content to exceed the container), hidden (exceeding part is hidden), scroll (adding a scroll bar to the container) and auto ( display the necessary scroll bars). For tables, we can use hidden or auto values, so that the excess cells or rows are hidden or scrolled.

Here is a simple example that places a table with multiple rows and columns of cells inside a container:

<div class="table-container">  <table>    <thead>      <tr>        <th>Header 1</th>        <th>Header 2</th>        <th>Header 3</th>        <th>Header 4</th>        <th>Header 5</th>        <th>Header 6</th>        <th>Header 7</th>        <th>Header 8</th>        <th>Header 9</th>        <th>Header 10</th>      </tr>    </thead>    <tbody>      <tr>        <td>1</td>        <td>2</td>        <td>3</td>        <td>4</td>        <td>5</td>        <td>6</td>        <td>7</td>        <td>8</td>        <td>9</td>        <td>10</td>      </tr>      <tr>        <td>11</td>        <td>12</td>        <td>13</td>        <td>14</td>        <td>15</td>        <td>16</td>        <td>17</td>        <td>18</td>        <td>19</td>        <td>20</td>      </tr>      <tr>        <td>21</td>        <td>22</td>        <td>23</td>        <td>24</td>        <td>25</td>        <td>26</td>        <td>27</td>        <td>28</td>        <td>29</td>        <td>30</td>      </tr>      <tr>        <td>31</td>        <td>32</td>        <td>33</td>        <td>34</td>        <td>35</td>        <td>36</td>        <td>37</td>        <td>38</td>        <td>39</td>        <td>40</td>      </tr>      <tr>        <td>41</td>        <td>42</td>        <td>43</td>        <td>44</td>        <td>45</td>        <td>46</td>        <td>47</td>        <td>48</td>        <td>49</td>        <td>50</td>      </tr>      <tr>        <td>51</td>        <td>52</td>        <td>53</td>        <td>54</td>        <td>55</td>        <td>56</td>        <td>57</td>        <td>58</td>        <td>59</td>        <td>60</td>      </tr>      <tr>        <td>61</td>        <td>62</td>        <td>63</td>        <td>64</td>        <td>65</td>        <td>66</td>        <td>67</td>        <td>68</td>        <td>69</td>        <td>70</td>      </tr>      <tr>        <td>71</td>        <td>72</td>        <td>73</td>        <td>74</td>        <td>75</td>        <td>76</td>        <td>77</td>        <td>78</td>        <td>79</td>        <td>80</td>      </tr>      <tr>        <td>81</td>        <td>82</td>        <td>83</td>        <td>84</td>        <td>85</td>        <td>86</td>        <td>87</td>        <td>88</td>        <td>89</td>        <td>90</td>      </tr>      <tr>        <td>91</td>        <td>92</td>        <td>93</td>        <td>94</td>        <td>95</td>        <td>96</td>        <td>97</td>        <td>98</td>        <td>99</td>        <td>100</td>      </tr>    </tbody>  </table></div>

We put it in a packageIn a CSS container with overflow property:

.table-container { overflow: auto; max-width: 800px; }

This will ensure that the table content can be scrolled or hidden when needed within an area with a maximum width of 800 pixels.

The content that exceeds the container will display scroll bars in the horizontal or vertical direction. Use the scroll value if you wish to display scrollbars in all cases.

.table-container { overflow-x: scroll; overflow-y: scroll; max-width: 800px; }

This will display horizontal and vertical scroll bars.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us