为什么我的列内容从不同的高度开始?

时间:2019-02-04 22:07:51

标签: html css css3 css-multicolumn-layout

我读到一个人不应该使用表格作为布局的方式,而应该使用CSS进行所有样式设置,因此我尝试了使用CSS multi来模拟 表格的最基本的方法-列布局。

我创建了一个CodePen来说明问题。如您所见,两列的内容从不同的高度开始,我不明白为什么。

.skill-explanation {
  -webkit-column-count: 2;
  -moz-column-count: 2;
  -ms-column-count: 2;
  column-count: 2;
}

.align-left {
  text-align: left;
}

.align-right {
  text-align: right;
}
<div class="skill-explanation">
  <div class="align-right">
    <p>&#9733;&#9734;&#9734;&#9734; &#8210; <br>
    &#9733;&#9733;&#9734;&#9734; &#8210; <br> 
    &#9733;&#9733;&#9733;&#9734; &#8210; <br> 
    &#9733;&#9733;&#9733;&#9733; &#8210;
   </p>
  </div>
  <div class="align-left">
    <p>I can put it into context and am able to use it's basics <br>
    intermediate knowledge, used several times <br>
    good undestanding, used frequently <br>
    deep understanding, used on a daily basis
    </p>
  </div>
</div>

我想对这篇文章进行更新,以防有人偶然发现。 1)标记的答案是对我的改进,但我仍然觉得它不直观,因为它使用默认的伸缩流,将主轴置于水平方向。
从逻辑的角度来看,应采用列tho的方式。

因此,这里有一个更新的解决方案,它是最简单直观的(至少在我看来):

.center-wrapper {
  margin: 0 auto;
  display:table;
}

.skill-explanation {
  list-style: none;
}

.rating {
}

.skill {
}

.skill-item {
  list-style: none;
}
<div class="center-wrapper" <ul class="skill-explanation">
  <li class="skill-item">
    <span class="rating">&#9733;&#9734;&#9734;&#9734; &#8210;</span>
    <span class="skill">I can put it into context and am able to use it's basics</span>
  </li>
  <li class="skill-item">
    <span class="rating">&#9733;&#9733;&#9734;&#9734; &#8210;</span>
    <span class="skill"></span>
    intermediate knowledge, used several times
  </li>
  <li class="skill-item">
    <span class="rating"> &#9733;&#9733;&#9733;&#9734; &#8210;</span>
    <span class="skill"></span>
    good undestanding, used frequently
  </li>

  <li class="skill-item">
    <span class="rating">&#9733;&#9733;&#9733;&#9733; &#8210;</span>
    <span class="skill"></span>
    deep understanding, used on a daily basis
  </li>
  </ul>
</div>

3 个答案:

答案 0 :(得分:2)

您可以将padding: 1px 0添加到两个容器.align-left.align-right中。这样,p标签的默认顶部和底部边距保留在容器内,不会像码笔一样由于边距的塌陷而引起任何垂直偏移。

https://codepen.io/anon/pen/YBxWbY

答案 1 :(得分:1)

编辑 如果您想使用已有的代码。从设置为浏览器的“ p”标签段落中删除边距。然后将线条高度设置为所需的高度。
原始答案
如果您想轻松排列项目,将需要使用诸如flexbox之类的工具。您可能还需要设置行高,以确保unicode图标和文本正确对齐
这将使您可以完全控制文本和图标的对齐位置。 More info on flexbox at W3schools

p{
  margin: 0;
  line-height: 1.5em;
}

Flex Box示例:

.skill-explanation {
  display: flex;
  align-items: center;
}
.align-left, .align-right {
  line-height: 1.5rem;
}

.skill-explanation {
  display: flex;
  align-items: center;
}
.align-left, .align-right {
  line-height: 1.5em;
}
<div class="skill-explanation">
          <div class="align-right">   
            <p>&#9733;&#9734;&#9734;&#9734; &#8210; <br>
              &#9733;&#9733;&#9734;&#9734; &#8210; <br>
              &#9733;&#9733;&#9733;&#9734; &#8210; <br>
              &#9733;&#9733;&#9733;&#9733; &#8210;</p>
          </div>
          <div class="align-left">
            <p>I can put it into context and am able to use it's basics <br>
              intermediate knowledge, used several times <br>
              good undestanding, used frequently <br>
              deep understanding, used on a daily basis</p>
          </div>
        </div>

但是您最好还是按照其他人的建议重新访问HTML代码并将其放入列表中,因为这样做在语义上会更正确并且更易于维护

  <ul>
    <li>&#9733;&#9734;&#9734;&#9734; &#8210; I can put it into context and am able to use it's basics</li>
    <li>&#9733;&#9733;&#9734;&#9734; &#8210; intermediate knowledge, used several times</li>
    <li>&#9733;&#9733;&#9733;&#9734; &#8210; good undestanding, used frequently</li>
    <li>&#9733;&#9733;&#9733;&#9733; &#8210; deep understanding, used on a daily basis</li>
  </ul>

答案 2 :(得分:1)

您的问题是p元素上的边距正在收缩,就像div垂直堆叠一样。 See Johannes answer

但是,您的概念有缺陷。您可能实际上想将等级与技能相关联

ul.skill-explanation {
  list-style: none;
  padding-left: 0;
}

.skill-item {
  display: flex;
}

.skill-item > div {
  width:50%;
}

.rating {padding-right: 0.5em; 
text-align:right;
}
<ul class="skill-explanation">
  <li class="skill-item">
    <div class="rating">&#9733;&#9734;&#9734;&#9734; &#8210;</div>
    <div class="skill">I can put it into context and am able to use it's basics</div>
  </li>
  <li class="skill-item">
    <div class="rating">&#9733;&#9733;&#9734;&#9734; &#8210;</div>
    <div class="skill">intermediate knowledge, used several times</div>
  </li>
  <li class="skill-item">
    <div class="rating">&#9733;&#9733;&#9733;&#9734; &#8210;</div>
    <div class="skill">good undestanding, used frequently</div>
  </li>

  <li class="skill-item">
    <div class="rating"> &#9733;&#9733;&#9733;&#9733; &#8210;</div>
    <div class="skill">deep understanding, used on a daily basis</div>
  </li>
</ul>