为什么我的专栏内容从不同的高度开始?
Why are my columns' contents starting at different heights?
我读到不应使用表格作为布局方式,而应使用 CSS 进行所有样式设置,因此我尝试了我能想到的最基本的方法来 模仿 个表格,使用 CSS 多列布局。
我创建了一个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>★☆☆☆ ‒ <br>
★★☆☆ ‒ <br>
★★★☆ ‒ <br>
★★★★ ‒
</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>
我想就此 post 提供更新,以防有人无意中发现它。
1) 标记的答案是我的改进,但我仍然觉得它不直观,因为它使用默认的 flex 流,将主轴放在水平方向上。
从逻辑的角度来看,这应该是列。
因此这里有一个更新的解决方案,这是最简单直观的(至少在我看来):
.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">★☆☆☆ ‒</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">★★☆☆ ‒</span>
<span class="skill"></span>
intermediate knowledge, used several times
</li>
<li class="skill-item">
<span class="rating"> ★★★☆ ‒</span>
<span class="skill"></span>
good undestanding, used frequently
</li>
<li class="skill-item">
<span class="rating">★★★★ ‒</span>
<span class="skill"></span>
deep understanding, used on a daily basis
</li>
</ul>
</div>
您可以将 padding: 1px 0
添加到 .align-left
和 .align-right
这两个容器中。这样一来,p
标签的默认顶部和底部边距就保留在它们的容器内,不会像您的代码笔那样由于折叠边距而导致任何垂直偏移。
编辑
如果您想使用已有的代码。删除设置为浏览器的段落 "p" 标记的边距。然后将行高设置为你想要的高度。
原始答案
如果你想轻松地排列你的项目,你会想要使用类似 flexbox 的东西。您可能还想设置行高,以确保您的 unicode 图标和文本正确对齐
这将允许您完全控制文本和图标对齐的位置。 More info on flexbox at W3schools
p{
margin: 0;
line-height: 1.5em;
}
弹性框示例:
.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>★☆☆☆ ‒ <br>
★★☆☆ ‒ <br>
★★★☆ ‒ <br>
★★★★ ‒</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>★☆☆☆ ‒ I can put it into context and am able to use it's basics</li>
<li>★★☆☆ ‒ intermediate knowledge, used several times</li>
<li>★★★☆ ‒ good undestanding, used frequently</li>
<li>★★★★ ‒ deep understanding, used on a daily basis</li>
</ul>
您的问题是 p
元素的边距正在折叠,就像 div 垂直堆叠时一样。 .
但是,你的概念是有缺陷的。您可能真的想将评级与技能相关联
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">★☆☆☆ ‒</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">★★☆☆ ‒</div>
<div class="skill">intermediate knowledge, used several times</div>
</li>
<li class="skill-item">
<div class="rating">★★★☆ ‒</div>
<div class="skill">good undestanding, used frequently</div>
</li>
<li class="skill-item">
<div class="rating"> ★★★★ ‒</div>
<div class="skill">deep understanding, used on a daily basis</div>
</li>
</ul>
我读到不应使用表格作为布局方式,而应使用 CSS 进行所有样式设置,因此我尝试了我能想到的最基本的方法来 模仿 个表格,使用 CSS 多列布局。
我创建了一个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>★☆☆☆ ‒ <br>
★★☆☆ ‒ <br>
★★★☆ ‒ <br>
★★★★ ‒
</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>
我想就此 post 提供更新,以防有人无意中发现它。
1) 标记的答案是我的改进,但我仍然觉得它不直观,因为它使用默认的 flex 流,将主轴放在水平方向上。
从逻辑的角度来看,这应该是列。
因此这里有一个更新的解决方案,这是最简单直观的(至少在我看来):
.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">★☆☆☆ ‒</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">★★☆☆ ‒</span>
<span class="skill"></span>
intermediate knowledge, used several times
</li>
<li class="skill-item">
<span class="rating"> ★★★☆ ‒</span>
<span class="skill"></span>
good undestanding, used frequently
</li>
<li class="skill-item">
<span class="rating">★★★★ ‒</span>
<span class="skill"></span>
deep understanding, used on a daily basis
</li>
</ul>
</div>
您可以将 padding: 1px 0
添加到 .align-left
和 .align-right
这两个容器中。这样一来,p
标签的默认顶部和底部边距就保留在它们的容器内,不会像您的代码笔那样由于折叠边距而导致任何垂直偏移。
编辑
如果您想使用已有的代码。删除设置为浏览器的段落 "p" 标记的边距。然后将行高设置为你想要的高度。
原始答案
如果你想轻松地排列你的项目,你会想要使用类似 flexbox 的东西。您可能还想设置行高,以确保您的 unicode 图标和文本正确对齐
这将允许您完全控制文本和图标对齐的位置。 More info on flexbox at W3schools
p{
margin: 0;
line-height: 1.5em;
}
弹性框示例:
.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>★☆☆☆ ‒ <br>
★★☆☆ ‒ <br>
★★★☆ ‒ <br>
★★★★ ‒</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>★☆☆☆ ‒ I can put it into context and am able to use it's basics</li>
<li>★★☆☆ ‒ intermediate knowledge, used several times</li>
<li>★★★☆ ‒ good undestanding, used frequently</li>
<li>★★★★ ‒ deep understanding, used on a daily basis</li>
</ul>
您的问题是 p
元素的边距正在折叠,就像 div 垂直堆叠时一样。
但是,你的概念是有缺陷的。您可能真的想将评级与技能相关联
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">★☆☆☆ ‒</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">★★☆☆ ‒</div>
<div class="skill">intermediate knowledge, used several times</div>
</li>
<li class="skill-item">
<div class="rating">★★★☆ ‒</div>
<div class="skill">good undestanding, used frequently</div>
</li>
<li class="skill-item">
<div class="rating"> ★★★★ ‒</div>
<div class="skill">deep understanding, used on a daily basis</div>
</li>
</ul>