CSS-给li边框

时间:2019-01-30 06:53:02

标签: html css css-grid

我正在尝试在列表中加上边框。我为此使用李。我已经创建了一些东西,并在上面附加了内容,请检查一下。我使用额外的div做到了。但是我需要删除所有多余的div,仅使用css来像这样给边框。我们能做到吗?

.counter {
  position: relative;
  max-width: 600px;
}

.counter ul {
  display: inline-block;
  max-width: 600px;
  margin: 0 auto;
  width: 100%;
}

.counter li {
  float: left;
  width: 33.33%;
  height: 100px;
}

.counter li .c-grid {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  align-content: stretch;
  height: 100%;
  width: 100%;
}

.line-1 {
  height: 1px;
  width: 100%;
  background: #000;
  position: absolute;
  left: 0;
  right: 0;
  top: 65%;
}

.line-2 {
  height: 1px;
  width: 100%;
  background: #000;
  position: absolute;
  left: 0;
  right: 0;
  top: 35%;
}

.line-3 {
  height: 100%;
  width: 1px;
  background: #000;
  position: absolute;
  right: 35%;
  top: 0;
  bottom: 0;
}

.line-4 {
  height: 100%;
  width: 1px;
  background: #000;
  position: absolute;
  right: 65%;
  top: 0;
  bottom: 0;
}
<div class="counter">
  <div class="line-1"></div>
  <div class="line-2"></div>
  <div class="line-3"></div>
  <div class="line-4"></div>
  <ul>
    <li>
      <div class="c-grid"> 1 </div>
    </li>
    <li>
      <div class="c-grid"> 2 </div>
    </li>
    <li>
      <div class="c-grid"> 3 </div>
    </li>
    <li>
      <div class="c-grid"> 4 </div>
    </li>
    <li>
      <div class="c-grid"> 5 </div>
    </li>
    <li>
      <div class="c-grid"> 6 </div>
    </li>
    <li>
      <div class="c-grid"> 7 </div>
    </li>
    <li>
      <div class="c-grid"> 8 </div>
    </li>
    <li>
      <div class="c-grid"> 9 </div>
    </li>
  </ul>
</div>

3 个答案:

答案 0 :(得分:0)

添加list-style-position:inside;

.borderlist {
list-style-position:inside;
border: 1px solid black;

}

答案 1 :(得分:0)

在这里,我删除了四行div,并使用了li边框,您可以在其中添加尽可能多的行,它将继续跟随 还添加了box-sizing: border-box;以避免破坏网格

* {
  box-sizing: border-box;
}
.counter {
  position: relative;
  max-width: 600px;
}

.counter ul {
  display: inline-block;
  max-width: 600px;
  margin: 0 auto;
  width: 100%;
  padding: 0;
}

.counter li {
  float: left;
  width: 33.33333%;
  height: 100px;
}

.counter li .c-grid {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  align-content: stretch;
  height: 100%;
  width: 100%;
}

li {
  border-right: 1px solid #000;
  border-bottom: 1px solid #000;
}

li:nth-child(3n) {
  border-right: none;
}

li:nth-last-child(-n+3),
li:nth-last-child(-n+2),
li:last-child {
  border-bottom: none;
}
<div class="counter">
  <ul>
    <li>
      <div class="c-grid"> 1 </div>
    </li>
    <li>
      <div class="c-grid"> 2 </div>
    </li>
    <li>
      <div class="c-grid"> 3 </div>
    </li>
    <li>
      <div class="c-grid"> 4 </div>
    </li>
    <li>
      <div class="c-grid"> 5 </div>
    </li>
    <li>
      <div class="c-grid"> 6 </div>
    </li>
    <li>
      <div class="c-grid"> 7 </div>
    </li>
    <li>
      <div class="c-grid"> 8 </div>
    </li>
    <li>
      <div class="c-grid"> 9 </div>
    </li>
  </ul>
</div>

答案 2 :(得分:0)

最简单的方法是将CSS Grid与某些nth-child边框样式一起使用。使用这种方法,我们可以删除:

  • 不必要的<div>
  • 浮动
  • 多余的样式

html, body, ul {
  margin: 0;
  padding: 0;
}

ul {
  list-style: none;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

ul > li {
  padding: 20px 15px;
  text-align: center;
}

/* First 6 items */
ul > li:nth-child(-n + 6)  {
  border-bottom: 1px solid black;
}

/* Every 3rd item, starting from the 2nd item */
ul > li:nth-child(3n + 2) {
  border-left: 1px solid black;
  border-right: 1px solid black;  
}
<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>
</ul>

enter image description here

https://jsfiddle.net/2hg9zr4q/3/