使用浮动设计:右浮动和左浮动。使用案例
我想听听您对我的网页标题设计的建议。
其设计的总体结构如下所示:
它的 HTML 部分:
<div class="header">
<div class="logo1"></div>
<div class="logo2"></div>
</div>
它的 CSS 部分:
.header{
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 160px;
border: 1px solid #48ace1;
}
.logo1{
float: left;
width: 655px;
height: 160px;
background: url(images/logo1.png) no-repeat 0px 2px;
}
.logo2{
float: right;
width: 465px;
height: 160px;
background: url(images/logo2.png) no-repeat 0px 2px;
position: relative; /* it is set to relative because inside this layer I have
several elements with absolute position.*/
}
问题:
当我在宽屏电脑上打开此网页,它打开完美,没有任何问题,但是当我用小宽度屏幕的计算机打开它时,第二个徽标 (.logo2
) 下降到下一行,如下图所示:
发生这种情况是因为 .logo1
和 .logo2
的总宽度为655px+465px=1120px
。因此,一旦浏览器的宽度小于或等于 1200px,第二个徽标 (.logo2
) 就无法在 .header
中找到位置,并且会自动下降到下一行。
在这种情况下,如果浏览器的宽度小于两个徽标的总宽度(.logo1),我希望
.logo2
重叠 .logo1
code> 和 .logo2
,在我的例子中或多或少是 1200px)。我怎样才能达到这种效果?请注意,我需要将 .logo1
对齐到左侧,并将 .logo2
对齐到右侧。
谢谢。
I would like to get your advices about the design of a header of my webpage.
The general structure of its design is shown below:
Its HTML part:
<div class="header">
<div class="logo1"></div>
<div class="logo2"></div>
</div>
Its CSS part:
.header{
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 160px;
border: 1px solid #48ace1;
}
.logo1{
float: left;
width: 655px;
height: 160px;
background: url(images/logo1.png) no-repeat 0px 2px;
}
.logo2{
float: right;
width: 465px;
height: 160px;
background: url(images/logo2.png) no-repeat 0px 2px;
position: relative; /* it is set to relative because inside this layer I have
several elements with absolute position.*/
}
The problem:
When I open this webpage in a computer with a wide screen it opens perfectly, without any problems, but when I open it with a computer with a small-width screen the second logo (.logo2
) falls down to the next line as shown in the following figure:
This happens because totaly the width of .logo1
and .logo2
is 655px+465px=1120px
. So, once the width of the browser is less then 1200px more or less, the second logo (.logo2
) couldn't find place inside the .header
and it automatically falls down to the next line.
In such a cases, I want .logo2
to overlap .logo1
if the width of the browser is less then the total width of two logos (.logo1
and .logo2
, in my case more or less 1200px). How can I achieve this affect? Note, that I need .logo1
to be justified to the left-hand and .logo2
to be justified to the right-hand.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么使用浮动?通过
position:absolute;
使用 CSS 定位就完成了一半! http://jsfiddle.net/6sFY5/1/Why use float? You're halfway to using CSS positioning with that
position:absolute;
! http://jsfiddle.net/6sFY5/1/您可以使用绝对定位:
http://jsfiddle.net/LuRDk/
You can use absolute positioning:
http://jsfiddle.net/LuRDk/
试试这个,
Try this,
为 logo1 添加负边距,如下所示:
这意味着窗口可以变得尽可能窄,并且 logo2 永远不会下降。
已编辑,因为第一次尝试仍然会使徽标 2 的宽度小于 655 像素。
编辑 2: 您还可以在 .logo2 上设置 z-index,以便它覆盖logo1,如果这是你想要的。
Add a negative margin to logo1, like so:
This will mean the window can get as narrow as possible, and logo2 will never drop down.
Edited, since first try would still make logo 2 drop at width less than 655px.
Edit 2: You can also set a z-index on .logo2 so that it will cover logo1, if that is what you want.