InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How will you fix browser-specific styling issues? |
|
Answer» Different ways to fix browser-specific issues.
There are some ways for AVOIDING browser compatibility issues too. They are as follows:
CSS plays the most important role in the field of web development. This is because CSS helps in achieving beautiful, responsive or adaptive websites depending on the business requirements. CSS helps in building lighter and flexible layouts that help in loading pages faster and making the content visually appealing. CSS is continuously evolving and is becoming more powerful thereby making it the most sought-after technology by various companies to develop websites. In this article, we have SEEN the most commonly asked interview questions in CSS, more particularly CSS3. Useful Resources
|
|
| 2. |
Why do we need to use clear property along with floats in CSS? |
|
Answer» The clear property ALONG with floats is used for specifying which SIDE of floating elements is not supposed to float. An element having clear property ensures that the element does not move up ADJACENT to the float. But the element will be moved down past the float. Let us understand this with the help of an example. We know that the floated objects do not add to the height of the objects where they reside. Consider we have a div element with class “floated_div” within another div element with id “main_div”. <html> <head> <style> #main_div { width: 400px; margin: 10px auto; border: 4px solid #cccccc; PADDING: 5px; } .floated_div { float: LEFT; width: 50px; height: 50px; border: 2px solid #990000; margin: 10px; } </style> </head> <body> <div id="main_div"> <p>Clear Float Demo</p> <div class="floated_div"></div> <div class="floated_div"></div> <div class="floated_div"></div> <div class="floated_div"></div> <div class="floated_div"></div> </div> </body></html>The result of this code would be as shown below. We see that the squares that are expected to be within dev are not within the main parent div. How do we fix this? We can do it just by adding <div style="clear:both"></div> line at the end of the last floated element so that the floated elements are fit in properly within the main div container. <html> <head> <style> #main_div { width: 400px; margin: 10px auto; border: 4px solid #cccccc; padding: 5px; } .floated_div { float: left; width: 50px; height: 50px; border: 2px solid #990000; margin: 10px; } </style> </head> <body> <div id="main_div"> <p>Clear Float Demo</p> <div class="floated_div"></div> <div class="floated_div"></div> <div class="floated_div"></div> <div class="floated_div"></div> <div class="floated_div"></div> <div style="clear:both"></div> <!-- Adding this fixed the issue --> </div> </body></html>The result of this will be: |
|
| 3. |
What do you understand by tweening in CSS? |
|
Answer» Tweening is the process of filling the gaps between the key sequences, i.e between the keyframes that are already created. Keyframes are those frames that represent start and end point of animation action. Tweening involves generating intermediate keyframes between two images that give the impression that the first one has evolved smoothly to the second image. For this purpose, we use properties like transforms - matrix, translate, scale, rotate etc. In the below example, we are generating intermediate frames of paragraph elements to SLIDE through from the start to the right edge of the browser. p { animation-duration: 2s; animation-name: slidethrough;}@keyframes slidethrough { from { margin-left: 100%; width: 300%; } to { margin-left: 0%; width: 100%; }}Here, the paragraph element specifies that the animation process should take 2 seconds for execution from start to the finish. This is done by using the animation-duration property. The animation-name of the @keyframes is DEFINED by using the property animation-name. The intermediate keyframes are defined by using @keyframes rule. In the example, we have just 2 keyframes. The first keyframe starts at 0% and runs till the left margin of 100% which is the rightmost edge of the containing element. The second keyframe starts at 100% where the left margin is set as 0% and the width to be set as 100% which results in finishing the animation flush against the left edge of the CONTAINER AREA. |
|
| 4. |
What is the importance of CSS Sprites? |
|
Answer» CSS sprites are used for combining multiple images in a single larger image. They are commonly used for representing icons that are used in the user interfaces. The main advantages of using sprites are:
Consider an example where our APPLICATION requires 3 images as shown below (Without Sprites Section). If we are trying to load the images independently, we require 3 different HTTP Requests to get the data. But if we have CSS Sprites where all 3 images are combines into 1 separated by some spaces, then we require only 1 HTTP Request. We can access each image from the sprite by accessing the positioning properties as shown in the below code: <!DOCTYPE html><html><head><style>#home-icon { left: 0px; width: 46px; background: url('spriteFile.gif') 0 0;}#prev-icon { left: 63px; width: 43px; background: url('spriteFile.gif') -47px 0;}#next-icon { left: 129px; width: 43px; background: url('spriteFile.gif') -91px 0;}</style></head><body><img id="home-icon" src="spriteFile.gif" width="1" height="1"> <!-- To display home icon here --><img id="next-icon" src="spriteFile.gif" width="1" height="1"> <!-- To display next icon icon here --><img id="prev-icon" src="spriteFile.gif" width="1" height="1"> <!-- To display PREVIOUS icon icon here --></body></html>In the above code, we are trying to access each element - house, previous and next icon - from the sprite file by using the left, width properties. The image is displayed in the img section by means of the background property. Do note that the source of the image (src attribute of the img tag) is just one file which is the spriteFile.gif and depending on the rules SPECIFIED in the id selectors, the images are loaded ACCORDINGLY. |
|
| 5. |
How is the nth-child() different from nth of type selectors? |
|
Answer» Both are pseudo-classes (Pseudo-classes are those keywords that specifies the special state of the selected ELEMENT). The nth-child() pseudo-class is used for matching elements based on the number that represents the position of an element based on the siblings. The number is used to match an element on the basis of the element’s position amongst its siblings. For example, in the below piece of code, if we give nth-child(4) for the example class, then the 4th child of the example class is selected irrespective of the element TYPE. Here, the fourth child of the example class is the div element. The element is selected and a background of black is added to it. .example:nth-child(4) { background: black; }<div class="example"> <p>This is a paragraph.</p> <p>This is a paragraph.</p> <p>This is a paragraph.</p> <div>This is a div.</div> <!-- 4th Element to select and apply style--> <div>This is a div.</div> <p>This is a paragraph.</p> <p>This is a paragraph.</p> <div>This is a div.</div></div>The nth-of-type() pseudo-class is similar to the nth-child but it helps in matching the selector based on a number that represents the position of the element within the elements that are the siblings of its same type. The number can also be given as a FUNCTION or give keywords LIKE odd or even. For example, in the below piece of code, if we give p:nth-of-type(even) for the example class, then all the even paragraph tags are selected within the example class and the style of background black is applied to them. The selected elements are MARKED in comments in the below code: .example p:nth-of-type(even) { background: black; }<div class="example"> <p>This is a paragraph.</p> <p>This is a paragraph.</p> <!-- Select this and apply style--> <p>This is a paragraph.</p> <div>This is a div.</div> <div>This is a div.</div> <p>This is a paragraph.</p> <!-- Select this and apply style--> <p>This is a paragraph.</p> <div>This is a div.</div> <p>This is a paragraph.</p> <!-- Select this and apply style--> <div>This is a div.</div></div> |
|
| 6. |
What do you have to do to automatically number the heading values of sections and categories? |
|
Answer» We can use the concept of CSS counters. This lets us adjust the appearance of the content based on the location in a document. While using this, we need to FIRST initialize the value of the counter-reset property which is 0 by default. The same property is also used for changing the value to any number that we need. Post initialization, the counter’s value can be incremented or decremented by using the counter-increment property. The name of the counter cannot be CSS KEYWORDS like “none”, “initial”, “INHERIT” etc. If the CSS keywords are used, then the declaration would be ignored. Consider an example as shown below: BODY { counter-reset: header; /* define counter named 'header' whose initial value is 0 by default */}h2::before { counter-increment: header; /* The value of header counter by 1.*/ content: "Header " counter(header) ": "; /* To display word Header and the value of the counter with colon before it.*/}Here, we are trying to achieve auto count increment and display feature for the h2 tag. Wherever we use h2 tag, the content will be prefixed by "Header 1 : " , "Header 2 : ", "Header 3 : " etc. |
|
| 7. |
How is margin different from padding in CSS? |
|
Answer» Margin property using which we can create space around the elements. We can also create space for borders defined at the exteriors. We have the following properties for DEFINING the margin:
The padding property is used for generating the space around the element’s CONTENT and inside any known border. The padding also has sub-properties like:
It is to be noted that the padding does not ALLOW negative values. From the below image, we can see that the Margin is the outermost entity of the CSS Box Model that lies outside of the borders whereas the padding lies within the borders. |
|
| 8. |
How will you align content inside the p tag at the exact center inside the div? |
|
Answer» We can add the text-align: center property inside the parent div for aligning the contents horizontally. But it will not align the contents vertically. We can align the CONTENT vertically by making the parent element have relative positioning and the child element have absolute positioning. The child element should have the values of top, bottom, RIGHT, left as 0 to center it in the middle vertically. Then we need to SET the margin as AUTO. It is assumed that both the child and mother ELEMENTS will have height and width values. Consider we have a div element of height and width taking 20% of the screen size, and we have a paragraph element taking the height of 1.2em and width of 20%. If we want to align the paragraph element at the center (vertically and horizontally), we write the following styles: div { position : relative; // Make position relative height : 20%; width : 20%; text-align : center; //Align to center horizontally}p { position : absolute; // Make position absolute top:0; // Give values of top, bottom,left, right to 0 bottom:0; left:0; right:0; margin : auto; // Set margin as auto height : 1.2 em; width : 20%;} |
|
| 9. |
How does this property work overflow: hidden? |
|
Answer» The overflow property in CSS is used for specifying whether the content has to be clipped or the scrollbars have to be added to the content area when the content size exceeds the specified container size where the content is enclosed. If the value of overflow is HIDDEN, the content gets clipped post the size of the container thereby making the content invisible. For example, div { width: 150px; HEIGHT: 50px; overflow: hidden;}If the content of the div is very large and exceeds the height of 50px, the content gets clipped post 50px and the rest of the content is not made VISIBLE. |
|
| 10. |
How does the absolute positioning work? |
|
Answer» Absolute positioning is a very powerful positioning mechanism that allows users to PLACE any element WHEREVER they want in an exact location. The CSS properties right, left, top, bottom and define the exact locations where you need to place the element. In absolute positioning, the FOLLOWING points need to be considered:
|
|
| 11. |
How to determine if the browser supports a certain feature? |
|
Answer» The @SUPPORT in CSS can be very useful to scan if the current browser has support for a certain FEATURE. @supports (DISPLAY: grid) { div { display: grid; }} |
|
| 12. |
Does style1.css have to be downloaded and parsed before style2.css can be fetched? |
|
Answer» <head> <link H ref=" stylel. css" rel=" stylesheet"> <link href="style2.css" rel="stylesheet"></head> No, the browsers will DOWNLOAD the CSS in the order of its appearance on the HTML PAGE. |
|
| 13. |
What are the advantages of using translate() instead of absolute position? |
|
Answer» TRANSLATE() does not cause the browser to trigger REPAINT and LAYOUT and instead only acts on the compositor. The absolute POSITION triggers the repaint or DOM reflow. So, translate() gives better performance. |
|
| 14. |
What is progressive rendering? How do you implement progressive rendering in the website?. What are the advantages of it? |
|
Answer» Progressive rendering is the name given to techniques used to improve the performance of a webpage (in particular, improve perceived load time) to render content for display as quickly as possible. We can implement the progressive rendering of the page by LOADING the lazy loading of the images. We can use INTERSECTION Observer API to lazy load the image. The API makes it simple to detect when an element enters the viewport and take an action when it does. Once the image enters the viewport, we will start loading the images. A sample snippet is given below. <img class="lazy"src="placeholder-image.jpg"data-src="image-to-lazy-load-1x.jpg"data-srcset="image-to-lazy-load-2x.jpg 2x, image-to-lazy-load-1x.jpg 1x"alt="I'm an image!">document.addEventListener("DOMContentLoaded", function() { var lazyImages = [].slice.call(document.querySelectorAll("img.lazy")); if ("IntersectionObserver" in window) { let lazyImageObserver = new IntersectionObserver(function(entries, observer) { entries.forEach(function(entry) { if (entry.isIntersecting) { let lazyImage = entry.target; lazyImage.src = lazyImage.dataset.src; lazyImage.srcset = lazyImage.dataset.srcset; lazyImage.classList.remove("lazy"); lazyImageObserver.unobserve(lazyImage); } }); }); lazyImages.forEach(function(lazyImage) { lazyImageObserver.observe(lazyImage); }); } else { // Possibly fall back to EVENT handlers here }}); |
|
| 15. |
What is specificity? How to calculate specificity? |
|
Answer» 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 the class value (or pseudo-class or attribute SELECTOR), the universal selector (*) has no specificity. ID selectors have a higher specificity than attribute selectors. |
|
| 16. |
What does !important mean in CSS? |
|
Answer» The style is having the IMPORTANT will have the highest precedence and it OVERRIDES the cascaded PROPERTY. p { color: red !important; } #thing { color: green; } <p id="thing">Will be RED.</p> |
|
| 17. |
What does * { box-sizing: border-box; } do? What are its advantages? |
Answer»
|
|
| 18. |
What is the difference between CSS variables and preprocessor(SASS, LESS, Stylus) variables? |
Answer»
|
|
| 19. |
What do CSS Custom properties variables mean? |
|
Answer» Custom PROPERTIES (sometimes referred to as CSS variables or cascading variables) are defined by users that contain specific values to be reused throughout a document. The value is SET using -- notion. And the values are accessed using the VAR() function. :ROOT { --main-bg-COLOR: brown}.one { color: white; background-color· var (--main-bg-color); margin: l0px, width: 50px, height: 5Opx; display: inline-block;} |
|
| 20. |
How does Calc work? |
|
Answer» The CSS3 calc() function allows US to perform mathematical operations on property values. Instead of declaring, for example, static pixel values for an element's width, we can use calc() to specify that the width is the result of the addition of two or more numeric values. .foo { Width: calc(100PX + 50px)} |
|
| 21. |
Difference between CSS grid vs flexbox? |
Answer»
|
|
| 22. |
How do I restore the default value of a property? |
|
Answer» The KEYWORD INITIAL can be USED to reset it to its default value. |
|
| 23. |
What does Accessibility (a11y) mean? |
|
Answer» Accessibility refers to how software or hardware combinations are designed to MAKE a system accessible to PERSONS with disabilities, such as visual impairment, hearing loss, or limited dexterity. For example, a WEBSITE developed with accessibility in mind might have text-to-speech capabilities. In the USA public websites have to have accessible COMPLIANCE. It’s defined in 508 compliance. It GIVES the guidelines and best practices for all website users that should be met with key areas of accessibility. |
|
| 24. |
What does the :root pseudo-class refer to? |
|
Answer» The :ROOT SELECTOR allows you to TARGET the highest-level “parent” element in the DOM, or document tree. It is defined in the CSS Selectors Level 3 specification. |
|
| 25. |
What are the different ways to hide the element using CSS? |
Answer»
|
|
| 26. |
What is the grid system? |
|
Answer» CSS Grid Layout is the most powerful layout system AVAILABLE in CSS. It is said to be a 2-dimensional system, meaning it can handle both COLUMNS and rows, UNLIKE flexbox which is largely a 1-dimensional system. |
|
| 27. |
Can you name the four types of @media properties? |
| Answer» | |
| 28. |
How to center align a div inside another div? |
Answer»
HTML: <div class=”cn”><div class=”inner”>your content</div></div>CSS: .cn { display: table-cell; WIDTH: 500px; height: 500px; vertical-align: middle; text-align: center;}.inner { display: inline-block; width: 200px; height: 200px;}
HTML: <div class="cn"><div class="inner">your content</div></div>CSS: .cn { position: RELATIVE; width: 500px; height: 500px;}.inner { position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); width: 200px; height: 200px;}
HTML: <div class="cn"><div class="inner">your content</div></div>CSS: .cn { display: flex; justify-content: center; align-items: center;}
HTML: <div class=”wrap_grid”> <div id=”CONTAINER”>vertical aligned text<br />some more text here </div></div>CSS: .wrap-grid { display: grid; place-content: center;} |
|
| 29. |
Different Box Sizing Property? |
|
Answer» The box-sizing CSS property sets how the total width and height of an element are calculated.
|
|
| 30. |
When does DOM reflow occur? |
|
Answer» Reflow is the name of the web browser process for re-calculating the positions and geometries of elements in the document, for the purpose of re-rendering part or all of the document. Reflow OCCURS when: |
|
| 31. |
Explain CSS position property? |
Answer»
|
|