background image

Content tagged with: css

Eric's picture

I recently signed up for Google AdSense and I thought it would be fun to show ads for Internet Explorer users. Of course, you could use this tutorial for more productive purposes. Here is how I integrated this functionality into my theme.

I added 2 regions to THEME.info file:

regions[ad_left] = Ad Left
regions[ad_right] = Ad Right

I then included the regions in my theme by adding some code to my theme's page.tpl.php file:

<?php if ($ad_left): ?>
  <div id='ad_left' class='google_ads'>
    <?php print $ad_left; ?>
  </div>
<?php endif; ?>

<?php if ($ad_right): ?>
  <div id='ad_right' class='google_ads'>
    <?php print $ad_right; ?>
  </div>
<?php endif; ?>

Next, I added some CSS in my style.css theme file to absolutely position the regions in a fixed position:

.google_ads {
  position: absolute;
  bottom: 10px;
  width: 120px;
  height: 600px;
  display: none;
}

.google_ads .block {
  padding: 0;
  margin: 0;
}

body > .google_ads {
  position: fixed;
}

#ad_left {
  left: 10px; 
}

#ad_right {
  right: 10px;
}

Now that my regions were setup, I added 2 blocks containing the Google AdSense code and assigned the blocks to my new regions. You'll need to make sure the input filter you choose allows for javascript to be included.

At this point, the ads are included for every browser, but they will not be shown due to the "display: none" property I added to the "google_ads" CSS class. I added a few lines of jQuery in my theme's script.js file to show the regions for IE users:

$(document).ready(function(){
  // per IE ads
  if ($.browser.msie) {
    $('.google_ads').show();  
  }
});

If you view this page in IE, you'll see my code in action ^_^

My additional thoughts... this code executes the Google AdSense javascript regardless of your browser, but it is only shown to IE users. A more efficient method would be to use AJAX to include the javascript ONLY if the browser is IE, or of course, by implementing a server side browser detection library.

Eric's picture

I recently implemented the Panels module to create a page layout with 3 columns. I added background images to the columns, repeating on the y axis to span the entire length of the column. The problem I encountered: you cannot predict how tall the content will be in your columns and they will be staggered. To provide a more uniform styling, I added some jQuery to find the tallest column and set the shorter ones to the max height.

$(document).ready(function(){

  // keep track of the tallest column
  var tallest = 0;

  // loop through columns and find the tallest
  $('#panel_other_programs .panel-panel').each(function(){
    if ( $(this).outerHeight(true) > tallest )
      tallest = $(this).outerHeight(true);
  });

  // loop through columns and adjust height as necessary
  $('#MY-PANEL-UNIQUE-IDENTIFIER .panel-panel').each(function(){
   
    // check if current column needs to be adjusted
    if ( $(this).outerHeight(true) < tallest ) {
      // set new height
      $(this).height( tallest );
    }
  });

});

Now, all the columns should have a uniform height and the backgrounds should all span the entire length of each column.

Eric's picture

This morning I realized my blog needed some CSS tweaks for Camino (sorry, Camino users). Here's the code I used to test for the Camino browser and conditionally load a style sheet to fix the CSS issues.

<?php
// I put this function in my theme's template.php file:
function MYTHEME_preprocess_page(&$variables) {
  if (
stripos($_SERVER['HTTP_USER_AGENT'], 'Camino')!==false) {
   
$variables['styles'] .= "<link type='text/css' rel='stylesheet' media='all' href='" . base_path() . path_to_theme() . "/style.camino.css' />";
  }
}
?>

I then created a file in my theme directory called "style.camino.css" and put my CSS tweaks in there.

Eric's picture

At some point your design may call for a horizontal user login block. Luckily this can be accomplished with just a few lines of CSS. I added the following CSS to my theme:

.block #user-login-form { text-align: left; }
.block #user-login-form #edit-name-wrapper { float: left; margin-right: 10px; }
.block #user-login-form #edit-pass-wrapper { float: left; margin-right: 10px; }
.block #user-login-form #edit-submit { float: left; margin-top: 25px; }
.block #user-login-form .item-list { clear: both; text-align: right; }

Before:

After:

Eric's picture

In this article I'll explain how to implement drop down menus in the easiest way possible. Before I begin, please checkout the original "suckerfish" dropdown tutorials like: A List Apart and Son of Suckerfish Dropdowns. Unfortunately, most CSS drop down tutorials still require you to add javascript to resolve IE6 issues. CSS menus require the ":hover" attribute on LIs, and IE6 just can't handle it. I figured since Drupal already includes jQuery, I'd write a tutorial that takes advantage of jQuery's simplicity and keeping the CSS to a minimum for ease of maintenance.

The first thing I did was create my menu structure. For this tutorial to work, you'll need to ensure all the menu items are set to expanded (which makes sure they are all rendered when the menu is sent to the theme layer).


For purposes of this tutorial, I create a very blank theme (I started off use Garland, but I wanted to keep the CSS generic). I assigned the menu block (primary links) to a region on my page:


Next, I added the following CSS. As you can see, I'm using the unique identifier on the primary link block in this example. Check out the CSS comments to better understand what each line of CSS does. This CSS works with any number of child menus. NOTE: If you wanted your menus to scale with the font size, you could replace all the measurements with "ems".

/* remove any previously set margins and paddings */
#block-menu-primary-links * { margin: 0; padding: 0; }

/* set width/height on <li>, <a>, <li><ul> */
#block-menu-primary-links li ul,
#block-menu-primary-links li,
#block-menu-primary-links a { width: 125px; height: 25px; }

/* remove <li> list styling off */
#block-menu-primary-links li { list-style: none; }

/* display <a> as block */
#block-menu-primary-links a { display: block; }

/* set <li> position */
#block-menu-primary-links li { float: left; position: relative; }

/* set position of <ul> in <li> */
#block-menu-primary-links li ul { top: 25px; left: 0px; position: absolute; }

/* position child <ul> */
#block-menu-primary-links li ul ul { margin: -25px 0 0 125px; }


For demonstration purposes, I left off one very import line of CSS to show the new layout of the menu structure. As you can see, the LIs are now positioned vertical their parent, and the child ULs are positioned off to the right. I wanted to show all the menu items in their desired layout to give you the opportunity to adjust the CSS (height, width, padding, etc).


Now, you can add the last line of CSS which hides the display of all the child ULs:

/* set visibility of <ul> in <li> */
#block-menu-primary-links li ul { display: none; }


The previous line of CSS will leave you with just the top level menu items:


Lastly, I added a hover jQuery function to attach a javascript hide/show event on every LI:

$(document).ready(function(){
  $('#block-menu-primary-links li').hover(
    function(){
      $('ul:first', $(this)).show();
    },
    function(){
      $('ul', $(this)).hide();
    }
  );
});


Here's an example of what the final result will look like. If you implement this code, and mouseover the menu structure, the child ULs will appear and disappear.