用css重现这种布局效果的方法?

时间:2016-07-18 06:16:50

标签: html css html-table

如果没有创建一个7列/ 3行表,其中1像素图像可以展开以模拟线条,那么如何使用HTML和CSS重现这种布局,而无需使用表格或图像来划分"分隔符& #34;

我真正喜欢的是线条不相交的方式。

enter image description here



<!-- layout reproduced -->
<TABLE BORDER="0" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
    <TR ALIGN="center">
        <TD CLASS="boldedcolor4text">the first</TD>
        <TD><IMG SRC="/gfx/Style1/color1.gif" ALT="" WIDTH="1" HEIGHT="20"></TD>
        <TD CLASS="boldedcolor4text">the second</TD>
        <TD><IMG SRC="/gfx/Style1/color1.gif" ALT="" WIDTH="1" HEIGHT="20"></TD>
        <TD CLASS="boldedcolor4text">the third</TD>
        <TD><IMG SRC="/gfx/Style1/color1.gif" ALT="" WIDTH="1" HEIGHT="20"></TD>
        <TD CLASS="boldedcolor4text">the fourth</TD>
    </TR>
    <TR ALIGN="center">
        <TD COLSPAN="7"><IMG SRC="/gfx/Style1/color1.gif" ALT="" WIDTH="520" HEIGHT="1"></TD>
    </TR>
    <TR ALIGN="center">
        <TD><A CLASS="image" HREF="><IMG SRC="1.jpg" ALT="" BORDER="0"></A></TD>
        <TD><IMG SRC="/gfx/Style1/color1.gif" ALT="" WIDTH="1" HEIGHT="135"></TD>
        <TD><A CLASS="image" HREF="><IMG SRC="2.jpg" ALT="" BORDER="0"></A></TD>
        <TD><IMG SRC="/gfx/Style1/color1.gif" ALT="" WIDTH="1" HEIGHT="135"></TD>
        <TD><A CLASS="image" HREF="><IMG SRC="3.jpg" ALT="" BORDER="0"></A></TD>
        <TD><IMG SRC="/gfx/Style1/color1.gif" ALT="" WIDTH="1" HEIGHT="135"></TD>
        <TD><A CLASS="image" HREF="><IMG SRC="4.jpg" ALT="" BORDER="0"></A></TD>
    </TR>
</TABLE>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:11)

我的解决方案没有依赖关系。我创建了一个flexbox .container,然后创建了四个flex .item。 flexbox的默认方向是row,这是我们想要的外部布局,但不是行中每个项目的内容。为了调整这一点,我将子项flex-direction.item)更改为column,因为在您的示例中,图像堆叠在锚点下方。

对于锚点和图像之间的非破坏边界,我采用了一些快捷方式并将样式移动到伪类。请注意position: relative;上的.item。这样做是为了创建一个安全的容器,在其中保持绝对定位的伪类边界。

&#13;
&#13;
.container, .item {
  display: flex;
}

.item {
  flex-direction: column;
  position: relative;
}

.item:before {
  content: '';
  border-top: 1px solid;
  position: absolute;
  width: 100%;
  top: 2em;
}

.item:first-child:before {
  left: 1.5em;
}

.item:last-child:before {
  left: -1.5em;
}

.item:last-child .link,
.item:last-child .img {
  border-right: none;
}

.link,
.img {
  border-right: 1px solid;  
}

.link {
  text-align: center;
}

.img {
  margin-top: 2em;
  padding: 0.5em 1em 0.5em;
}
&#13;
<div class="container">
  <div class="item">
    <a class="link" href="">link 1</a>
    <img class="img" src="http://placehold.it/100x130" alt="" />
  </div>
  <div class="item">
    <a class="link" href="">link 2</a>
    <img class="img" src="http://placehold.it/100x130" alt="" />
  </div>
  <div class="item">
    <a class="link" href="">link 3</a>
    <img class="img" src="http://placehold.it/100x130" alt="" />
  </div>
  <div class="item">
    <a class="link" href="">link 4</a>
    <img class="img" src="http://placehold.it/100x130" alt="" />
  </div>
</div>
&#13;
&#13;
&#13;

小提琴:https://jsfiddle.net/1sbx2h5e/

答案 1 :(得分:2)

您可以尝试:https://jsfiddle.net/wh48rjvt/

SCSS:

$border: 1px solid grey;
.table-row {
  border-bottom: $border;
  &:last-child {
    border-bottom: 0;
  }
  .table-box {
    border-right: $border;
    margin: 0.75rem 0;
    &:last-child {
      border-right: 0;
    }
  }
}

这使得以下CSS:

.table-row {
  border-bottom: 1px solid grey;
}
.table-row:last-child {
  border-bottom: 0;
}
.table-row .table-box {
  border-right: 1px solid grey;
  margin: 0.75rem 0;
}
.table-row .table-box:last-child {
  border-right: 0;
}

HTML:

<div class="container">
  <div class="table-row row">
    <div class="table-box col-xs-3">1</div>
    <div class="table-box col-xs-3">2</div>
    <div class="table-box col-xs-3">3</div>
    <div class="table-box col-xs-3">4</div>
  </div>
  <div class="table-row row">
    <div class="table-box col-xs-3">5</div>
    <div class="table-box col-xs-3">6</div>
    <div class="table-box col-xs-3">7</div>
    <div class="table-box col-xs-3">8</div>
  </div>
  ...
</div>

我使用Bootstrap作为网格。