显示在工具提示顶部的内容

时间:2021-05-27 11:51:44

标签: html css

我正在为我的网站上的内容设置工具提示。工具提示外的内容显示在我的工具提示之一上

Demo Link:

代码:

/* 
  You want a simple and fancy tooltip?
  Just copy all [data-tooltip] blocks:
*/

[data-tooltip] {
  position: relative;
  z-index: 10;
}


/* Positioning and visibility settings of the tooltip */

[data-tooltip]:before,
[data-tooltip]:after {
  position: absolute;
  visibility: hidden;
  opacity: 0;
  left: 50%;
  bottom: calc(100% + 5px);
  /* 5px is the size of the arrow */
  pointer-events: none;
  transition: 0.2s;
  will-change: transform;
}


/* The actual tooltip with a dynamic width */

[data-tooltip]:before {
  content: attr(data-tooltip);
  padding: 10px 18px;
  min-width: 50px;
  max-width: 300px;
  width: max-content;
  width: -moz-max-content;
  border-radius: 6px;
  font-size: 14px;
  background-color: rgba(59, 72, 80, 0.9);
  background-image: linear-gradient(30deg, rgba(59, 72, 80, 0.44), rgba(59, 68, 75, 0.44), rgba(60, 82, 88, 0.44));
  box-shadow: 0px 0px 24px rgba(0, 0, 0, 0.2);
  color: #fff;
  text-align: center;
  white-space: pre-wrap;
  transform: translate(-50%, -5px) scale(0.5);
}


/* Tooltip arrow */

[data-tooltip]:after {
  content: '';
  border-style: solid;
  border-width: 5px 5px 0px 5px;
  /* CSS triangle */
  border-color: rgba(55, 64, 70, 0.9) transparent transparent transparent;
  transition-duration: 0s;
  /* If the mouse leaves the element, 
                              the transition effects for the 
                              tooltip arrow are "turned off" */
  transform-origin: top;
  /* Orientation setting for the
                              slide-down effect */
  transform: translateX(-50%) scaleY(0);
}


/* Tooltip becomes visible at hover */

[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
  visibility: visible;
  opacity: 1;
}


/* Scales from 0.5 to 1 -> grow effect */

[data-tooltip]:hover:before {
  transition-delay: 0.3s;
  transform: translate(-50%, -5px) scale(1);
}


/* 
  Arrow slide down effect only on mouseenter (NOT on mouseleave)
*/

[data-tooltip]:hover:after {
  transition-delay: 0.5s;
  /* Starting after the grow effect */
  transition-duration: 0.2s;
  transform: translateX(-50%) scaleY(1);
}


/*
  That's it.
*/


/*
  If you want some adjustability
  here are some orientation settings you can use:
*/


/* LEFT */


/* Tooltip + arrow */

[data-tooltip-location="left"]:before,
[data-tooltip-location="left"]:after {
  left: auto;
  right: calc(100% + 5px);
  bottom: 50%;
}


/* Tooltip */

[data-tooltip-location="left"]:before {
  transform: translate(-5px, 50%) scale(0.5);
}

[data-tooltip-location="left"]:hover:before {
  transform: translate(-5px, 50%) scale(1);
}


/* Arrow */


/* BOTTOM */

[data-tooltip-location="bottom"]:before,
[data-tooltip-location="bottom"]:after {
  top: calc(100% + 5px);
  bottom: auto;
}

[data-tooltip-location="bottom"]:before {
  transform: translate(-50%, 5px) scale(0.5);
}

[data-tooltip-location="bottom"]:hover:before {
  transform: translate(-50%, 5px) scale(1);
}

[data-tooltip-location="bottom"]:after {
  border-width: 0px 5px 5px 5px;
  border-color: transparent transparent rgba(55, 64, 70, 0.9) transparent;
  transform-origin: bottom;
}


/* Settings that make the pen look nicer */

