尝试创建带有箭头的标题并呼吁采取行动

时间:2019-02-04 22:55:21

标签: html css

在浏览了多个CSS网站之后,我已经快到了,但是我很乐意提供一些专家建议。

我正在尝试为包含3个部分的网站创建部分标题:

  1. 带有按钮的标题,以获取可能的工具提示
  2. 说明
  3. 号召性用语按钮

已附加样机。

我正在尝试调整箭头的宽度,以使其不那么“尖”,因为它占用了下一个区域的额外空间。

Mockup

到目前为止,我有:

* {
  box-sizing: border-box;
}

#RPheader {
  float: left;
  position: relative;
  width: 40%;
  padding: 10px;
  height: 40px;
  color: #FFFFFF;
  background: #004851;
}

#RPheader:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 0;
}

#RPheader:before {
  content: "";
  position: absolute;
  right: -20px;
  bottom: 0;
  width: 0;
  height: 0;
  border-left: 20px solid #004851;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  z-index: 1;
}

#RPdesc {
  float: left;
  position: relative;
  width: 50%;
  padding: 10px;
  height: 40px;
  color: #555555;
  background-color: #F1ECEA;
}

#RPdesc:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 0;
}

#RPdesc:before {
  content: "";
  position: absolute;
  right: -20px;
  bottom: 0;
  width: 0;
  height: 0;
  border-left: 20px solid #F1ECEA;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
}

#RPheaderButton {
  float: left;
  width: 10%;
  padding: 10px;
  height: 40px;
  color: #FFFFFF;
  background-color: #00afd7;
}
<div id="RPheaderRow">
  <div id="RPheader">Header title here</div>
  <div id="RPdesc">This is where my description will go...</div>
  <div id="RPheaderButton"> CTA </div>
</div>

我还没有完成工具提示部分...我将设法弄清楚这一点。.yikes!

非常感谢任何帮助。

克里斯。

3 个答案:

答案 0 :(得分:1)

您快到了,只需要将:before元素上的10px调整为right值,然后将-10px属性也更改为{{1 }}。我还更新了RPDesc元素和RPheaderButton元素上的左右填充。

https://jsfiddle.net/disinfor/evy952bc/10/

* {
  box-sizing: border-box;
}

#RPheader {
  float: left;
  position: relative;
  width: 40%;
  padding: 10px;
  height: 40px;
  color: #FFFFFF;
  background: #004851;
}

#RPheader:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 0;
}

#RPheader:before {
  content: "";
  position: absolute;
  right: -5px;
  bottom: 0;
  width: 0;
  height: 0;
  border-left: 5px solid #004851;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  z-index: 1;
}

#RPdesc {
  float: left;
  position: relative;
  width: 50%;
  padding: 10px 20px;
  height: 40px;
  color: #555555;
  background-color: #F1ECEA;
}

#RPdesc:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 0;
}

#RPdesc:before {
  content: "";
  position: absolute;
  right: -10px;
  bottom: 0;
  width: 0;
  height: 0;
  border-left: 10px solid #F1ECEA;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
}

#RPheaderButton {
  float: left;
  width: 10%;
  padding: 10px 10px 10px 20px;
  height: 40px;
  color: #FFFFFF;
  background-color: #00afd7;
}
<div id="RPheaderRow">
  <div id="RPheader">Header title here</div>
  <div id="RPdesc">This is where my description will go...</div>
  <div id="RPheaderButton"> CTA </div>
</div>

您可以根据需要调整这两个值,但这将使您更接近所需的值。

答案 1 :(得分:1)

您可以考虑使用背景轻松实现这一目标,而无需使用伪元素:

* {
  box-sizing: border-box;
}

#RPheader {
  float: left;
  width: 40%;
  padding: 10px;
  height: 40px;
  color: #FFFFFF;
  background: 
    linear-gradient(to top right   ,transparent 49.8%,#F1ECEA 50%) top   right/20px 50%,
    linear-gradient(to bottom right,transparent 49.8%,#F1ECEA 50%) bottom right/20px 50%,
    #004851;
  background-repeat:no-repeat;
}



#RPdesc {
  float: left;
  width: 50%;
  padding: 10px;
  height: 40px;
  color: #555555;
  background:
    linear-gradient(to top right   ,transparent 49.8%,#00afd7 50%) top   right/20px 50%,
    linear-gradient(to bottom right,transparent 49.8%,#00afd7 50%) bottom right/20px 50%,
   #F1ECEA;
  background-repeat:no-repeat;
}
#RPheaderButton {
  float: left;
  width: 10%;
  padding: 10px;
  height: 40px;
  color: #FFFFFF;
  background-color: #00afd7;
}
<div id="RPheaderRow">
  <div id="RPheader">Header title here</div>
  <div id="RPdesc">This is where my description will go ...</div>
  <div id="RPheaderButton"> CTA </div>
</div>

您还可以像下面那样优化代码:

* {
  box-sizing: border-box;
}
#RPheaderRow > div {
 float: left;
  padding: 10px;
  height: 40px;
  color:#fff;
  background: 
    linear-gradient(to top right   ,transparent 49.5%,var(--c,transparent) 50%) top    right/var(--s,20px) 50%,
    linear-gradient(to bottom right,transparent 49.5%,var(--c,transparent) 50%) bottom right/var(--s,20px) 50%;
  background-repeat:no-repeat;
}

div#RPheader { 
  width: 40%;
  --c:#F1ECEA; /*adjust the color*/
  background-color:#004851;
}
div#RPdesc {
  width: 50%;
  color: #555555;
  --c:#00afd7;
  --s:10px; /*adjust the size of the arrow*/
  background-color:#F1ECEA;
}
div#RPheaderButton {
  width: 10%;
  background-color: #00afd7;
}
<div id="RPheaderRow">
  <div id="RPheader" >Header title here</div>
  <div id="RPdesc" >This is where my description will go ...</div>
  <div id="RPheaderButton" > CTA </div>
</div>

答案 2 :(得分:0)

在模拟您要获取的长尾鸟之前,我会在本节中添加一个顶部箭头和一个底部箭头。

* {
  box-sizing: border-box;
}

#RPheader {
  float: left;
  position: relative;
  width: 40%;
  padding: 10px;
  height: 40px;
  color: #FFFFFF;
  background: #004851;
}

#RPheader:after {
  content: "";
  position: absolute;
  right: -20px;
  bottom: 0;
  width: 0;
  height: 0;
  border-bottom: 20px solid #F1ECEA;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
}

#RPheader:before {
  content: "";
  position: absolute;
  right: -20px;
  top: 0;
  width: 0;
  height: 0;
  border-top: 20px solid #F1ECEA;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
}

#RPdesc {
  float: left;
  position: relative;
  width: 50%;
  padding: 10px;
  height: 40px;
  color: #555555;
  background-color: #F1ECEA;
}

#RPdesc:after {
  content: "";
  position: absolute;
  right: -20px;
  bottom: 0;
  width: 0;
  height: 0;
  border-bottom: 20px solid #00afd7;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
}

#RPdesc:before {
  content: "";
  position: absolute;
  right: -20px;
  top: 0;
  width: 0;
  height: 0;
  border-top: 20px solid #00afd7;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
}

#RPheaderButton {
  float: left;
  width: 10%;
  padding: 10px;
  height: 40px;
  color: #FFFFFF;
  background-color: #00afd7;
}
<div id="RPheaderRow">
  <div id="RPheader">Header title here</div>
  <div id="RPdesc">This is where my description will go...</div>
  <div id="RPheaderButton"> CTA </div>
</div>