2016-05-31 79 views
1

嗨,我一直努力使spanh1元素在同一行。但是float:right将导致跨越顶部。我尝试过不同的方法,我不想使用保证金属性。对此有何评论?浮动右移span元件?

<div class="block"> 
    <h1 class="pull-left">Carl</h1> 
    <span class="pull-right">$4.81</span> 
    <div style='clear:both'></div> 
    </div> 

CSS:

.pull-left{ 
    float:left; 
    width:70%; 
} 
.pull-right:{ 
    float:right; 
    width:20%; 
} 

任何帮助,将不胜感激。

回答

0

您已在<h1>上应用pull-left类,并且所有的html标题默认都有一些浏览器应用的余量。如果您将pull-left应用到<div>那么您将不会看到任何额外空间,但如果您将使用<h1>或其他标题,则需要从标题中删除默认margin

.pull-left { 
 
    float: left; 
 
    width: 70%; 
 
} 
 
h1.pull-left { 
 
    margin: 0; 
 
} 
 
span.pull-right { 
 
    padding-top: 10px; 
 
} 
 
.pull-right { 
 
    float: right; 
 
    width: 20%; 
 
}
<div class="block"> 
 
    <h1 class="pull-left">Carl</h1> 
 
    <span class="pull-right">$4.81</span> 
 
    <div style='clear:both'></div> 
 
    </div>

如果你想要航向和跨度中间对齐,然后更好地使用inline-block如下:

.block h1, 
 
.block span { 
 
    vertical-align: middle; 
 
    display: inline-block; 
 
} 
 

 
.block h1 { 
 
    width: 70%; 
 
    margin: 0; 
 
} 
 

 
.block span { 
 
    width: 20%; 
 
}
<div class="block"> 
 
    <h1>Carl</h1> 
 
    <span>$4.81</span> 
 
</div>

+0

u能解释的答案:) – divakar

+0

不过跨度移动到顶部 – divakar

+0

@divakar我已经更新了我的答案。 –

0

使用内嵌式地显示CSS值:

<h1 class="pull-left" style="display:inline">Carl</h1> 
<span class="pull-right" style="display:inline">$4.81</span> 
+0

测试并放置显示:inline;在CSS类中 –

0

使用line-height可以在同一级别制作不同大小的文本。

.pull-left { 
 
    float: left; 
 
    width: 70%; 
 
    line-height: 30px; 
 
    margin: 0; 
 
} 
 
.pull-right { 
 
    float: right; 
 
    width: 20%; 
 
    line-height: 30px; 
 
}
<div class="block"> 
 
    <h1 class="pull-left">Carl</h1> 
 
    <span class="pull-right">$4.81</span> 
 
    <div style='clear:both'></div> 
 
    </div>

我们在这里做,我们做了两个h1span相同的高度,即使他们有不同的font-size

检查MDN链接:

https://developer.mozilla.org/en/docs/Web/CSS/line-height

0

它只是默认h1标签的是这样做保证金,你需要删除。

如果你想垂直居中基于H1那么就使用的line-height跨度:H1

Fiddle Here

<div class="block"> 
    <h1 class="pull-left">Carl</h1> 
    <span class="pull-right">$4.81</span> 
    <div style='clear:both'></div> 
</div> 

CSS的字体大小如下

.block h1 
{ 
    margin:0px; 
} 
.block span 
{ 
    line-height:2em; 
} 
.pull-left{ 
    float:left; 
    width:70%; 
} 
.pull-right:{ 
    float:right; 
    width:20%; 
} 
0

使用表格&垂直对齐,同样垂直对齐文字

.block{ 
 
display:table; 
 
width:100%; 
 
} 
 
.pull-left{ 
 
    width:70%; 
 
    display:table-cell; 
 
    vertical-align:middle; 
 
} 
 
.pull-right{ 
 
    width:20%; 
 
    display:table-cell; 
 
    vertical-align:middle; 
 
}
<div class="block"> 
 
    <h1 class="pull-left">Carl</h1> 
 
    <span class="pull-right">$4.81</span> 
 
    <div style='clear:both'></div> 
 
    </div>

0

添加这在你的CSS文件的开头:

html, body, div, span, applet, object, iframe, 
 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
 
a, abbr, acronym, address, big, cite, code, 
 
del, dfn, em, img, ins, kbd, q, s, samp, 
 
small, strike, strong, sub, sup, tt, var, 
 
b, u, i, center, 
 
dl, dt, dd, ol, ul, li, 
 
fieldset, form, label, legend, 
 
table, caption, tbody, tfoot, thead, tr, th, td, 
 
article, aside, canvas, details, embed, 
 
figure, figcaption, footer, header, hgroup, 
 
menu, nav, output, ruby, section, summary, 
 
time, mark, audio, video { 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t border: 0; 
 
\t font-size: 100%; 
 
\t font: inherit; 
 
\t vertical-align: baseline; 
 
} 
 
/* HTML5 display-role reset for older browsers */ 
 
article, aside, details, figcaption, figure, 
 
footer, header, hgroup, menu, nav, section { 
 
\t display: block; 
 
} 
 
body { 
 
\t line-height: 1; 
 
} 
 
ol, ul { 
 
\t list-style: none; 
 
} 
 
blockquote, q { 
 
\t quotes: none; 
 
} 
 
blockquote:before, blockquote:after, 
 
q:before, q:after { 
 
\t content: ''; 
 
\t content: none; 
 
} 
 
table { 
 
\t border-collapse: collapse; 
 
\t border-spacing: 0; 
 
}