CSS Flashcards

1
Q

What is CSS selector specificity and how does it work?

A
"Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector. When multiple declarations have equal specificity, the last declaration found in the CSS is applied to the element (because it is a cascading style sheet).
Specificity works by calculating the weight of each declaration. For this explanation, I'll say they are a, b, c, and d - going from highest specificity to the lowest. a is inline style elements since they always override any external stylesheets, they hold the highest specificity weight of 1000. b is ID selectors weight 100. c is class, attribute, and pseudo class selectors, weight 10. d is all of your type selectors, weight 1. For each selector, you add 1 to its column. You don't get a score from these weights rather a matrix of values that can be compared. a declaration of two class selectors (c) and 1 type selector(d) will hold a higher weight than five type selectors because your matrix is 0021 which = 21, vs 0005 which = 5."
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
  • Resetting is meant to strip all default browser styling on elements
  • Normalizing preserves useful default styles rather than completely resetting all styles. Additionally, when elements have different default styling in different browsers, normalize.css aims to make those styles consistent and in line with modern standards when possible.
  • For more complex projects, I would choose resetting CSS because I am able to have more control over content placement and it is easier to make changes in case I need to add or take away features. I can search for the selector and its properties rather than rely on its default styling.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe floats and how they work.

A

“Float is a CSS positioning property. Floated elements remain a part of the flow of the page, and will affect the positioning of other elements (e.g. text will flow around floated elements), unlike position: absolute elements, which are removed from the flow of the page.

The CSS clear property can be used to be positioned below left/right/both floated elements.

If a parent element contains nothing but floated elements, its height will be collapsed to nothing. It can be fixed by clearing the float after the floated elements in the container but before the close of the container.

The .clearfix hack uses a clever CSS pseudo selector (:after) to clear floats. Rather than setting the overflow on the parent, you apply an additional class clearfix to it. “

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

“The z-index property in CSS controls the vertical stacking order of elements that overlap. z-index only affects elements that have a position value which is not static.

Without any z-index value, elements stack in the order that they appear in the DOM (the lowest one down at the same hierarchy level appears on top). Elements with non-static positioning (and their children) will always appear on top of elements with default static positioning, regardless of HTML hierarchy.

A stacking context is an element that contains a set of layers. Within a local stacking context, the z-index values of its children are set relative to that element rather than to the document root. Layers outside of that context — i.e. sibling elements of a local stacking context — can’t sit between layers within it. If an element B sits on top of element A, a child element of element A, element C, can never be higher than element B even if element C has a higher z-index than element B.

Each stacking context is self-contained - after the element’s contents are stacked, the whole element is considered in the stacking order of the parent stacking context. A handful of CSS properties trigger a new stacking context, such as opacity less than 1, filter that is not none, and transform that is not none.

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

“Block Formatting contexts is part of the CSS rendering of a webpage that determines how block boxes are laid out. They are commonly be floated elements, positioned elements that aren’t static or relative, table-cells, inline-block, flex / grid items and a few other box elements that I won’t mention. It’s used to determine from how elements will be positioned or cleared within the same parent element. Floats don’t affect the layout of the content inside other block formatting contexts, and clear only clears past floats in the same block formatting context. Margin collapsing also occurs only between blocks that belong to the same block formatting context.

<div>
<div>I am a floated box!</div>
<p>I am content inside the container.</p>
</div>

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

“Empty div method - <div style=""> </div>.
Clearfix method -

The .clearfix hack uses a clever CSS pseudo selector (:after) to clear floats. Rather than setting the overflow on the parent, you apply an additional class clearfix to it. Then apply this CSS:

.clearfix:after {
  content: ' ';
  visibility: hidden;
  display: block;
  height: 0;
  clear: both;
}

overflow: auto or overflow: hidden method - Parent will establish a new block formatting context and expand to contains its floated children.

In large projects, I would write a utility .clearfix class and use them in places where I need it. overflow: hidden might clip children if the children is taller than the parent and is not very ideal.”

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

“CSS sprites combine multiple images into one single larger image. It is a commonly-used technique for icons (Gmail uses it). How to implement it:

Use a sprite generator that packs multiple images into one and generate the appropriate CSS for it.
Each image would have a corresponding CSS class with background-image, background-position and background-size properties defined.
To use that image, add the corresponding class to your element.
Advantages:

Reduce the number of HTTP requests for multiple images (only one single request is required per spritesheet). But with HTTP2, loading multiple images is no longer much of an issue.
Advance downloading of assets that won’t be downloaded until needed, such as images that only appear upon :hover pseudo-states. Blinking wouldn’t be seen.”

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

“After identifying the issue and the offending browser, use a separate style sheet that only loads when that specific browser is being used. This technique requires server-side rendering though.
Use libraries like Bootstrap that already handles these styling issues for you.
Use autoprefixer to automatically add vendor prefixes to your code.
Use Reset CSS or Normalize.css.
If you’re using Postcss (or a similar transpiling library), there may be plugins which allow you to opt in for using modern CSS syntax (and even W3C proposals) that will transform those sections of your code into corresponding safe code that will work in the targets you’ve used.”

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

