In Android 2.2+, there is a JS object for this.
You can write a class to use CSS depending on the type of connection. But it is not available on iOS for mobile websites, as far as I know.
var connection, connectionSpeed, htmlNode, htmlClass;
connection = navigator.connection || {"type":"0"};
switch(connection.type) {
case connection.CELL_3G: connectionSpeed = "mediumbandwidth"; break;
case connection.CELL_2G: connectionSpeed = "lowbandwidth"; break;
default: connectionSpeed = 'highbandwidth';
}
htmlNode = document.body.parentNode;
htmlClass = htmlNode.getAttribute("class") || "";
htmlNode.setAttribute("class", htmlClass + " " + connectionSpeed);
Code from slide 24 in this presentation:
http://davidbcalhoun.com/present/mobile-performance/