我正在尝试创建一个具有垂直标题的可隐藏侧边栏。
当侧边栏被隐藏时,只有它的标题应该是可见的。
一般来说,下面的代码可以工作,但我不知道如何在sidebar_header元素中居中sidebar_title。
对于横排文本,我会在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>
发布于 2019-05-09 23:21:10
只需将此CSS应用于sidebar_header。
display: flex;
align-items: center;发布于 2019-05-09 23:23:17
一种方法是将父潜水设置为
display:table然后让你的标题div
display: table-cell;
vertical-align: middle;但也有其他方式,我希望这个链接是有帮助的:vertical aligning text
发布于 2019-05-09 23:29:12
我建议你去看看writing-mode,它会让你的生活变得简单得多。
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(-180deg);
writing-mode: tb;
height:100%;
}
#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>
https://stackoverflow.com/questions/56062598
复制相似问题