CSS - responsively center div, position fixed - HTML CSS CSS Layout

HTML CSS examples for CSS Layout:Fixed Position

Description

CSS - responsively center div, position fixed

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">
.block {<!--   w  w w.  ja  va  2s.co m-->
   background:red;
   height:100px;
   position:fixed;
   top:51px;
   left:51%;
   margin-left:-201px;
   width:401px;
}

@media only screen and (max-width: 600px) {
   .block {
      position:fixed;
      top:51px;
      margin-left:21px;
      margin-right:21px;
      left:0;
      width:calc(100% - 41px);
   }

}
</style> 
 </head> 
 <body> 
  <div class="block"></div>  
 </body>
</html>

Related Tutorials