Flexbox内部边框和负边距

时间:2018-11-01 14:04:14

标签: html css css3 flexbox

我只想为内部伸缩项目创建边框,而没有伸缩容器的外部边框。我尝试对margin使用负值来基于此codepen隐藏外部边界,但在我的情况下,外部边界没有隐藏。

.view {
  display: flex;
  flex-direction: column;
  border: none;
}

.container {
  display: flex;
  flex-wrap: wrap;
  margin: 0 -5px -5px 0;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  background: yellow;
  padding: 20px;
  border-right: 1px solid black;
  border-bottom: 1px solid black;
}

.head {
  text-align: center;
}
<div class="view">
<div class="container">
   <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Total balance</div>
   </div>
   <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Available balance</div>
   </div>
   <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Orders</div>
   </div>
   <div class="content">
      <div class="sc-eTyWNx dIQzIE">500</div>
      <div class="head">Wallet balance</div>
   </div>
   <div class="content">
      <button class="sc-gzVnrw cnAjsC" type="button">Send</button>
   </div>
</div>
</div>

2 个答案:

答案 0 :(得分:1)

您只是忘了添加overflow:hidden,以便使用负边距隐藏在外部制作的边框:

.view {
  display: flex;
  flex-direction: column;
  border: none;
}

.container {
  display: flex;
  flex-wrap: wrap;
  margin: 0 -5px -5px 0;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  background: yellow;
  padding: 20px;
  border-right: 1px solid black;
  border-bottom: 1px solid black;
}

.head {
  text-align: center;
}
<div class="view">
  <div class="container">
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Total balance</div>
    </div>
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Available balance</div>
    </div>
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Orders</div>
    </div>
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">500</div>
      <div class="head">Wallet balance</div>
    </div>
    <div class="content">
      <button class="sc-gzVnrw cnAjsC" type="button">Send</button>
    </div>
  </div>
</div>

以防万一,您将保留黄色,这是另一个可以使用伪元素覆盖不需要的边距的想法:

.container {
  display: flex;
  flex-wrap: wrap;
  position:relative;
}
.container:before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  border:1px solid yellow;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  background: yellow;
  padding: 20px;
  border-left: 1px solid black;
  border-bottom: 1px solid black;
}

.head {
  text-align: center;
}
<div class="container">
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Total balance</div>
    </div>
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Available balance</div>
    </div>
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Orders</div>
    </div>
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">500</div>
      <div class="head">Wallet balance</div>
    </div>
    <div class="content">
      <button class="sc-gzVnrw cnAjsC" type="button">Send</button>
    </div>
  </div>

答案 1 :(得分:0)

您可以赋予.content(除最后一个以外的所有 )所有的边框权吗?

.view {
  display: flex;
  flex-direction: column;
  border: none;
}

.container {
  display: flex;
  flex-wrap: wrap;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  background: yellow;
  padding: 20px;
}

.content:not(:last-child) {
  border-right: 1px solid black;
}

.head {
  text-align: center;
}
<div class="view">
  <div class="container">
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Total balance</div>
    </div>
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Available balance</div>
    </div>
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">0</div>
      <div class="head">Orders</div>
    </div>
    <div class="content">
      <div class="sc-eTyWNx dIQzIE">500</div>
      <div class="head">Wallet balance</div>
    </div>
    <div class="content">
      <button class="sc-gzVnrw cnAjsC" type="button">Send</button>
    </div>
  </div>
</div>