html {
  width: 100%;
  height: 100%;
  font-family: 'Roboto', sans-serif;
  color: white;
  font-size: 1.2em;
  background: linear-gradient(45deg, #243949, #2cacd1, #35eb93);
  background-size: 120% 120%;
  animation: moveFocus 5s ease infinite alternate;
}

@keyframes moveFocus {
  0% {
    background-position: 0% 100%
  }
  100% {
    background-position: 100% 0%
  }
}

body {
  background: none;
  display: flex;
  flex-direction: column;
  height: 100%;
  margin: 0;
}

main {
  padding: 0 4%;
  display: flex;
  flex-direction: row;
  margin: auto 0;
}

button {
  margin: 0;
  padding: 0.7rem 1.4rem;
  cursor: pointer;
  text-align: center;
  border: none;
  border-radius: 4px;
  outline: inherit;
  text-decoration: none;
  font-family: Roboto, sans-serif;
  font-size: 0.7em;
  background-color: rgba(174, 184, 192, 0.55);
  color: white;
  -webkit-appearance: none;
  -moz-appearance: none;
  transition: background 350ms ease-in-out, transform 150ms ease;
}

button:hover {
  background-color: #484f56;
}

button:active {
  transform: scale(0.98);
}

button:focus {
  box-shadow: 0 0 2px 2px #298bcf;
}

button::-moz-focus-inner {
  border: 0;
}

.example-elements {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  align-content: center;
  justify-content: center;
  text-align: center;
  padding-right: 4%;
}

.example-elements p {
  padding: 6px;
  display: inline-block;
  margin-bottom: 5%;
  color: #fff;
}

.example-elements p:hover {
  border-left: 1px solid lightgrey;
  border-right: 1px solid lightgrey;
  padding-left: 5px;
  padding-right: 5px;
}

.example-elements a {
  margin-left: 6px;
  margin-bottom: calc(5% + 10px);
  color: #76daff;
  text-decoration: none;
}

.example-elements a:hover {
  margin-bottom: calc(5% + 9px);
  border-bottom: 1px solid #76daff;
}

.example-elements button {
  margin-bottom: 20px;
}

.info-wrapper {
  flex-grow: 8;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: justify;
  padding-left: 6%;
  border-left: 3px solid #35ea95;
}

.info-wrapper p {
  color: rgba(255, 255, 255, 0.69);
}

.info-wrapper p {
  max-width: 600px;
  text-align: justify;
}

.info-wrapper .title-question {
  display: block;
  color: #fff;
  font-size: 1.36em;
  font-weight: 500;
  padding-bottom: 24px;
}

@media (max-height: 450px) {
  main {
    margin: 2rem 0;
  }
}

@media (max-width: 800px) {
  html {
    font-size: 0.9em;
  }
}


/* Thumbnail settings */

@media (max-width: 750px) {
  html {
    animation-duration: 0.6s;
    font-size: 1em;
  }
  body {
    display: flex;
    background: none;
    height: 100%;
    margin: 0px;
  }
  main {
    font-size: 1.1em;
    padding: 6%;
  }
  .info-wrapper p:before,
  .info-wrapper p:after {
    display: none;
  }
  .example-elements {
    max-width: 150px;
    font-size: 22px;
  }
  .example-elements a,
  button {
    display: none;
  }
  .example-elements p:before,
  .example-elements p:after {
    visibility: visible;
    opacity: 1;
  }
  .example-elements p:before {
    content: "Tooltip";
    font-size: 20px;
    transform: translate(-50%, -5px) scale(1);
  }
  .example-elements p:after {
    transform: translate(-50%, -1px) scaleY(1);
  }
  [data-tooltip]:after {
    bottom: calc(100% + 3px);
  }
  [data-tooltip]:after {
    border-width: 7px 7px 0px 7px;
  }
}
<main>
  <div class="info-wrapper">
    <p>
      <span class="title-question">
        You want a simple, animated & easy-to-use tooltip?
      </span>
      <span>
        Just copy all the CSS declarations blocks starting with
        <code>[data-tooltip].</code>
      </span>
    </p>
    <p data-tooltip="This is an example of a super long tooltip text that goes over multiple lines. &#xa; &#xa; Note: The tooltip size is dynamically adjusted to the content. However, a minimum and a maximum width are defined." data-tooltip-location="bottom">
      To use the tooltip, simply add the attribute »data-tooltip« with the desired text to an element. P.S. Hover over me to see a long tooltip.
    </p>
    <p data-tooltip="This is an example of a super long tooltip text that goes over multiple lines. &#xa; &#xa; Note: The tooltip size is dynamically adjusted to the content. However, a minimum and a maximum width are defined." data-tooltip-location="top">
      To use the tooltip, simply add the attribute »data-tooltip« with the desired text to an element. P.S. Hover over me to see a long tooltip.
    </p>
  </div>
</main>

1 个答案:

答案 0 :(得分:3)

z-index 删除为 [data-tooltip]

z-index 添加到 [data-tooltip]:before, [data-tooltip]:after

[data-tooltip] {
  position: relative;
  /*z-index:1; */ /* Remove this */
}
[data-tooltip]:before,
[data-tooltip]:after {
  z-index:1;
}
相关问题