【问题标题】:Make my border triangle responsive [duplicate]使我的边界三角形响应[重复]
【发布时间】:2016-11-09 22:55:56
【问题描述】:

我有一个使用 CSS 边框的三角形 div。

当前设置为 500 像素宽度。但是我想让它占据屏幕的整个宽度,同时保持它的三角形尖头形状

https://jsfiddle.net/hra17z5t/1/

#one {
  width: 500px;
  background-color: aqua;
  height: 300px;
}
#two{
  border-top: 100px solid red;
  border-right: 250px solid transparent;
  border-bottom: 100px solid transparent;
  border-left: 250px solid transparent;
  width: 0;
  height: 0;    
}
<div id="one"></div>
<div id="two"></div>

【问题讨论】:

  • 您不能对边框执行此操作,因为它们不采用 % 值。你可能不得不重新考虑。也许是 SVG?

标签: html css


【解决方案1】:

我有updated your code example:因为你想让它响应整个浏览器宽度,所以很简单:

#one {
    width: 100vw;
    background-color: aqua;
    height: 300px;
}

#two {
    border-top: 100px solid red;
    border-right: 50vw solid transparent;
    border-bottom: 100px solid transparent;
    border-left: 50vw solid transparent;
    width: 0;
    height: 0;
}
<div id="one"></div>
<div id="two"></div>

在这里,我们只是将框的宽度设置为100vw,即浏览器宽度的 100%。由于三角形的尺寸以相同的方式响应,根据您的示例,您可以简单地将边框宽度设置为50vw(或浏览器宽度的一半),它会响应地增长和缩小。

【讨论】:

    【解决方案2】:

    使用 jQuery:

    $(window).resize(function () {
      var divWidth = $('#one').width();
        $('#two').css({
          borderLeftWidth: divWidth / 2,
          borderRightWidth: divWidth / 2
        });
    });
    
    $(window).trigger('resize');
    
    
    #one {
      max-width: 100%;
    }
    

    JSFIDDLE

    【讨论】:

      【解决方案3】:

      搜索“响应式 CSS 三角形”时在 Google 上找到了这个

      https://jsfiddle.net/josedvq/3HG6d/

      相关代码如下:

      /*Down pointing*/
      .triangle-down {
          width: 10%;
          height: 0;
          padding-left:10%;
          padding-top: 10%;
          overflow: hidden;
      }
      .triangle-down:after {
          content: "";
          display: block;
          width: 0;
          height: 0;
          margin-left:-500px;
          margin-top:-500px;
      
          border-left: 500px solid transparent;
          border-right: 500px solid transparent;
          border-top: 500px solid #4679BD;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-02-19
        • 2019-08-12
        • 2016-06-14
        • 2014-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-27
        • 2014-03-21
        相关资源
        最近更新 更多