首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在div中居中竖排文本?

问在div中居中竖排文本?
EN

Stack Overflow用户
提问于 2019-05-09 23:15:56
回答 4查看 65关注 0票数 0

我正在尝试创建一个具有垂直标题的可隐藏侧边栏。

当侧边栏被隐藏时,只有它的标题应该是可见的。

一般来说,下面的代码可以工作,但我不知道如何在sidebar_header元素中居中sidebar_title。

对于横排文本,我会在sidebar_header上使用text-align: center,但这在这里似乎不起作用。

有人能给点建议吗?

​

代码语言:javascript
复制
function main() {
            
                let hide_button = document.getElementById('hide_sidebar'),
                     unhide_button = document.getElementById('unhide_sidebar')
                     sidebar = document.getElementById('sidebar')
                     sidebar_contents = document.getElementById('sidebar_contents')
                     sidebar_header = document.getElementById('sidebar_header')
                
                hide_button.addEventListener('click', hide_sidebar)
                unhide_button.addEventListener('click', unhide_sidebar)
                
                function hide_sidebar() {
                    sidebar_contents.style.display = 'none'  
                    sidebar.style.width = sidebar_header.offsetWidth + "px"
                    sidebar_header.style.width = "100%"
                }
                
                function unhide_sidebar() {
                    sidebar_contents.style.display = 'block'
                    sidebar.style.width = "20%"
                    sidebar_header.style.width = "30%"
                }
                
            }
            
            main()
代码语言:javascript
复制
        #horizontal_panel {
            height: 300px;
            background-color: powderblue;
        }
        #sidebar {
            width: 20%;
            height: 100%;
            background-color: green;
            float: right;
        }
        #sidebar_header {
            background-color: red;
            width: 30%;
            height: 100%;
            float: left;
            text-align: center;
         }
        #sidebar_title {
            transform: rotate(-90deg);
            //margin-top: 400%;
        }
        #sidebar_contents {
            background-color: yellow;
            width: 70%;
            height: 100%;
            float: left;
        }
代码语言:javascript
复制
<!DOCTYPE HTML>
<html>
    <head>
    </head>
    <body>
        <p>
            <button id="hide_sidebar">Hide sidebar</button>
            <button id="unhide_sidebar">Unhide sidebar</button>
        </p>
        <div id="horizontal_panel">
            <div id="sidebar">
                <div id="sidebar_header">
                    <div id="sidebar_title">sidebar title</div>
                </div>
                <div id="sidebar_contents">sidebar contents</div>
            </div>
        </div>
    </body>
</html>

​

EN

回答 4

Stack Overflow用户

发布于 2019-05-09 23:21:10

只需将此CSS应用于sidebar_header。

代码语言:javascript
复制
display: flex;
align-items: center;
票数 1
EN

Stack Overflow用户

发布于 2019-05-09 23:23:17

一种方法是将父潜水设置为

代码语言:javascript
复制
display:table

然后让你的标题div

代码语言:javascript
复制
display: table-cell;
   vertical-align: middle;

但也有其他方式,我希望这个链接是有帮助的:vertical aligning text

票数 0
EN

Stack Overflow用户

发布于 2019-05-09 23:29:12

我建议你去看看writing-mode,它会让你的生活变得简单得多。

​

代码语言:javascript
复制
function main() {

  let hide_button = document.getElementById('hide_sidebar'),
    unhide_button = document.getElementById('unhide_sidebar')
  sidebar = document.getElementById('sidebar')
  sidebar_contents = document.getElementById('sidebar_contents')
  sidebar_header = document.getElementById('sidebar_header')

  hide_button.addEventListener('click', hide_sidebar)
  unhide_button.addEventListener('click', unhide_sidebar)

  function hide_sidebar() {
    sidebar_contents.style.display = 'none'
    sidebar.style.width = sidebar_header.offsetWidth + "px"
    sidebar_header.style.width = "100%"
  }

  function unhide_sidebar() {
    sidebar_contents.style.display = 'block'
    sidebar.style.width = "20%"
    sidebar_header.style.width = "30%"
  }

}

main()
代码语言:javascript
复制
#horizontal_panel {
  height: 300px;
  background-color: powderblue;
}

#sidebar {
  width: 20%;
  height: 100%;
  background-color: green;
  float: right;
}

#sidebar_header {
  background-color: red;
  width: 30%;
  height: 100%;
  float: left;
  text-align: center;
}

#sidebar_title {
  transform: rotate(-180deg);
  writing-mode: tb;
  height:100%;
}

#sidebar_contents {
  background-color: yellow;
  width: 70%;
  height: 100%;
  float: left;
}
代码语言:javascript
复制
<!DOCTYPE HTML>
<html>

<head>
</head>

<body>
  <p>
    <button id="hide_sidebar">Hide sidebar</button>
    <button id="unhide_sidebar">Unhide sidebar</button>
  </p>
  <div id="horizontal_panel">
    <div id="sidebar">
      <div id="sidebar_header">
        <div id="sidebar_title">sidebar title</div>
      </div>
      <div id="sidebar_contents">sidebar contents</div>
    </div>
  </div>
</body>

</html>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56062598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档