简体   繁体   English

使用CSS垂直对齐具有不同字体大小的两段文本

[英]Vertically align two pieces of text with different font-sizes using CSS

At the top of my page I want to have the title of the page aligned to the left of the screen with a short nav menu aligned to the right of the screen. 在我的页面顶部,我希望页面标题与屏幕左侧对齐,并在屏幕右侧对齐一个简短的导航菜单。 I can achieve this using float but the two elements have different baslines ie the baseline of the text appears different. 我可以使用float来实现这个,但是这两个元素有不同的基线,即文本的基线看起来不同。 Is there any way to get this to work using css? 有没有办法让这个使用CSS工作? I have a sample of what I'm trying to do up on jsfiddle http://jsfiddle.net/nBPCG/63/ 我有一个关于我想在jsfiddle http://jsfiddle.net/nBPCG/63/做什么的样本

Hi you can use display:inline-block in your h1 您好,您可以在您的h1中使用display:inline-block

or see the Fiddle:- http://jsfiddle.net/nBPCG/101/ 或者看小提琴: - http://jsfiddle.net/nBPCG/101/

First I'd suggest using a ul to wrap the links rather than an h3 , that structure doesn't make sense. 首先,我建议使用ul来包装链接而不是h3 ,这种结构没有意义。 Then I'd just add some padding to the ul . 然后我只是在ul添加一些填充。 Here's a cleaned up example of the markup: 这是一个清理过的标记示例:

<article >
<header>
    <h1>This is Title</h1>
    <nav>
        <ul>
            <li><a href="">Home</a></li>
            <li><a href="">Works</a></li>
            <li><a href="">Blog</a></li>
            <li><a href="">Contact</a></li>  
        </ul>
    </nav>
    <div class="clr"></div>
</header>
</article>

And the styles: 和风格:

body {
    font-family:"Verdana", Verdana, sans-serif;
    font-size: 1em;
    font-weight:400;
}

h1 {
    font-family:"Century Gothic", Verdana, sans-serif;
    font-size: 4em;
    font-weight:400;
    float: left;
    margin-left:10px;
}

header nav {
    margin-right: 10px;
    float: right;
}

nav ul {
    list-style-type: none;
    margin: 0;
    padding: 2em 0 0 0;
}

nav ul li {
    display: inline;
    font-size: 1.2em;
    font-weight:400;
}

nav a {
    padding: 0 1em;
    border-right: 1px solid #000;
}

nav li:last-child a {
    padding-right: 0;
    border-right: none;
}

.clr {clear:both;}

Fiddle: http://jsfiddle.net/nBPCG/98/ 小提琴: http//jsfiddle.net/nBPCG/98/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM