Animation to Explain CSS Triangles
codepen.io
codepen.io
This hack on the other hand will work in every browser all the way back to Netscape Navigator or earlier.
<svg height=10 width=20>
<polygon fill=red stroke-width=0
points="0,10 20,10 10,0" />
</svg>
If you want to include it in your CSS, you can include it via a data URI, though you need to embed it as an actual XML document (within an HTML document, you can skip the namespace and use HTML syntax which doesn't require quotes around attributes) and can't include line breaks so the syntax is a little bit more cumbersome: .triangle {
background: url("data:image/svg+xml;charset=utf-8,<svg height='10' width='20' xmlns='http://www.w3.org/2000/svg'><polygon fill='red' stroke-width='0' points='0,10 20,10 10,0' /></svg>") no-repeat left center;
width: 20px;
height: 10px;
}
Examples of both in action: http://jsfiddle.net/jtR4G/If you want to make a triangle, without an additional (unnecessary) HTTP request, you should use the appropriate web tech which is SVG.
Just like all other resources you can easily embed an SVG in your HTML if you're being stingy on the requests (which is a good thing).
If your compatibility goals are pixel-perfect compatibility with IE8, this is an issue (and my thoughts and prayers are with you), but if your site simply needs to be functional, callout triangles wouldn't be on my top 10 things to worry about.
If you want compatibility with older versions of IE, use both SVG and VML, depending on the browser (VML for IE 8 and earlier, SVG for everything else). This gives you a lot more flexibility (you can draw any polygon, arc, or bezier curve that you want), and compatibility all the way back to IE 5.
Here's a series of examples showing how it works (not a fun animation like the OP, but a little easier to follow): http://jsfiddle.net/X8gXS/
Basically, you can get any triangle as long as at least one of the sides is horizontal or vertical.
sadly, we will see some dozen iterations of 'flash' until that day finally comes. and html5 js shenanigans are only the third...
The web isn't just one visual presentation layer, its a multitude of presentation layers for all kind of people and that demands some compromise.
Also: A lot of the shiny new web apps developed during the last few years are unusable with screen readers and/or on low end devices. As long as we can have graceful degradation, i have nothing against more graphical aspects.
Everyone that needs accessibility is already screwed with regular web presentation hacks. And having to add more presentation hacks to get the presentation right only screw them more.
Accepting the web is a presentation, the effort a few sites do to add accessibility to the hacks could be spent adding micro formats to the page content or something, benefiting blinds and people just wanting a easier way to get the content.
There are several solutions to problems. And the current state of accessibility on the web is not a prime example of a good one.
SVG would be awesome if it could be freely intermixed with HTML. I think I'd ditch HTML for everything other than text boxes and forms.
As for HTML in SVG, there is foreignObject [4.72], but browser support is spotty in places. And wrapping text in arbitrary shapes is on the roadmap for SVG 2, if I remember correctly. But we'll be a bit older when that's universally implemented ;-)
[4.72] https://developer.mozilla.org/en-US/docs/Web/SVG/Element/for...
The beauty for me is not in the hack itself though, it is in the way it is presented - I adore the simplicity of this approach. Kudos!
Example: http://jsfiddle.net/9pMJS/
#box { position: relative } #box:after {position: absolute; top: -10px; left: 45%; ... }
The fact that this exists and is lauded as clever rather than being ridiculed for its opaqueness really gets my goat.
<svg width='20' height='10'>
<path fill='#D94948' d='M0,20h20L10,0'/>
</svg>
Should be supported pretty much everywhere canvas is, but without the need of scripting to actually get a triangle. <svg height=10 width=20>
<polygon fill=red stroke-width=0
points="0,10 20,10 10,0" />
</svg>
And you can put it in your CSS using a data URI: .triangle {
background: url("data:image/svg+xml;charset=utf-8,<svg height='10' width='20' xmlns='http://www.w3.org/2000/svg'><polygon fill='red' stroke-width='0' points='0,10 20,10 10,0' /></svg>") no-repeat left center;
width: 20px;
height: 10px;
} <img src='triangle.svg'>
really that much harder than <div class='triangle'></div>?You declare a symbol in the defs section of an SVG image and you can instantiate it with use. E.g.
<svg>
<defs>
<symbol id='foo'>
<circle r='5' cx='10' cy='3'/>
</symbol>
</defs>
<use xlink:href='#foo' x='2' y='5'/>
<use xlink:href='#foo' x='10' y='10' fill='red' stroke='blue'/>
</svg>
(does not actually work, because namespaces are missing and the symbol likely is missing things like a viewbox, but just to get the general idea across)Some more - http://www.fileformat.info/info/unicode/block/geometric_shap...
Better site for this, IMO.
In fact, I think it's awesome that a triangle can be created from a "very broken rectangle", along with many other hacks that make this simple language quite powerful.
Don't get me wrong, I love dirty hacks, just not if they're forced onto me. I guess in a couple of weeks stockholm syndrome will set in, and I'll love it, but for the beginner, especially if you're moving there from either design (What, I need to hack things to get this typography display right?), or programming (What, I need to learn a whole bag of tricks to get the site to style properly?), CSS seems like that dirty pimp down the street to you. Yeah, he's fabulous, but he always wants you to do tricks for him.
SASS and LESS provide really nice language improvements, but they don't fix all the crap about box models and positioning and polyfills and byzantine precedence rules and the necessity for silly hacks like this. It's just too complicated and painful to make things look the way you want them to look, which is the point of CSS. I've gotten pretty good at it and I've come to care a lot about how it works (e.g. this little visualization is a really nice demonstration), but this state of affairs saddens me; I wish I could concentrate on other stuff, and not how to make a goddamn triangle.
(At some level of technical hair-splitting, this isn't per-se CSS's fault and is more general DOM crapiness, but that distinction is largely irrelevant. They're still problems I'm expected to solve with CSS.)
That's the thing though, we have to deal with it. I don't get why there isn't just a complete replacement trending on HN every other month. Sure, it needs browser support and it would forgo a lot of the CSS-specific optimizations baked into the browsers but there has to be a more efficient way to handle the DOM.
But maybe I think this because I don't view it as a language, to me it's more of a configuration file that holds sets of instructions. It's getting closer to a proper language though.
You can have your opinion I suppose, but nearly every web tech I've worked with over the years has had its transitions from good to bad to hated only to end up somehow good again.
Here's a fun one, make a div display: inline-block and then use text-align: center on its parent. Why the hell does the div center when the rule is for text? Is it badly implemented or is it badly named? Without reading the actual spec it's a mystery.
I use CSS extensively nearly every day. I learned CSS before IE6 existed. You don't like dirty hacks? I've likely forgotten more dirty hacks then you'll ever be required to know about. Compared to how things started, CSS is a godsend to us today. But I'm not going to yell to you to get off my lawn, because the fun has only just started and you should feel free to join in.
The problems you listed that you have with CSS, you do realize that's mostly because of how browsers implemented CSS and some related technologies right? CSS isn't a dirty pimp, he's actually the guy down the street that knows all about fashion and design but everybody keeps wanting him to build cars.
Eg. a triangle pointing down:
.triangle-down {
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-top: 7px solid #444;
} .triangle-down {
border: 1em solid transparent;
border-top-color: #444;
}Deleted comment
Anyway, here's a link to the video: http://yow.eventer.com/yow-2012-1012/the-story-of-the-teapot...
As for the compatibility thing, you're coming at it the wrong way. Don't tell Google et al. to support yet another standard for CSS shapes. Let them spend their time implementing SVG and Canvas according to the existing spec. Inventing a new spec not only doesn't solve the problem, it actually re-introduces it.
I think we said the same stuff - came at it from slightly different angles. I´m not advocating yet another standard. Im saying - lets make it easy enough to use the appropriate (and already existing) standards to draw - rather than be at the mercy of a seemingly prehistoric styling system. My point with google et al was - get together and agree to a way to do simple things like images - the same way across browsers.
Does that help clarify?
I always knew how it worked but it was nice to see the animation play out.
OK, that's it everybody. Trying to do something? Is it done yet? Nope? Pack it in. All the things have been done.
HN reader who's not a designer: "don't like, bleh CSS, should SVG, used to do that in VIM, TIMTOWTDI" and other similar useless rants missing the point.
Part of web work is knowing immediately what you're dealing with. dimensionless rectangles that show up as triangles is a layer of complexity (hack) that disrupts flow.
Get it?
I suppose I'll understand once I realize what you mean by "fixing" is actually "replacing". Specifically with your preferred method of accomplishing the same thing.
You aren't one of those programmers that can't help themselves in "fixing" everybody else's code that comes across your desk because they didn't do it the way you would, regardless of whether it works or not? I mean, the attitude I'm sensing in your text suggests that, but I'm sure you're a better person than that in real life.
that's the real world of design and collaborative environments. Branding, code quality, standards they all start with setting a direction that makes sense in order to improve efficiency across multiple disciplines and experience levels.
Hackey fixes have always been available. Instead of using h3 tags you can use b tags and scale them up to a particular font size.
Even when something works, you have collaboration and reputation to operate by. Case in point: Somebody actually had to create an animation to explain what the hell is going on with this hack to people who already know html and css, whereas with a unicode character or SVG you wouldn't have to have meta instructions. You could say good code or technique often requires no explanation.
that's what I'm trying to get it.