Monthly Archive for May, 2009

CSS Quiz

How good of a CSS expert are you? Here would be a few interesting questions for you… all of these should be answered with CSS 2.1 in mind.

You need to paint a div, which can be defined in XHTML in three different ways:

<div class="blue">         Blue   </div>
<div class="yellow">       Yellow </div>
<div class="blue yellow">  Green  </div>

The blue one is painted blue, the yellow one is painted yellow and the blue-and-yellow one is painted green. What would be the shortest way of doing this?

(click to see answer)

div.blue        { background : #0FF }
div.yellow      { background : #FF0 }
div.blue.yellow { background : #0F0

Whitespace was added for clarity, you can remove all of it to further compress the stylesheet. Also note the missing end brace, which is not mandatory.

You see the following rules in a CSS file:

body { font-size: 10px }
p {
  line-height: 2em;
  font-size: 2em
}

What are, in pixels, the line height and font size of a paragraph element that appears directly in the body of the document?

(click to see answer)

The font size is computed from the inherited font size (in this case, 1em = 10px). Every other property using em will use the element font size (in this case, 1em = 20px). So, the font size will be 20px and the line height will be 40px.

In XHTML 1.0 Strict DTD, the button element has a “type” attribute that is “submit” by default (although don’t rely on Internet Explorer for that). Keeping that in mind, is there any difference between these two buttons as far as CSS is concerned?

<button>Send!</button>

<button type="submit">Send!</button>

(click to see answer)

Default values defined by the DTD are not necessarily taken into account by CSS (although they could). A conforming CSS implementation could have the second button match button[type="submit"], whereas the first wouldn’t match. This is why it is advised to provide a default CSS rule for the default value of the attribute.

The first-letter CSS pseudo-element lets you apply specific properties to the first letter of an element. The question is, assuming no other stylesheet rule attempts to change the color of the text, under which circumstances does the following rule affect the letter “W” in the document below it?

div:first-letter { color : red }

<div><em>Welcome</em>, whispered the ghost.</div>

(click to see answer)

The first letter of the first child element (or of the first child of the first child, and so on) is matched by the first-letter pseudo-element, which means the rule would apply to that letter and make it red. However, if the emphasis element is displayed as an inline block (em { display:inline-block }) its first letter cannot be affected by the first-letter pseudo-element of a parent. In that case, none of the letters in the example would be red.

Consider this set of nested div blocks, and five CSS rules (present as such in the author’s stylesheet) that all apply to the contents of the innermost block.

.blue .blue   { color : blue }
* * *         { color : black }
div div div   { color : yellow }
.or.red * *   { color : red }
div.green * * { color : green }

<div class="green or red">
  <div class="blue">
    <div class="blue">
      What color?
    </div>
  </div>
</div>

What is the color of the text?
(click to see answer)

The text is red. The five rules all apply to the same element, so they have to be sorted by order of precedence, and the one with the highest precedence applies. Since they all belong to the author stylesheet and none are important, they are sorted by specificity, then by order of appearance. The first rule has precedence (0,0,2,0) since it contains two class specifiers and zero element specifiers. The second rule has precedence (0,0,0,0) since it has no specifiers at all. The third rule has precedence (0,0,0,3) since it has three element specifiers. The fourth rule has precedence (0,0,2,0) as well. The fifth rule has precedence (0,0,1,1). This means two rules (first and fourth) are tied for highest precedence, so the last one to appear is used (in this case, the fourth).

A designer constructs two nested div blocks using the following template:

<div id="outer">
  <div id="inner">
  </div>
</div>

And he gives them the following stylesheet rules:

#outer { width : 200px ; height : 100px ; margin : 0px ; background : blue }
#inner { width : 20px ; height : 20px ; margin : 10% ; background : red }

What would be the relative position of the top-left corner of the red square to the top-left corner of the blue rectangle? What would happen if the outer block received a black border?

(click to see answer)

The relative horizontal position would be 20 pixels from the left edge, as expected (10% of 200px is 20px). The relative vertical position is zero: overlapping vertical margins merge, so the red square would move up until its top edge hit the top edge of the blue rectangle, and the top margin of the blue rectangle would be equal to max(inner top margin, outer top margin). Adding a black border to the blue rectangle would prevent the margins from overlapping (since the red square margin cannot cross the blue rectangle border) so the relative vertical position would be 20 pixels from the top edge: percentage margins (including vertical ones) are all computed from the width of the container, which in this case gives 10% of 200px, or 20 pixels.

Tangane Blog

My employer, Tangane, has recently opened a corporate blog [fr]. I will be one of the editors, so expect to find articles from me regularly—the target audience is not as technical as the one for my own blog, so the posts will probably be more about general considerations and strategy in IT.

Registered Spam

Some websites require me to enter an e-mail address before they let me use them. Since I am in control of my own web server, I can be reached at any address of the form victor-{foobar}@nicollet.net, which means I can determine which website sold my address. For instance, when I registered on mint.com, I used victor-mint@nicollet.net.

The main benefit of doing this is that I have a very simple way of un-registering from mass mailings of these websites: set up a message rule to send all incoming mail for a certain address to a spam inbox. This way, I don’t have to make myself known to the sender (or wander aimlessly on their web page or fight against customer support) so I can get my address removed from their lists. And I get the minor satisfaction that I use up a few hundred bytes in their databases :)

Here are some figures from my “Spammy” inbox:

Amazon: I opened an account to buy a video game as a birthday gift. To use Amazon, I had to agree to their terms of use, which include:

When you visit Amazon.com or send e-mails to us, you are communicating with us electronically. You consent to receive communications from us electronically. We will communicate with you by e-mail or by posting notices on this site. You agree that all agreements, notices, disclosures and other communications that we provide to you electronically satisfy any legal requirement that such communications be in writing.

I received 9 pieces of advertisment in a month (that is, about one every three days), all of them quite obviously targeted based on my purchase. 

Mint.com: I opened an account to connect to my bank online, only to discover once the account had been opened that my bank was not supported yet because it was an european bank and their website only supports US institutions. But I still agreed to their terms of use:

By providing us with your e-mail address, you agree to receive all required notices electronically, to that e-mail address.

I received six pieces of advertisment for Mint.com services in a month (or about one every five days), and I could argue that they were hardly required. But what is quite sad is that I told them twice that I couldn’t use their website, and they still tell me I should use it:

You already know that I couldn’t connect my bank account to Mint.com
because I use an unsupported bank. You even sent me out a mail telling
me that you’re doing your best, and that you will notify me as soon as
it’s ready.

I was intending to wait quietly until you supported my bank.

But it seems that until then, you will be sending me mail about what
services I should benefit from despite the fact that I cannot use them.
If this is what you intend to do, I would rather unsubscribe from
Mint.com and come back when (and if) I remember about you to see if my
bank is supported yet.

And on neither occasion would they accept to unregister me—instead taking time to tell me that I can perform the unregistration operation myself (I suspect it was a canned response, but who would be insane enough to answer a dissatisfied customer concerned enough to send you a mail with a canned response?)

Le Figaro: the website of a french newspaper, where I entered my e-mail address because it was required to post a comment. At no point in the process did I give my assent to the e-mail address being used to send any form advertisment. In fact, the mere possibility of the e-mail address being used to send any kind of e-mail (beyond those related to the posting of a comment) is not mentioned in any reasonably obvious place on the website (terms of use, legal notices, or the page where the address is entered).

Since I posted that comment two months ago, I have received four pieces of unsolicited advertisment, one of which had been sent by a partner of Le Figaro and had nothing to do with the newspaper at all (it tried to sell tickets for a soccer game).

Oracle Nonogram

A nonogram is a number and paint puzzle. The player is given a grid of cells, some of which must be painted black. For every row and every column, the player is given the number of contiguous black segments, as well as the number of cells in each. This lets the player deduce the position of black cells.

Nonogram being solved (courtesy of Wikipedia)

Originally a pen-and-paper game, there have been several implementations as video games, notably by Nintendo. The online gaming website ArmorGames proposes a variant of the game: when you paint a tile that should not be painted, the game notifies you that the tile is actually unpainted and you lose one life—you lose after five misses.

Armor Picross 2 : an Oracle Nonogram

Armor Picross 2 : an Oracle Nonogram

The Armor Games version is an oracle nonogram: there is an all-knowing oracle that can answer truthfully a certain set of questions about the solution that cannot be determined easily (or at all).

The original version of the nonogram game can be solved by the computer in NP time (and some specific classes of nonograms can be solved in polynomial time). Can the oracle nonogram game be solved faster?

No. In fact, there is no general solution for the oracle nonogram. The wording on the oracle introduces a slight difference between the two problems: any way of painting blocks that is compatible with the constraints is a solution to the original nonogram game, but only one way of painting the blocks is a solution to the oracle nonogram (and it is against this solution that the oracle nonogram checks any paint attempts). So if there are several possible solutions to a given non-oracle nonogram, adding an oracle forces the player to ask the oracle the correct questions to determine which of the several solutions is the correct one.

In an NxN nonogram where each row and each column contains a single painted cell, there are N! different solutions: the top-left to bottom-right diagonal is an obvious solution, and any other solution can be obtained from that diagonal by shuffling the coumns. In the N=15 example above, the number of solutions:

15! = 1 307 674 368 000

You can get as many guesses as you want to find the correct solution, but you lose after five incorrect guesses. However, for any guessing strategy, I can create at least two solutions that guarantee the first four guesses will be incorrect (this is only possible if N ≥ 3, of course)…



1170 feed subscribers
(readers who polled a feed this week)