“Graceful degradation - The practice of building an application for modern browsers while ensuring it remains functional in older browsers.
Progressive enhancement - The practice of building an application for a base level of user experience, but adding functional enhancements when a browser supports it.
Use caniuse.com to check for feature support.
Autoprefixer for automatic vendor prefix insertion.
Feature detection using Modernizr.
Use CSS Feature queries @support”

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

“These techniques are related to accessibility (a11y).

visibility: hidden. However, the element is still in the flow of the page, and still takes up space.
width: 0; height: 0. Make the element not take up any space on the screen at all, resulting in not showing it.
position: absolute; left: -99999px. Position it outside of the screen.
text-indent: -9999px. This only works on text within the block elements.
Metadata. For example by using Schema.org, RDF, and JSON-LD.
WAI-ARIA. A W3C technical specification that specifies how to increase the accessibility of web pages.
Even if WAI-ARIA is the ideal solution, I would go with the absolute positioning approach, as it has the least caveats, works for most elements and it’s an easy technique.”

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

I like the float-based grid system because it still has the most browser support among the alternative existing systems (flex, grid). It has been used in Bootstrap for years and has been proven to work.

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

Yes I’ve used media queries and mobile specific CSS on multiple personal and client projects. Responsive Web-Design is important to me because it ensures users across all devices can access and interact with content easily.

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

Are you familiar with styling SVG?

A

“Yes, there are several ways to color shapes (including specifying attributes on the object) using inline CSS, an embedded CSS section, or an external CSS file. Most SVG you’ll find around the web use inline CSS, but there are advantages and disadvantages associated with each type.

Basic coloring can be done by setting two attributes on the node: fill and stroke. fill sets the color inside the object and stroke sets the color of the line drawn around the object. You can use the same CSS color naming schemes that you use in HTML, whether that’s color names (that is red), RGB values (that is rgb(255,0,0)), Hex values, RGBA values, etc.”

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

“Yes, there are four types of @media properties (including screen):

all - for all media type devices
print - for printers
speech - for screen readers that ““reads”” the page out loud
screen - for computer screens, tablets, smart-phones etc.”

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

What are some of the “gotchas” for writing efficient CSS?

A

“Firstly, understand that browsers match selectors from rightmost (key selector) to left. Browsers filter out elements in the DOM according to the key selector and traverse up its parent elements to determine matches. The shorter the length of the selector chain, the faster the browser can determine if that element matches the selector. Hence avoid key selectors that are tag and universal selectors. They match a large number of elements and browsers will have to do more work in determining if the parents do match.

BEM (Block Element Modifier) methodology recommends that everything has a single class, and, where you need hierarchy, that gets baked into the name of the class as well, this naturally makes the selector efficient and easy to override.

Be aware of which CSS properties trigger reflow, repaint, and compositing. Avoid writing styles that change the layout (trigger reflow) where possible.

//Better answer with Leon,
-Question is asking about the hacks, such as: what hacks do you need to know when using CSS? like box model hacks, clear fix when floating, and etc..."
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

“Advantages:

CSS is made more maintainable.
Easy to write nested selectors.
Variables for consistent theming. Can share theme files across different projects.
Mixins to generate repeated CSS.
Sass features like loops, lists, and maps can make configuration easier and less verbose.
Splitting your code into multiple files. CSS files can be split up too but doing so will require an HTTP request to download each CSS file.
Disadvantages:

Requires tools for preprocessing. Re-compilation time can be slow.
Not writing currently and potentially usable CSS. For example, by using something like postcss-loader with webpack, you can write potentially future-compatible CSS, allowing you to use things like CSS variables instead of Sass variables. Thus, you’re learning new skills that could pay off if/when they become standardized.”

17
Q

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

A

“Likes:

Mostly the advantages mentioned above.
Less is written in JavaScript, which plays well with Node.
Dislikes:

I use Sass via node-sass, which is a binding for LibSass written in C++. I have to frequently recompile it when switching between node versions.
In Less, variable names are prefixed with @, which can be confused with native CSS keywords like @media, @import and @font-face rule.”

18
Q

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

A

Use @font-face and define font-family for different font-weights.

19
Q

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

A

“This part is related to the above about writing efficient CSS. Browsers match selectors from rightmost (key selector) to left. Browsers filter out elements in the DOM according to the key selector and traverse up its parent elements to determine matches. The shorter the length of the selector chain, the faster the browser can determine if that element matches the selector.

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></p></span></p></span>

20
Q

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

A

“A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s). They can be used for decoration (:first-line, :first-letter) or adding elements to the markup (combined with content: …) without having to modify the markup (:before, :after).

:first-line and :first-letter can be used to decorate text.
Used in the .clearfix hack as shown above to add a zero-space element with clear: both.
Triangular arrows in tooltips use :before and :after. Encourages separation of concerns because the triangle is considered part of styling and not really the DOM. It’s not really possible to draw a triangle with just CSS styles without using an additional HTML element.”

21
Q

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

A

