March 8, 2012

css tricks to shorten my redundant code

Question by Wondering Coder

I have an html that look like this:

<ul>
  <li>
    <img src="<?php echo base_url();?>assets/images/blue_background.png" class ="menu-bg"/>
    <a href="#" class="menu-portal"></a>
  </li>
  <li>
    <img src="<?php echo base_url();?>assets/images/blue_background.png" class ="menu-bg"/>
    <a href="#" class="menu-tags"></a>
  </li>
  <li>
    <img src="<?php echo base_url();?>assets/images/blue_background.png" class ="menu-bg"/>
    <a href="#" class="menu-requests"></a>
  </li>
  <li>
    <img src="<?php echo base_url();?>assets/images/blue_background.png" class ="menu-bg"/>
    <a href="#" class="menu-videos"></a>
  </li>
</ul>

As you can see the code above I kept repeating my blue background which in element img. Can anyone help me shorten this up. For example if I can insert the img in my li element, that would do.

Any help will be great.

Answer by Starx

Use it as a background, instead a defining a separate image tag.

ul li { 
   background-image: url("assets/images/blue_background.png");
   height: 30px; /* height of the image */
   width: 30px; /* width of the image if the size is fixed */
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!