CSS Flashcards

1
Q

What is CSS selector specificity and how does it work?

A

Explanation: The means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.

Use: Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector.

Example: A selector of #id .class tag would have 111 points as id’s count for 100, classes for 10 and tags 1.

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

What’s the difference between “resetting” and “normalizing” CSS? Which would you choose, and why?

A

Explanation: “Normalize” alters the default styles of various browser to match each other. “Reset” will remove the browsers default styles so you are starting from scratch.

Use: Applying one or the other is done to try and make websites visually consistent across different browsers. I prefer to use a mix of both. Starting with the normalize to keep it conscise and then add some elements like anchors and headers with a reset. Going full “nuke” is often unnecessary and creates a larger, harder to debug file.

Example:

Normalize:
/**
* Correct the font size and margin on h1 elements within section and
* article contexts in Chrome, Firefox, and Safari.
*/

h1 {
font-size: 2em;
margin: 0.67em 0;
}

Reset:
html,
body,
h1,
h2,
h3,
h4,
h5,
h6 {
margin: 0;
padding: 0;
}

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

Describe floats and how they work.

A

Explanation: Floats are a positioning property where the element that is floated will be removed from the flow of the page and affect the elements around it. A parent element will collapse to zero height if it contains only floated elements, to fix this it was common to use a .clearfix hack.

Use: It was used prior to flex and grid to layout pages in a more flexible manner.

Example: You could float three elements left and give them widths of 33% to create three even width columns.

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

Describe z-index and how stacking context is formed.

A

Explanation: The z-index property in CSS controls the vertical stacking order of elements that overlap. A stacking context is an element that contains a set of layers. The z-index values of its children are set relative to that element rather than to the document root. Layers outside of that context can’t sit between layers within it.

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

Describe BFC (Block Formatting Context) and how it works.

A

Explanation: A BFC is an HTML box that satisfies at least one of the following conditions:
The value of float is not none.
The value of position is neither static nor relative.
The value of display is table-cell, table-caption, inline-block, flex, or inline-flex, grid, or inline-grid.
The value of overflow is not visible.

Use: Knowing how to establish a block formatting context is important, because without doing so, the containing box will not contain floated children.

Example: Without forming a BFC you could have content of a float that is taller than the content alongside it. The border of the parent element could then “cut-through” the floated box.

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

What are the various clearing techniques and which is appropriate for what context?

A

Explanation:
- Empty div method
- Clearfix method
- overflow: auto or overflow: hidden method

Use: .clearfix utility class is probably the best method to use in general as it doesn’t take long to construct and doesn’t suffer from clipping issues like the overflow methods.

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

Explain CSS sprites, and how you would implement them on a page or site.

A

Explanation: CSS Sprites are a means of combining multiple images into a single image file for use on a website, to help with performance.

Use: Browsers limit the number of concurrent requests a site can make so leading several images with a single HTTP request helps increase page load speed.

Example: An example would be combining press logo’s for Wired, NY Times and The Washington Post into a single image file. Then on the site, with CSS, placing the file three times and moving/cropping it to display the applicable logo.

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

How would you approach fixing browser-specific styling issues?

A

Explanation: There are a handful of ways to solve the issue such as browser specific stylesheets, using a library like bootstrap, etc. MY preference would be to use a combination normalize/reset style sheet. I’d rather use a combination as going full nuke with a reset isn’t necessary and makes it a little harder to debug.

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

How do you serve your pages for feature-constrained browsers? What techniques/processes do you use?

A

Explanation: My preference is to try and build lightweight simple websites that incorporate progressive enhancement.

Use: Build the base level of HTML/CSS with semantics and accessibility in the forefront you get a site that works well on feature-constrained browsers. I would then add any CSS on JavaScript enhancements deliberately, checking caniuse.com and using vendor prefixs and polyfills if required.

Example: Instead of filling the site with <div> using more semantically appropriate tags like <section> <aside> <article> <header> <footer>

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

What are the different ways to visually hide content (and make it available only for screen readers)?

A

Explanation:
- Make the element have a size of zero width: 0; height: 0
- Absolute position off screen position: absolute; left: -99999px
- Text indent off screen if within block element text-indent: -9999px
aria-label which will read the string given to the attribute.

Use: I typically absolutely position the element off screen as it covers the most scenarios.

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

Have you ever used a grid system, and if so, what do you prefer?

A

Explanation: I typically use a 12 column “grid” system when doing my initial web layout.

Use: I find that it works well for laying out the average website and giving the site some visual consistency. When if comes to coding the site I find it helps speed up the layout immensely.

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

Have you used or implemented media queries or mobile specific layouts/CSS?

A

Explanation: I use them quite frequently.

Use: I use them on every website and typically build mobile first. The breakpoints and media queries are then used to convert the layout from mobile to desktop.

Example: Some examples is changing a bunch of cards from being a single column stack on mobile to a three column layout on desktop.

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

Are you familiar with styling SVG?

A

Explanation: Yes there are a few ways to style them including inline CSS, embedded CSS or an external style sheet. Basic coloring can be done with the fill and stroke attributes.

Example:
< rect width=”100” height=”100” stroke=”blue” fill=”purple” />

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

Can you give an example of an @media property other than screen?

A

Explanation & Use: There are four types:
all - for all media type devices
print - for printers
speech - for screenreaders that “reads” the page out loud
screen - for computer screens, tablets, smart-phones etc.

Example: An example of using print and making all the text black:
@media print {
body {
color: black;
}
}

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

What are two “gotchas” for writing efficient CSS?

A

