dynamic height calculation enhanced to include margins