How can the code be modified to incorporate CSS classes for styling the elements instead of inline styling?
To incorporate CSS classes for styling the elements instead of inline styling, we can add a class attribute to each HTML element and define the styles in an external CSS file. This helps in separating the content from the presentation, making the code more maintainable and easier to update in the future.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<?php
$names = array("Alice", "Bob", "Charlie");
echo "<ul>";
foreach ($names as $name) {
echo "<li class='name'>$name</li>";
}
echo "</ul>";
?>
</body>
</html>
```
styles.css:
```css
.name {
color: blue;
font-weight: bold;
}
Related Questions
- What is the recommended method for escaping XML special characters in PHP, especially when dealing with Umlauts?
- Are there any best practices for maintaining transparency when resizing PNG images in PHP?
- How can the issue of mutual access between classes be resolved in object-oriented programming in PHP?