Archive for Development

Collapsing margins

Let’s say you tried the following HTML/CSS:

<div style="background: #cfc;"> <p style="border: 1px solid black; margin: 1em;">Heya!</p> </div>

It would look like this:

Heya!

You probably expected it to look like this:

Heya!

The cause of this confusion is called collapsing margins. It only happens to vertical margins, which is why you can you can still see the gray margins outside the left and right border of the paragraph in the first demonstration. According to the CSS 2.1 specs, section 10.6.3:

If [a block-level non-replaced element in normal flow when 'overflow' computes to 'visible'] has block-level children, the height is the distance between the top border-edge of the topmost block-level child box that doesn’t have margins collapsed through it and the bottom border-edge of the bottommost block-level child box that doesn’t have margins collapsed through it. However, if the element has a non-zero top padding and/or top border, or is the root element, then the content starts at the top margin edge of the topmost child. (The first case expresses the fact that the top and bottom margins of the element collapse with those of the topmost and bottommost children, while in the second case the presence of the padding/border prevents the top margins from collapsing.) Similarly, if the element has a non-zero bottom padding and/or bottom border, then the content ends at the bottom margin edge of the bottommost child.

Note the mention of ‘a non-zero top padding and/or top border’. This means that setting either will stop margins from collapsing. In the second example, I added 1 pixel of padding to the DIV for the desired effect. Thanks to Minz Meyer for the solution.

Comments

Procedural textures; take two

So, Rick and I came up with a new pseudo-random number distribution:

double random_pow(int power) { double num = 2.0 * rand() / RAND_MAX - 1; return (copysign(pow(num, power), num) + 1) / 2; }

I’m using the standard C library versions of rand() and RAND_MAX, so this function generates a random number in [0, 1] biased towards 0.5 using the exponent power. I thought it might be useful in making most of my field green but having a few brown or yellow flecks at the end points, but I’m not sure it worked out correctly. Grass 1 and Grass 2 are my tries using this system. The code for the first one is:

Color lawn_grass(int x, int y) { static color darkGreen(23, 90, 0); static color lightGreen(51, 175, 15); static color brown(87, 67, 6); color green = clint(darkGreen, lightGreen, rand_double()); return clint(green, brown, rand_double()).convert(); }

For the second one, it’s:

Color lawn_grass(int x, int y) { static color darkGreen(27, 81, 0); staric color lightGreen(75, 150, 25); static color brown(90, 97, 16); color green = clint(darkGreen, lightGreen, rand_double()); return clint(green, brown, rand_double()).convert(); }

Crap. It occurs to me that neither of my grass functions use my random_pow() function. I’ll have to look around to see if I saved the originals.

Comments (1)

Procedural textures

Generating an 8192 by 4096 pixel canvas on the fly takes a while, but I’m putting up with it over and over again to see if I can come up with a good algorithm to make a forest-grass field. The old algorithm created a field that was almost perfect and meshed almost invisibly with our current tiles, but Justin said that it didn’t have enough brown flecks to represent leaves, so I’ve been trying something new. This is my first try, but I can’t remember the color values that I used for the life of me.

Comments