Showing posts with label development. Show all posts
Showing posts with label development. Show all posts

Feb 28, 2015

HTML & CSS naming convention – a modern approach

 

Excerpts original from Stackoverflow


First, if you are not aware of this common (good?) practice: avoid IDs as styling hooks, try to only use Classes

Because:

  • only very few blocks (ie. page-header, page-footer) can 100% garantee the fact that they will not be reused elsewhere

  • you want to keep specificity low, there will always be times you need to override it (without using an extra ID selector or !important)


Common requirements/conventions:
  • Names should be intuitive/meaningfull
  • Do NOT abbreviate names unless its a really well known abbreviation (ie. msg for Message, accnt for account)
  • Use known/common names: .site-nav, .aside-nav, .global-head, .btn-primary, .btn-secondary
  • Allow structural hierarchy (ie. BEM convention)
  • Use - or _ in namings: probably subjective (devs' opinions) & depending on the keyboard languages used. Note that camelCase has been left aside for browser-compatibility issues I believe, although I never found a proof for this.
  • Never use elements in selectors unless exceptional case: this allows for more flexibility, ie. you have buttons you created using <input type="button"></input> and you want to switch to using <button></button>, if you used element types in some selectors then you can plan some annoying/time-consuming refactoring/testing/prod-bug-fixing, whereas if you use element-less selectors then the switch will be infinitely easier. SMACCS also has it in its conventions
  • For states, try to match known conventions from other languages (php, java, c#): ie, use "is-active", "is-badass", & so on
  • Name from left-to-right: from the most generic to the most precise, ie. btn-bluetheme-create-accnt or accordion-modrnlook-userlist
  • a class or id name should always be specific enough to be searched across a whole project & return only the relevant results
  • Prefer direct descendant if you use descendent selectors - use .module-name > .sub-module-name vs .module-name .sub-module-name - you'll save yourself some headache in the future (simpler CSS but also more performant, although the term "CSS performance might be laughable")

As discussed below you may want to consider using Structural naming convention OR/AND Presentational naming convention (or skins). I'd prefer the structural one overall but then there always are cases where it's needed (ie. using different button styles on 1 website is not unusual & it might be handy/tempting to name them such as btn-dark & btn-light).


Known conventions:

Structural naming convention: name element's class by describing what it is, rather than where it is or how it looks. See examples below.

.page-container
.page-wrap-header-n-content
.page-header
.branding-logo
.branding-tagline
.wrapper-search
.page-nav-main
.page-main-content
.page-secondary-content
.nav-supplementary
.page-footer
.site-info

Presentational naming convention: name element's class by describing its location and/or appearance. See examples below.

.theme-ocean-blue
.skin-red-tango

BEM naming convention: stands for "Block, Element, Modifier". Syntax is such as <module-name>__<sub-module-name>--<modifier-or-state>. Block is a the "main" container, a kind of module/component whatever you call it. Element is just a child component of the main component (the Block). Modifier is some kind of state. Peculiarities: the syntax (usage of dbl underscore & dbl dash), & child elements must contain their closest component's name. See examples below.

.nav-primary
.nav-primary__list
.nav-primary__item
.nav-primary__link
.nav-primary__link--is-active

.grid
.grid__item
.grid__description
.grid__img-wrap
.grid__img

.global-footer
.global-footer__col
.global-footer__header
.global-footer__link

OCSS naming convention: stands for Object Oriented CSS. Uses skins for branding purposes or consistency of design (ie. bg color, border color, ...). Abstracts the structural styles. Example of abstract structural style below.

 .global-width {
min-width: 780px; /* minimum width */
max-width: 1400px; /* maximum width */
margin: 0 auto; /* Centering using margin: auto */
position: relative; /* Relative positioning to create a positioning context for child elements */
padding-left: 20px;
padding-right: 20px;
overflow: hidden; /* Overflow set to "hidden" for clearfixing */
}




Some CSS guidelines:

There has been a "trend" to share your CSS styleguide, here are a few you can read to pick & choose whatever seems to fit for your need (naming convention but also much more, this may be out of scope of your question):





Resources:

Oct 28, 2011

Fix IIS error System.ServiceModel.Activation.HttpModule

 

If you hit this error, probably you are install .NET 4.0 after you installed IIS:

Could not load type ‘System.ServiceModel.Activation.HttpModule’ from assembly ‘System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′.


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.


Exception Details: System.TypeLoadException: Could not load type ‘System.ServiceModel.Activation.HttpModule’ from assembly ‘System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′.

Solution:

Just run this command from Visual Studio Command Prompt:

aspnet_regiis.exe /i

The aspnet_regiis.exe is in

  • %windir%\Microsoft.NET\Framework\v4.0.30319
  • %windir%\Microsoft.NET\Framework64\v4.0.30319 (on a 64-bit machine)

Aug 26, 2011

Server.Transfer vs. Response.Redirect

ping from http://www.developer.com/net/asp/article.php/3299641/ServerTransfer-Vs-ResponseRedirect.htm 

If you read a lot of industry magazines and ASP.NET code samples, you may find that, although the majority use Response.Redirect to send the user to another page, some seem to prefer the rather mysterious-sounding Server.Transfer. So, what's the difference?

Well, Response.Redirect simply sends a message down to the browser, telling it to move to another page. So, you may run code like:

Response.Redirect("WebForm2.aspx")

or

Response.Redirect("http://www.karlmoore.com/")

to send the user to another page.

Server.Transfer is similar in that it sends the user to another page with a statement such asServer.Transfer("WebForm2.aspx"). However, the statement has a number of distinct advantages and disadvantages.

Firstly, transferring to another page using Server.Transfer conserves server resources. Instead of telling the browser to redirect, it simply changes the "focus" on the Web server and transfers the request. This means you don't get quite as many HTTP requests coming through, which therefore eases the pressure on your Web server and makes your applications run faster.

But watch out: because the "transfer" process can work on only those sites running on the server, you can't use Server.Transfer to send the user to an external site. Only Response.Redirect can do that.

Secondly, Server.Transfer maintains the original URL in the browser. This can really help streamline data entry techniques, although it may make for confusion when debugging.

That's not all: The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a statement such as Server.Transfer("WebForm2.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to.

For example, if your WebForm1.aspx has a TextBox control called TextBox1 and you transferred to WebForm2.aspx with the preserveForm parameter set to True, you'd be able to retrieve the value of the original page TextBox control by referencing Request.Form("TextBox1").

This technique is great for wizard-style input forms split over multiple pages. But there's another thing you'll want to watch out for when using the preserveForm parameter. ASP.NET has a bug whereby, in certain situations, an error will occur when attempting to transfer the form and query string values. You'll find this documented at http://support.microsoft.com/default.aspx?id=kb;en-us;Q316920.

The unofficial solution is to set the enableViewStateMac property to True on the page you'll be transferring to, then set it back to False. This records that you want a definitive False value for this property and resolves the bug.

So, in brief: Response.Redirect simply tells the browser to visit another page. Server.Transfer helps reduce server requests, keeps the URL the same and, with a little bug-bashing, allows you to transfer the query string and form variables.

Top Tip: Don't confuse Server.Transfer with Server.Execute, which executes the page and returns the results. It was useful in the past, but, with ASP.NET, it's been replaced with fresher methods of development. Ignore it.

About the Author

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book, plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.