Showing posts with label css. Show all posts
Showing posts with label css. Show all posts

Saturday, August 18, 2012

Using Less and Twitter Bootstrap in ASP.NET MVC4

UPDATE 05/02/2013:

Changed ScriptBundle and StyleBundle to just Bundle, per Andrey Taritsyn:
Bundle Transformer it is not recommended to use together with the StyleBundle and ScriptBundle classes, because these classes already contain transformations (instances of the built-in minifiers: CssMinify and JsMinify). Use a Bundle class.

UPDATE 11/08/2012:
  1. Create a new MVC4 Internet Application
  2. Add the following Nuget Packages
  3. Rename /Content/site.css to /Content/site.less 
    • Edit BundleConfig.cs in the App_Start folder and update the css file name to site.less like so:
      bundles.Add(new var cssTransformer = new CssTransformer();
      var jsTransformer = new JsTransformer();
      var nullOrderer = new NullOrderer();
      
      var defaultScriptsBundle = new Bundle("~/bundles/default.js").Include(
                  "~/Scripts/jquery-{version}.js",
                   "~/Scripts/jquery-ui-{version}.js",
                  "~/Scripts/site.js");
      defaultScriptsBundle.Transforms.Add(jsTransformer);
      defaultScriptsBundle.Orderer = nullOrderer;
      bundles.Add(defaultScriptsBundle);
      
      var defaultStylesBundle = new Bundle("~/Content/default.css").Include("~/Content/site.less");
      defaultStylesBundle.Transforms.Add(cssTransformer);
      defaultStylesBundle.Orderer = nullOrderer;
      bundles.Add(defaultStylesBundle);
      
    • Add the following to the top of /Content/site.less in order to have access to all of the Bootstrap mixins:
      @import "less/bootstrap.less"; 
      body {
            padding-top: 60px;
            padding-bottom: 40px;
      }
      @import "less/responsive.less"; 
      

    =================== THE REST OF THIS POST IS OUT OF DATE ==========

    UPDATE 8/20/2012: I've created a Nuget package that performs the steps below. I'd suggest reviewing what the package does below and then running:
    Install-Package Twitter.Bootstrap.Less.MVC4

    ASP.NET MVC4 has a great new feature that can bundle and minify your CSS and Javascript files.

    But in order to get Twitter Bootstrap to work it takes a bit more work. Here are the steps that worked for me.

    1. Create a new MVC4 Internet Application
    2. Add the following Nuget Packages
    3. Create an Infrastructure folder and add the following two files: (hat tip to this Stackoverflow question)
    4. Rename /Content/site.css to /Content/site.less 
    5. Edit BundleConfig.cs in the App_Start folder and replace the default Content/css bundle with the following:
      var css = new Bundle("~/Content/css").Include("~/Content/site.less");
      css.Transforms.Add(new LessMinify());
      bundles.Add(css);
      

    6. Add the following to the top of /Content/site.less in order to have access to all of the Bootstrap mixins:
      @import "less/bootstrap.less";
      

    If you have "shared" .less files from other projects I found importing them in site.less right after boostrap.less to work pretty well. They'll have access to all the mixins.
    @import "less/bootstrap.less";
    @import "less/shared.less";
    

    The completed solution is available on Github here

    If there is a better/easier way to do this please let me know in the comments!




    Sunday, May 13, 2012

    Add support for LESS to Node.js connect-assetmanager package

    The connect-assetmanager Node.js package allows you to combine multiple javascript and CSS files into one and also do other manipulations like minification.

    It's part of Mathias Pettersson's (mape) excellent node-express-boilerplate template.

    If you're also using LESS for CSS here is some code you can add to get connect-assetmanager to process you're LESS files:

    var assetsSettings;
    assetsSettings = {
      js: {
        route: /\/static\/js\/[a-z0-9]+\/.*\.js/,
        path: "./public/javascripts/",
        dataType: "javascript",
        files: ["libs/underscore-min.js" "libs/jquery.mobile-1.1.0.min.js", siteConf.uri + "/nowjs/now.js", "site.js"],
        debug: siteConf.debug,
        stale: !siteConf.debug
      },
      css: {
        route: /\/static\/css\/[a-z0-9]+\/.*\.css/,
        path: "./public/stylesheets/",
        dataType: "css",
        files: ["libs/jquery.mobile-1.1.0.min.css", "style.less"],
        debug: siteConf.debug,
        stale: !siteConf.debug,
        preManipulate: {
          "^": [
            function(file, path, index, isLast, callback) {
              var match;
              match = path.match(/\.less$/);
              if ((match != null) && match.length > 0) {
                return less.render(file, function(e, css) {
                  return callback(css);
                });
              } else {
                return callback(file);
              }
            }
          ]
        }
      }
    };
    

    The import part is the preManipulate line under css but I include my entire assetsSettings from a project to make it easier to understand.

    Saturday, March 12, 2011

    Make reCAPTCHA Smaller

    I don't recommend this solution BUT if it's your only option he's how to make reCAPTCHA smaller.

    First you'll need to use a custom theme. Add the following before the form element:

    <script type="text/javascript">
    var RecaptchaOptions = {
        theme : 'custom',
        custom_theme_widget: 'recaptcha_widget'
    };
    </script>
    

    Next add the HTML for the custom widget where you want it to show up inside the form.

    <div id="recaptcha_widget" style="display:none">
    
       <div id="recaptcha_image"></div>
       <div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>
    
       <span class="recaptcha_only_if_image">Enter the words above:</span>
       <span class="recaptcha_only_if_audio">Enter the numbers you hear:</span>
    
       <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
    
       <div><a href="javascript:Recaptcha.reload()">Get another CAPTCHA</a></div>
       <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">Get an audio CAPTCHA</a></div>
       <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></div>
    
       <div><a href="javascript:Recaptcha.showhelp()">Help</a></div>
    
     </div>
    
     <script type="text/javascript"
        src="http://www.google.com/recaptcha/api/challenge?k=your_public_key">
     </script>
     <noscript>
       <iframe src="http://www.google.com/recaptcha/api/noscript?k=your_public_key"
            height="300" width="500" frameborder="0"></iframe><br>
       <textarea name="recaptcha_challenge_field" rows="3" cols="40">
       </textarea>
       <input type="hidden" name="recaptcha_response_field"
            value="manual_challenge">
     </noscript>
    

    This is standard code from the reCAPTCHA documentation up to this point. The trick I used to make the img and div tags smaller is to set the width in CSS with the !important flag. Add this CSS to your page.

        #recaptcha_image,
        #recaptcha_image img 
        {
            width: 200px !important;
            cursor: pointer;
        }
        #recaptcha_image img:hover
        {
            position: absolute;
            width: 300px !important;
        }
        .recaptcha_only_if_image,
        .recaptcha_only_if_audio
        {
            display: block;
        }
    

    This will make the reCAPTCHA fit in a space a little over 200px wide. I've added an img:hover style to make the image full size when the mouse rolls over it. The :hover style doesn't work on older browsers though so the next step would be to use jQuery to make the image bigger on hover or click. I'll leave that up to you to figure out. ;)

    Thursday, January 20, 2011

    Android Browser Issues

    Ran into a really frustrating issue with Android's Webkit browser today. I'm using jQuery Mobile in a project but have tweaked it quite a bit. The site works awesome on an iPhone. Android (2.2.1) not so much.

    The symptoms:
    1) Regular html select inputs where not opening up with the list of options.
    2) Certain links and buttons had difficulty registering clicks.
    3) One of my text boxes would get scrolled to the top of the page as soon as you started entering text.

    The solution?
    Make sure -webkit-backface-visibility is not set to hidden on any parent elements. The jQuery Mobile CSS has it set to hidden for the ui-page class. Overriding it for android by setting it to visible fixed all 3 of the above issues.

    This took about 6 hours to solve.

    Kill me now.