News:

Build a stunning handcrafted website with IT Acumens

Main Menu

CSS Interview Questions And Answers _ 5

Started by ganeshbala, Mar 28, 2008, 12:04 PM

Previous topic - Next topic

ganeshbala

CSS Interview Questions And Answers

How do I have a non-tiling (non-repeating) background image?
With CSS, you can use the background-repeat property. The background repeat can be included in the shorthand background property, as in this example:

body {
background: white url(example.gif) no-repeat ;
color: black ;
}

CSS is clearly very useful for separating style from content. But apparently people tend to have problems when using it for layouts. Would you say this is because people have not yet understood how to properly do layout in CSS, or is it CSS that is lacking in this area? What can be done to improve the situation? --- Would the web benefit from HTML and CSS being complemented with some kind of "layout language"?
Layout and style should be tackled by the same language and the two are intertwined. Trying to split the two is like splitting the HTML specification in two, one specification describing inline elements and the other describing block elements. It's not worth the effort. CSS is capable of describing beautiful and scalable layouts. The CSS Zen Garden has been a eye-opening showcase of what is possible today. If MS IE had supported CSS tables, another set of layouts would have been possible. So, there is still lots of potential in the existing CSS specifications which should be the next milestone.

I always wanted to have "included" substyles or "aliases" in my CSS definition, to save redundancy.
(For includes)

.class1 { color:#ff0000; }
.class2 { background-color:#ffffff; }
.class3 { include:class1,class2;font-weight:bold; }

(For aliases)

@alias color1 #ff0000;
@alias color2 #ffffff;
@alias default_image url('/img/image1.jpg');

.class1 { color:color1; }
.class2 { background-image:default_image;background-color:co lor2; }

This way we could change colors or images for a whole webpage
by editing a reduced number of lines.

Had you considered any of these ideas in the past? If so, why were they rejected?


Yes, aliases and constants have been considered. CSS is already an indirection. Instead of putting properties and values directly on elements, it associates properties and values with selectors. What you (and others) are proposing is to add another layer of indirection. By doing so, one could possible write shorter, more manageable style sheets. However, there are also some downsides. It requires a new syntactic construct (@alias) and implementations must be able to remember a list of aliases. What if aliases are defined in one style sheet and referenced in another -- should that work? If so, what if the first style sheet isn't available?..

Styles not showing?
There are different ways to apply CSS to a HTML document with a stylesheet, and these different ways can be combined:

* inline (internal) (Deprecated for XHTML)
* embedded (internal)
* linked (external) and
* @import (external)

Note: An external stylesheet is a text file that contains only CSS Styles. HTML comments are not supposed to be in there and can lead to misinterpretation (> is the CSS "Child" selector!).

How do I quote font names in quoted values of the style attribute?

The attribute values can contain both single quotes and double quotes as long as they come in matching pairs. If two pair of quotes are required include single quotes in double ones or vice versa:

<P STYLE="font-family: 'New Times Roman'; font-size: 90%">
<P STYLE='font-family: "New Times Roman"; font-size: 90%'>

It's been reported the latter method doesn't work very well in some browsers, therefore the first one should be used.

Why is my external stylesheet not working ?
There may be several different reasons behind that, but one very common mistake is to have an external stylesheet that contains HTML markup in some form.

An external stylesheet must contain only CSS rules, and if required, correctly formed CSS comments; never include any HTML syntax, such as <style type="text/css">...
CSS comments are defined as anything that is placed between
/* (the comment start mark) and
*/ (the comment end mark). I.e. as follows...

/* This text right here is a correct CSS comment */

CSS comments may span multiple lines in the stylesheet. Nesting of CSS comments is not allowed.

Another reason for external stylesheets (and even embedded and inline stylerules) not to function as expected may be that you have tried to make use of some CSS-features that are not supported in the browser you are using.

External stylesheets shall also be served from the www-server with a MIME-type of 'text/css' in its 'Content Type:' HTTP header.
You may need to negotiate with your server admin to add this MIME type to your server if you are not able to configure the server yourself.

What can be done with style sheets that can not be accomplished with regular HTML?

Many of the recent extensions to HTML have been tentative and somewhat crude attempts to control document layout. Style sheets go several steps beyond, and introduces complex border, margin and spacing control to most HTML elements. It also extends the capabilities introduced by most of the existing HTML browser extensions. Background colors or images can now be assigned to ANY HTML element instead of just the BODY element and borders can now be applied to any element instead of just to tables. For more information on the possible properties in CSS, see the Index DOT Css Property Index.

How do I make my div 100% height?
You need to know what the 100% is of, so the parent div must have a height set. One problem that people often come up against is making the main page fill the screen if there's little content. You can do that like this :
CSS
body, html {
height:100%;
}
body {
margin:0;
padding:0;
}
#wrap {
position:relative;
min-height:100%;
}
* html #wrap {
height:100%;
}

Here, the #wrap div goes around your whole page - it's like a sub-body.

You need to use 'min-height' rather than 'height' for Firefox because otherwise it will set it to 100% of the viewport and no more. Internet Explorer, being well... crap, treats 'height' as it should be treating 'min-height' which it doesn't recognise. (You can target IE by preceding your code with ' * html ').

To make floated divs within this #wrap div 100% of the #wrap div... well that's more difficult. I think the best way is to use the 'faux columns' technique which basically means that you put the background in your body rather than your columns. If the body has columns and your floats don't then it looks like your floated content is in a column that stretches to the bottom of the page. I've used this technique in my layout demos.

