1 Line CSS Positioning Blazor Style

I found a really good YouTube video from Google Chrome Developer channel that I am piggybacking off of for this post.  I found the content very interesting and I wanted to see what it would take to first port it s Blazzor application, then how to implement the layouts in a Blazor component library where I can use them in any project.  

Please note, even though I am a fan of Bootstrap and other CSS frameworks, I have been on a push lately to write my own CSS.  These layouts are pure HTML and CSS.

I won't cover the details of the 10 different layouts and the 1 kay line that makes them work, I do highly recommend you watch the video, it is very educational on CSS layouts.

To demonstrate the 10 layouts, I have created a razor page to display the layout.  You can review the source code provided for the details

10 Layouts:

1. Center - All child content is centered within the parent container

                            Centered Layout

2. Clamping - Clamps child content within the parent container

                              Clamp Layout


3.  Classic - Most common layout for web sites and applications

Classic Layout


4. Deconstruction Pancake - has 3 rows that auto-resize

Deconstructed Pancake Layout


5. Grid - Standard 12 column grid

12 column grid layout


6. Line Up - Great for cards and images

Line up layout



7. RAM (Repeat Auto MinMax) - Responsive layout

Repeat Auto Min Max


8. SideBar- Basic Blazor Layout

Side Bar layout

9. Stack - Another very common layout

Stack Layout

10. Aspect Ratio - This is using an experiential CSS property "aspect ratio"

Aspect Ration Layout


Looking at the available layouts, it is easy to see how you would be able to use multiple of them in an application.  From using the sidebar for the main layout to using the center for the login page.


Create the Component Library


Now that we have our layout pages, we want to create a Blazor component library and add our pages to them so that we can reuse the different layouts in other projects.

Actually creating the project is very straight forward.

      1. Create a Razor Component Library from "Create new Project"
Create new Project in Visual Studio

Create class library



2. Add the Project to your Blazor Project
3. Add the Route Assembly into the app.razor file.
    a. This is a very important part.  You have to let the Blazor router know where to route the request to for pages in the component Library. 



Calling pages from the Component Library


In this project, we have added the pages to the stand Blazor Nav.   We have to include a using statement for the component library:

       @using SimpleLayouts.Layouts

In addition, we need to add the style sheet from the component library to the Blazor project so we can access the styles.  The key part is to make sure you include the _content/Library_Name in the path for the style sheet:

       <link rel="stylesheet" href="_content/SimpleLayouts/layoutLib.css" />

With those items set, you are good to go to have a class library of popular layouts all ready to be used.

Gotchas

There are a couple of gotchas I ran into when building this project.
  1. Don't forget to add the route to the App.razor component so it can find the pages in the library
  2. Use the _content when referencing files in the library, CSS Images, JS
  3. Watch for name class conclusions on the CSS.  If you have several component libraries the CSS class is available anywhere in the application.
  4. I used pure CSS for the layouts, but keep Bootstrap for the main page.  This caused some conflicts for the APP HTML section that is the _Host.html.  Going forward I will stay consistent with either a CSS framework or no framework
Again a big thank you to the Google Chrome Developer youtube channel for an excellent CSS layout video.


Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor - Cool Blur Loading Page

Blazor new and improved Search Box