问题
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).
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.
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.
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?
回答1:
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
回答2:
Some possible solutions floating through my head:
1) Set margins of blocks negative according to where they are. You could have classes (i.e. right-edge, left-edge, sandwiched) and manipulate the margins so the borders overlap.
2) Set the left and right borders to 0px for blocks in the middle. Two ways I'd go by:
CSS
.sandwiched {border:1px 0px 1px 0px;}
.sandwiched {border-left:0px; border-right:0px;}
EDIT: It's a good idea to learn about the whitespace annoyance linked to in the comment. Just be aware that applying that fix will shift the pixels you need to adjust.
来源:https://stackoverflow.com/questions/42215700/html-grid-layout-with-border-collapse