gpt4 book ai didi

css - 在 div 中居中垂直文本?

转载 作者:太空宇宙 更新时间:2023-11-04 06:15:00 24 4
gpt4 key购买 nike

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

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

通常,下面的代码可以工作,但我不知道如何将 sidebar_title 放在 sidebar_header 元素的中心。

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

谁能给个建议?

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()
        #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;
}
<!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>

最佳答案

只需将此 CSS 应用于 sidebar_header

display: flex;
align-items: center;

关于css - 在 div 中居中垂直文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56062598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com