html - 使用 column-count 时避免将一个元素分成两列

标签 html css css-multicolumn-layout

我正在尝试使用多列制作一组 。 但是它将一个 (Link4) 分成两列。 :( 只需将鼠标悬停在 Link4 上。它的一部分被拆分到第二列。 :( 有什么办法可以避免这种情况。

div.Nav {
  height: 100% !important;
}

div.Nav a.icon {
  position: absolute;
  right: 0;
  top: 0;
}

div.Nav a {
  transition: 0.5s;
  line-height: 25px;
  text-align: center;
  font-weight: lighter;
  color: black;
  padding: 8px 16px;
  text-decoration: none;
  font-size: 20px;
  float: none !important;
  display: block;
  text-align: left;
}

div.Nav a.active {
  background-color: #4CAF50;
  color: white;
  cursor: default;
}

div.Nav a:hover:not(.active) {
  background-color: rgb(56, 62, 50);
  color: #fffd78;
  transition: 0.5s;
}

div.Nav div.newspaper {
  -webkit-column-count: 3;
  -moz-column-count: 3;
  column-count: 3;
  -webkit-column-rule: 2px grey solid;
  -moz-column-rule: 2px grey solid;
  column-rule: 2px grey solid;
  -webkit-column-gap: 0;
  -moz-column-gap: 0;
  column-gap: 0;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

<body>
  <div id="Nav" class="Nav">
    <a href="javascript:void(0)" class="active"><i class="fa fa-home fa-lg" aria-hidden="true"></i></a>
    <div class="newspaper">
      <a href="javascript:void(0)" class="show">Link1</a>
      <a href="javascript:void(0)" target="_self">Link2</a>
      <a href="javascript:void(0)" target="_self">Link3</a>
      <a href="javascript:void(0)" target="_self">Link4</a>
      <a href="javascript:void(0)" target="_self">Link5</a>
      <a href="javascript:void(0)" target="_self">Link6</a>
      <a href="javascript:void(0)" target="_self">Link7</a>
      <a href="javascript:void(0)" target="_self">Link8</a>
      <a href="javascript:void(0)" target="_self">Link9</a>
      <a href="javascript:void(0)" target="_self">Link10</a>
    </div>
  </div>
</body>

</html>

最佳答案

使用 break-inside: avoid;(或适当的等价物)来防止标签流入下一列:

div.Nav {
  height: 100% !important;
}

div.Nav a.icon {
  position: absolute;
  right: 0;
  top: 0;
}

div.Nav a {
  transition: 0.5s;
  line-height: 25px;
  text-align: center;
  font-weight: lighter;
  color: black;
  padding: 8px 16px;
  text-decoration: none;
  font-size: 20px;
  float: none !important;
  display: block;
  text-align: left;
  
  -webkit-column-break-inside: avoid; /* Chrome, Safari, Opera */
  page-break-inside: avoid; /* Firefox */
  break-inside: avoid; /* IE 10+ */
}

div.Nav a.active {
  background-color: #4CAF50;
  color: white;
  cursor: default;
}

div.Nav a:hover:not(.active) {
  background-color: rgb(56, 62, 50);
  color: #fffd78;
  transition: 0.5s;
}

div.Nav div.newspaper {
  -webkit-column-count: 3;
  -moz-column-count: 3;
  column-count: 3;
  -webkit-column-rule: 2px grey solid;
  -moz-column-rule: 2px grey solid;
  column-rule: 2px grey solid;
  -webkit-column-gap: 0;
  -moz-column-gap: 0;
  column-gap: 0;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

<body>
  <div id="Nav" class="Nav">
    <a href="javascript:void(0)" class="active"><i class="fa fa-home fa-lg" aria-hidden="true"></i></a>
    <div class="newspaper">
      <a href="javascript:void(0)" class="show">Link1</a>
      <a href="javascript:void(0)" target="_self">Link2</a>
      <a href="javascript:void(0)" target="_self">Link3</a>
      <a href="javascript:void(0)" target="_self">Link4</a>
      <a href="javascript:void(0)" target="_self">Link5</a>
      <a href="javascript:void(0)" target="_self">Link6</a>
      <a href="javascript:void(0)" target="_self">Link7</a>
      <a href="javascript:void(0)" target="_self">Link8</a>
      <a href="javascript:void(0)" target="_self">Link9</a>
      <a href="javascript:void(0)" target="_self">Link10</a>
    </div>
  </div>
</body>

</html>

关于html - 使用 column-count 时避免将一个元素分成两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603333/

相关文章:

javascript - css - bootstrap carousel 依赖项,需要导入哪些文件才能工作

jquery - slider 中的图像无法正常工作

css - Bootstrap : Clutter Columns In Concurrent Use Push & Pull Column Ordering

html - CSS3 列未按预期填充宽度且元素位置困惑

html - 为什么我的专栏内容从不同的高度开始?

html - CSS 网格的复杂混合

html - 为什么我的内容中心不会

javascript - 如何实现滑动监听器?

html - 如何将 handlebars 表达式包装在 span 标签中,并使 span 标签可点击?

html - 在不破坏子元素的情况下将 div 拆分为多列