ASP.NET Web Forms: Master Page & CSS File [closed]
Asked Answered
T

3

9

I have a project (Web Application) that I need to implement with Web Forms. I have already prepared the design for the website and I will use one of the CSS templates that I already developed. In this case:

  1. Should I use a master page?
  2. Is there any relationship between the master page and the CSS file? If yes, how should I define master page with the CSS file?

By the way, the CSS file is a ready template.

Taxexempt answered 16/9, 2011 at 12:27 Comment(0)
F
12
  1. I would highly recommend using a master page. It gives you the ability to have one consistent look across all your pages. Also if you decide to change the layout your only changing one file!

  2. Simply reference the CSS file in your master page and it will be applied to every page the master page is used on. Something like <link href="~/styles/Style.css" rel="stylesheet" type="text/css" />

It should be noted that you can also have more then one master page. Visual studio also allows you to create pages and select the master page, it's one of the other options when adding an item.

Fantan answered 16/9, 2011 at 12:51 Comment(0)
G
1

You should have a master page if many or all of your pages share a common User Interface (such as a header, footer, navigation bar, etc).

It really doesn't matter where you put the link to the CSS file, because the CSS will be applied to the whole page either way. If the CSS is common to the entire website (or all the ones that share the same master page) put it in the Master Page. If the CSS file is page specific and you can't put it in the Master page because it conflicts with another page specific, put it in the page (in the head ContentSection).

Guest answered 16/9, 2011 at 12:53 Comment(2)
I get what you were saying, but it does matter where the link to the CSS file is. If using master pages, the CSS file should be added to the header in the master page.Barquisimeto
Well yes, I meant rather that it doesn't matter which file. CSS should be in the <head> element so the website progressively renders. So if you are in the master page add it there. If you are in a page, add it to the ContentSection headed so that it gets added to the <head> element, and in the correct order (after stylesheets that get applied to the page as a whole).Guest
B
0

I would suggest using master pages to control the general layout of your site, so you don't have to repeat the layout on every page. You don't need to do anything special in the CSS file to account for the master page, but you should add the link to the CSS file in the header of the master page, so it carries through to all of the pages using the master page.

Master page header:

<head runat="server">    
    <link rel="Stylesheet" href="~/pathtocssfile/cssfilename.css" type="text/css" />
</head>
Barquisimeto answered 16/9, 2011 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.