.parent {
width: 80vw;
height: 200px;
background-color: red;
margin: 0 auto;
text-align: center;
}
.child {
width: 100vw;
height: 100px;
background-color: blue;
}
<div class="parent">
<div class="child"></div>
</div>
当父div
宽度小于div
时,如何将孩子child
的中心对齐?如果父div
的宽度为100vw
,设置宽度为div
时,如何设置子80vw
?
答案 0 :(得分:3)
如果您正在尝试这样做,请将父级居中,然后使用相对位置和左侧负值使孩子居中。
app.controller("loginCtrl", function($scope, $window){
$scope.submit = function(){
$window.localStorage.setItem = ("username", $scope.username);
};
});
app.controller("homeCtrl", function($scope, $window){
$scope.logout = function(){
$window.localStorage.getItem = ("username");
};
});
&#13;
.parent {
width: 200px;
height: 200px;
background-color: red;
margin: 0 auto; /* center the parent in the screen */
}
.child {
position: relative;
width: 100vw;
height: 100px;
background-color: blue;
/* center the child using a negative left */
/* left: calc(50% - 50vw); */
/* or */
left: 50%;
transform: translateX(-50%);
}
body {
margin: 0;
}
&#13;
答案 1 :(得分:0)
只需在子元素中设置margin-left: -10vw;
。
.parent {
width: 80vw;
height: 200px;
background-color: red;
margin:auto;
}
.child {
width: 100vw;
height: 100px;
background-color: blue;
margin-left: -10vw;
}
&#13;
<div class="parent">
<div class="child"></div>
</div>
&#13;