DEV Community

Narmatha
Narmatha

Posted on

Css interview questions

1.What is CSS?
CSS stands for Cascading Style Sheets. It is a styling language used to design and control the appearance of HTML web pages.
HTML is used to create the structure of a webpage, while CSS is used to make the webpage beautiful and user-friendly.
CSS can be used to change:
Text color
Font size and style
Background color
Width and height
Margin and padding
Borders
Page layout
Responsive design
Example:
p {
color: blue;
font-size: 20px;
}

Here, the paragraph text becomes blue and its font size becomes 20px.

  1. What are the types of CSS? **There are three types of CSS:

1. Inline CSS
CSS is written directly inside an HTML element using the style attribute.
HTML
<p style="color: red;">Hello</p>
It is useful when we want to apply a style to a single element.

**2. Internal CSS
**CSS is written inside the tag in the HTML document.<br> HTML<br> <code>&lt;style&gt;<br> p {<br> color: blue;<br> }<br> &lt;/style&gt;</code><br> It is useful when styling a single webpage.</p> <p><strong>3. External CSS</strong><br> CSS is written in a separate .css file and connected to the HTML file.<br> HTML<br> <code>&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;</code><br> This is commonly preferred for larger websites because the same CSS file can be used for multiple webpages.</p> <p><strong>3. What is a CSS Selector?<br> **A CSS selector is used to select HTML elements that we want to style.<br> **For example:</strong><br> <code>p {<br> color: red;<br> }</code><br> Here, p is the selector, and the style is applied to all <p> elements.<br> Common selectors include:<br> Element selector: p<br> Class selector: .title<br> ID selector: #header<br> Universal selector: *<br> Attribute selector: [type=&quot;text&quot;]<br> <strong>Example:</strong><br> <code>.title {<br> color: green;<br> }</code><br> This applies the style to elements having the class title.</p> <p><strong>4. What is the difference between Class and ID in CSS?</strong><br> Both class and ID are used to identify HTML elements, but they are used differently.<br> <strong>Class:</strong><br> Written using . in CSS.<br> Can be used on multiple elements.<br> Useful when applying the same style to several elements.<br> .student {<br> color: blue;<br> }<br> <strong>ID:</strong><br> Written using # in CSS.tq<br> Normally used for one unique element.<br> Useful when identifying a specific element.</p> <h1> <a name="header-" href="#header-" class="anchor"> </a> header { </h1> <p>color: red;<br> }<br> **Simple difference:<br> **Class → Multiple elements<br> ID → One unique element</p> <p><strong>5. What is the CSS Box Model?</strong><br> The CSS Box Model describes how every HTML element is represented as a rectangular box.<br> It consists of four parts:<br> <strong>Content</strong> – The actual text or image.<br> <strong>Padding</strong> – Space between the content and border.<br> <strong>Border</strong> – The line around the padding and content.<br> <strong>Margin</strong> – Space outside the border.<br> The order is:<br> Content → Padding → Border → Margin<br> *<em>Example:<br> *</em>.<code>box {<br> width: 200px;<br> padding: 20px;<br> border: 2px solid black;<br> margin: 10px;<br> }</code><br> The Box Model is very important for controlling the size and spacing of elements.</p> <p><strong>6. What is Padding in CSS?</strong><br> Padding is the space between the content and the border of an element.<br> <strong>For example:</strong><br> .<code>box {<br> padding: 20px;<br> }</code><br> This creates 20px of space between the content and the border.<br> We can also specify padding for individual sides:<br> <code>.box {<br> padding-top: 10px;<br> padding-right: 20px;<br> padding-bottom: 10px;<br> padding-left: 20px;<br> }</code><br> <strong>Simple example:</strong><br> If a box contains the word &quot;Hello&quot;, padding creates space around &quot;Hello&quot; inside the box.</p> <p><strong>7. What is Margin in CSS?<br> **Margin is the space outside the border of an element.<br> It is used to create space between elements.<br> **Example:</strong><br> <code>.box {<br> margin: 20px;<br> }</code><br> This creates 20px of space around the outside of the element.<br> We can also use:<br> margin-top: 10px;<br> margin-right: 20px;<br> margin-bottom: 10px;<br> margin-left: 20px;<br> Padding vs Margin<br> **Padding: **Space inside the border.<br> **Margin: **Space outside the border.</p> <p><strong>8. What is display: flex in CSS?</strong><br> Flexbox is a CSS layout system used to arrange elements in a row or column.<br> <strong>To use Flexbox:</strong><br> <code>.container {<br> display: flex;<br> }</code><br> By default, the child elements are arranged in a row.<br> Some important Flexbox properties are:<br> display: flex<br> flex-direction<br> justify-content<br> align-items<br> gap<br> flex-wrap<br> <strong>Example:</strong><br> <code>.container {<br> display: flex;<br> justify-content: center;<br> align-items: center;<br> }</code><br> Flexbox is commonly used to create navigation bars, cards, buttons, and page layouts.</p> <p><strong>9. What is CSS Grid?<br> **CSS Grid is a layout system used to arrange elements in rows and columns.<br> **Example</strong>:<br> <code>.container {<br> display: grid;<br> grid-template-columns: 1fr 1fr 1fr;<br> }</code><br> This creates three columns of equal size.<br> Grid is useful for creating:<br> Website layouts<br> Image galleries<br> Product cards<br> Dashboard layouts<br> Rows and columns<br> Flexbox vs Grid<br> <strong>Flexbox: **Mainly works in one direction — row or column.<br> **Grid:</strong> Works in two directions — rows and columns.</p> <p><strong>10. What is the difference between Flexbox and Grid?<br> **Both Flexbox and Grid are CSS layout systems, but they are designed for different purposes.<br> **Flexbox</strong><br> Flexbox is mainly a one-dimensional layout system.<br> It is useful when arranging elements in:<br> A row<br> A column<br> <strong>Example:<br> **<code>.container {<br> display: flex;<br> justify-content: space-between;<br> }</code><br> Grid<br> Grid is a two-dimensional layout system.<br> It is useful when working with:<br> Rows<br> Columns<br> Complete page layouts<br> **Example:</strong><br> <code>.container {<br> display: grid;<br> grid-template-columns: 1fr 1fr;<br> }</code></p>

Top comments (0)