デモ
See the Pen tool tip by logsuke (@design-list) on CodePen.
コード
<main>
<section id="section1" class="section">
<button id="top" class="button">top</button>
<button id="right" class="button">right</button>
<button id="bottom" class="button">bottom</button>
<button id="left" class="button">left</button>
</section>
</main>
/*=== Base ===*/
* {
margin: 0;
padding: 0;
box-sizing: border-box
}
:root {
--nav-height: 8rem;
--max-width: 1024px;
--container-padding: 0 4rem;
--gap: 2rem;
--bg-color: rgb(240, 240, 240);
--first-color: rgb(172, 245, 255);
}
/*=== Font ratio ===*/
html {
scroll-behavior: smooth;
font-size: 62.5%;
}
body {
font-size: 1.6rem;
}
ul {
list-style: none;
}
a {
text-decoration: none;
}
.section {
width: var(--max-width);
margin: 0 auto;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
gap: 10rem;
}
/* tool tip */
.button {
width: 150px;
height: 50px;
padding: .5rem 1rem;
background: var(--first-color);
border: none;
font-size: 2rem;
border-radius: .5rem;
cursor: pointer;
position: relative;
}
/* top */
#top::before {
content: 'top';
position: absolute;
top: -75px;
left: 50%;
width: 100%;
transform: translate(-50%, 5px);
background: #ccc;
padding: 1rem 2rem;
border-radius: .5rem;
transition: transform .3s;
visibility: hidden;
user-select: none;
}
#top::after {
content: '';
border-top: 15px solid #ccc;
border-right: 15px solid transparent;
border-bottom: 15px solid transparent;
border-left: 15px solid transparent;
position: absolute;
top: -35px;
left: 50%;
transform: translate(-50%, 5px);
transition: transform .3s;
visibility: hidden;
user-select: none;
}
#top:hover::before, #top:hover::after {
visibility: visible;
user-select: auto;
transform: translate(-50%, 0);
}
/* right */
#right::before {
content: 'right';
position: absolute;
top: 50%;
right: calc(-100% - 75px);
width: 100%;
transform: translate(-5px, -50%);
background: #ccc;
padding: 1rem 2rem;
border-radius: .5rem;
transition: transform .3s;
visibility: hidden;
user-select: none;
}
#right::after {
content: '';
border-top: 15px solid transparent;
border-right: 15px solid #ccc;
border-bottom: 15px solid transparent;
border-left: 15px solid transparent;
position: absolute;
top: 0;
left: 0;
top: 50%;
right: -36px;
transform: translate(-5px, -50%);
transition: transform .3s;
visibility: hidden;
user-select: none;
}
#right:hover::before, #right:hover::after {
visibility: visible;
user-select: auto;
transform: translate(0, -50%);
}
/* bottom */
#bottom::before {
content: 'boottom';
position: absolute;
bottom: -75px;
left: 50%;
width: 100%;
transform: translate(-50%, -5px);
background: #ccc;
padding: 1rem 2rem;
border-radius: .5rem;
transition: transform .3s;
visibility: hidden;
user-select: none;
}
#bottom::after {
content: '';
border-top: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 15px solid #ccc;
border-left: 15px solid transparent;
position: absolute;
bottom: -35px;
left: 50%;
transform: translate(-50%, -5px);
transition: transform .3s;
visibility: hidden;
user-select: none;
}
#bottom:hover::before, #bottom:hover::after {
visibility: visible;
user-select: auto;
transform: translate(-50%, 0);
}
/* left */
#left::before {
content: 'left';
position: absolute;
top: 50%;
left: calc(-100% - 75px);
width: 100%;
transform: translate(5px, -50%);
background: #ccc;
padding: 1rem 2rem;
border-radius: .5rem;
transition: transform .3s;
visibility: hidden;
user-select: none;
}
#left::after {
content: '';
border-top: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 15px solid transparent;
border-left: 15px solid #ccc;
position: absolute;
top: 0;
left: 0;
top: 50%;
left: -36px;
transform: translate(5px, -50%);
transition: transform .3s;
visibility: hidden;
user-select: none;
}
#left:hover::before, #left:hover::after {
visibility: visible;
user-select: auto;
transform: translate(0, -50%);
}