简体   繁体   English

如何使用背景创建三角形(固定高度,宽度= 100%)

[英]How to create an triangle shape (fixed height, width=100%) with background

I have a graphic background, and I need to display a colored triangle in the top left corner (independing the resolution). 我有一个图形背景,我需要在左上角显示一个彩色三角形(取决于分辨率)。

Can I create a triangle shaped element using only HTML/CSS/JS with width = 100% and height = 200px with background = red? 我是否可以仅使用宽度= 100%且高度= 200px且背景=红色的HTML / CSS / JS创建三角形元素?

I can create it by IMG with width=100%, but I was hoping for a better solution than resizing an image. 我可以通过宽度= 100%的IMG创建它,但我希望有一个比调整图像大小更好的解决方案。

The solution needs to be compatible with IE7+ and using browser's versions (more than 2%). 该解决方案需要与IE7 +兼容并使用浏览器版本(超过2%)。

Thanks 谢谢

Because you can't create a border which has a percentage, try using vw (viewer width) instead. 因为您无法创建具有百分比的边框,请尝试使用vw(查看器宽度)。 So: 所以:

.triangle{
   width: 0;
   height: 0;
   border-bottom: 600px solid blue;
   border-left: 100vw solid transparent;
  }

Vw units aren't supported by IE8, you will need to use a JS fallback for browsers that don't support these units. IE8不支持Vw单元,对于不支持这些单元的浏览器,您需要使用JS后备。

Here is a jQuery script that sets the border-width according to the window size and adjusts it on window resize. 这是一个jQuery脚本,它根据窗口大小设置边框宽度,并在窗口大小调整时调整它。 Tested in IE8 (IE tester) : 在IE8(IE测试仪)中测试:

 $(document).ready(function() { function triangle() { var width = $('#wrap').width(), border = width / 4; $("#wrap .tr").css({ "border-left": border + "px solid #fff", "border-bottom": border + "px solid transparent" }); } triangle(); $(window).on('resize', triangle); }); 
 body { background: #fff; } #wrap { position: relative; min-height: 500px; background: teal; } .tr { position: absolute; left: 0; top: 0; border-left: 200px solid #fff; border-bottom: 200px solid transparent; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="wrap"> <div class="tr"></div> </div> 

To expand on web-tiki's answer , I think this is actually what you're going for: 为了扩展web-tiki的答案 ,我认为这实际上就是你想要的:

 $(document).ready(function() { function triangle() { $("#wrap .tr").css({ "border-left": $('#wrap').width() + "px solid #fff", "border-bottom": "200px solid transparent" }); } triangle(); $(window).on('resize', triangle); }); 
 body { background: #fff; } #wrap { position: relative; min-height: 500px; background: teal; } .tr { position: absolute; left: 0; top: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="wrap"> <div class="tr"></div> </div> 

I think it would be best to use background instead of borders: 我认为最好使用背景而不是边框​​:

 .my-triangle { width: 100%; height: 200px; background: linear-gradient(to left top, transparent 50%, red 50%); } 
 <div class="my-triangle"></div> 

Note that in order for it to be cross-browser compatible you will need to fiddle around with CSS prefixes, IE filters and SVG. 请注意,为了使其与跨浏览器兼容,您需要摆弄CSS前缀,IE过滤器和SVG。 (I don't readily have access to IE so I'll leave that one for you, but it would be something along these lines:) (我不能轻易访问IE,所以我会留给你一个,但它会是这样的:)

  background-image: -webkit-gradient(linear, right bottom, left top, color-stop(0, transparent), color-stop(0.5, transparent), color-stop(0.5, #FF0000), color-stop(1, #FF0000));
  background-image: -webkit-linear-gradient(bottom right, transparent 0%, transparent 50%, #FF0000 50%, #FF0000 100%);
  background-image: -moz-linear-gradient(bottom right, transparent 0%, transparent 50%, #FF0000 50%, #FF0000 100%);
  background-image: -ms-linear-gradient(bottom right, transparent 0%, transparent 50%, #FF0000 50%, #FF0000 100%);
  background-image: -o-linear-gradient(bottom right, transparent 0%, transparent 50%, #FF0000 50%, #FF0000 100%);
  background-image: linear-gradient(to top left, transparent 0%, transparent 50%, #FF0000 50%, #FF0000 100%);

Just take a div element, give a class name 'triangle-topleft', and write the below given css 只需要一个div元素,给一个类名'triangle-topleft',然后写下面给出的css

.triangle-topleft {
    width: 0;
    height: 0;
    border-top: 100px solid red;
    border-right: 100px solid transparent;
}

color of border-top would be the div's background color..Here it's red. border-top的颜色将是div的背景颜色。这里它是红色的。 For more triangle structures, follow this link.. [ http://css-tricks.com/examples/ShapesOfCSS/][1] 有关更多三角形结构,请点击此链接.. [ http://css-tricks.com/examples/ShapesOfCSS/] [1 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM