There are three variants, hyphens, en-dashes, and em-dashes. A manual of style will give “rules” for their use, but remember that style is important only in a formal final product. What you do when typing your manuscript or communicating can be very different and totally idiosyncratic. In the olden days, a an editor would mark up a manuscript to indicate what went into the final typeset copy.
There are zillions of possible uses for them, so I’m going to pick out a few of the most important, as give in the Chicago Manual of Style, 13th ed.
used in compound words: court-martial; clearing-house
used to break words at the end of lines in justified text:
But writers who find themselves under-
lining frequently for emphasis may consider…
em dash (used as *the *dash in the book)
used to denote a sudden break in thought: Will he—can he—obtain the necessary signatures?
en dash; one half the length of an em dash
used in dates: 1968–1972; May–June 1967
used in compound adjectives: New York–London flight; post–Civil War period
used for sections: see sections 5.82–96
used for page numbers: 334–402
There are also 2-em dashes and 3-em dashes used in specialized situations like missing words: A vessel which left the ——— in July …
The Chicago manual advises never to use spaces around dashes (except for an odd nitpick or two). Other sources would tell you to use spaces around an em dash. That’s pure style, not a rule.
I have no idea how your browser will parse the symbols, but I used real em and en dashes instead of hyphens where appropriate.