Skip to content

Instantly share code, notes, and snippets.

@Eronmmer
Last active February 15, 2020 01:35
Show Gist options
  • Save Eronmmer/67b0191469b45846da1273e0a3467f1a to your computer and use it in GitHub Desktop.
Save Eronmmer/67b0191469b45846da1273e0a3467f1a to your computer and use it in GitHub Desktop.
Link and Button CSS tricks.
  • To create a link that triggers a download, do the following
<a href="/files/file.pdf" download>Download PDF</a>
  • If your link contains just an icon or emoji, use aria-label or a visually-hidden class like the following
<a href="/">
  <!-- Hide the icon from assistive technology -->
  <svg aria-hidden="true" focusable="false"> ... </svg>
  <!-- Acts as a label that is hidden from view -->
  <span class="visually-hidden">Useful link text</span>
</a>
.visually-hidden {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}
  • Buttons in forms have some neat tricks up their sleeve
<form action="/" method="POST">
  <input type="text" name="name" id="name">
  <button>Submit</button>

  <!-- If you want to be more explicit... -->
  <button type="submit">Submit</button>

  <!-- ...or clear the form inputs back to their initial values -->
  <button type="reset">Reset</button>

  <!-- This prevents a `submit` action from firing which may be useful sometimes inside a form -->
  <button type="button">Non-submitting button</button>
</form>

<!-- And even more!  -->

<form action="/" method="get">

  <!-- override the action -->
  <button formaction="/elsewhere/" type="submit">Submit to elsewhere</button>

  <!-- override encytype -->
  <button formenctype="multipart/form-data" type="submit"></button>

  <!-- override method -->
  <button formmethod="post" type="submit"></button>

  <!-- do not validate fields -->
  <button formnovalidate type="submit"></button>

  <!-- override target e.g. open in new tab -->
  <button formtarget="_blank" type="submit"></button>

</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment