首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义下划线效果,而不是为其创建背景图像

问自定义下划线效果,而不是为其创建背景图像
EN

Stack Overflow用户
提问于 2017-07-08 16:04:51
回答 3查看 66关注 0票数 2

有没有更好的方法来通过CSS创建这种样式的“下划线”,而不是为它创建背景图像?

为了清楚起见,我只对“复制线条”效果感兴趣,一条更粗更短的线条直接位于不同颜色的更细更长的线条上。谢谢!

​

​

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-07-08 16:10:06

您可以在这里使用伪元素,即:before和:after。在这里,我所做的是,使用一个将其显示为inline-block的h1元素。稍后,我们需要使用CSS定位来设置两个底部边框,因为边框比您的元素小。

稍后,再次使用CSS定位,我们将小的border放在大的上面。请注意,我使用left: 50%;和transform: translateX(-50%)将边框定位在水平中心。

确保不要错过z-index,因为在这里使用它很重要,否则另一个边框将在较小的边框上渲染。

​

代码语言:javascript
复制
@import url('https://fonts.googleapis.com/css?family=Varela+Round');

* {
  margin: 0;
  padding: 0;
  outline: 0;
  box-sizing: border-box;
}

h1 {
  position: relative;
  display: inline-block;
  font-family: Varela Round;
  font-size: 24px;
  text-transform: uppercase;
  font-weight: bold;
  color: #401f1c;
  margin: 40px; /* not required, only for demo purpose */
}

h1 span {
  color: #efcc4c;
}

h1:before,
h1:after {
  content: '';
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

h1:before {
  bottom: -11px;
  width: 40px;
  border-bottom: 3px solid #efcc4c;
  z-index: 1;
}

h1:after {
  width: 80%;
  border-bottom: 1px solid #ddd;
  bottom: -10px;
}
代码语言:javascript
复制
<h1>Our <span>Services</span></h1>

​

编辑:重构了我的代码,使演示更加精确。

票数 4
EN

Stack Overflow用户

发布于 2017-07-08 16:14:58

尝尝这个

HTML

代码语言:javascript
复制
<div class="text">
  <span>our</span>
  Services
</div>

CSS

代码语言:javascript
复制
.text{
  font-weight:600;
  font-size:25px;
  color:red;
  position: relative;
  display:inline-block;
}
.text::after,
.text::before{
  content:"";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -5px;
  margin:auto;
  border-radius:5px;
  height:0px;
}

.text::before{
  width:100%;
  border:1px solid #ccc;
}
.text::after{
  width:50%;
  border:2px solid red;
  bottom:-6px;
}

.text span{
  color:#000000;
}

Link for reference

希望这能帮上忙..

票数 1
EN

Stack Overflow用户

发布于 2017-07-08 16:25:42

我总是创建"divider",像这样:

代码语言:javascript
复制
<div class='divider'>
    <div class='divi-1'></div>
    <div class='divi-2'></div>
    <div class='divi-3'></div>
</div>

CSS:

代码语言:javascript
复制
.divider{
    padding-top:15px; //or other
    text-align:center;
    display:block; // or column in bootstrap like col-md-12
}

.divider .divi-1{
    display:inline-block;
    height:2px; //or other
    width:50px; // or other
    background:#e5e5e5;
.

.divider .divi-2{
    display:inline-block;
    height:2px;
    width:50px;
    background:#000000;
}
.divider .divi-1{
    display:inline-block;
    height:2px; //or other
    width:50px; // or other
    background:#e5e5e5;
}

就是这样。你也可以使用垂直对齐的内联块,这样你就有了更多的选项来垂直移动线条...此外,它在流中,因此您知道它的大小,并可以确保其他元素不会与其重叠。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44983983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档