CSS Layout How to - Center element next to a float








Question

We would like to know how to center element next to a float.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#container {<!--  w w  w . j a  va2 s .c om-->
  width: 175px;
  text-align: center;
  position: relative;
  border: 1px solid black;
  padding: 5px;
  margin: 5px;
}

#left {
  position: absolute;
  border: 1px solid black;
  padding: 5px;
  margin: 5px;
}

#centre {
  display: inline-block;
  border: 1px solid black;
  padding: 5px;
  margin: 5px;
}
</style>
</head>
<body>
  <div id="container">
    <div id="left">Left</div>
    <div id="centre">Centred</div>
  </div>
</body>
</html>

The code above is rendered as follows: