css或javascritp 居中一个float或一个块

CSS代码:

[cce_xml]
<style type="text/css">
    .con {
        position: relative;
        float: left;
        left: 50%;
        background: lightblue;
    }
    .box {
        position: relative;
        float: left;
        left: -50%;
        width: 100px;
        height: 100px;
        background: lightcoral;
    }
</style>
<body>
    <div class="con">
        <div class="box"></div>  
    </div>
</body>
[/cce_xml]

javascript代码:

[cce]
<body>
    <div id="con" class="box"></div>  
    <script type="text/javascript">
        var con = document.getElementById('con');
        con.style.position = 'relative';
        con.style.left = (document.body.offsetWidth - con.clientWidth) / 2 + 'px';
    </script>
</body>
[/cce]

jq代码:

[cce]
<body>
    <div class="box"></div>
    <script type="text/javascript" src="../js/jquery-1.12.3.js"></script>
    <script type="text/javascript">
         $('.box').css({
              'position' : 'relative',
              'left' : ($(window).width() - $('.box').outerWidth()) / 2 + 'px'
          })
     </script>
</body>
[/cce]