C

CSS Handbook

Clean • Professional

CSS Colors & Backgrounds — Interview Questions & Answers

3 minute

CSS Colors & Backgrounds — Interview Questions & Answers

Ques: How are colors defined in CSS?

Ans: Colors in CSS can be defined in multiple formats, including names, HEX, RGB, RGBA, HSL, and HSLA.

TypeExampleDescription
Named Colorscolor: red;147 predefined color names
HEX Codecolor: #ff0000;Hexadecimal color value
RGBcolor: rgb(255, 0, 0);Red-Green-Blue combination
RGBAcolor: rgba(255, 0, 0, 0.5);RGB + Alpha (transparency)
HSLcolor: hsl(0, 100%, 50%);Hue, Saturation, Lightness
HSLAcolor: hsla(0, 100%, 50%, 0.5);HSL + Alpha transparency

Example:

h1 {
  color: rgb(34, 193, 195);
}
p {
  color: hsl(200, 100%, 40%);
}

Ques: What is the difference between RGB and HSL color models?

ModelStands ForBased OnExample
RGBRed, Green, BlueLight intensityrgb(255, 100, 50)
HSLHue, Saturation, LightnessColor wheelhsl(10, 70%, 60%)

Ques: What is opacity in CSS?

Ans: The opacity property sets the transparency level of an element, including its content.

ValueMeaning
1Fully opaque
0Fully transparent
0.550% transparent

Example:

img {
  opacity: 0.6;
}

Ques: What is the difference between opacity and RGBA/HSLA transparency?

PropertyAffectsApplies ToExample
opacityEntire element (including children)Whole boxopacity: 0.5;
rgba()/hsla()Only colorSpecific propertybackground-color: rgba(0,0,0,0.5);

Ques: What is the background-color property used for?

Ans: background-color sets the background color of an element.

Example:

div {
  background-color: lightblue;
}

Ques: What is the background-image property?

Ans: background-image allows you to set an image as a background.

Example:

body {
  background-image: url("bg.jpg");
}

Ques: What are different background properties in CSS?

PropertyDescription
background-colorSets background color
background-imageSets background image
background-repeatControls image repetition
background-positionSets the starting position
background-sizeScales background image
background-attachmentFixed or scroll behavior
background-originDefines where background starts
background-clipControls painting area of background

Ques: Explain background-repeat property.

ValueDescription
repeatRepeats both horizontally & vertically (default)
repeat-xRepeats horizontally
repeat-yRepeats vertically
no-repeatDisplays once only

Example:

div {
  background-image: url("pattern.png");
  background-repeat: no-repeat;
}

Ques: What does background-size property do?

ValueDescription
autoOriginal size (default)
coverScales to cover the container
containFits image inside container
100% 100%Stretches to exact size

Example:

div {
  background-image: url("banner.jpg");
  background-size: cover;
}

Ques: What is background-position in CSS?

Ans: Defines where the background image is placed.

ExampleMeaning
background-position: top left;Align top-left
background-position: center;Center image
background-position: 50% 50%;Centered both ways

Ques: What is background-attachment used for?

Ans: It determines whether the background image scrolls with the page or stays fixed.

ValueDescription
scrollMoves with page scroll
fixedStays fixed in viewport
localScrolls with element content

Example:

body {
  background-image: url("bg.jpg");
  background-attachment: fixed;
}

Ques: What are multiple backgrounds in CSS?

Ans: You can add multiple background images by separating them with commas.

Example:

div {
  background-image: url("stars.png"), url("moon.png");
  background-position: center, right top;
  background-repeat: no-repeat, no-repeat;
}

Ques: What are gradients in CSS?

Ans: Gradients are smooth transitions between colors — created using CSS instead of images.

TypeFunctionExample
Linear Gradientlinear-gradient()background: linear-gradient(to right, red, yellow);
Radial Gradientradial-gradient()background: radial-gradient(circle, blue, white);
Conic Gradientconic-gradient()background: conic-gradient(from 0deg, red, yellow, blue);

Ques: What is background-clip in CSS?

Ans: Specifies where the background is drawn.

ValueDescription
border-boxDefault; extends under border
padding-boxStops at padding edge
content-boxOnly background behind content

Example:

div {
  background: yellow;
  background-clip: content-box;
  border: 5px solid black;
  padding: 20px;
}

Ques: What is mask-image in CSS?

Ans: mask-image hides parts of an element based on image transparency — like a stencil effect.

Example:

div {
  mask-image: url("mask.png");
  mask-size: cover;
}

 

Article 0 of 0