div的CSS内联定位

发布于 2024-12-28 06:26:09 字数 1081 浏览 1 评论 0原文

我正在尝试创建一个分为 3 个 div 的标头 它们都将被设置为显示:inline-block 左侧标题部分将包含一个口号和一个徽标,但我不希望该口号出现在该处 徽标的右侧

问题是我的徽标 div 和我的口号 div 总是一个放在另一个的上面。

据我了解,内联元素将放置在最后一个内联元素旁边 在同一个块中,请注意 header-left div 的宽度为 250px,并且每个 子 div 的宽度为 100px,那么为什么它们不一个挨着一个放置呢?

我的标记:

  <div id="header">
       <div id="header-left">
              <div id="logo" />
              <div id="slogan">
                 <span> Buy For U</span>           
              </div>
       </div>  
 </div>

我的CSS:

div#header
{
    border: 1px solid black ;
    height: 200px;
}
div#header div#header-left
{
    display: inline-block;    
    height: 100%;
    width: 250px;

}
div#header div#header-left div#logo
{
    display: inline-block;
    background: url("Google-Desktop-64.png") no-repeat center;
    background-size: 25%;
    height: inherit;
    width: 100px;
}
div#header div#header-left div#slogan
{
    display: inline-block;
    height: inherit;
    width:100px;
}

i'm attempting to create an header which is divided into 3 divs
they will all be set to display: inline-block
the left header part will contain a slogan and a logo which i wan't the slogan to be at
the right of the logo

the problem is my logo div and my slogan div are always placed one on top of the other .

to my understanding an inline element would be placed next to the last inline element
with in the same block , notice that the header-left div has 250px width and each of the
child div's have 100px width so why are they not placed one next to the other ?

my markup :

  <div id="header">
       <div id="header-left">
              <div id="logo" />
              <div id="slogan">
                 <span> Buy For U</span>           
              </div>
       </div>  
 </div>

my css :

div#header
{
    border: 1px solid black ;
    height: 200px;
}
div#header div#header-left
{
    display: inline-block;    
    height: 100%;
    width: 250px;

}
div#header div#header-left div#logo
{
    display: inline-block;
    background: url("Google-Desktop-64.png") no-repeat center;
    background-size: 25%;
    height: inherit;
    width: 100px;
}
div#header div#header-left div#slogan
{
    display: inline-block;
    height: inherit;
    width:100px;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情话墙 2025-01-04 06:26:09

一切都很好。只需正确关闭徽标的

<div id="header">
       <div id="header-left">
              <div id="logo"></div>
              <div id="slogan">
                 <span> Buy For U</span>           
              </div>
       </div>  
 </div>

我还建议使用 作为徽标(并使其成为主页的链接),而不仅仅是背景。空的

标签在验证过程中很容易出错。

everything's fine. just close the logo's <div> properly. "self-closing" tags.

<div id="header">
       <div id="header-left">
              <div id="logo"></div>
              <div id="slogan">
                 <span> Buy For U</span>           
              </div>
       </div>  
 </div>

i also suggest using an <img> for the logo (and make it a link to homepage) rather than just a background. empty <div> tags are prone to errors during validation.

叫思念不要吵 2025-01-04 06:26:09

奇怪的是,您的 #header 的宽度为 200 像素,子 #header-left 的宽度为 250 像素,但除此之外,我认为最好使用浮动。这意味着两个 div 彼此相邻:

div#header div#header-left div#logo
{
    float: left;
    background: url("Google-Desktop-64.png") no-repeat center;
    background-size: 25%;
    height: inherit;
    width: 100px;
}

div#header div#header-left div#slogan
{
    float: left;
    height: inherit;
    width:100px;
}

并且您需要在 html/css 中明确:

.clear_left { clear: left; }

以及 html:

<div id="header">
    <div id="header-left">
        <div id="logo" />
        <div id="slogan"><span> Buy For U</span></div>
        <div class="clear_left"></div>
    </div>  
</div>

It is stange that your #header has a width of 200 pixels and the child #header-left 250 pixels, but apart from that I think it's better to use a float. This means that the two divs are next to each other:

div#header div#header-left div#logo
{
    float: left;
    background: url("Google-Desktop-64.png") no-repeat center;
    background-size: 25%;
    height: inherit;
    width: 100px;
}

div#header div#header-left div#slogan
{
    float: left;
    height: inherit;
    width:100px;
}

And you nead a clear in your html/css:

.clear_left { clear: left; }

And the html:

<div id="header">
    <div id="header-left">
        <div id="logo" />
        <div id="slogan"><span> Buy For U</span></div>
        <div class="clear_left"></div>
    </div>  
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文