[英]Trying to center a rotated text inside a floated div
我想旋转文本,使其以div为中心。
我正在尝试一种解决方案, 它不会让我在父div上改变任何东西(位置,浮动等),也不会在旋转的div(margin,top)上手动处理像素 。 有没有办法( 最佳实践 )来实现它?
我不介意浏览器兼容性 ,只是在没有修改结构或修复长度的情况下寻找最好的方法
FIDDLE: http : //jsfiddle.net/w5K4j/29/
<div id="parent">
<div id="unknown"></div>
<div id="child">
<h3>Click this overflowing text that I'd like to V/H center when rotated</h3>
</div>
</div>
样式表:
#parent {
height:300px;
width:70px;
float:left;
border: 1px solid;
}
#unknown {
float:right;
height:50px;
width:20px;
background-color: yellow
}
#child {
float:right;
height:100px;
width:70px;
clear:right;
background-color: orange;
/*text-align: center;*/
}
h3 {
/*vertical-align: middle;*/
white-space:nowrap;
border: 1px solid;
}
.rotated {
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
-o-transform: rotate(-90deg);
}
我没有得到您的问题,但我认为您只想将您的标题h3
与中心对齐。
一种方法是使用css属性line-height
#child {
float:right;
height:100px;
width:70px;
clear:right;
background-color: orange;
line-height: 50px;
/*text-align: center;*/
}
你可以根据你的要求改变它。 FIDDLE: http : //jsfiddle.net/w5K4j/31/
小提琴: http : //jsfiddle.net/w5K4j/32/
你想让h3
上的inline-block
使它的盒子覆盖它的长度:
h3 {
display: inline-block;
}
并保持text-align: center
在父级上。
编辑:此外,你需要通过给它一个负的左偏移宽度的一半使文本居中:
$('h3').css('margin-left', -$('h3').width()/2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.