The problem is often not that the columns aren't 100% height, but that they're not equal lengths. Columns usually don't start from the top of the page and end at the bottom - there's often a header and a footer or sometimes, more interesting designs don't have a recognisable columnar layout, but do require div boxes to be equal heights. This can be done with the aid of a couple of images and some css or with some javascript.

What is property?
Property is a stylistic parameter (attribute) that can be influenced through CSS, e.g. FONT or WIDTH. There must always be a corresponing value or values set to each property, e.g. font: bold or font: bold san-serif.

How do I write my style sheet so that it gracefully cascades with user's personal sheet ?
You can help with this by setting properties in recommended places. Style rules that apply to the whole document should be set in the BODY element -- and only there. In this way, the user can easily modify document-wide style settings.

What are pseudo-elements?
Pseudo-elements are fictional elements that do not exist in HTML. They address the element's sub-part (non-existent in HTML) and not the element itself. In CSS1 there are two pseudo-elements: 'first-line pseudo-element' and 'first-letter pseudo-element'. They can be attached to block-level elements (e.g. paragraphs or headings) to allow typographical styling of their sub-parts. Pseudo-element is created by a colon followed by pseudo-element's name, e.g:

P:first-line
H1:first-letter

and can be combined with normal classes; e.g:

P.initial:first-line

First-line pseudo-element allows sub-parting the element's first line and attaching specific style exclusively to this sub-part; e.g.:

P.initial:first-line {text-transform: uppercase}

<P class=initial>The first line of this paragraph will be displayed in uppercase letters</P>

First-letter pseudo-element allows sub-parting the element's first letter and attaching specific style exclusively to this sub-part; e.g.:

P.initial:first-letter { font-size: 200%; color: red}

<P class=initial>The first letter of this paragraph will be displayed in red and twice as large as the remaining letters</P>

As a developer who works with CSS every day, I find one complication that continues to bother me in my daily work. Support for CSS has always been good on the horizontal scope, but vertical positioning has always been quite complicated. Alone the procedure to affix a footer to the bottom of a screen in dependance of the amount of content is unnecessarily difficult. The old table method provided much easier methods for this. What are your thoughts on this and do you see improvement following in future CSS revisions?

Indeed, the CSS formatting model allows more control horizontally than vertically. This is due to (typically) having a known width, but an unknown height. As such, the height is harder to deal with. However, CSS2 fixed positioning allows you to place content relative to the viewport (which is CSS-speak for window) instead of the document. For example, by setting position: fixed; bottom: 0 on an element, it will stick to the bottom. This works in Opera, Safari and Mozilla-based browsers. IE6 doesn't support it, however. It remains to be seen if IE7 will support it.

How can I make a page look the same in e.g. NS and MSIE ?
The simple answer is, you can't, and you shouldn't waste your time trying to make it exactly the same. Web browsers are allowed, per definition, to interpret a page as they like, subject to the general rules set down in the HTML and CSS specifications. As a web author you can not have a prior knowledge of the exact situation and/or medium that will be used to render your page, and it's almost always rather counterproductive to try to control that process. There is no necessity for a well-written page to look the same in different browsers. You may want to strive to ensure that it looks good in more than one browser, even if the actual display (in the case of graphical browsers) comes out a bit different. "Looking good" can be achieved by adopting sensible design and guidelines, such as not fixing the size or face of your fonts, not fixing the width of tables, etc... Don't fight the medium; most web users only use one browser and will never know, or bother to find out, that your page looks different, or even "better", in any other browser.

Is there anything that CAN'T be replaced by Style Sheets?
Quite a bit actually. Style sheets only specify information that controls display and rendering information. Virtual style elements that convey the NATURE of the content can not be replaced by style sheets, and hyperlinking and multimedia object insertion is not a part of style sheet functionality at all (although controlling how those objects appear IS part of style sheets functionality.) The CSS1 specification has gone out of its way to absorb ALL of the HTML functionality used in controlling display and layout characteristics. For more information on the possible properties in CSS, see the Index DOT Css Property Index.
Rule of Thumb: if an HTML element or attribute gives cues as to how its contents should be displayed, then some or all of its functionality has been absorbed by style sheets.

Can I include comments in my Style Sheet?
Yes. Comments can be written anywhere where whitespace is allowed and are treated as white space themselves. Anything written between /* and */ is treated as a comment (white space). NOTE: Comments cannot be nested.

What is the difference between ID and CLASS?
ID identifies and sets style to one and only one occurrence of an element while class can be attached to any number of elements. By singling out one occurrence of an element the unique value can be declared to said element.

CSS
#eva1 {background: red; color: white}
.eva2 {background: red; color: white}

HTML - ID
<P ID=eva1>Paragraph 1 - ONLY THIS occurrence of the element P (or single occurrence of some other element) can be identified as eva1</P>
<P ID=eva1>Paragraph 2 - This occurrence of the element P CANNOT be identified as eva1</P>

HTML - CLASS
<P class=eva2>Paragraph 1 - This occurrence of the element P can be classified as eva2</P>
<P class=eva2>Paragraph 2 - And so can this, as well as occurrences of any other element, </P>

How to make text-links without underline?
a:link, a:visited {text-decoration: none}

or

<a style="text-decoration: none" HREF="...">

...will show the links without underlining. However, suppressing the underlining of links isn't a very smart idea as most people are used to having them underlined. Also, such links are not spotted unless someone coincidentally runs a mouse over them. If, for whatever reason, links without underline are required background and foreground colors can be instead declared to them so that they can be distinguished from other text, e.g.;

a:link, a:visited {text-decoration: none; background: red; color: blue}

or

<a style="text-decoration: none; background: red; color: blue" HREF="...">

Both background and foreground colors should be specified as the property that is not specified can be overridden by user's own settings.