CSS Value and Units Flashcards

1
Q

What are CSS units, and why are they important?

A

CSS units define how values like width, height, margin, padding, and font sizes are measured in a webpage. They ensure that elements scale properly based on the design requirements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between absolute and relative units in CSS?

A

Absolute units (e.g., px, cm, in) have a fixed size that does not change based on the screen or parent element.

Relative units (e.g.,em, rem, %, vw, vh) depend on other elements or the viewport, making them more flexible and responsive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between px and em in CSS?

A

px (pixels) is an absolute unit that represents a fixed size.

em is relative to the font size of its parent element. If the parent has font-size: 16px, then 1em = 16px.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does rem stand for, and how is it different from em?

A

rem stands for “root em” and is relative to the root <html> element’s font size, while em is relative to its nearest parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do viewport units (vw, vh) work?

A

vw (viewport width) is a percentage of the browser window’s width.

vh (viewport height) is a percentage of the browser window’s height.
For example, 50vw means 50% of the viewport width.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does ch measure in CSS?

A

ch is a relative unit that measures the width of the “0” character in the current font. It’s useful for setting widths of text-based elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the ex unit?

A

The ex unit is relative to the height of the lowercase letter “x” in the selected font. It is less commonly used than em or rem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are percentage (%) units used for?

A

Percentage values are relative to the size of their parent container. For example, width: 50% means the element will take up 50% of its parent’s width.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you make an element’s width half of the browser’s width using

A
div {
  width: 50vw;
}

This sets the width of the <div> to 50% of the viewport width.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you set an element’s font size to be twice the size of its parent

A
p {
  font-size: 2em;
}

Since 1em equals the parent’s font size, 2em makes it twice as large

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you set an element’s padding to be 10% of its parent’s width?

A
.box {
  padding: 10%;
}

This makes the padding 10% of the width of .box’s parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you make an element’s height exactly equal to the viewport height?

A
.full-screen {
  height: 100vh;
}

This ensures the element fills the entire screen height.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

This ensures the element fills the entire screen height.

A

It will set the width to 10 times the font size of the parent element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you create a text box that is always 40 characters wide, regardless of font size?

A
input {
  width: 40ch;
}

The ch unit ensures that the input remains 40 characters wide

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you set the font size of a paragraph to always be 5% of the viewport width?

A
p {
  font-size: 5vw;
}

This makes the text size scale dynamically based on the viewport width.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between min-width: 50% and max-width: 50%?

A

min-width: 50% ensures the element will not be smaller than 50% of its parent.

max-width: 50% ensures the element will not be larger than 50% of its parent.

17
Q

What unit would you use for a scalable button that remains proportional to the text inside?

A

em or rem is the best choice because they scale with the font size. Example:

button {
  padding: 0.5em 1em;
  font-size: 1.2rem;
}
18
Q

How can you set the margin of an element to always be half of its parent’s width?

A
.box {
  margin: 50%;
}

This makes the margin size dependent on the parent’s width.

19
Q

How can you make an element’s width grow based on the font size of the root element?

A
.container {
  width: 10rem;
}

Since rem is based on the root font size, changing the <html> font siz

20
Q

What would happen if you set font-size: 10vw; on a heading?

A

The text size will be 10% of the viewport width, making it scale dynamically with the browser size.

21
Q

What are angle units in CSS, and where are they used?

A

Angle units define rotations and are used in transforms and gradients.

  • deg → Degrees (e.g., rotate(45deg))
  • rad → Radians
  • grad → Gradians
  • turn → Full turns (1 turn = 360deg)
22
Q

How do you rotate an element by 90 degrees using CSS?

A
.box {
  transform: rotate(90deg);
}
23
Q

What is the difference between s and ms in CSS?

A
  • s (seconds) → 1s = 1000ms
  • ms (milliseconds) → 500ms = 0.5s

Used in animations and transitions.

24
Q

How do you create an animation that lasts for 2 seconds?

A
@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

.box {
  animation: fade 2s ease-in-out;
}
25
Q

What are resolution units, and where are they used?

A
  • dpi → Dots per inch
  • dpcm → Dots per centimeter
  • dppx → Dots per pixel (1dppx = 96dpi)
    Used in media queries to target high-resolution screens.
26
Q

How do you apply styles only for high-resolution screens?

A
@media (min-resolution: 2dppx) {
  body {
    background: url('high-res-image.png');
  }
}
27
Q

What does calc() do in CSS?

A

calc() allows mathematical operations for dynamic sizing.
Example:

.box {
  width: calc(100% - 50px);
}

This makes the width flexible but subtracts 50px.

28
Q

What is the difference between min(), max(), and clamp()?

A

min(value1, value2): Picks the smallest value.

max(value1, value2): Picks the largest value.

clamp(min, preferred, max): Keeps a value within a range.

29
Q

How do you ensure a heading’s font size is responsive but never smaller than 16px or bigger than 5vw?

A
h1 {
  font-size: clamp(16px, 2vw, 5vw);
}

This ensures the font is at least 16px but scales with the viewport up t

30
Q

How do you create a box that is at least 200px wide but not more than 80% of the parent width?

A
.box {
  width: min(80%, 200px);
}

The element will never be smaller than 200px but won’t exceed 80% of its