Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:13:25+00:00 2026-05-27T09:13:25+00:00

I need to override some <style> elements within my Razor page so I’ve inserted

  • 0

I need to override some <style> elements within my Razor page so I’ve inserted the styles immediately after the opening code block:

@{
    ViewBag.Title = "New Account";
    Layout = "~/Views/Shared/_Layout_Form.cshtml";
}
<style type="text/css">
    #main
    {
        height: 400px;
    }
</style>

The page renders correctly in the browser but Visual Studio green squiggles at <style> complaining that:

<Validation (XHTML 1.0 Transitional): Element 'style' cannot be nested within element 'style'>

I’ve doublechecked the master page – no problems highlighted there.

What’s Visual Studio expecting at this point? What’s the correct way to override styles on a page by page basis? Via jQuery?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:13:25+00:00Added an answer on May 27, 2026 at 9:13 am

    The style element cannot be nested under the <body> or child elements of the <body>, instead it should go to the <head> element of the page.

    If you try to override the styles like this, they get inserted into the default section of your layout page by @RenderBody(), which I assume is inside the <body> of the layout page, while the default styles still stay in the <head>.

    The proper way to override some part of the layout page from the content page would be something along these lines, using Razor sections:

    _layout.cshtml

    <head>
    @if (IsSectionDefined("Style"))
    {
        @RenderSection("Style");
    }
    else
    {
        <style type="text/css">
        /* Default styles */
        </style>
    }
    </head>
    <body>
    @RenderBody()
    ...
    

    page.cshtml

    @{        
        Layout = "layout.cshtml";
    }
    @section Style
    {
        <style type="text/css">        
        #main        
        {        
            height: 400px;        
        }
        </style>
    }
    <!-- Body content here -->
    ...
    

    Now if the Style section is defined on the content page, its contents will override the defaults from the layout page.

    I suggest you read more on Razor layouts and sections. A nice article on this by ScottGu can be found here.

    Regarding Visual Studio markup validation warnings

    Visual Studio has a problem when markup that makes up a single page is being split up between different files like this. In most cases there is no way for Visual Studio (or any such IDE for that matter) to know how the different page fragments will be dynamically put together in the end. So usually you can’t avoid some of these warnings on a project.

    Personally I would ignore the warnings if I know what I’m doing and the resulting pages pass the markup validation (http://validator.w3.org/).

    If you really want to hide the warnings, you need to disable the appropriate validation options in Visual Studio. E.g. for HTML in Visual Studio 2012 this can be done in Tools > Options > Text Editor > HTML > Validation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to insert selected text on the page into textarea. There must be some
I need add new style for some DataGrid rows, depending on variable. For example:
I've got a table with CSS3 alternating row styles but I need to override
I need to override some of the base class' nested class members, while keeping
need ask you about some help. I have web app running in Net 2.0.
Need some help about with Memcache. I have created a class and want to
Need to call a filter function on some options based on a radio selected
need to add a 2nd css stylesheet to a page. do i add a
Hey all, i am in need of some help with figuring out how to
Long story short, I'm trying to write some toast-style popup notifications (similar to Growl)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.