Home >Web Front-end >CSS Tutorial >Why Isn't My CSS 'left' Property Centering My Child Div?
CSS "left" Property Not Functioning?
When attempting to center the left border of a child div within its parent div using the "left: 50%" property, the desired effect may not be achieved. This can be attributed to the default positioning of HTML elements, which is "static." In this default state, the "left" property has no effect.
Solution: Absolute or Relative Positioning
To enable the "left" property to position the child div correctly, you need to set its position property to either "absolute" or "relative." This informs the browser that the child div should be positioned relative to its containing element rather than the entire document.
Updated CSS:
#inner { width: 400px; height: 300px; background-color: #090; position: absolute; left: 50%; }
By setting the position property to "absolute," the child div becomes positioned relative to its parent, and the "left" property of 50% positions its left border exactly halfway within the parent div.
The above is the detailed content of Why Isn't My CSS 'left' Property Centering My Child Div?. For more information, please follow other related articles on the PHP Chinese website!