带边框折叠的 HTML 网格布局 [英] HTML grid layout with border-collapse

查看:38
本文介绍了带边框折叠的 HTML 网格布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页上的数据列表,我想将其显示为网格.传统上,我可能已经使用 <table> 完成了此操作,但数据在语义上不是表格形式,因此没有理由拥有固定数量的列(我希望单元格自动环绕, 并随着父元素的宽度变化而回流).

I have a list of data on a Web page that I want to display as a grid. Classically, I might have done this using <table>, but the data is not semantically tabular, and so there's no reason to have a fixed number of columns (I would like the cells to wrap around automatically, and reflow as the width of the parent element changes).

我可以通过将显示元素设置为 display: inline-block 并设置它们的 width 来做到这一点.这在布局方面是我想要的.但是,当我想设置 border 时,它也会导致问题;元素之间的边框显示为双倍,外部仅显示一个边框.

I can do this by setting the display elements to display: inline-block and setting their width. This does what I want in terms of layout. However, it also causes problems when I want to set a border; the border is shown doubled between elements, and only single on the outside.

如果我使用表格,我会设置border-collapse:collapse,这将使它起作用.但这似乎对 inline-block 解决方案没有任何作用.

If I were using a table, I would set border-collapse: collapse, which would make it work. But this doesn't seem to do anything with the inline-block solution.

如何折叠连续的 inline-block 元素之间的边框?或者是否有其他方法可以制作网格布局以允许这样做?

How can I collapse the borders between consecutive inline-block elements? Or is there some other way of making a grid layout to allow this?

推荐答案

现在您可能会使用 flex .

nowdays you might use flex .

对于边框,您可以从阴影中绘制它们并添加负边距以重叠它们:

For the borders, you may draw them from a shadow and add a negative margin to overlap them:

ul , li{
  display:flex;
  flex-wrap:wrap;
  padding:1em;
}
li {
  min-width:40px;/* you can let content decide */
  min-height:40px;/* you can let content decide */
  box-shadow:inset 0 0 0 1px;
  margin:0 -1px -1px 0;
  /* flex makes also x,y alignement easy */
  align-items:center;
  justify-content:center;
}

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

http://codepen.io/gc-nomade/pen/BpMpZb

这篇关于带边框折叠的 HTML 网格布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