<div class = "centered"> <h4> block要素</h4> <div class = "blk"> <span class = "text red" data-content = " Css "> Css </span> <span class = "text green" data-content = " Is "> Is </span> <span class = "text blue" data-content = " Awesome. "> Awesome. </span> </div> <h4> inline-block要素</h4> <div> <span class = "text red" data-content = " Css "> Css </span> <span class = "text green" data-content = " Is "> Is </span> <span class = "text blue" data-content = " Awesome. "> Awesome. </span> </div> </div>
.centered{ position:absolute; top:340px; left:50%; width:auto; transform:translate(-50%,-50%); } .text{ font-size:28px; background:#efefef; padding:5px 0; position:relative; margin-top:5px; margin-right:5px; float:left; } .text:last-child{ margin-right:0; } .blk .text{ display:block; float:none; margin:0; } .text::after{ background:rgb(33,33,33); content:attr(data-content); position:absolute; color:#fff; left:0px; top:0px; padding:inherit; width:0; transition: all 700ms cubic-bezier(0.51, 0.04, 0.12, 0.99); overflow:hidden; } .text:hover::after{ width:100%; visibility:visible; } .text.red::after{ background:rgb(250,56,56); } .text.green::after{ background:rgb(56,200,56); } .text.blue::after{ background:rgb(56,56,250); }