![](/img/trans.png)
[英]How to center a rotated child div to a rotated parent div and stick to the top of parent div?
[英]How to make the parent div adjust in size to contain the CSS-Rotated child div?
標題說明了一切:“如何使父div調整大小以包含CSS-Rotated子div?”
這就是我目前得到的:
HTML:
<div id="container">
<div id="rotator">
<h1>TEXT</h1>
</div>
</div>
CSS:
#container {
display: inline-block;
border: 1px solid gray;
padding: 10px;
overflow: hidden;
}
#rotator {
display: inline-block;
width: 200px;
height: 500px;
background-color: rgb(130, 310, 130);
border: 1px solid blue;
text-align: center;
line-height: 500px;
-moz-transform:rotate(90deg);
-webkit-transform: rotate(90deg);
-o-transform:rotate(90deg);
-ms-transform:rotate(90deg);
transform:rotate(90deg);
}
那么,無論如何要讓父母與孩子一起調整大小? 僅CSS解決方案是最好的。 如果這是不可能的,那么最好的javascript方式是什么? 應該支持IE9
根據@ misterManSam的建議,我更新了實例以顯示解決方案。 在這里找到
旋轉時更改div.container
的寬度。 在CSS中為#rotator.deg90
添加負邊距和左邊距。
使用Javascript
function to90() {
var elm = document.getElementById("rotator");
console.log(elm);
elm.className = "deg90";
//add these lines
document.getElementById('container').style.width = "500px";
document.getElementById('container').style.height = "200px";
}
CSS
#rotator.deg90 {
/*add this*/
margin-top: -150px;
margin-left: 200px;
-moz-transform:rotate(90deg);
-webkit-transform: rotate(90deg);
-o-transform:rotate(90deg);
-ms-transform:rotate(90deg);
transform:rotate(90deg);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.