Home >Web Front-end >Front-end Q&A >How to achieve the hidden effect of table content in css

How to achieve the hidden effect of table content in css

PHPz
PHPzOriginal
2023-04-26 16:38:04956browse

In web design, tables are commonly used layout tools. However, when the content of the table exceeds the width of the container, it will cause defects in the page layout. In order to solve this problem, we can use the overflow attribute in CSS to hide the content of the table.

In CSS, the overflow attribute has four possible values: visible (default value, allowing content to exceed the container), hidden (the excess part is hidden), scroll (adding scroll bars to the container) and auto (displaying necessary scrollbars). For tables, we can use hidden or auto values ​​to make excess cells or rows hidden or scrolled.

The following is a simple example of placing 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 place it inside a CSS container that contains the overflow property Medium:

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

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

Content that exceeds the container portion will display scroll bars horizontally or vertically. If you want the scroll bar to be displayed under all circumstances, you can use the scroll value.

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

This will display horizontal and vertical scroll bars.

In short, we can use the overflow attribute in CSS to properly handle the table content when it exceeds the container, making the layout of the page more beautiful.

The above is the detailed content of How to achieve the hidden effect of table content in css. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn