I. Introduction of Before/after
The Before and after pseudo-class selectors in CSS were introduced as early as CSS2, and the properties were supported by all major browsers.
Before and after-name, respectively, refers to the pseudo-element before/after the element to add content, by default they are display is inline, but you can use CSS set to block.
The application of before/and after is also relatively simple, for example:
A:after { content: "After"; Display: block; coloe:red;}
Can be seen in the browser, a tag element after a paragraph more than a text after. (Pseudo-class elements used in CSS3 are like a::after, but there is no difference between them at the moment).
(pseudo elements are not available through DOM, IE6/7 is not supported for this property)
After and before pseudo-elements have many uses, and can simplify the code, such as writing a counter, tip small triangle, clear floating ... Especially in the CSS3 some of the new attributes are enriched its application, here is a small tutorial, with before/after pseudo-elements to implement a pure CSS3 tooltip.
Second, the ToolTip implementation tutorial
Demo:https://hwlv.github.io/fullpage/test/after.html
Here we mainly use grass After/before pseudo element content in the attr attribute, first to see the implementation of the appearance:
1. Implementing Styles
2. Code
A tooltip small label appears after the mouse hover button.
The code is labeled first:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Demo</title> <style>. BTN{position:relative;Display:Block;margin:200px Auto;width:200px;padding:10px 20px;font-size:20px;background:#fff;Color:#6bdf4e;Border:1px solid #6bdf4e;cursor:Pointer; }. Btn::after{content:attr (Data-tip);Display:None;position:Absolute;padding:5px 10px; Left:50%;Bottom:100%;Margin-bottom:12px;Transform:TranslateX ( -50%);font-size:16px;background:#000;Color:#fff;cursor:default; }. Btn::before{content: " ";position:Absolute;Display:None; Left:50%;Bottom:100%;Transform:TranslateX ( -50%);Margin-bottom:3px;width:0;Height:0;Border-left:6px Solid Transparent;Border-right:6px Solid Transparent;Border-top:9px Solid #000; }. Btn:hover::after,. Btn:hover::before{Display:Block; } </style></Head><Body> <Buttonclass= "BTN"Data-tip= "ToolTip">button</Button></Body></HTML>
3. Implementation process
- One. Create a label, and then add an attribute to the label data-[] = "ToolTip", [] denotes a custom property name, and the quotation marks are the content that the TOOLTIP needs to display.
- Two. To add a style to the label, position is set to relative, since the pseudo-element needs to set the absolute positioning to set the position.
- Three. For after style, after is the tooltip that needs to be displayed, through Content:attr (data-tip); Get what you need to display, set absolute positioning, adjust position.
- Four. For before style, before needs to be set to a small triangular tip placed under after.
- Five. The display for After/before is set to none.
- Six. Set after/before display to block when selecting hover for element pseudo class that needs tooltip, it is important to note that the After/before default display is inline, So we created the debug before the display should be set to block first.
- Seven. Open the browser to see the effect
HTML TIP Implementation