Centering a variable-width div positioned with left: x% - HTML CSS CSS Layout

HTML CSS examples for CSS Layout:Position

Description

Centering a variable-width div positioned with left: x%

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">
.x {<!--from  w w w . j  a  v a  2  s. c o  m-->
   width:301px;
   font-size:17px;
   line-height:21px;
}

.y {
   display:inline-block;
   background-color:Chartreuse;
   padding:0 4px;
}

.a {
   height:6px;
   background-color:yellow;
}

.b
 {
   height:100%;
   background-color:blue;
   position:relative;
}

.c
 {
   position:absolute;
   width:201%;
   text-align:center;
   bottom:100%;
}

body {
   padding:51px;
}
</style> 
 </head> 
 <body> 
  <div class="x"> 
   <div class="a"> 
    <div class="b" style="width:12%;"> 
     <div class="c"> 
      <div class="y">
        Lorem i 
      </div> 
     </div> 
    </div> 
   </div> 
  </div> 
  <br> 
  <br> 
  <div class="x"> 
   <div class="a"> 
    <div class="b" style="width:19%;"> 
     <div class="c"> 
      <div class="y">
        Lorem i 
      </div> 
     </div> 
    </div> 
   </div> 
  </div>  
 </body>
</html>

Related Tutorials