Styling with Bootstrap Cards
To quickly create a professional layout, wrap your data loop in Bootstrap classes. This structures content into a responsive card-based grid without writing complex CSS from scratch.
div class="card-columns": The main container that arranges cards into columns.div class="card": An individual card for each product in the loop.div class="card-image": A container for the product's image.div class="card-body": A container for the product's text content, like the title.h5 class="card-title": A styled heading for the product title.
Using Razor Syntax for Dynamic Content
Razor Pages allow you to seamlessly embed C# code directly within your HTML markup. The @ symbol is the key to switch from HTML to C#.
Note
You can use Razor syntax to dynamically set HTML attributes. Here, it sets the background image URL from the product model:
style="background-image: url('@product.Image')"
Debugging CSS with Browser Tools
If visual elements don't appear as expected, your browser's developer tools are essential for debugging.
Tip
Right-click an element and choose "Inspect" to view its HTML and applied CSS. In this video, inspection revealed that the product images were present but had a
heightof 0 pixels, making them invisible. You can live-edit CSS in the developer tools to quickly test fixes.
Customizing Styles in a CSS File
While inline styles are useful for quick, dynamic styling, it's best practice to consolidate shared styles into an external stylesheet. In an ASP.NET Core project, this is typically the site.css file located in the wwwroot/css directory.
- By adding rules for classes like
.card-imagetosite.css, you can define properties likeheightto ensure your layout renders correctly. - This approach keeps your
.cshtmlfiles cleaner and makes styles easier to manage and reuse.
The Shared _Layout.cshtml Page
Important
The
_Layout.cshtmlfile, located in thePages/Sharedfolder, serves as the master template for your website. Changes made here will affect all pages that use it.
- It defines the overall page structure, including the
<html>,<head>, and<body>tags. - This is the ideal place to put recurring elements like the site navigation bar (navbar), footer, and to link site-wide assets.
- You can add links to external resources like Google Fonts in the
<head>section. - The
@RenderBody()method is a placeholder where the content of an individual page (likeIndex.cshtml) is injected.
