The thing of floating object is Archimedes law.
Archimedes' principle states that the upward buoyant force that is exerted on a body immersed in a fluid, whether fully or partially submerged, is equal to the weight of the fluid that the body displaces and acts in the upward direction at the center of mass of the displaced fluid. Archimedes' principle is a law of physics fundamental to fluid mechanics. It was formulated by Archimedes of Syracuse.This is the principle explaining the motion of an object immersed in a fluid.
But it only explain upward motion.
Basically if the weight of the volume of fluid equivalent to the volume of the immersed object is superior to the weight of the object, the object will float.
Aka it floats if the upward force equivalent to the weight of the volume of fluid occupied by the object is greater than gravity force pulling it downward.
If not it will sink due to gravity.
The density difference only explain upward motion. And even the upward motion is related to the notion of pressure differential , which wouldnt happen without gravity.
https://en.m.wikipedia.org/wiki/Archimedes%27_principle#FormulaConsider a cube immersed in a fluid, with its sides parallel to the direction of gravity. The fluid will exert a normal force on each face, and therefore only the forces on the top and bottom faces will contribute to buoyancy. The pressure difference between the bottom and the top face is directly proportional to the height (difference in depth). Multiplying the pressure difference by the area of a face gives the net force on the cube – the buoyancy, or the weight of the fluid displaced. By extending this reasoning to irregular shapes, we can see that, whatever the shape of the submerged body, the buoyant force is equal to the weight of the fluid displaced.
The weight of the displaced fluid is directly proportional to the volume of the displaced fluid (if the surrounding fluid is of uniform density). The weight of the object in the fluid is reduced, because of the force acting on it, which is called upthrust. In simple terms, the principle states that the buoyant force on an object is equal to the weight of the fluid displaced by the object, or the density of the fluid multiplied by the submerged volume times the gravitational constant, g. Thus, among completely submerged objects with equal masses, objects with greater volume have greater buoyancy.Without gravity, there is no pressure, no weight, no density, no upthrust force, no Archimedes, all stay in place.