I am trying to achieve a vertical navigation menu which has links and I have rotated the links text to 270 degree with css3. I have rotated it because I wanted the text to be bottom to top. The problem is when I add a padding top it is getting inconsistent spacing. you can see my code here. I am unable to understand the different space being taken. I also tried giving li a height but it did not work. Please if someone could help me out. Here is my code:
HTML:
<div class="main-nav">
<ul class="nav">
<li><a href="#">HOME</a></li>
<li><a href="#">METHODOLGY</a></li>
<li><a href="#">PORTFOLIO</a></li>
<li><a href="#">SERVICES</a></li>
<li><a href="#">TEAM</a></li>
<li><a href="#">CONTACT</a></li>
</ul>
</div>
CSS:
.rotate{
-webkit-transform:rotate(270deg);-moz-transform:rotate(270deg);-o-transform:rotate(270deg);writing-mode:lr-tb}
html, body {
min-height:100%;
max-height:100%;
height:100%;
font-family:Oswald, sans-serif, Arial;
font-size:14px;
background:#fff
}
a {
text-decoration:none
}
li {
list-style:none
}
ul {
padding:0;
margin:0
}
.main-nav {
width:45px;
float:left;
height:100%;
position:fixed;
background:#4c4c4c
}
ul.nav {
width:21px;
margin:0 auto
}
ul.nav li:first-child {
padding-top:35px
}
ul.nav li {
padding-top:124px
}
ul.nav li a {
-webkit-transform:rotate(270deg);
-moz-transform:rotate(270deg);
-o-transform:rotate(270deg);
writing-mode:lr-tb;
float:left;
width:21px;
color:#fff
}
Rotating each
a
element orli
element will make us encounter some problem spacing, positioning the elements exactly as we want. We should build the navigation menu so that it spreads horizontally, looks OK first then we just to need rotate the whole container of the navigation menu. Here is the updated code:The menu is supposed to have fixed height of
45px
(after rotated, it will be the width). We useline-height:45px
to center thea
element vertically (after rotated, it will be horizontally). At first the.main-nav
will lie horizontally like this:we need to rotate it
-90deg
(which is counter-clockwise) around the pointleft - top
specified bytransform-origin: left top
. After rotated, all the.main-nav
will be out of view like this:So we need to translate it down a distance of
100% of width
, however note that we don't usetranslateY
which seems to mean translate it vertically, because after rotated, the X axis becomes vertical (not horizontal as before), so we have to usetranslateX(-100%)
(the positive direction is upwards, it's rightwards before rotated). Then we have:It's just a simple use case related to transform in CSS3. For the
vh
unit, it's the unit relative to the viewport's height.100vh
means100% of viewport's height
. We have to use100vh
for thewidth
because after rotated,width
becomesheight
. It should fill the full height of the viewport. However you can set somemin-width
for thewidth
by px to limit thewidth
's minimum value. It's because when you resize the window, the viewport's height may become small and hence thewidth
will be shrunk accordingly. Also note that instead of usingfloat:left
for theli
elements, we have to usefloat:right
so that the Home menu appears first from top to bottom, otherwise (usingfloat:left
), the Home menu will appear at the end (at bottom). There is a little advanced usage of thetransform
here (to newbie) it we use more than 1 transform for atransform
property, all the transforms are separated by space and the order of transforms is important. Such asrotate(-90deg) translateX(-100%)
means rotating-90deg
first, then translating along the X axis-100%
, whiletranslateX(-100%) rotate(-90deg)
is reverse to that, it's a totally different thing and of course won't work (makes an unexpected result).Jsbin Demo.