在没有父容器的情况下对齐两个

发布于 2024-11-26 20:21:37 字数 1667 浏览 0 评论 0原文

我试图定位两个

元素,使它们的左侧对齐,就像这张照片中的顶部导航栏和侧边栏:

输入你自己的该死的描述。

通常,我会使用父

来帮助解决这个问题。问题是我计划使用 PHP include() 作为标题和侧边栏,以节省在整个网站上的复制和粘贴。这意味着
标记必须在 include() 末尾保持打开状态,以便为右侧的内容留出空间。这看起来是一种可怕的做法,我可能会忘记在页面末尾添加

有没有一种方法可以对齐块而不将它们包含在父容器中?

编辑:这是我到目前为止所拥有的。

<!-- Common code.  Designed to be include()'d into each page. -->
<div id = "Header">
    <a href = "/"><img id = "Logo" src = "Foo.jpg"></a>
    <!-- #Logo is centered in CSS. -->

    <ul id = "TopNav">
        <li><a href = "/">Home</a></li>
        <li><a href = "Browse">Home</a></li>
    </ul>
    <!-- #TopNav is also centered in CSS. There are also
    some fancy link effects, but those are irrelevant. -->
</div>

<div id = "Sidebar">
    <!-- Contains stuff like advertisements and a 
    table of contents. -->
</div>
<!-- No CSS effects are currently applied to the sidebar, meaning it's on the
far left of the page.  I'm trying to line it up with #TopNav, which is
closer to the center.

<!-- The content section to the right of the sidebar is not included
in this file.  That's defined on a page-by-page basis.  Whatever that
content is, it should fall to the right of the sidebar.  It'd be nice
if the left side of the content never fell past the right side of the
sidebar, though not an absolute necessity. -->

I'm trying to position two <div> elements so that that their left sides line up, like the top navigation bar and sidebar in this photo:

Enter your own goddamn description.

Normally, I would use a parent <div> to help with this. The problem is that I plan on using a PHP include() for the header and sidebar, to save copying and pasting across the site. This means that the <div> tag has to be left open at the end of the include(), to leave room for the content on the right. This seems like horrible practice, and I might forget to add </div> at the end of the page.

Is there a way to align the blocks without enclosing them in a parent container?

EDIT: Here's what I have so far.

<!-- Common code.  Designed to be include()'d into each page. -->
<div id = "Header">
    <a href = "/"><img id = "Logo" src = "Foo.jpg"></a>
    <!-- #Logo is centered in CSS. -->

    <ul id = "TopNav">
        <li><a href = "/">Home</a></li>
        <li><a href = "Browse">Home</a></li>
    </ul>
    <!-- #TopNav is also centered in CSS. There are also
    some fancy link effects, but those are irrelevant. -->
</div>

<div id = "Sidebar">
    <!-- Contains stuff like advertisements and a 
    table of contents. -->
</div>
<!-- No CSS effects are currently applied to the sidebar, meaning it's on the
far left of the page.  I'm trying to line it up with #TopNav, which is
closer to the center.

<!-- The content section to the right of the sidebar is not included
in this file.  That's defined on a page-by-page basis.  Whatever that
content is, it should fall to the right of the sidebar.  It'd be nice
if the left side of the content never fell past the right side of the
sidebar, though not an absolute necessity. -->

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

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

发布评论

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

评论(3

来日方长 2024-12-03 20:21:37

您仍然可以使用父容器对齐两个 div。无需担心 div 标签保持打开状态。只需像这样编写 html/php 即可:

<div id="parent">
    <?php include 'header_sidebar.php'; ?>
    <div id="content">
    </div>
</div>

You can still align the two div's using a parent container. There is no need to worry about leaving div tags open. Just write your html/php like this:

<div id="parent">
    <?php include 'header_sidebar.php'; ?>
    <div id="content">
    </div>
</div>
不交电费瞎发啥光 2024-12-03 20:21:37

我想你所追求的只是点击。

您可以使用 body 元素作为三个 div 的容器。

像这样: http://jsfiddle.net/4st5e/

虽然我仍然不确定为什么你可以'只需添加一个包装器 div..

What I think you're after just clicked.

You can use the body element as your container for the three divs.

Like this: http://jsfiddle.net/4st5e/

Though I'm still not sure why you can't just add a wrapper div..

黑寡妇 2024-12-03 20:21:37

我做了这个快速模拟: http://jsfiddle.net/LXwmF/1/

我不知道您当前是否是这样设置的,但您可以使用我的作为模板。您可以看到您可以设置每个父 div 的样式。

I've made this quick mock up: http://jsfiddle.net/LXwmF/1/

I don't know if that's how you currently have it setup but you could use mine as a template. You can see you can style each parent div.

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