How CSS Positioning Works

Undoubtedly, the position property is one of the most useful tools that designers have in their CSS toolbox. However, there are a few concepts about positioning that might baffle beginners.

First, what is positioning?

When your browser opens a webpage, it starts rendering elements (divs, paragraphs, headings etc.) in the order they appear in the HTML markup of the page. Positioning is a natural way of organizing how the elements are displayed and an easy resolution to situations like overlaps. There are four types of positioning: staticrelativeabsolute and fixed.

Elements are statically positioned by default

The default positioning, which is implicitly applied to every element on the page, is static. Static positioning means that every element is positioned in the natural order it is added to the page. Block elements are displayed under other block elements, and inline elements are displayed next to other inline elements.

Relative positioning

Setting the positioning to relative does not by itself produce any noticeable difference in the way the element is displayed on the page. But you can now move it from its normal position with the topleft,bottom and right CSS properties. This comes really handy when dealing with situations in which you need to offset an element, but not anchor it to a specified position on the page.

.relativeDiv{

	/*
		Moving the div to the top-left
		from where it would normally show:
	 */

	position:relative;
	top:-50px;
	left:-100px;
}

Fixed positioning

Adding position:fixed to the CSS declarations of an element, positions it in relation to the browser window. This allows you to display toolbars, buttons or navigation menus, which are anchored at a fixed position and scroll with the page.

.fixedDiv{

	/*
		Fixing the div 20 px from the bottom
		of the browser window:
	 */

	position:fixed;
	right:20px;
	bottom:20px;
}

Probably the only drawback is that this positioning is not supported in older browsers like IE6, if you can’t afford the luxury of dropping support for these.

Absolute positioning

An absolutely positioned element is displayed on the page in relation to the document, or a parent element with a positioning different from static. This way, using the topleftbottom and right CSS properties, you can specify an exact position. This also gives you the ability to center divs horizontally and vertically, which we discussed in the “Centering a Div Both Horizontally And Vertically” MicroTut.

.parentDiv{

	/* Absolute and Fixed positioning would work as well: */

	position:relative;
}
.absoluteDiv{

	/*
		Displaying the div 50px from the right and 90px from
		the top of its parent because of the explicit positioning
	 */

	position:absolute;
	right:50px;
	top:90px;
}

The benefits

Using these positioning properties you can achieve all kinds of advanced page layouts, which will bring your designs to the next level. In conjunction with the z-index property, you can change the default overlay rules that apply to elements (elements which appear later in the code are shown above the previous ones).

What is the coolest design you’ve done with the positioning properties? Brag in the comment section below.

Advertisements

OVER and PARTITION BY

OVER

OVER allows you to get aggregate information without using a GROUP BY. In other words, you can retrieve detail rows, and get aggregate data alongside it. For example, this query:

SELECT SUM(Cost) OVER () AS Cost
, OrderNum
FROM Orders

Will return something like this:

Cost  OrderNum
10.00 345
10.00 346
10.00 347
10.00 348

Quick translation:

  • SUM(cost) – get me the sum of the COST column
  • OVER – for the set of rows….
  • () – …that encompasses the entire result set.

OVER(PARTITION BY)

OVER, as used in our previous example, exposes the entire resultset to the aggregation…”Cost” was the sum of all [Cost]  in the resultset.  We can break upthat resultset into partitions with the use of PARTITION BY:

SELECT SUM(Cost) OVER (PARTITION BY CustomerNo) AS Cost
, OrderNum
, CustomerNo

FROM Orders

My partition is by CustomerNo – each “window” of a single customer’s orders will be treated separately from each other “window”….I’ll get the sum of cost for Customer 1, and then the sum for Customer 2:

Cost  OrderNum   CustomerNo
8.00 345        1
8.00 346        1
8.00 347        1
2.00 348        2

The translation here is:

  • SUM(cost) – get me the sum of the COST column
  • OVER – for the set of rows….
  • (PARTITION BY CustomerNo) – …that have the same CustomerNo.

Ref: http://www.midnightdba.com/Jen

Difference Between C# Cast Syntax and the AS Operators

You know what? Sometimes, the most obvious and easy questions are those that are the most frequently asked. One of such questions about C# is the difference between the cast syntax and the as operator. This is sometimes referred to as prefix-casting and as-casting.

Without giving much thought to the difference, you may think that having two constructs for casting is somewhat redundant. However, that’s not true. C# is an extremely elegant language and every construct has its place and helps you provide clean code. Moreover, these casts have dedicated operations in the Intermediate Language (i.e. the .NET language).

Let’s first list the differences. So, what we have is this.

1. The as operator returns null when casting is impossible (for example, the variable you are trying to convert is not of the requested type). Prefix casting will instead throw an exception.

2. The as operator can be applied only when you want to cast a reference type variable to a reference type. The prefix casting is free of this constraint.

3. The as operator cannot perform user-defined conversions (for example, explicit or implicit conversion operators). A prefix cast can perform these types of conversions.

However, the impact on your code can be more than the above three points describe. First things first. Prefix casting yields to a more reliable code. Why? Because if the cast fails, an exception is thrown right at the moment of casting. With the as operator, no exception is thrown. However, now you have a null reference and once you use the variable, a null reference exception is thrown. Unfortunately, this can be quite far from the place where the cast was performed. Because of this, applications using the as operator can be more difficult to debug.

Some programmers consider the old C style casting ugly. Personally I don’t have anything against the prefix casting (probably for our passion towards the C programming language). However, there is one serious benefit in favor of using the as operator. It’s way too fast when compared to the prefix casting. We have not done a scientific test but we’ve heard that the difference is on the factor of five. Imagine now prefix casting used in loops. A real slowdown for your code.

Thus, whenever possible, eliminate the casting at all, especially in long loops. If, however, casting is impossible to avoid, then use the as operator. But try to do a check right after casting. This will give you a fast and a clean code and a code that is elegant to read and easy to debug.