【问题标题】:Fixed bars with vertical text带有垂直文本的固定条
【发布时间】:2014-01-03 16:29:39
【问题描述】:

所以,我遇到了一个我想不通的有趣问题。

我正在尝试制作一个垂直条菜单,所有窗口的高度均为 100%。他们有一个固定的位置。我希望文本是垂直的。请检查我制作的草图:

还有这个JSFiddle

HTML 结构如下所示:

<header>
    header
</header>

<nav>
    <ul>
        <li><a href="#">bar 1</a></li>
        <li><a href="#">bar 2</a></li>
        <li><a href="#">bar 3</a></li>
        <li><a href="#">bar 4</a></li>
    </ul>
</nav>

还有这样的 CSS:

header {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: 250px;
    padding: 20px;
    background: rgb(42,42,42);
    background: rgba(10,10,10,0.95);
}
nav {
    position: fixed;
    left: 300px;
    width: 100%;
    margin-left: -50%;
    top: 50%;
    height: 75px;

    -webkit-transform: rotate(-90deg);
    -moz-transform: rotate(-90deg);
    -ms-transform: rotate(-90deg);
    -o-transform: rotate(-90deg);
    transform: rotate(-90deg);

    -webkit-transform-origin: 50% 50%;
    -moz-transform-origin: 50% 50%;
    -ms-transform-origin: 50% 50%;
    -o-transform-origin: 50% 50%;
    transform-origin: 50% 50%;

    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
    background: blue;
    border: 2px solid red;
}
nav ul {
    margin: 0;
    padding: 0;
    list-style: none;
    width: 100%;
}
nav ul li {
    width: 100%;
}
nav ul li a {
    display: block;
    width: 100%;
    height: 32px;
    background: #f3b200;
}
nav ul li:nth-child(1) a { background: #c61c05; }
nav ul li:nth-child(2) a { background: #dc572e; }
nav ul li:nth-child(3) a { background: #d27b26; }
nav ul li:nth-child(4) a { background: #f3b200; }

现在的问题是我不知道如何像我想要的那样定位nav。我给出的每个测量值(宽度、高度、顶部位置、左侧位置)都是基于 0 度旋转给出的。之后进行旋转。

所以高度不是视口高度的 100%,而是视口宽度的 100%。

我还尝试将ul 转入nav,并将li 转入其中,但无法找出放置此nav 条的正确方法。

我正在寻找纯 CSS 解决方案。

【问题讨论】:

  • 如果你尝试这样的事情会怎样:jsfiddle.net/aYTWG/3。这只会旋转链接中的文本,而是以父级的高度而不是宽度为目标。
  • @oGeez,谢谢。将direction: rtlwhite-space: no-wrap 添加到span 后,我得到了它的完美运行。请将其添加为答案,以便我接受。

标签: css css-position css-transforms


【解决方案1】:

如果我的想法是正确的,您希望所有内容都达到页面高度的 100%,请使用页面流而不是更改 position。将导航浮动到标题旁边等等。然后,您可以在链接中旋转一些 &lt;span&gt;s 而不是整个内容。这是一个改动。剩下的就交给你了:

HTML

<header>header</header>
<ul>
    <li><a href="#"><span>bar 1</span></a></li>
    <li><a href="#"><span>bar 2</span></a></li>
    <li><a href="#"><span>bar 3</span></a></li>
    <li><a href="#"><span>bar 4</span></a></li>
</ul>
<div class="other">Loads of other content</div>

CSS

html, body {
    margin:0;
    padding:0;
    height:100%;
}
header {
    float:left;
    height:100%;
    width: 250px;
    background: rgb(42, 42, 42);
    background: rgba(10, 10, 10, 0.95);
}
ul{
    float:left;
    display:block;
    margin:0;
    padding:0;
    height:100%;
}
ul li{
    display:block;
    margin:0;
    padding:0;
    float:left;
    height:100%;
}

ul li a{
    display:block;
    height:100%;
}

ul li a span{
    position:relative;
    top:20px;
    color:#FFF;
    display:block;
    -webkit-transform: rotate(-90deg);
    -moz-transform: rotate(-90deg);
    -ms-transform: rotate(-90deg);
    -o-transform: rotate(-90deg);
    transform: rotate(-90deg);
    font-size:26px;
    width:60px;
    direction: rtl;
    white-space: nowrap;
}

JSFiddle

【讨论】:

    【解决方案2】:

    这看起来怎么样?

    html:

    <div class="mainWrap">
        <header class="mainBody">
    
        </header>
        <nav>
            <ul>
                <li class="sBar s-1"><span class="rot">Bar 1</span></li>
                <li class="sBar s-2"><span class="rot">Bar 2</span></li>
                <li class="sBar s-3"><span class="rot">Bar 3</span></li>
                <li class="sBar s-4"><span class="rot">Bar 4</span></li>
            </ul>
        </nav>
    </div>
    

    css:

    html, body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }
    
    body {
        border: 4px solid red;
    }
    
    * {
        box-sizing: border-box;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
    }
    
    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
    }
    
    .mainWrap, .mainWrap nav, .mainWrap ul {
        width: 100%;
        height: 100%;
    }
    
    .mainWrap li, .mainWrap header {
        float: left;
    }
    
    .mainBody {
        width: 40%;
        height: 100%;
        background: #2b2b2b;
    }
    
    .sBar {
        width: 50px;
        height: 100%;
        padding-top: 30px;
        display: block;
    }
    
    .s-1 {
        background: #c61c05;
    }
    
    .s-2 {
        background: #dc572e;
    }
    
    .s-3 {
        background: #d27b26;
    }
    
    .s-4 {
        background: #f3b200;
    }
    
    .rot {
        -webkit-transform: rotate(-90deg);
        display: block;
    }
    

    小提琴:Remove "show" in url to view and alter code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-11
      • 2011-05-29
      • 2015-04-13
      • 2013-09-22
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多