CSS Coding Interview Questions and Answers
-
How do you apply a background color to a webpage?
Use the following CSS code:
body { background-color: lightblue; }
-
How do you center an element horizontally using CSS?
Use the following CSS code:
.container { display: flex; justify-content: center; }
-
How do you create a grid layout in CSS?
Use the following CSS code:
.container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
-
How do you make a rounded button with CSS?
Use the following CSS code:
button { border-radius: 12px; padding: 10px 20px; background-color: #008CBA; color: white; border: none; }
-
How do you change the font style of an element?
Use the following CSS code:
p { font-family: 'Arial', sans-serif; font-size: 16px; }
-
How do you create a drop shadow effect on a box?
Use the following CSS code:
.box { box-shadow: 5px 5px 15px rgba(0,0,0,0.3); }
-
How do you create a responsive grid layout using CSS Grid?
Use the following CSS code:
.container { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; } .item { background-color: #f0f0f0; padding: 20px; border: 1px solid #ccc; }
-
How do you create a CSS animation to make an element bounce?
Use the following CSS code:
@keyframes bounce { 0%, 20%, 50%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-30px); } 60% { transform: translateY(-15px); } } .bouncing-element { animation: bounce 2s infinite; }
-
How do you create a CSS tooltip that appears on hover?
Use the following CSS code:
.tooltip { position: relative; display: inline-block; } .tooltip .tooltiptext { visibility: hidden; width: 120px; background-color: black; color: #fff; text-align: center; border-radius: 5px; padding: 5px; position: absolute; z-index: 1; bottom: 125%; /* Position above the tooltip element */ left: 50%; margin-left: -60px; opacity: 0; transition: opacity 0.3s; } .tooltip:hover .tooltiptext { visibility: visible; opacity: 1; }
-
How do you create a CSS transition effect for changing background color?
Use the following CSS code:
.transition-element { background-color: #4CAF50; transition: background-color 0.5s ease; } .transition-element:hover { background-color: #45a049; }
-
How do you implement a CSS flexbox layout for centering items horizontally and vertically?
Use the following CSS code:
.container { display: flex; justify-content: center; align-items: center; height: 100vh; /* Full viewport height */ } .item { background-color: #f0f0f0; padding: 20px; border: 1px solid #ccc; }
-
How do you hide an element using CSS?
Use the following CSS code:
.hidden { display: none; }
-
How do you add padding inside an element?
Use the following CSS code:
.container { padding: 20px; }
-
How do you apply a margin outside an element?
Use the following CSS code:
.container { margin: 20px; }
-
How do you style a link without an underline?
Use the following CSS code:
a { text-decoration: none; color: blue; }
-
How do you create a hover effect on a button?
Use the following CSS code:
button:hover { background-color: green; color: white; }
-
How do you create a full-width layout using CSS?
Use the following CSS code:
.container { width: 100%; max-width: 1200px; margin: 0 auto; }
-
How do you change the color of text in CSS?
Use the following CSS code:
p { color: red; }
-
How do you add a border around an element in CSS?
Use the following CSS code:
.container { border: 2px solid black; }
-
How do you create a fixed navigation bar?
Use the following CSS code:
nav { position: fixed; top: 0; width: 100%; background-color: #333; color: white; padding: 10px; z-index: 1000; }