Categories
CSS

How to set width/height as a percentage minus pixels with CSS?

To set width/height as a percentage minus pixels with CSS, we use calc.

For instance, we write

div {
  height: calc(100% - 18px);
}

to set the div’s height to 100% minus 18px with calc.

Categories
CSS

How to center a position: fixed element with CSS?

To center a position: fixed element with CSS, we set the transform property.

For instance, we write

div {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

to set the left and top to 50% to move the element near the center.

Then we add transform: translate(-50%, -50%); to move the div to the center.

Categories
CSS

How to hide an element when printing a web page with CSS?

To hide an element when printing a web page with CSS, we use the @media print query.

For instance, we write

@media print {
  .no-print,
  .no-print * {
    display: none !important;
  }
}

to hide elements with class .no-print and everything inside them with display: none when the page is being printed with the @media print rule.

Categories
CSS

How to change the checkbox size using CSS?

To change the checkbox size using CSS, we set the zoom property.

For instance, we write

<input type="checkbox" />

to add a checkbox.

Then we write

input[type="checkbox"] {
  zoom: 1.5;
}

to set the zoom to 1.5 to increase the checkbox size.

Categories
CSS

How to select elements by attribute in CSS?

To select elements by attribute in CSS, we use the attribute selector.

For instance, we write

[data-role="page"] {
  //...
}

to select the elements with the data-role attribute equal to page.