父元素宽度100px居中两边对齐子元素如何定位到屏幕最左侧
可以使用绝对定位和transform属性来实现。具体步骤如下:
-
父元素设置为相对定位,宽度为100px,左右对齐居中。
-
子元素设置为绝对定位,left属性设置为负的父元素宽度的一半,即left: -50px。
-
子元素使用transform属性将自身向左平移50px,即transform: translateX(-50px)。
代码示例:
<div class="parent">
<div class="child">子元素</div>
</div>
.parent {
position: relative;
width: 100px;
margin: 0 auto;
text-align: center;
}
.child {
position: absolute;
left: -50px;
transform: translateX(-50px);
}
原文地址: https://www.cveoy.top/t/topic/bhXc 著作权归作者所有。请勿转载和采集!