Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade For Teachers Spaces Get Certified Upgrade For Teachers Spaces
   ❮     
     ❯   

CSS Tutorial

CSS HOMECSS IntroductionCSS SyntaxCSS SelectorsCSS How ToCSS CommentsCSS ErrorsCSS ColorsCSS BackgroundsCSS BordersCSS MarginsCSS PaddingCSS Height/WidthCSS Box ModelCSS OutlineCSS TextCSS FontsCSS IconsCSS LinksCSS ListsCSS TablesCSS DisplayCSS Max-widthCSS PositionCSS Z-indexCSS OverflowCSS FloatCSS Inline-blockCSS AlignCSS CombinatorsCSS Pseudo-classesCSS Pseudo-elementsCSS OpacityCSS Navigation BarsCSS DropdownsCSS Image GalleryCSS Image SpritesCSS Attr SelectorsCSS FormsCSS CountersCSS UnitsCSS InheritanceCSS SpecificityCSS !importantCSS Math FunctionsCSS OptimizationCSS AccessibilityCSS Website Layout

CSS Advanced

CSS Rounded CornersCSS Border ImagesCSS BackgroundsCSS ColorsCSS Color KeywordsCSS GradientsCSS ShadowsCSS Text EffectsCSS Custom FontsCSS 2D TransformsCSS 3D TransformsCSS TransitionsCSS AnimationsCSS TooltipsCSS Image StylingCSS Image ModalCSS Image CenteringCSS Image FiltersCSS Image ShapesCSS object-fitCSS object-positionCSS MaskingCSS ButtonsCSS PaginationCSS Multiple ColumnsCSS User InterfaceCSS VariablesCSS @propertyCSS Box SizingCSS Media QueriesCSS MQ Examples

CSS Flexbox

Flexbox IntroFlex ContainerFlex ItemsFlex Responsive

CSS Grid

Grid IntroGrid ContainerGrid ItemsGrid 12-column LayoutCSS @supports

CSS Responsive

RWD IntroRWD ViewportRWD Grid ViewRWD Media QueriesRWD ImagesRWD VideosRWD FrameworksRWD Templates

CSS SASS

SASS Tutorial

CSS Examples

CSS TemplatesCSS ExamplesCSS EditorCSS SnippetsCSS QuizCSS ExercisesCSS WebsiteCSS SyllabusCSS Study PlanCSS Interview PrepCSS BootcampCSS Certificate

CSS References

CSS ReferenceCSS SelectorsCSS CombinatorsCSS Pseudo-classesCSS Pseudo-elementsCSS At-rulesCSS FunctionsCSS Reference AuralCSS Web Safe FontsCSS AnimatableCSS UnitsCSS PX-EM ConverterCSS ColorsCSS Color ValuesCSS Default ValuesCSS Browser Support

CSSTooltip


CSS Tooltip

A CSS tooltip is used to specify extra information about something when the user moves the mouse pointer over an element:

TopTooltip text
RightTooltip text
BottomTooltip text
LeftTooltip text


CSS Create a Basic Tooltip

Create a tooltip that appears when the user moves the mouse over an element:

Example

<style>
/* Tooltip container */
.tooltip {
  position: relative;
  display: inline-block;
  border-bottom: 1px dotted black; /* Add dots under the hoverable text */
  cursor: pointer;
}

/* Tooltip text */
.tooltiptext {
  visibility: hidden; /* Hidden by default */
  width: 130px;
  background-color: black;
  color: #ffffff;
  text-align: center;
  padding: 5px 0;
  border-radius: 6px;
  position: absolute;
  z-index: 1; /* Ensure tooltip is displayed above content */
}

/* Show the tooltip text on hover */
.tooltip:hover .tooltiptext {
  visibility: visible;
}
</style>

<div>Hover over me
  <span class="tooltiptext">Some tooltip text</span>
</div>
Try it Yourself »

Example Explained

HTML:

  • Use a container element (like <div>) and add the"tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text.
  • The tooltip text is placed inside an inline element (like <span>) withclass="tooltiptext".

