Master_T Master_T - 2 years ago 146
HTML Question

Printing a checked checkbox / tick box with HTML and CSS

I have the following problem: I have to use an HTML->PDF conversion service to render a piece of HTML. However, this service is a bit limited in it's functionality, so I need a way to "work around" it.

I'm mainly just printing text, so it's not a big deal, but the only problem is that I have to print some "unticked" and some "ticked" check boxes, my converter is failind at this. In particular I've tried:

  • Using the unicode
    ("☐") and
    ("☑") characters, but the converter doesn't render them (probably the font it's using doesn't
    have them)

  • Using the WingDing characters
    but again, the wingding font is not recognized

  • The converter doesn't support images, so can't just use an image

I was thinking, at this point, to "simulate" a checkbox by using spans with borders, something like:

<span style="border: 1px solid black; height: 12px; width: 12px;"></span>

However, I can't make it look correct (no fault of the converter this time, even browsers show the above as just one vertival line.

Can anyone help me "draw" checkboxes using just "basic" html elements? What would be the cleanest way?

Answer Source

You're on the right track.

Using HTML and CSS:

/* The standalone checkbox square*/
.checkbox {
  border: 1px solid #000;

/* Adding the .checked class will build the checkmark */
.checkbox.checked {
  display: inline-block;

/* This is what simulates a checkmark icon */
.checkbox.checked:after {
  content: '';
  display: block;
  width: 4px;
  height: 7px;
  /* "Center" the checkmark */
  border: solid #000;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
<div class="checkbox"></div>
<div class="checkbox checked"></div>

The reason YOUR code didn't work was because you were using a span element, which is an inline element. You can use a span for this, but you'll need to add the style of display: block to the element (making it act as a block element instead of an inline element).

The div tag is a block, so no need for setting it's display style. If you would like the div to display inline, set the display: inline-block

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download