Skip to content

Commit 9b2b69a

Browse files
Update README.md
1 parent a613e0b commit 9b2b69a

1 file changed

Lines changed: 77 additions & 99 deletions

File tree

README.md

Lines changed: 77 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -264,76 +264,7 @@ Render tree construction follows the following order:
264264

265265
<br/>
266266

267-
## Q. What is the CSS Box Model?
268-
269-
Every HTML element is treated as a rectangular box by the browser. The **CSS Box Model** describes this rectangular box through four areas that wrap around the element\'s content: **content**, **padding**, **border**, and **margin**.
270-
271-
```
272-
+------------------------------------------+
273-
| Margin |
274-
| +------------------------------------+ |
275-
| | Border | |
276-
| | +------------------------------+ | |
277-
| | | Padding | | |
278-
| | | +------------------------+ | | |
279-
| | | | Content | | | |
280-
| | | +------------------------+ | | |
281-
| | +------------------------------+ | |
282-
| +------------------------------------+ |
283-
+------------------------------------------+
284-
```
285-
286-
* **Content** – The area where the actual content (text, images, etc.) is displayed.
287-
* **Padding** – Transparent space between the content and the border.
288-
* **Border** – A line that wraps around the padding and content.
289-
* **Margin** – Transparent space outside the border, separating the element from others.
290-
291-
<div align="right">
292-
<b><a href="#">↥ back to top</a></b>
293-
</div>
294-
295-
## Q. What is the difference between `box-sizing: content-box` and `box-sizing: border-box`?
296-
297-
The `box-sizing` property controls how the total width and height of an element is calculated.
298-
299-
| Property | Width Calculation |
300-
|---|---|
301-
| `content-box` (default) | `width` = content only. Padding and border are **added** on top. |
302-
| `border-box` | `width` = content + padding + border. They are **included** inside the declared width. |
303-
304-
**Example:**
305-
306-
```css
307-
/* content-box (default): total rendered width = 200 + 20 + 20 + 5 + 5 = 250px */
308-
.box-content {
309-
box-sizing: content-box;
310-
width: 200px;
311-
padding: 20px;
312-
border: 5px solid #333;
313-
}
314-
315-
/* border-box: total rendered width = 200px */
316-
.box-border {
317-
box-sizing: border-box;
318-
width: 200px;
319-
padding: 20px;
320-
border: 5px solid #333;
321-
}
322-
```
323-
324-
A common best practice is to apply `border-box` globally:
325-
326-
```css
327-
*, *::before, *::after {
328-
box-sizing: border-box;
329-
}
330-
```
331-
332-
<div align="right">
333-
<b><a href="#">↥ back to top</a></b>
334-
</div>
335-
336-
## Q. Explain the CSS “box model” and the layout components that it consists of?
267+
## Q. Explain the CSS "box model" and the layout components that it consists of?
337268

338269
The CSS box model is a rectangular layout paradigm for HTML elements that consists of the following:
339270

@@ -388,6 +319,47 @@ The size of the box itself is calculated like this:
388319
<b><a href="#">↥ back to top</a></b>
389320
</div>
390321

322+
## Q. What is the difference between `box-sizing: content-box` and `box-sizing: border-box`?
323+
324+
The `box-sizing` property controls how the total width and height of an element is calculated.
325+
326+
| Property | Width Calculation |
327+
|---|---|
328+
| `content-box` (default) | `width` = content only. Padding and border are **added** on top. |
329+
| `border-box` | `width` = content + padding + border. They are **included** inside the declared width. |
330+
331+
**Example:**
332+
333+
```css
334+
/* content-box (default): total rendered width = 200 + 20 + 20 + 5 + 5 = 250px */
335+
.box-content {
336+
box-sizing: content-box;
337+
width: 200px;
338+
padding: 20px;
339+
border: 5px solid #333;
340+
}
341+
342+
/* border-box: total rendered width = 200px */
343+
.box-border {
344+
box-sizing: border-box;
345+
width: 200px;
346+
padding: 20px;
347+
border: 5px solid #333;
348+
}
349+
```
350+
351+
A common best practice is to apply `border-box` globally:
352+
353+
```css
354+
*, *::before, *::after {
355+
box-sizing: border-box;
356+
}
357+
```
358+
359+
<div align="right">
360+
<b><a href="#">↥ back to top</a></b>
361+
</div>
362+
391363
## Q. What is margin collapsing in CSS?
392364

