在我的网站上,我有一个网格,其技能评分为1-5,含星号。当您将鼠标悬停在一行上时,您会看到提示信息,用于解释星级:
问题是,在较小的屏幕上,某些技能名称包含多行,这会使工具提示跳了很多:
下面是该源的最小重新创建版本,没有全部内容。音符的唯一选择器是tooltip
和tooltip-text
。基本上,我检查了W3 Schools's tooltips以学习如何创建自己的代码。
它的工作原理:您将tooltip
包装器及其嵌套的tooltip-text
添加到任何要悬停的元素内。该父元素需要将位置设置为relative
。这允许tooltip
包装器以100%的宽度和高度进行绝对定位,因此,悬停父对象实际上等同于悬停工具提示。然后,您可以相对于tooltip
包装器定位文本。
但是是什么原因导致工具提示在较小的屏幕上未对齐?
NOTE:请原谅HTML格式不正确。我超出了字符数限制,不得不即兴创作。为节省空间,满星用f
表示,空星用e
表示。
.tooltip {
height: 100%;
position: absolute;
width: 100%;
z-index: 2;
}
.tooltip .tooltip-text {
align-items: center;
background-color: #555;
color: white;
display: none;
font-size: 1.4rem;
justify-content: center;
left: 0;
border-radius: 5px;
padding: 5px;
position: relative;
}
.tooltip .tooltip-text::after {
content: "";
position: absolute;
border-width: 5px;
border-style: solid;
}
.tooltip .tooltip-top {
bottom: calc(100% + 10px);
}
.tooltip .tooltip-top::after {
top: 100%;
left: 50%;
margin-left: -5px;
border-color: #555 transparent transparent transparent;
}
.tooltip .tooltip-right {
left: calc(100% + 10px);
margin-left: 10px;
}
.tooltip .tooltip-right::after {
top: 50%;
right: 100%;
margin-top: -5px;
border-color: transparent #555 transparent transparent;
}
.tooltip .tooltip-bottom {
top: calc(100% + 10px);
}
.tooltip .tooltip-bottom::after {
bottom: 100%;
left: 50%;
margin-left: -5px;
border-color: transparent transparent #555 transparent;
}
.tooltip .tooltip-left {
margin-right: 10px;
right: 100%;
}
.tooltip .tooltip-left::after {
top: 50%;
left: 100%;
margin-top: -5px;
border-color: transparent transparent transparent #555;
}
.tooltip:hover .tooltip-text {
display: flex;
}
#skills #skill-grid {
column-gap: 100px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
row-gap: 40px;
}
#skills .skill-category {
font-size: 1.8rem;
margin-bottom: 20px;
}
#skills .skill-item {
column-gap: 10px;
display: grid;
grid-template-columns: 1fr 1fr;
margin-bottom: 10px;
position: relative;
}
#skills .skill-item .tooltip-text {
max-width: 100px;
left: calc(100% - 100px);
}
#skills .skill-item .skill-name {
grid-column: 1;
}
#skills .skill-item .skill-rating {
align-self: center;
display: inline;
grid-column: 2;
text-align: right;
}
#skills .skill-item .skill-rating .star {
vertical-align: middle;
}
.star {
height: 18px;
width: 18px;
}
<section id="skills" class="container section">
<h2 class="heading-with-image">
<span>Skills and Abilities</span>
</h2>
<div id="skill-grid">
<div>
<h3 class="skill-category">Programming Languages</h3>
<div class="skill-item">
<span class="skill-name">C++</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Python</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">TypeScript (ES6)</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Kotlin</span>
<div class="skill-rating">ffffe</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">C# (Unity)</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Frontend & Backend</h3>
<div class="skill-item">
<span class="skill-name">HTML5</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">CSS/SASS/LESS</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">React (+Ant Design)</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">SQL (Oracle, postgres)</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">NodeJS, Express</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Software Development</h3>
<div class="skill-item">
<span class="skill-name">OOP</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Debugging</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Code review</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Scrum</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Refactoring</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Game dev</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Design patterns</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Operating Systems</h3>
<div class="skill-item">
<span class="skill-name">Windows (7-10)</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Linux (Ubuntu)</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Linux kernel</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">VMs (VirtualBox)</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">macOS (Mojave)</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">IDEs and Editors</h3>
<div class="skill-item">
<span class="skill-name">VS Code</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Visual Studio</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Qt Creator</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Android Studio</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Collaboration</h3>
<div class="skill-item">
<span class="skill-name">Git, GitHub</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Slack</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">G Suite</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Jira</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Confluence</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Languages</h3>
<div class="skill-item">
<span class="skill-name">English</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Armenian</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">German</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Other</h3>
<div class="skill-item">
<span class="skill-name">Copy editing</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Technical writing</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Copy writing</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
</div>
</div>
</section>
问题是,绝对位置和底部位置在不同的元素上,因此,css底部给出的结果与预期有所不同。
tooltip-top
必须为position:absolute,因为您正在计算该位置的底数
从MDN引用:
底部是父母身高+ 10像素,因此对于占据2行的父母来说,身高更大并且移动超过1行父母。当位置为绝对时,即使占据2条线的元素的高度更大,底部也将从该父对象的底部开始计算,因此高度间接无效,并且其行为符合预期。
.tooltip-text .tooltip-top {
position: absolute;
bottom: calc(50% + 10px);
}
.tooltip {
height: 100%;
position: absolute;
width: 100%;
z-index: 2;
}
.tooltip .tooltip-text {
align-items: center;
background-color: #555;
color: white;
display: none;
font-size: 1.4rem;
justify-content: center;
left: 0;
border-radius: 5px;
padding: 5px;
position: relative;
}
.tooltip .tooltip-text::after {
content: "";
position: absolute;
border-width: 5px;
border-style: solid;
}
.tooltip .tooltip-top {
bottom: calc(50% + 10px);
position: absolute;
}
.tooltip .tooltip-top::after {
top: 100%;
left: 50%;
margin-left: -5px;
border-color: #555 transparent transparent transparent;
}
.tooltip .tooltip-right {
left: calc(100% + 10px);
margin-left: 10px;
}
.tooltip .tooltip-right::after {
top: 50%;
right: 100%;
margin-top: -5px;
border-color: transparent #555 transparent transparent;
}
.tooltip .tooltip-bottom {
top: calc(100% + 10px);
}
.tooltip .tooltip-bottom::after {
bottom: 100%;
left: 50%;
margin-left: -5px;
border-color: transparent transparent #555 transparent;
}
.tooltip .tooltip-left {
margin-right: 10px;
right: 100%;
}
.tooltip .tooltip-left::after {
top: 50%;
left: 100%;
margin-top: -5px;
border-color: transparent transparent transparent #555;
}
.tooltip:hover .tooltip-text {
display: flex;
}
#skills #skill-grid {
column-gap: 100px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
row-gap: 40px;
}
#skills .skill-category {
font-size: 1.8rem;
margin-bottom: 20px;
}
#skills .skill-item {
column-gap: 10px;
display: grid;
grid-template-columns: 1fr 1fr;
margin-bottom: 10px;
position: relative;
}
#skills .skill-item .tooltip-text {
max-width: 100px;
left: calc(100% - 100px);
}
#skills .skill-item .skill-name {
grid-column: 1;
}
#skills .skill-item .skill-rating {
align-self: center;
display: inline;
grid-column: 2;
text-align: right;
}
#skills .skill-item .skill-rating .star {
vertical-align: middle;
}
.star {
height: 18px;
width: 18px;
}
<section id="skills" class="container section">
<h2 class="heading-with-image">
<span>Skills and Abilities</span>
</h2>
<div id="skill-grid">
<div>
<h3 class="skill-category">Programming Languages</h3>
<div class="skill-item">
<span class="skill-name">C++</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Python</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">TypeScript (ES6)</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Kotlin</span>
<div class="skill-rating">ffffe</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">C# (Unity)</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Frontend & Backend</h3>
<div class="skill-item">
<span class="skill-name">HTML5</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">CSS/SASS/LESS</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">React (+Ant Design)</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">SQL (Oracle, postgres)</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">NodeJS, Express</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Software Development</h3>
<div class="skill-item">
<span class="skill-name">OOP</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Debugging</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Code review</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Scrum</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Refactoring</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Game dev</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Design patterns</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Operating Systems</h3>
<div class="skill-item">
<span class="skill-name">Windows (7-10)</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Linux (Ubuntu)</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Linux kernel</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">VMs (VirtualBox)</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">macOS (Mojave)</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">IDEs and Editors</h3>
<div class="skill-item">
<span class="skill-name">VS Code</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Visual Studio</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Qt Creator</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Android Studio</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Collaboration</h3>
<div class="skill-item">
<span class="skill-name">Git, GitHub</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Slack</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">G Suite</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Jira</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Confluence</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Languages</h3>
<div class="skill-item">
<span class="skill-name">English</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Armenian</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">German</span>
<div class="skill-rating">
fffee
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Intermediate
</div>
</div>
</div>
</div>
<div>
<h3 class="skill-category">Other</h3>
<div class="skill-item">
<span class="skill-name">Copy editing</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Technical writing</span>
<div class="skill-rating">
fffff
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Advanced
</div>
</div>
</div>
<div class="skill-item">
<span class="skill-name">Copy writing</span>
<div class="skill-rating">
ffffe
</div>
<div class="tooltip">
<div class="tooltip-text tooltip-top">
Competent
</div>
</div>
</div>
</div>
</div>
</section>