【问题标题】:layout is not setting. How to place divs on top of main div布局未设置。如何将 div 放在主 div 之上
【发布时间】:2023-03-05 12:14:01
【问题描述】:

我想设置类似的布局

我认为我必须设置这样的东西

表示主 div,在主 div 的顶部,我有其他 div。我用过这样的东西

<div id="headerImage" >

        <div id="help_About">               
            <a href="#">Help</a>
            <a href="#">About</a>                                           
        </div>

        <div id="myLogout">               
            <input type="text" value="Basit" />
            <a href="#">Logout</a>                                                          
        </div>

        <div id="myImage">
            <img width="20" height="20" src="resources/images/tools_icon.png" /> 
        </div>

        <img src="resources/images/2b.jpg"/>

    </div>

    #headerImage{ 
        border-style :solid;
        border-width: thin;
        width: 100%;
        height: 20%;           
        background: url(${imgUrl}/2b.jpg) no-repeat top left;            
        padding: 0px;
        border-top: 0px;
        border-bottom: 5px #eeeeee solid;
        z-index: 1;
    }

    #headerImage #help_About{ 
        margin-left: 10%
        margin-top: 2%;
        display: inline-block;
        border-style :solid;
        border-width: thin;                                 
        padding: 2px;
        border-top: 2px;
        z-index: 2;
    }

    #headerImage #myLogout{ 
        margin-left: 30%
        margin-top: 4%;
        display: inline-block;
        border-style :solid;
        border-width: thin;                                 
        padding: 2px;
        border-top: 2px;
        z-index: 2;
    }

    #headerImage #myImage{ 
        margin-right: 10%
        margin-top: 4%;
        display: inline-block;
        border-style :solid;
        border-width: thin;                                 
        padding: 2px;
        border-top: 2px;
        z-index: 2;
    }

我只是给出了物理外观的边框样式和宽度和高度。但后来我得到了这样的东西

我做错了什么?如何设置 div 的布局?

谢谢

