jquery - 尝试将旋转文本置于 float div 中

标签 jquery html css

我想旋转文本,使其以 div 为中心。

我正在尝试一种解决方案,它不会让我更改父 div 上的任何内容(位置、 float 等),也不会手动处理旋转 div(边距、顶部)上的像素。有什么方法(最佳实践)可以实现吗?

我不介意浏览器兼容性,只是寻找最好的方式来做到这一点而不修改结构或固定长度

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);
}

最佳答案

fiddle :http://jsfiddle.net/w5K4j/32/

您希望 h3 上的 inline-block 使其框覆盖其长度:

h3 {
    display: inline-block;
}

并在父级上保持 text-align: center

编辑:此外,您需要通过将文本向左偏移其宽度的一半来使文本居中:

$('h3').css('margin-left', -$('h3').width()/2)

关于jquery - 尝试将旋转文本置于 float div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18161690/

相关文章:

jquery - 使用给定 id 的详细信息填充元素

MVC 问题中的 JQuery Slider

php - 如何制作缩放工具来像谷歌地图一样缩放图像?

php - 如何使用 php 和 html 将 csv 表导入 mysql 数据库

javascript - 如何选择不包含 anchor 的元素?

javascript - Html 页面中选定的 div 突出显示

javascript - 为 jQuery fadeIn 函数添加延迟

javascript - 在 Bootstrap 弹出窗口中包含表单?

jquery - 移动浏览器错误地将数字呈现为电话

html - 同时从多个页面中删除未使用的 css(整个网站)