李:以后不工作

时间:2017-05-04 12:10:30

标签: html css pseudo-element

我使用li:afterli之后添加内容,但无效。



body {
  margin: 0;
  padding: 0;
}

#banner {
  height: 30px;
  width: 750px;
  margin: 0 auto;
  background-color: aquamarine;
}

#banner ul {
  list-style: none;
  height: 30px;
  padding: 0;
  display: flex;
  align-items: center;
}

#banner ul li {
  float: left;
  margin: auto 0;
  height: 30px;
  line-height: 30px;
  width: 100px;
  text-align: center;
}

#banner ul li :after {
  content: "a";
  top: 1px;
  bottom: 1px;
  right: 0;
  width: 5px;
  background-color: black;
}

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>title</title>
  <link rel="stylesheet" href="styles/index.css" type="text/css">
</head>

<body>
  <div id="banner">
    <ul>
      <li>Home</li>
      <li>link</li>
      <li>product</li>
      <li>phone</li>
      <li>cat</li>
      <li>about</li>
    </ul>
  </div>

</body>

</html>
&#13;
&#13;
&#13;

为什么内容不显示?

3 个答案:

答案 0 :(得分:1)

因为您拥有此top: 1px; bottom: 1px; right: 0;,所以您需要使用position并且a::after之间有空格

使用该空格,它会将::after应用于li的所有子项。所以它与

相同
#banner ul li *::after

查看代码段

body {
  margin: 0;
  padding: 0;
}

#banner {
  height: 30px;
  max-width: 750px;
  margin: 0 auto;
  background-color: aquamarine;
}

#banner ul {
  list-style: none;
  height: 30px;
  padding: 0;
  display: flex;
  align-items: center;
}

#banner ul li {
  float: left;
  margin: auto 0;
  height: 30px;
  line-height: 30px;
  width: 100px;
  text-align: center;
  position:relative;
}

#banner ul li::after {
  content: "a";
  position:absolute;
  top: 1px;
  bottom: 1px;
  right: 5px;
  width: 5px;
  background-color: red;
}
<div id="banner">
  <ul>
    <li>Home</li>
    <li>link</li>
    <li>product</li>
    <li>phone</li>
    <li>cat</li>
    <li>about</li>
  </ul>
</div>

答案 1 :(得分:0)

元素(li)和:after之间不应有空格。

应该是:

#banner ul li:after

#banner ul li :after

答案 2 :(得分:-2)

&#13;
&#13;
body {
  margin: 0;
  padding: 0;
}

#banner {
  height: 30px;
  width: 750px;
  margin: 0 auto;
  background-color: aquamarine;
}

#banner ul {
  list-style: none;
  height: 30px;
  padding: 0;
  display: flex;
  align-items: center;
}

#banner ul li {
  float: left;
  margin: auto 0;
  height: 30px;
  line-height: 30px;
  width: 100px;
  text-align: center;
}

#banner ul li:after {
  content: "a";
  top: 1px;
  bottom: 1px;
  right: 0;
  width: 5px;
  background-color: black;
}
&#13;
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>title</title>
  <link rel="stylesheet" href="styles/index.css" type="text/css">
</head>

<body>
  <div id="banner">
    <ul>
      <li>Home</li>
      <li>link</li>
      <li>product</li>
      <li>phone</li>
      <li>cat</li>
      <li>about</li>
    </ul>
  </div>

</body>

</html>
&#13;
&#13;
&#13;