【问题讨论】:

    标签: html css


    【解决方案1】:

    试试这个 - http://jsfiddle.net/SQwFK/

    HTML

    <header>
        <div id="left"></div>
    
        <div id="rightTop"></div>
        <div id="rightBottom"></div>
    </header>
    

    CSS

        header {
        background: url(http://lorempixel.com/600/300);
        height: 300px;
        width: 600px;
        position: relative;
    }
    
    #left {
        position: absolute;
        top: 100px;
        left: 50px;
        background: #ffe;
        height: 100px;
        width: 250px;
    }
    
    #rightTop, #rightBottom {
        position: absolute;
        right: 50px;
        top: 25px;
        background: #fef;
        height: 100px;
        width: 100px;
    }
    
    #rightBottom {
        top: 175px;
    }
    

    新编辑 -------------------------------------------------- --------------------------

    工作

    <div id="headerImage" >
    ....
    </div>
    <div id="menuBodyContainer" >
        <div id="menu">
        </div>
        <div id="myBody">
        </div>  
    </div>
    

    现在我尝试使用您的代码,例如

      #headerImage{ 
            position: absolute;
            border-style :solid;
            border-width: thin;
            width: 100%;
            height: 20%;           
            background: url(${imgUrl}/2b.jpg) no-repeat top left;            
            padding: 0px;
            border-top: 0px;
            border-bottom: 5px #eeeeee solid;
            z-index: 1;            
        }
    
        #headerImage #help_About{ 
            position: absolute;
            display: inline-block;
            top: 4%;
            left: 4%;
            opacity:0.4;
            z-index: 2;            
        }
    
        #headerImage #myLogout{ 
            position: absolute;
            display: inline-block;
            right: 50px;
            top: 25px;
            z-index: 2;           
        }
    
        #headerImage #myImage{
            position: absolute;
            top: 2%;
            right: 2%;
            z-index: 2;           
        }
    

    这是有效的。

    但是我现在有另一个问题。如您所见,我设置了#help_About and #myLogout {display:inline-block}。但是#help_About 做了一个大正方形。我也希望白色方块不显示。看起来 div 中的元素实际上在图像上。 #myLogout 也是如此。我将 display 设置为 inlne-block 但它们出现在两条不同的行上,我想要然后 inline 并且它显示白色正方形作为我不想要的背景。我该怎么做?如果我设置透明度,那么它会设置整个 div 的透明度,包括元素而不仅仅是 div。谢谢

    现在呢?

    【讨论】:

    • 我已经从我的 headerImage div 中删除了 &lt;img src="resources/images/2b.jpg"/&gt; 行。将位置设置为相对位置,并将所有内部 div 设置为绝对值,但为什么我会得到像上面这样的屏幕:(
    • 我认为是因为height: 20% 尝试给它一个基于px 的值高度,看看是否有帮助
    • 我从那些 div-s 中删除了高度、宽度和背景。更新了您的编辑 CSS - 使用它
    • 谢谢,但如果我用萤火虫检查,div 仍然占据整个正方形。没有办法让 div 只获取包含内容的宽度和高度吗?我还有两个链接,如&lt;div id="help_About"&gt; &lt;a href="#"&gt;Help&lt;/a&gt;&lt;a href="#"&gt;About&lt;/a&gt; &lt;/div&gt;&lt;div id="myLogout"&gt; &lt;h:outputLabel value="Basit" /&gt; &lt;a href="#"&gt;Logout&lt;/a&gt; &lt;/div&gt; 我如何在两个元素之间放置一个空格?谢谢
    • 什么 div 占据了整个正方形?我在这里检查过 - jsfiddle.net/ku6bW 一切似乎都很好。我还为第一个 &lt;a&gt; 添加了一条规则,并对其应用了 margin-right,因此您可以更改它以增加/减少链接之间的空间
    【解决方案2】:

    您需要创建带有波浪图像背景的标题 div。然后放置所有内部控件:

    html

    <div id="header">
        <ul>
            <li><a href="#">Home</a><li>        
            <li><a href="#">Help</a><li>
        </ul>
        <a class="logo" href="#"></a>
        <a class="logout">Logout</a>
    </div>​
    

    css

    body { font-family: Verdana; }
    #header { height:100px; background: url('http://dl.dropbox.com/u/18372729/header-bg.png')}
    #header ul { top:23px; left:270px; position:absolute; }
    #header ul li { display:inline; }
    #header ul li a { color:#fff; text-transform:uppercase; text-decoration:none; margin:20px; font-size:10pt; font-weight:bold; }
    #header ul li a:hover { text-decoration:underline; }
    #header .logo { position:absolute; top:20px; right:200px; background:url('http://dl.dropbox.com/u/18372729/logo.png'); width:217px; height:75px; display:block; position:absolute; }
    #header .logout { font-size:9pt; float:right; margin-top:80px; margin-right:20px; }​
    

    演示:http://jsfiddle.net/xsDQP/1/

    【讨论】:

      【解决方案3】:

      您需要绝对定位您的 div。然后使用 top、left、right 或 bottom 属性将 div 对齐到所需的位置。

      您设置为背景的图像也被用于图像标签中,这可能是您的标题图像加倍的原因。

      更新

      将父 div(在您的情况下为 #headerImage)设置为 position:relative;,并且所有子 div 都应指定为 position:absolute;

      【讨论】:

      • 您的意思是在所有 div 上或仅在主 div 上设置绝对位置?
      • @Basit 只需要您想要重叠的部分就足够了 - 因此,除主 div 之外的所有 div(您不移动的位于底部的那个)
      猜你喜欢
      • 1970-01-01
      • 2022-06-16
      • 2013-09-03
      • 1970-01-01
      • 2012-06-02
      • 2014-08-25
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多