首頁  >  問答  >  主體

正確應用`:after`偽元素到styled components的React.js指南

我發現了這個漂亮的底線動畫

<ul>
  <li><a href="#">关于</a></li>
  <li><a href="#">作品集</a></li>
  <li><a href="#">博客</a></li>
  <li><a href="#">联系</a></li>
</ul>

body,html {
  margin: 0;
  font: bold 14px/1.4 'Open Sans', arial, sans-serif;
  background: #000;
}
ul { 
  margin: 150px auto 0; 
  padding: 0; 
  list-style: none; 
  display: table;
  width: 600px;
  text-align: center;
}
li { 
  display: table-cell; 
  position: relative; 
  padding: 15px 0;
}
a {
  color: #fff;
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 0.15em;
  
  display: inline-block;
  padding: 15px 20px;
  position: relative;
}
a:after {    
  background: none repeat scroll 0 0 transparent;
  bottom: 0;
  content: "";
  display: block;
  height: 2px;
  left: 50%;
  position: absolute;
  background: #fff;
  transition: width 0.3s ease 0s, left 0.3s ease 0s;
  width: 0;
}
a:hover:after { 
  width: 100%; 
  left: 0; 
}
@media screen and (max-height: 300px) {
    ul {
        margin-top: 40px;
    }
}

我知道我必須將上面的程式碼應用到我的 NavItem 元件中。動畫和一切都很好,但看起來它適用於我的 NavItem 元件,但適用於整個導航欄 NavItem CSS 和應用程式圖片

我只是幾個小時的會員,所以我不知道在我的本地主機中顯示網站的正確方法是什麼。也對五月英語感到抱歉。 :)

P粉574268989P粉574268989179 天前1330

全部回覆(1)我來回復

  • P粉627136450

    P粉6271364502024-04-05 14:24:52

    我建議您使用css.modules並在元件中新增一個className。首先創建css模組,很簡單。請按照以下步驟進行操作:

    • 在與您的元件相同的位置建立一個css檔案:name.module.css,為您的css建立邏輯
    • #為您的元件匯入此模組:
    import classes from './name.module.css';

    (常數的名稱由您自己選擇)

    • 在元件的標籤中加入className:
    <NavItem className={classes.nameofclass} />

    驗證類別是否套用了css程式碼。

    回覆
    0
  • 取消回覆