CSS question
What does CSS stand for?
Answer: CSS stands for Cascading Style Sheets. It is used to describe the presentation of a document written in HTML or XML.What is the purpose of CSS?
Answer: CSS is used to control the style and layout of web pages—including colors, fonts, spacing, positioning, and more—making them visually appealing and user-friendly.How can you add CSS to a web page?
Answer: CSS can be added in three ways:Inline CSS: Within the HTML elements using the
styleattribute.<p style="color: red; font-size: 20px;">This is a red paragraph with inline CSS.</p>
Internal CSS: Within a
<style>tag in the<head>section.<style>
p {
color: blue;
font-size: 18px;
}
</style>
</head>
External CSS: By linking an external
.cssfile using<link>tag.<head>
<link rel="stylesheet" href="styles.css">
</head>
What is the basic syntax of a CSS rule?
Answer: A CSS rule is written as:selector { property: value; }For example:p { color: red; }changes paragraph text to red.How do you change the text color of an element?
Answer: Using thecolorproperty. Example:h1 { color: blue; }How do you change the background color of a web page or element?
Answer: Use thebackground-colorproperty. Example:body { background-color: lightgrey; }Which property controls the size of the text?
Answer: Thefont-sizeproperty. Example:p { font-size: 16px; }What are some common units used in CSS?
Answer:Absolute units:
px,cm,mm,inRelative units:
%,em,rem,vw,vh
How can you make text bold using CSS?
Answer: Use thefont-weightproperty with valuebold. Example:strong { font-weight: bold; }What is the default position value of an HTML element in CSS?
Answer: The default value isstatic, which means the element is positioned according to the normal flow of the page.What does the
displayproperty do in CSS?
Answer: Thedisplayproperty specifies the display behavior (block, inline, flex, etc.) of an element.What's the difference between a class and an ID in CSS?
Answer:
Class (
.): Can be used multiple times across a page. Class is reusable and starts with.ID (
#): Must be unique and used only once per page. ID starts with#
.my-class {color: red;}#my-id {color: blue;}
How do you define a class selector in CSS?
Answer: With a dot before the class name:.classname { ... }How do you define an ID selector in CSS?
Answer: With a hash symbol before the ID:#idname { ... }What is the difference between margin and padding?
Answer:
Margin: Space outside the border.
Padding: Space inside the border, around the content.
How can you center a block element horizontally?
Answer: Set a width and usemargin: 0 auto;.Which property changes the font style?
Answer: Usefont-family. Example:p { font-family: Arial, sans-serif; }What is the CSS box model?
Answer: It consists of 4 parts: content, padding, border, and margin, which determine the total size of an element.What is the default display value of a
<div>element?
Answer:blockWhat is the default display value of a
<span>element?
Answer:inlineHow do you add a border to an element?
Answer: Use theborderproperty. Example:div { border: 1px solid black; }How can you round the corners of a box in CSS?
Answer: Use theborder-radiusproperty. Example:div { border-radius: 10px; }What does the
z-indexproperty do?
Answer: It controls the vertical stacking order of elements. Higher values are shown in front.Difference between
position: absoluteandrelative?
Answer:
absolute: Positioned relative to the nearest positioned ancestor.relative: Positioned relative to its original position.
How do you hide an HTML element with CSS?
Answer: Usingdisplay: none;orvisibility: hidden;What is the
:first-childpseudo-class used for?
Answer: Selects the first child of a parent element.What is the
:last-childpseudo-class used for?
Answer: Selects the last child of a parent element.What are pseudo-elements in CSS?
Answer: These allow you to style specific parts of elements. Example:::before,::afterHow do you apply a style when a user hovers over an element?
Answer: Use the:hoverpseudo-class. Example:a:hover { color: red; }What is CSS specificity?
Answer: It's a system of scoring to determine which CSS rule is applied when multiple rules match the same element.
What is
inheritin CSS?
Answer: Theinheritkeyword forces a property to inherit its value from the parent element.How do you apply styles to multiple selectors?
Answer: Separate selectors with commas. Example:h1, h2, p { color: blue; }What is a media query in CSS?
Answer: Media queries apply CSS rules based on screen/device conditions like width or resolution.How do you target screens smaller than 600px using a media query?
Answer:@media (max-width: 600px) { ... }What's the difference between
emandrem?
Answer:
em: Relative to the font-size of the parent.rem: Relative to the root (html) font-size.
What are pseudo-classes in CSS?
Answer: Special keywords used to define a special state of an element, like:hover,:focus,:active.How can you make an image responsive?
Answer: By settingmax-width: 100%andheight: auto.What is Flexbox in CSS?
Answer: A layout model that arranges elements efficiently in a container, even when their size is unknown or dynamic.How do you define a flex container?
Answer: By settingdisplay: flex;on the parent container.How do you center content with Flexbox?
Answer: Usejustify-content: center;andalign-items: center;.What does
flex-wrapdo?
Answer: It allows flex items to wrap onto multiple lines instead of overflowing.Difference between
min-widthandmax-width?
Answer:
min-width: Sets the minimum width of an element.max-width: Sets the maximum width it can grow to.
What does
position: fixeddo?
Answer: It fixes the element relative to the viewport. It stays in place even when the page is scrolled.What is the
overflowproperty used for?
Answer: It controls what happens when content overflows its container. Values includevisible,hidden,scroll, andauto.How to show text overflow as an ellipsis?
Answer:
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;What does
opacitycontrol?
Answer: It sets the transparency level of an element (0 is fully transparent, 1 is fully opaque).Why use
transitionin CSS?
Answer: To animate changes in CSS properties smoothly over a duration.Write an example of a transition.
Answer:transition: background-color 0.3s ease;What is
box-shadow?
Answer: It adds shadow around an element. Syntax:box-shadow: x-offset y-offset blur color;What is
:nth-child(n)used for?
Answer: To select elements based on their position among siblings.
What is the difference between
visibility: hidden;anddisplay: none;?
Answer:visibility: hidden;hides the element but it still takes up space in the layout.display: none;removes the element from the layout entirely.How do you make a div fill the height of its parent?
Answer: Set the div's height to100%and make sure the parent has a defined height.What does
calc()do in CSS?
Answer: It allows you to perform calculations for CSS property values. Example:width: calc(100% - 50px);How do you vertically center an element using Flexbox?
Answer: Applydisplay: flex; align-items: center;to the container.What is the difference between relative and absolute units?
Answer: Relative units (like %, em, rem) scale based on other elements. Absolute units (like px, cm) do not.What is a CSS Grid?
Answer: CSS Grid is a layout system for creating complex web page layouts using rows and columns.How do you define a grid container?
Answer: Applydisplay: grid;to the parent element.How do you define grid columns?
Answer: Usegrid-template-columns. Example:grid-template-columns: 1fr 2fr;What does
frmean in CSS Grid?
Answer:frstands for fraction and is used to distribute available space in the grid.How do you create space between grid items?
Answer: Use thegapproperty. Example:gap: 10px;What is the difference between inline and block elements?
Answer: Block elements take full width and start on a new line. Inline elements only take up as much width as necessary.How do you apply a linear gradient in CSS?
Answer: Use thebackgroundproperty. Example:background: linear-gradient(to right, red, blue);How do you make a website mobile-friendly using CSS?
Answer: Use responsive design principles including media queries, flexible grids, and relative units.What is the default value of
positionin CSS?
Answer: The default isstatic.What is
object-fitused for?
Answer: It defines how media (like images or videos) should fit in their container. Example:object-fit: cover;What is
pointer-eventsin CSS?
Answer: It controls how an element responds to mouse/touch interactions. Example:pointer-events: none;How can you animate an element in CSS?
Answer: Use the@keyframesrule and theanimationproperty.What does
transformdo in CSS?
Answer: It applies transformations like rotate, scale, translate, and skew. Example:transform: rotate(45deg);What is
backface-visibilityin CSS?
Answer: It defines whether the back face of an element is visible when rotated.What is
will-changein CSS?
Answer: It hints to the browser which properties will change, optimizing performance for those changes.
What is specificity hierarchy in CSS?
Answer: Specificity hierarchy determines which rule is applied when multiple selectors target the same element. Inline styles > IDs > Classes/Pseudo-classes/Attributes > Elements/Pseudo-elements.
/* Less specific */p { color: red; }/* More specific */#main p { color: blue; }
What is the
!importantdeclaration in CSS?
Answer:!importantforces a style to override any other rules, regardless of specificity. However, it's considered bad practice if overused. p { color: red !important; }What is the difference between
emandrem?
Answer:emis relative to the font-size of its parent, whileremis relative to the root element’s font-size.
html { font-size: 16px; }p { font-size: 2em; } /* 2 times parent */div { font-size: 2rem; } /* 2 times root */
What is the difference between
min-width,max-width, andwidth?
Answer:
width: Sets a fixed or responsive width.min-width: Ensures the element is not narrower than a certain size.max-width: Prevents the element from growing beyond a size.
div {min-width: 200px;max-width: 800px;width: 100%;}
How do media queries work in CSS?
Answer: Media queries apply styles conditionally based on screen size, resolution, or device features. Example:@media (max-width: 768px) { ... }
What is a CSS preprocessor?
Answer: Tools like Sass or LESS extend CSS with variables, nesting, and functions, which are then compiled into regular CSS.
What is the difference between
nth-child()andnth-of-type()?
Answer:
nth-child(): Targets the nth child regardless of element type.nth-of-type(): Targets the nth child of a specific type (e.g.,p:nth-of-type(2)).
li:nth-child(2) { color: red; }li:nth-of-type(2) { color: blue; }
How do you create a sticky header using CSS?
Answer: Useposition: sticky; top: 0;on the header element.What is stacking context in CSS?
Answer: A stacking context is a 3D space where elements are stacked according to z-index. It's created by properties likeposition,opacity,transform, andz-index.
div {position: relative;z-index: 10;}
How do you apply different styles for print media?
Answer: Use@media printto define styles that apply only when printing a webpage.
What is the difference between inline-block and block?
Answer:inline-blockbehaves like inline in terms of flow but accepts box model properties likewidth,height,margin.blockbreaks the line before and after the element.
What are keyframes in CSS?
Answer: Keyframes define the intermediate steps of a CSS animation. Example: @keyframes move { 0% { left: 0; } 100% { left: 100px; } }
What is the difference between transition and animation in CSS?
Answer:
transitionanimates between two states.animationallows more complex and multi-step animations using keyframes.
What are pseudo-classes and how do they differ from pseudo-elements?
Answer:
Pseudo-classes (
:hover,:nth-child) select elements in a certain state.Pseudo-elements (
::before,::after) style specific parts of elements.
Comments
Post a Comment