What is the best practice for calculating percentage-based widths in PHP for styling elements?

When calculating percentage-based widths in PHP for styling elements, it's important to ensure that the total width adds up to 100%. One common approach is to calculate the percentage width based on the total width of the container element. This can be done by dividing the desired width by the total width and multiplying by 100.

$totalWidth = 1000; // total width of the container element
$desiredWidth = 300; // desired width of the element

$percentageWidth = ($desiredWidth / $totalWidth) * 100;

echo "width: " . $percentageWidth . "%;";