How can CSS be utilized in conjunction with PHP to alternate the alignment of elements in a list?
To alternate the alignment of elements in a list using CSS and PHP, you can use PHP to generate a unique class for each list item and then apply CSS styles to these classes to control the alignment. By dynamically adding classes to list items, you can easily achieve alternating alignments without hardcoding them in the HTML.
<ul>
<?php
$items = array("Item 1", "Item 2", "Item 3", "Item 4", "Item 5");
foreach($items as $key => $item) {
$class = ($key % 2 == 0) ? 'even' : 'odd';
echo "<li class='$class'>$item</li>";
}
?>
</ul>
<style>
.even {
text-align: left;
}
.odd {
text-align: right;
}
</style>