1

I am trying to learn how to create different shapes in my react-native app using styling. I came across the below example (scroll down to triangle) which will create a triangle.

This is the code that the blogger has used to create a triangle.

var Triangle = React.createClass({
  render: function() {
    return (
      <View style={[styles.triangle, this.props.style]} />
    )
  }
})
  triangle: {
    width: 0,
    height: 0,
    backgroundColor: 'transparent',
    borderStyle: 'solid',
    borderLeftWidth: 50,
    borderRightWidth: 50,
    borderBottomWidth: 100,
    borderLeftColor: 'transparent',
    borderRightColor: 'transparent',
    borderBottomColor: 'red'
  }

I have verified it in my app and it does draw a triangle but I am not sure why do the above styling properties result in a triangle being drawn on the screen. If anything, I should have just seen a 100 px red square because the left and right border colors are transparent and the bottom border is red with a height of 100px and the left and the right border would add 50px each.

How do these styles create a triangle on the screen?

Varun Gupta
  • 2,870
  • 6
  • 33
  • 73

2 Answers2

2

Basically borders are not squares but are trapizoids at the corners, yours just have a width of zero.

this may help understand:

    
#tri {
    width: 0px;
    background: antiquewhite;
    border-top: 60px solid transparent;
    border-bottom: 60px solid blue;
    border-left: 60px solid green;
    border-right: 65px solid green;
}
<div id="tri""></div>

Playing with the width and height will show their true nature

Kartik Prasad
  • 730
  • 7
  • 18
1

That's a very god question firstly, Check out this snippet. This isn't anything too out of the ordinary. It's just a div that is only displaying a bottom border. But, if you stare and it and think about it, imagine hiding most of that border so that what's left is a triangle. If you removed all of it except one of the edges, we'd have a very little triangle left over. Please take a look hope it is helpful to you.

div {
width:100px;height:50px;
border-color: transparent transparent #222 transparent;
border-style: solid; 
border-width: 6px; 
}
<div style="">
  test
</div>
Anmol Sandal
  • 1,468
  • 1
  • 9
  • 16