CSS - Inheritance, Combinators, Specificity, Box Model Flashcards

1
Q

Type selectors (e.g. p, div, span)
Inline styles (e.g. style=”color: red;”)
Class selectors (e.g. .my-class)
ID selectors (e.g. #my-id)

Rank these in order of specificity

A

inline, ID selectors, class, type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you reset the style sheet? And where should you apply this rule?

A

body {
margin: 0;
padding: 0;
}

OR

<head>
<link></link>
</head>

(for more properties)

At the top of the css sheet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In this code, which style will be applied and why? span {
font-size: 20px;
}
span {
font-size: 100px;
}

A

2nd one- takes precedence - read top to bottom, anything below overrides the top.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which tag has the lowest specificity?

A

Universal Selector *

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are pseudo classes?

A

Special selectors that allow you to style an element based on its state or context.

:hover - styles an element when the mouse pointer is over it.
:active - styles an element when it is being clicked or touched.
:focus - styles an element when it has focus, such as when a user clicks on a form field.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why is it better to style <body> for a universal style in html not *?

A

Body selects only elements that are shown on the page, not slow it down with <head> <title> etc. * Can style unintended elements like h1.</title>

if you style all h1 tags to have a particular font size with *, you may unintentionally affect the font size of other headings on the page, or even of other elements like form labels or buttons.

Readability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the browser default styling?

A

Provided by the browser. Can vary e.g

Font: The default font used by most browsers is usually a sans-serif font, such as Arial or Helvetica. The font size is typically 16px for most elements.

Text color: The default text color is usually black.

Background color: The default background color is usually white.

Margins and padding: . For example, the default margin for a body element is usually 8px.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a combinator and why are they used?

A

ul li {
/* styles here */
}

Descendant Selector (space): This combinator selects all elements that are descendants of a specified element. For example, the following selector targets all li elements that are descendants of a ul element:

WHY?

To create more specific selectors. E.g you may just want one h3 header to be green, the rest you want blue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is it called ‘box model?’

A

In CSS, the term “box model” refers to the way in which elements are represented as rectangular boxes on a web page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is content and padding?

A

Content: This is the actual content of the element, such as text, images, or other media.

Padding: This is the space between the content and the border of the element. Padding can be used to add additional space between the content and the border.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is border and margin?

A

Border: This is the line that surrounds the element. Borders can be used to add a decorative element or to separate the element from other elements on the page.

Margin: This is the space between the border of the element and the neighbouring elements. Margins can be used to create space between elements or to create a visual separation between elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why might a specific margin of a box be already styled? How can this be overcome?

A

From the body. Put margin: 0; in body tag.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is margin collapsing?

A

Margin collapsing, also known as margin collapse, is a phenomenon that occurs when the top and bottom margins of adjacent elements collapse into a single margin.

This happens when the margins of two adjacent elements come in contact with each other, and the larger margin value “wins” while the smaller margin value is discarded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are ‘block’ elements? And why is it called block?

A

div, p, h1 through h6, ul, ol, li, form, and table.

They create a rectangle and take up the full width of the page. They are stacked on top of each other like blocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In block level elements e.g what can change - the height or the width?

A

The height- according to amount of content.

By default, block-level elements take up 100% of the available width of their parent container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why are ‘shorthand’ properties important?

A

Lesser loading time - less code.
Readability, conciseness.

17
Q

Instead of this how can you make it shorthand?

padding-top, padding-right, padding-bottom, and padding-left

A

padding: 5px 10px 5px 10px

(top right bottom left) LIKE A COMPASS.

18
Q

Create a shorthand for border 5px solid black

A

border: 5px solid black;

19
Q

Why doesn’t this div take up the height of the whole page?

<section>
<div>TESTING HEIGHT</div>
</section>

section {
height: 100%;
background-color: red;
}

A

It fills up the parent whose height is calculated by the content it holds.

20
Q

What is box-sizing: content-box? And what issues might this cause?

A

This calculates height and width based on content only.

Can cause styling issues, like overflow.

21
Q

What is box-sizing: border-box; and why should this be applied * {
} universal selector?

A

When box-sizing: border-box is applied to an element, its size is calculated based on its content, padding, and border.

In other words, the element’s total size includes its padding and border, and any margin is calculated outside of that total size.

To ensure it is not overridden by default styling from the browser.

22
Q

How is the size of an element calculated with box-sizing: content-box?

A

the height and width you put in e.g 20px, 20px, but when you inspect it it will be different, since that’s only the content, not the actual size with padding, border etc.

23
Q

How is the size of an element calculated with box-sizing: border-box.

A

It is easier to calculate since the height and width includes content, padding, then border.

24
Q

Why is margin not calculated with box-sizing: border-box width and height?

A

Because it is meant for spacing between elements, not the element itself.

25
Q

What is text-decoration?

How to get rid of it?

text-decoration: none;

A

Default set by browser, e.g for links underlined. Or can be:

none: This is the default value and indicates no decoration should be applied to the text.

underline: Adds a line underneath the text.

overline: Adds a line above the text.

line-through: Adds a line through the middle of the text.

26
Q

What does vertical align do?

When is this useful?

A

In CSS, the vertical-align property is used to vertically align inline or inline-block elements within a line box or a table cell.

When you have an image - with text next to it - you could text-align: middle; of you want the text in the middle to the side of the image or

27
Q

How can I put two elements e.g a div with a logo and a nav with 3 links, in the same line inside a header element?

A

make sure that the nav does not take up 100% width.

nav
width: calc(100% - (width of div)

Now both can fit inline (block) together.

28
Q

Why would this not get rid of underline default style of texts for links?

.main-nav__items {
/* list style gets rid of default underlined links */
margin: 0;
padding: 0;
list-style: none;

/* text-decoration: none; */ }
A

Because it is provided directly to anchor tags.

These need to be targetted.

29
Q

How do add a pseudo class of hover and active to this element - and what is the difference?

div > button {

}

A

div > button:hover {
color: blue;
}

OR

div > button:active {
color: white;
}

Hover: Hover, Active, when pressed.

30
Q

Why are certain styles faded in ‘inspect’ css?

A

The style is being overridden by a more specific selector or a later rule in the CSS cascade.

31
Q

If I have two rules in css but one is at the top of the stylesheet and one at the bottom - which will be applied?

A

Bottom

32
Q

What does this code mean?

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>

li:not(.highlight) {
color: red;
}

A

If a list item does not have a highlight class, then make the colour of the text red.

33
Q

How can you check if the browser of your customer supports the css style?

A

MDN documents at the bottom or canIUse.com