make background of a div responsive - HTML CSS CSS Layout

HTML CSS examples for CSS Layout:Responsive Layout

Description

make background of a div responsive

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <style id="compiled-css" type="text/css">
figure {<!--from  ww w  .ja  va  2 s. c  o  m-->
   margin:0;
}

figure img {
   display:block;
   width:100%;
   height:auto;
}

.container {
   background:gray;
   border:2px solid Chartreuse;
}

.container .inner-w {
   max-width:401px;
   margin:0 auto;
   background:red;
}

.section-name .inner-w {
   min-height:301px;
   background-image:url('https://www.java2s.com/style/demo/Google-Chrome.png');
   background-size:cover;
   background-position:center center;
}
</style> 
 </head> 
 <body>
   Lorem ipsum dolor sit amet, consectetur adipis 
  <section class="container"> 
   <div class="inner-w"> 
    <figure> 
     <img src="https://www.java2s.com/style/demo/InternetExplorer.png" alt=""> 
    </figure> 
   </div> 
  </section> 
  <section class="container section-name"> 
   <div class="inner-w"> 
    <!-- ... --> 
   </div> 
  </section>  
 </body>
</html>

Related Tutorials