CSS:

  • Thetooltip class useposition:relative, which is needed to position the tooltip text (position:absolute).Tip: See examples below on how to position the tooltip.
  • Thetooltiptext class holds the actual tooltip text. It is hidden by default, and will be visible on hover.
  • The:hover selector is used to show the tooltip text when the user moves the mouse over the <div> withclass="tooltip".


Positioning the Tooltip

You can position the tooltip as you like. Here we will show how to position the tooltip to the left, right, top and bottom.

Right- and Left-aligned Tooltip

In this example, the tooltip is placed to the right (left:105%) of the "hoverable" text (<div>). Also note thattop:-5px is used to place it in the middle of its container element. We use the number5 because the tooltip text has a top and bottom padding of 5px. If you increase its padding, also increase the value of thetop property to ensure that it stays in the middle (if this is something you want). The same applies if you want the tooltip placed to the left.

Example

Right-aligned tooltip:

.tooltiptext {
  top: -5px;
  left: 105%;
}

Result:

Hover over meTooltip text
Try it Yourself »

Example

Left-aligned tooltip: 

.tooltiptext {
  top: -5px;
  right: 105%;
}

Result:

Hover over meTooltip text
Try it Yourself »

Top- and Bottom-aligned Tooltip

If you want the tooltip to appear on top or on the bottom, see examples below. Note that we use themargin-left property with a value of minus 65 pixels. This is to center the tooltip above/below the hoverable text. It is set to the half of the tooltip's width (130/2 = 65).

Example

Top-aligned tooltip: 

.tooltiptext {
  width: 130px;
  bottom: 100%;
  left: 65%;
  margin-left: -65px; /* Use half of the width (130/2 = 65), to center the tooltip */
}

Result:

Hover over meTooltip text
Try it Yourself »

Example

Bottom-aligned tooltip: 

 .tooltiptext {
  width: 130px;
  top: 100%;
  left: 50%;
  margin-left: -65px; /* Use half of the width (130/2 = 65), to center the tooltip */
}

Result:

Hover over meTooltip text
Try it Yourself »

Tooltip Arrows

To create an arrow that should appear from a specific side of the tooltip, add "empty" content after tooltip, with the pseudo-element class::after together with thecontent property. The arrow itself is created using borders. This will make the tooltip look like a speech bubble.

This example demonstrates how to add an arrow to the bottom of the tooltip:

Bottom Arrow

.tooltiptext::after {
  content: " ";
  position: absolute;
  top: 100%; /* At the bottom of the tooltip */
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: black transparent transparent transparent;
}

Result:

Hover over meTooltip text
Try it Yourself »

Example Explained

Position the arrow inside the tooltip:top: 100% will place the arrow at the bottom of the tooltip.left: 50% will center the arrow.

Note: Theborder-width property specifies the size of the arrow. If you change this, also change themargin-left value to the same. This will keep the arrow centered.

Theborder-color is used to transform the content into an arrow. We set the top border to black, and the rest to transparent. If all sides were black, you would end up with a black square box.

This example demonstrates how to add an arrow to the top of the tooltip. Notice that we set the bottom border color this time:

Top Arrow

.tooltiptext::after {
  content: " ";
  position: absolute;
  bottom: 100%;  /* At the top of the tooltip */
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent transparent black transparent;
}

Result:

Hover over meTooltip text
Try it Yourself »

This example demonstrates how to add an arrow to the left of the tooltip:

Left Arrow

.tooltiptext::after {
  content: " ";
  position: absolute;
  top: 50%;
  right: 100%; /* To the left of the tooltip */
  margin-top: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent black transparent transparent;
}

Result:

Hover over meTooltip text
Try it Yourself »

This example demonstrates how to add an arrow to the right of the tooltip:

Right Arrow

.tooltiptext::after {
  content: " ";
  position: absolute;
  top: 50%;
  left: 100%; /* To the right of the tooltip */
  margin-top: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent transparent transparent black;
}

Result:

Hover over meTooltip text
Try it Yourself »

Fade-in Tooltip

If you want a tooltip that fades in, use the CSStransition property and theopacity property, and go from being completely invisible to 100% visible, in a number of specified seconds (2 second in our example):

Example

.tooltiptext {
  opacity: 0;
  transition: opacity 2s;
}

.tooltip:hover .tooltiptext {
  opacity: 1;
}
Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp