使用浮动设计:右浮动和左浮动。使用案例

发布于 2024-12-04 06:59:30 字数 1571 浏览 1 评论 0原文

我想听听您对我的网页标题设计的建议。

其设计的总体结构如下所示:

网站标题

它的 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:

Header of the website

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:

Header with a browser with a small width

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九命猫 2024-12-11 06:59:30

为什么使用浮动?通过 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/

一梦浮鱼 2024-12-11 06:59:30

您可以使用绝对定位:

http://jsfiddle.net/LuRDk/

.header{
    position:relative;
    height:84px;
    padding:20px;
    border:1px solid green;
}

.logo{
    position:absolute;
    width:80px;
    height: 80px;
    border:1px solid red;
}

.right{right:20px;}
.left{left:20px;}

<div class="header">
    <div class="logo left"></div>
    <div class="logo right"></div>
</div>

You can use absolute positioning:

http://jsfiddle.net/LuRDk/

.header{
    position:relative;
    height:84px;
    padding:20px;
    border:1px solid green;
}

.logo{
    position:absolute;
    width:80px;
    height: 80px;
    border:1px solid red;
}

.right{right:20px;}
.left{left:20px;}

<div class="header">
    <div class="logo left"></div>
    <div class="logo right"></div>
</div>
尽揽少女心 2024-12-11 06:59:30

试试这个,

<div class="header">
   <div class="logo1"></div>
   <div class="logo2"></div>
   <div style="clear:both"></div>
</div>

Try this,

<div class="header">
   <div class="logo1"></div>
   <div class="logo2"></div>
   <div style="clear:both"></div>
</div>
一瞬间的火花 2024-12-11 06:59:30

为 logo1 添加负边距,如下所示:

.logo1
{
    float: left;
    width: 655px;
    margin-right: -655px;
    height: 160px;
    background: url(images/logo1.png) no-repeat 0px 2px;  
}

这意味着窗口可以变得尽可能窄,并且 logo2 永远不会下降。

已编辑,因为第一次尝试仍然会使徽标 2 的宽度小于 655 像素。

编辑 2: 您还可以在 .logo2 上设置 z-index,以便它覆盖logo1,如果这是你想要的。

Add a negative margin to logo1, like so:

.logo1
{
    float: left;
    width: 655px;
    margin-right: -655px;
    height: 160px;
    background: url(images/logo1.png) no-repeat 0px 2px;  
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文