【问题标题】:Creating a border like this using :before And :after Pseudo-Elements In CSS?在 CSS 中使用 :before 和 :after 伪元素创建这样的边框?
【发布时间】:2012-04-15 21:26:17
【问题描述】:

目标

我希望能够在 css 中创建像 this 这样的边框,可能使用伪元素然后是背景图像。

代码

HTML

<div id="footer"></div>

CSS

#footer {
    background: #4b4b4b;
    color: #868686;
    padding: 0;
    position: relative;
    height: 100px;
    width: 900px;
}

#footer:before {
    content: "";
    display: block;
    width: 220px;
    background-color: #e1e1e1;
    height: 8px;
}

【问题讨论】:

  • 会给你一个 jsfiddle,我设法得到 :before 来输出第一个边框。
  • 不要在 CSS 中使用 id!它吹走了特异性!

标签: css border pseudo-element


【解决方案1】:

看到下面的sn-p,这是你想要的吗?

body {
    background: silver;
    padding: 0 10px;
}

#content:after {
    height: 10px;
    display: block;
    width: 100px;
    background: #808080;
    border-right: 1px white;
    content: '';
}

#footer:before {
    display: block;
    content: '';
    background: silver;
    height: 10px;
    margin-top: -20px;
    margin-left: 101px;
}

#content {
    background: white;
}


#footer {
    padding-top: 10px;
    background: #404040;
}

p {
    padding: 100px;
    text-align: center;
}

#footer p {
    color: white;
}
<body>
    <div id="content"><p>#content</p></div>
    <div id="footer"><p>#footer</p></div>
</body>

JSFiddle

【讨论】:

  • 感谢@Yogu,这正是我所追求的。从未想过在内容元素上使用伪元素。
【解决方案2】:
#footer:after
{
   content: "";
    width: 40px;
    height: 3px;
    background-color: #529600;
    left: 0;
    position: relative;
    display: block;
    top: 10px;
}

【讨论】:

  • 你能详细说明你的答案吗?仅发布代码通常是不够的。
  • 此代码集 :after Pseudo-Elements In CSS for h4.
  • @VirendraYaduvanshi - 它没有。它将 :after-Elements 应用于 id 为“footer”的元素。
猜你喜欢
  • 2020-09-20
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
  • 2011-05-10
  • 2012-04-05
  • 1970-01-01
相关资源
最近更新 更多