Responsive Web Design for Menu, Image and Advertisements
Wall Script
Follow Me:
Sunday, December 30, 2012

Responsive Web Design for Menu, Image and Advertisements

This post is the continuation of my previous Responsive Web Design using CSS3. I had explained how to design a basic responsive web page using CSS3 and Modernizr for lower version browsers. In this post we want to explain how to design responsive collapsed navigation menu, images and advertisements grids for different media devices.

Responsive Web Design using CSS3


Download Script
     Live Demo

Try live demo with different screen resolutions.

Developer
Arun Kumar Sekar
Arun Kumar Sekar

Engineer, Plugin Expert
Chennai, INDIA

Images
Image responsive design is very simple, just give width and height 100%

HTML Code
<div class="responsiveImage">
<img src="image.jpg" />
</div>


CSS Code
.responsiveImage { width: 100%;clear: both;}
.responsiveImage img{ max-width: 100%; max-height: 100%; }

Menu
Collapsing navigation menu for mobile devices to reduce screen responsive.

HTML Code
<div class="menubar">
<div class="responsive-menu">
Menu <a href="javascript:void(0);"><span class="menuicon"></span></a>
</div>

<ul class="menu">
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
.....
.....
</ul>

</div>
<div style="clear:both;"></div>


CSS Code @media 768px +
Here .menu > li is float:left
a { text-decoration: none; }
.menubar { width:100%; height:39px; background-color: #006699; }
.menu { float: left; position: relative; padding: 10px; }
.menubar .menu > li { float: left; line-height: 20px; }
.menu > li > a { color: #ffffff; font-size: 15px; padding:10px 15px 10px; }
.menu > li > a:hover, .menu > li > a:active { background-color:#4682b4; border-radius:4px; }
.responsive-menu { display: none; color: #ffffff; font-weight: bold; padding: 5px; border-bottom: 2px solid #ffffff;width:100%; }

CSS Code @media 767px -
Here .menu > li is width:100%
@media (max-width: 767px) {
.menubar{clear:left; height:auto !important; display: table; }
.menu{ float: none; width:100%; padding:0px !important; display: none; }
.menubar .menu > li { width:100%; border-bottom: 1px solid #dedede; cursor: pointer; padding-top: 5px; padding-bottom: 5px; }
.menu > li:hover, .menu > li:active { background-color:#4682b4;}
.responsive-menu { display: inline-block !important; }
}

CSS Code for menu dropdown arrow.
/* Down Arrow Icon */
.menuicon {
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid #ffffff;
position: absolute;
right: 8px;
top:11px;
}

JavaScript
Contains javascipt code. $(".menuicon").click(function(){} - menuicon is the class name of the arrow. Using slideToggle $(".menu").slideToggle(); for drop down this list.
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{

$(".menuicon").click(function()
{
$(".menu").slideToggle();
});
// Window resizing function
$(window).resize(function()
{
if($(this).width() < 767)
{
$(".menu").hide();
}
else
{
$(".menu").show();
}

});
});
</script>

$(window).resize(function() is understand the screen resolution, based on width it will show and hide .menu part.

Advertisement Blocks

HTML Code
I have created three horizontal DIVs are .ad_big 728px, .ad_medium 468px and .ad_small 300px.
<div class="ad_big">728px Ad</div>
<div class="ad_medium">467px Ad</div>
<div class="ad_small">300px Ad</div>

CSS Code
Based on media resolution system will show advertisement blocks.
.ad_big,.ad_medium,.ad_small{ padding:5px;margin:5px; }
.ad_medium,.ad_small { display:none; }

/* Ads devices */
@media (min-width: 478px) and (max-width: 738px) {
.ad_medium{
display: block !important;
}
.ad_big,.ad_small {
display: none;
}
}

@media (min-width: 325px) and (max-width: 477px) {
.ad_small {
display:block !important;
}
.ad_big,.ad_medium {
display: none !important;
}

}
@media (min-width: 0px) and (max-width: 324px) {
.ad_small {
display:block !important;
}
.ad_big,.ad_medium {
display: none !important;
}

}

35 comments:

  1. Google Ads lligal like that

    ReplyDelete
  2. Thank you
    it's really very usefull

    ReplyDelete
  3. The Responsiveness of Advertisement section will be very helpful for me!

    ReplyDelete
  4. ie8 not supporting media sizes

    ReplyDelete
  5. Very nice post Arun. Responsive web designs are more useful than others.

    ReplyDelete
  6. Perfect one what i always wanted to get!! Thanks a lot.

    ReplyDelete
  7. Wow! Nice tutorial and Happy New Year 2013!

    ReplyDelete
  8. Hello Arun,

    I have 2 level of Menu. What should i do to mange that ? Your example is for 1st level only. It will be helpful if you update this with 2-3 level menu.

    Thank you

    ReplyDelete
  9. Why your site 9lessons is not yet responsive..

    ReplyDelete
  10. It's not working (I'm using the demo via my iPhone) but thanks for this post :)

    ReplyDelete
  11. great love the responsive menu

    ReplyDelete
  12. I have read your last post and read this also. keep sharing.

    ReplyDelete
  13. You always did an awesome work!!

    ReplyDelete
  14. I was looking forward that. Thanks for it.

    ReplyDelete
  15. I am looking forward for that and really a great thing you have explained here.

    ReplyDelete
  16. Nice tutorial. I would like to use this thing on my private blog also. Thanks for sharing this.

    ReplyDelete
  17. Nice Man,
    I'm trying to implement this code on the blogger template to create a responsive Blogger template. Thank you Shrinivas.

    ReplyDelete
  18. use display:inline-block and vertical-align:middle or top instead of float:left or right,
    learned some responsive design here, thanks

    ReplyDelete
  19. Great man it helped me a lot...really good work

    ReplyDelete
  20. I was learning twitter bootstrap for creating responsive page. Your tutorial is also very helpful.
    Thanks.

    ReplyDelete
  21. is it poosibe to added a search bar to the menu, if yes how?

    ReplyDelete
  22. I used this in my blog and enjoyed over 1 month a responsive design. Thanks mate for a nice tutorial.

    ReplyDelete