Maison >interface Web >tutoriel CSS >Pourquoi ma propriété CSS « gauche » ne centre-t-elle pas mon div enfant ?
In a scenario where you encounter difficulties aligning the leftmost boundary of a child div element within the center of its parent, this issue arises due to the incorrect implementation of CSS positioning rules. Specifically, the "left" property fails to take effect, leaving you wondering why your code isn't working.
The primary reason for this failure lies in the omission of position attributes for the div elements. By default, HTML elements reside in a static position, rendering the "left" property inoperable.
To rectify this issue and achieve the desired alignment, you need to switch the position attribute of the child div element to either absolute or relative. Here's an updated CSS code that will resolve the problem:
#inner { width: 400px; height: 300px; background-color: #090; position: absolute; left: 50%; }
By adding "position: absolute," you grant the "left" property autonomy and enable it to effectively center the child div within its parent div.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!