I am trying to create a hideable sidebar with a vertical title.
When the sidebar is hidden, only its title should be visible.
In general, the code below works, but I don't know how to center sidebar_title
inside sidebar_header
element.
With horizontal text, I would use text-align: center
on sidebar_header
, but this does not seem to work here.
Can anyone advise?
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>