【问题标题】:Align polygon triangle to bottom of div将多边形三角形对齐到 div 的底部
【发布时间】:2018-01-23 10:45:40
【问题描述】:

我有一个三角形多边形,但有一个问题,我想将它放在“onebysign”div 的正上方,但定位不起作用,它还必须保持响应,所以当我更改屏幕尺寸时,多边形停留在他的位置地点。

所以基本上:我希望多边形三角形 div 与“onebysign”(某物)div“连接”,当屏幕宽度发生变化时,它必须保持响应并且不移动。

JSfiddle:https://jsfiddle.net/fmg6orkd/

HTML 和 CSS:

body {
  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
  background-color: #f2f2f2;
}

.events {
  padding: 20px 100px;
}

.textInfo {
  text-transform: uppercase;
  font-weight: 600;
  color: #085DAD;
  padding: 10px;
  background-color: white;
}

.onebyone {
  background-color: grey;
  width: 100%;
  padding-top: 100%;
  /* 1:1 Aspect Ratio */
  position: relative;
  /* If you want text inside of it */
  background-size: cover;
}

.onebytext {
  position: absolute;
  top: 10px;
  left: 0;
  bottom: 0;
  right: 0;
  text-align: center;
  font-size: 32px;
  color: white;
  width: 90%;
  left: 5%;
}

.onebysign {
  position: absolute;
  left: 0;
  bottom: 0px;
  right: 0;
  text-align: center;
  font-size: 20px;
  background-color: red;
  font-size: 24px;
}

.onebytext,
.onebysign {
  position: absolute;
  text-align: center;
  color: white;
}

.submitBtn {
  background-color: #0099CC;
  text-transform: uppercase;
  padding: 10px;
  border-radius: 50px;
  border: 0px;
  width: 70%;
  margin: 10px 0;
}

.triangle {
  width: 100%;
  position: absolute;
  left: 0px;
  top: 0px;
}

svg {
  width: 100%;
  height: auto;
}
<div class="row events">
  <div class="onebyone">
    <div class="onebytext">
      <div class="textInfo">Test</div>
    </div>
    <div class="triangle" data-type="vertical_parallax" data-speed="2">
      <svg x="0px" y="0px" width="410" height="410" viewBox="0 0 310 310">
                <polyline fill="#CC0000" points="0,0 0,20 310,20" />
              </svg>
    </div>
    <div class="onebysign">
      <button class="submitBtn">Something</button>
    </div>
  </div>
</div>

【问题讨论】:

  • 喜欢this ?
  • @Pedram 不是真的,我希望三角形与另一个红色 DIV 连接并且仍然响应
  • 检查this 但是你不能让这个例子响应,因为你在每个部分多次使用absolute 位置。你应该重新编码。
  • @Pedram 是的,我确实已经尝试过您的解决方案,但我认为也许有一种方法可以解决它而无需重新编码整个事情:)
  • 其实absolute容器和里面的元素很难响应,建议你重新编码。

标签: html css svg polygon responsive


【解决方案1】:

一个简单的解决方案是从 svg 中删除宽度/高度并调整视图框以覆盖多边形的所需部分,然后您可以轻松使用定位:

body {
  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
  background-color: #f2f2f2;
}

.events {
  padding: 20px 100px;
}

.textInfo {
  text-transform: uppercase;
  font-weight: 600;
  color: #085DAD;
  padding: 10px;
  background-color: white;
}

.onebyone {
  background-color: grey;
  width: 100%;
  padding-top: 100%;
  /* 1:1 Aspect Ratio */
  position: relative;
  /* If you want text inside of it */
  background-size: cover;
}

.onebytext {
  position: absolute;
  top: 10px;
  left: 0;
  bottom: 0;
  right: 0;
  text-align: center;
  font-size: 32px;
  color: white;
  width: 90%;
  left: 5%;
}

.onebysign {
  position: absolute;
  left: 0;
  bottom: 0px;
  right: 0;
  text-align: center;
  font-size: 20px;
  background-color: red;
  font-size: 24px;
}

.onebytext,
.onebysign {
  position: absolute;
  text-align: center;
  color: white;
}

.submitBtn {
  background-color: #0099CC;
  text-transform: uppercase;
  padding: 10px;
  border-radius: 50px;
  border: 0px;
  width: 70%;
  margin: 10px 0;
}

.triangle {
  width: 100%;
  position: absolute;
  left: 0px;
  bottom: 51px;
}

svg {
  width: 100%;
  height: auto;
}
<div class="row events">
  <div class="onebyone">
    <div class="onebytext">
      <div class="textInfo">Test</div>
    </div>
    <div class="triangle" data-type="vertical_parallax" data-speed="2">
      <svg x="0px" y="0px" viewBox="0 0 310 20">
                <polyline fill="#CC0000" points="0,0 0,20 310,20" />
              </svg>
    </div>
    <div class="onebysign">
      <button class="submitBtn">Something</button>
    </div>
  </div>
</div>

另一种解决方案是使用带有线性渐变背景的伪元素来创建三角形,这样您需要管理的 HTML 代码就更少了:

body {
  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
  background-color: #f2f2f2;
}

.events {
  padding: 20px 100px;
}

.textInfo {
  text-transform: uppercase;
  font-weight: 600;
  color: #085DAD;
  padding: 10px;
  background-color: white;
}

.onebyone {
  background-color: grey;
  width: 100%;
  padding-top: 100%;
  /* 1:1 Aspect Ratio */
  position: relative;
  /* If you want text inside of it */
  background-size: cover;
}

.onebytext {
  position: absolute;
  top: 10px;
  left: 0;
  bottom: 0;
  right: 0;
  text-align: center;
  font-size: 32px;
  color: white;
  width: 90%;
  left: 5%;
}

.onebysign {
  position: absolute;
  left: 0;
  bottom: 0px;
  right: 0;
  text-align: center;
  font-size: 20px;
  background-color: red;
  font-size: 24px;
}

.onebytext,
.onebysign {
  position: absolute;
  text-align: center;
  color: white;
}

.submitBtn {
  background-color: #0099CC;
  text-transform: uppercase;
  padding: 10px;
  border-radius: 50px;
  border: 0px;
  width: 70%;
  margin: 10px 0;
}

.onebysign:before {
    content: "";
    height: 30px;
    width: 100%;
    position: absolute;
    background: linear-gradient(to top right, red 47%, transparent 50%);
    left: 0;
    top: -30px;
}
<div class="row events">
  <div class="onebyone">
    <div class="onebytext">
      <div class="textInfo">Test</div>
    </div>
    <div class="onebysign">
      <button class="submitBtn">Something</button>
    </div>
  </div>
</div>

【讨论】:

  • 你是英雄!为此苦苦挣扎了这么久,但这很完美,第二种解决方案确实对我最有效。谢谢! :)
猜你喜欢
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 2013-11-12
  • 2012-08-28
  • 1970-01-01
  • 2023-04-08
  • 2012-06-11
  • 1970-01-01
相关资源
最近更新 更多