Explanation:
Browsers match selectors from rightmost (key selector) to left. The shorter the length of the chain the faster the browser can find a match. Avoid using tag and universal selectors for your key selector.
Avoid using styles that trigger reflow.

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

What are the advantages/disadvantages of using CSS preprocessors?

A

Explanation: Some advantages would be:

The code is easier to maintain
More efficient to write with nested selectors
Mixins can be used for repeated styles
Ability to split into different files

Disadvantages would be:

Additional tooling is required
You aren’t able to use the most current features of standard CSS

17
Q

Describe what you like and dislike about the CSS preprocessors you have used.

A

Explanation: I’ve found that being able to split files and nest selectors is the most useful. A couple of downsides are that debugging is a little more difficult and having to wait for compilation.

18
Q

How would you implement a web design comp that uses non-standard fonts?

A

Explanation: Use @font-face and define font-family

19
Q

Explain how a browser determines what elements match a CSS selector.

A

Explanation: Browsers match selectors from rightmost (key selector) to left.

Example: For example with this selector p span, browsers firstly find all the <span> elements and traverse up its parent all the way up to the root to find the <p> element. For a particular <span>, as soon as it finds a <p>, it knows that the <span> matches and can stop its matching.</span></span></span>

20
Q

Describe pseudo-elements and discuss what they are used for.

A

Explanation & Use: A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s)

Example: ::first-line can be used to change the font of the first line of a paragraph

21
Q

Explain your understanding of the box model and how you would tell the browser, through CSS, to render your layout in different box models.

A

Explanation: The CSS box model describes the rectangular boxes that are generated for elements in the document tree and laid out according to the visual formatting model. Each box has a content area and optional surrounding padding, border, and margin areas.

Use: The standard box model calculates box size by taking a specified height and width, then adding the padding and border. However to change to the alternative box model you would set box-sizing: border-box which allows you to set the box size with height and width.

22
Q

What does * { box-sizing: border-box; } do? What are its advantages?

A

Explanation & Use: It allows you to specify the actual width and height of a box using the width and height properties. This allows you to input true sizes and not have to do any math to take padding and borders into account.

23
Q

What is the CSS display property and can you give a few examples of its use?

A

Explanation & Use: The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex.

Example: display: block would make the element consume the whole line width. display: grid would allow you to layout children in a grid system. If you wanted three columns you could pair it with something like grid-template-columns: 1fr 1fr 1fr

24
Q

What’s the difference between inline and inline-block?

A

Explanation:

  • inline
    CANNOT specify width and height
    Can only set margin and padding for the sides, not top and bottom.
  • inline-block
    CAN specify width and height
    Can set margin and padding on all sides
25
Q

What’s the difference between a relative, fixed, absolute and static positioned element?

A

Explanation:

  • Relative - Position is relative to it’s original static position. Original space on the page is preserved.
  • Fixed - Element removed from page flow and placed in spot relative to viewport. It won’t move when scrolled.
  • Absolute - Element removed from page flow and positioned relative to it closest “positioned” ancestor. Original space on the page is not preserved
    Static - The default position. top, right, bottom, left and z-index properties do not apply.
26
Q

What existing CSS frameworks have you used locally, or in production? How would you change/improve them?

A
  • Bootstrap - It takes a while to get the latest CSS features added. Your sites end up looking very similar to others.
  • Tailwind - The HTML can feel very cluttered. Reusing styles is a bit clunky.
27
Q

Have you played around with the new CSS Flexbox or Grid specs?

A

Explanation: I have used both flexbox and grid and like to employ both of them.

Use: I find grid to be useful for the top level page layout and any elements which have a typical grid layout. I prefer using flexbox for sections and other elements which don’t need a rigid grid alignment.

Example: If I had something like a tic-tac-toe board I would use grid as it is easy to get the boxes to align and be the same size. If I had some sort of information card with multiple pieces of information I would likely use flexbox.

28
Q

Can you explain the difference between coding a web site to be responsive versus using a mobile-first strategy?

A

Explanation: Making a website responsive means the some elements will respond by adapting its size or other functionality according to the device’s screen size. A mobile-first strategy is also responsive, however it agrees we should default and define all the styles for mobile devices, and only add specific responsive rules to other devices later.

Use: You would use media queries to make the above changes at certain screen size breakpoints.

Example:
An example of mobile first and responsive would be:
.my-class {
font-size: 12px;
}

@media (min-width: 600px) {
.my-class {
font-size: 24px;
}
}

29
Q

How is responsive design different from adaptive design?

A

Explanation: Both responsive and adaptive design attempt to optimize the user experience across different devices.

Use: Responsive design works on the principle of flexibility - a single fluid website that can look good on any device. Instead of one flexible design, adaptive design detects the device then provides the appropriate feature and layout based on a predefined set of viewport sizes and other characteristics. I believe responsive is the best approach to provide a great experience for all users.

30
Q

Have you ever worked with retina graphics? If so, when and what techniques did you use?

A

Explanation: Retina is just a marketing term to refer to high resolution screens with a pixel ratio bigger than 1. In order to have crisp, good-looking graphics that make the best of retina displays we need to use high resolution images whenever possible. However using highest resolution images will have an impact on page load times.

Use: To overcome this problem, we can use responsive images, as specified in HTML5 with the srcset attribute.

Example:
<img></img>

31
Q

Is there any reason you’d want to use translate() instead of absolute positioning, or vice-versa? And why?

A

Explanation: translate() is a value of CSS transform. transform causes the browser to create a GPU layer for the element but changing absolute positioning properties uses the CPU. translate() would be the more efficient solution with shorter paint times. If you do not want the original space of the element preserved you would want to use absolute positioning.