The CSS box model describes every element as a rectangular box with content area which could be text, an image, etc. Every box also has optional surrounding padding, border, and margin areas. The CSS box model is responsible for calculating how much space a block element takes up, whether or not borders or margins overlap or collapse, and a box’s dimensions. The dimensions of a block element are calculated by width, height, padding, borders, and margins.

22
Q

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

A

“By default, elements have box-sizing: content-box applied, and only the content size is being accounted for.
box-sizing: border-box changes how the width and height of elements are being calculated, border and padding are also being included in the calculation.
The height of an element is now calculated by the content’s height + vertical padding + vertical border width.
The width of an element is now calculated by the content’s width + horizontal padding + horizontal border width.
Taking into account paddings and borders as part of our box model resonates better with how designers actually imagine content in grids.”

23
Q

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

A

none, block, inline, inline-block, table, table-row, table-cell, list-item.

24
Q

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

A

There are a few differences between block, inline, and inline-block elements. Block elements start on a new line within the browser and do not allow other HTML elements beside them, unless they are floated. inline block and inline elements flow along with the content of the page and allows other elements beside them. To decide between inline and inline block, I consider whether or not I want my elements to be able to take on block properties such as width, height, margin, and padding (both horizontal and vertical)

25
Q

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

A

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

26
Q

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

A

“Bootstrap - Slow release cycle. Bootstrap 4 has been in alpha for almost 2 years. Add a spinner button component, as it is widely used.
Semantic UI - Source code structure makes theme customization extremely hard to understand. Its unconventional theming system is a pain to customize. Hardcoded config path within the vendor library. Not well-designed for overriding variables unlike in Bootstrap.
Bulma - A lot of non-semantic and superfluous classes and markup required. Not backward compatible. Upgrading versions breaks the app in subtle manners.”

27
Q

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

A

“Yes. Flexbox is mainly meant for 1-dimensional layouts while Grid is meant for 2-dimensional layouts.

Flexbox solves many common problems in CSS, such as vertical centering of elements within a container, sticky footer, etc. Bootstrap and Bulma are based on Flexbox, and it is probably the recommended way to create layouts these days. Have tried Flexbox before but ran into some browser incompatibility issues (Safari) in using flex-grow, and I had to rewrite my code using inline-blocks and math to calculate the widths in percentages, it wasn’t a nice experience.

Grid is by far the most intuitive approach for creating grid-based layouts (it better be!) but browser support is not wide at the moment.”

28
Q

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

A

“Note that these two 2 approaches are not exclusive.

Making a website responsive means the some elements will respond by adapting its size or other functionality according to the device’s screen size, typically the viewport width, through CSS media queries, for example, making the font size smaller on smaller devices.
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.
A mobile-first strategy has 2 main advantages:

It’s more performant on mobile devices, since all the rules applied for them don’t have to be validated against any media queries.
It forces to write cleaner code in respect to responsive CSS rules.
and helps you consider what are the most important functions and your features your user will see when they’re visiting your site on a mobile device”

29
Q

How is responsive design different from adaptive design?

A

“Both responsive and adaptive design attempt to optimize the user experience across different devices, adjusting for different viewport sizes, resolutions, usage contexts, control mechanisms, and so on.

Responsive design works on the principle of flexibility - a single fluid website that can look good on any device. Responsive websites use media queries, flexible grids, and responsive images to create a user experience that flexes and changes based on a multitude of factors. Like a single ball growing or shrinking to fit through several different hoops.

Adaptive design is more like the modern definition of progressive enhancement. Instead of one flexible design, adaptive design detects the device and other features and then provides the appropriate feature and layout based on a predefined set of viewport sizes and other characteristics. The site detects the type of device used and delivers the pre-set layout for that device. Instead of a single ball going through several different-sized hoops, you’d have several different balls to use depending on the hoop size.

Both have these methods have some issues that need to be weighed:

Responsive design can be quite challenging, as you’re essentially using a single albeit responsive layout to fit all situations. How to set the media query breakpoints is one such challenge. Do you use standardized breakpoint values? Or, do you use breakpoints that make sense to your particular layout? What if that layout changes?
Adaptive design generally requires user agent sniffing, or DPI detection, etc., all of which can prove unreliable.”

30
Q

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

A

“Retina is just a marketing term to refer to high resolution screens with a pixel ratio bigger than 1. The key thing to know is that using a pixel ratio means these displays are emulating a lower resolution screen in order to show elements with the same size. Nowadays we consider all mobile devices retina defacto displays.

Browsers by default render DOM elements according to the device resolution, except for images.

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 always the highest resolution images will have an impact on performance as more bytes will need to be sent over the wire.

To overcome this problem, we can use responsive images, as specified in HTML5. It requires making available different resolution files of the same image to the browser and let it decide which image is best, using the html attribute srcset and optionally sizes, for instance:”

31
Q

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

A

“translate() is a value of CSS transform. Changing transform or opacity does not trigger browser reflow or repaint but does trigger compositions; whereas changing the absolute positioning triggers reflow. transform causes the browser to create a GPU layer for the element but changing absolute positioning properties uses the CPU. Hence translate() is more efficient and will result in shorter paint times for smoother animations.

When using translate(), the element still occupies its original space (sort of like position: relative), unlike in changing the absolute positioning.”