393365
**Margin collapsing** occurs when the top and bottom margins of block-level elements combine into a single margin equal to the **larger** of the two values.
@@ -946,16 +918,16 @@ a:hover {
946918

947919
## Q. What is contextual selector?
948920

949-
Contextual selector addresses specific occurrence of an element. It is a string of individual selectors separated by white space (search pattern), where only the last element in the pattern is addressed providing it matches the specified contex.
950-
951-
It also check the context of the class in the html tree, assigning the style to the element through a specific route, taking into account the order of depth in the tree.
921+
A **contextual selector** targets an element only when it appears within a specific context (ancestor). It\'s a string of selectors separated by whitespace, where only the last element is styled — but only if it matches the specified ancestral path.
952922

953923
**Example:**
954924

955925
```css
956-
table p { property: value; }
926+
table p { color: red; }
957927
```
958928

929+
This styles `<p>` elements only when they are descendants of a `<table>` — not all `<p>` elements on the page.
930+
959931
<div align="right">
960932
<b><a href="#">↥ back to top</a></b>
961933
</div>
@@ -1093,7 +1065,7 @@ In the CSS, a class selector is a name preceded by a full stop (".") and an ID s
10931065
<b><a href="#">↥ back to top</a></b>
10941066
</div>
10951067

1096-
## Q. What is the difference between the nth-child() and nth-of-type() selectors?
1068+
## Q. What is the difference between the "nth-child()" and "nth-of-type()" selectors?
10971069

10981070
The `nth-child()` pseudo-class is used to match an element based on a number, which represents the element\'s position amongst it\'s siblings. More specifically, the number represents the number of siblings that exist before the element in the document tree (minus 1).
10991071

@@ -1137,34 +1109,40 @@ This number can also be expressed as a function, or using the keywords even or o
11371109

11381110
## Q. What is specificity?
11391111

1140-
A process of determining which css rule will be applied to an element. It actually determines which rules will take precedence. Inline style usually wins then ID then class value (or pseudo-class or attribute selector), universal selector (*) has no specificity. ID selectors have a higher specificity than attribute selectors.
1141-
1142-
**Selector Types**
1112+
**Specificity** is the algorithm browsers use to determine which CSS rule takes precedence when multiple rules target the same element.
11431113

1144-
The following list of selector types increases by specificity:
1114+
**Specificity Hierarchy (lowest → highest)**
11451115

1146-
* **Type selectors** (e.g., h1) and pseudo-elements (e.g., ::before).
1147-
* **Class selectors** (e.g., .example), attributes selectors (e.g., [type="radio"]) and pseudo-classes (e.g., :hover).
1148-
* **ID selectors** (e.g., #example).
1116+
| Selector Type | Example | Value |
1117+
|---------------|---------|-------|
1118+
| Universal selector | `*` | 0 |
1119+
| Element / pseudo-element | `h1`, `::before` | 0-0-1 |
1120+
| Class / attribute / pseudo-class | `.box`, `[type]`, `:hover` | 0-1-0 |
1121+
| ID selector | `#header` | 1-0-0 |
1122+
| Inline style | `style="..."` | Always wins |
1123+
| `!important` | `color: red !important` | Overrides all |
11491124

1150-
```css
1151-
/*wins*/
1152-
a#a-02 { background-image : url(n.gif); }
1153-
a[id="a-02"] { background-image : url(n.png); }
1154-
```
1125+
**Key Rules**
11551126

1156-
Contextual selectors are more specific than a single element selector.The embedded style sheet is closer to the element to be styled. The last rule defined overrides any previous, conflicting rules.
1127+
- **Higher specificity wins**, regardless of order.
1128+
- If specificity is **equal**, the **last rule** declared wins.
1129+
- A **class** beats any number of element selectors:
11571130

1158-
```css
1159-
p { color: red; background: yellow }
1160-
p { color: green } // wins
1161-
```
1131+
```css
1132+
.intro {} /* wins */
1133+
html body div div p {} /* loses */
1134+
```
1135+
- **ID** beats any number of classes:
1136+
```css
1137+
#main { color: red; } /* wins */
1138+
.box.card.active { color: blue; } /* loses */
1139+
```
11621140

1163-
A class selector beats any number of element selectors.
1141+
**Example**
11641142

11651143
```css
1166-
.introduction {} //wins
1167-
html body div div h2 p {}
1144+
a#nav-link.active:hover { color: red; }
1145+
/* ID=1, Class=2 (active + hover), Element=1 → specificity: 1-2-1 */
11681146
```
11691147

11701148
<div align="right">
@@ -1173,19 +1151,19 @@ html body div div h2 p {}
11731151

11741152
## Q. In CSS3, how would you select?
11751153

1176-
* Every ```<a>``` element whose href attribute value begins with https.
1154+
* Every ```<a>``` element whose href attribute value begins with "https".
11771155

11781156
```css
11791157
a[href^="https"]
11801158
```
11811159

1182-
* Every ```<a>``` element whose href attribute value ends with .pdf.
1160+
* Every ```<a>``` element whose href attribute value ends with ".pdf".
11831161

11841162
```css
11851163
a[href$=".pdf"]
11861164
```
11871165

1188-
* Every ```<a>``` element whose href attribute value contains the substring css.
1166+
* Every ```<a>``` element whose href attribute value contains the substring "css".
11891167

11901168
```css
11911169
a[href*="css"]
@@ -6729,7 +6707,7 @@ When using `translate()`, the element still occupies its original space (sort of
67296707

67306708
**Example:**
67316709

6732-
If we combine `position:relative` with one of the offset properties `top`, `bottom`, `left` or `right` the element will be moved from its original place in the layout whilst preserving the space in the document it once occupied. The element will be moved on to a new layer and its layer order or its stacking order can then be controlled with the `z-index` property.
6710+
If we combine `position:relative` with one of the offset properties `top`, `bottom`, `left` or `right` the element will be moved from its original place in the layout whilst preserving the space in the document it once occupied. The element will be moved on to a new layer and its "layer order" or its stacking order can then be controlled with the `z-index` property.
67336711

67346712
```css
67356713
.thing {

0 commit comments

Comments
 (0)