在没有父容器的情况下对齐两个
我试图定位两个
元素,使它们的左侧对齐,就像这张照片中的顶部导航栏和侧边栏:通常,我会使用父
来帮助解决这个问题。问题是我计划使用 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您仍然可以使用父容器对齐两个
div
。无需担心div
标签保持打开状态。只需像这样编写 html/php 即可:You can still align the two
div
's using a parent container. There is no need to worry about leavingdiv
tags open. Just write your html/php like this:我想你所追求的只是点击。
您可以使用
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 threediv
s.Like this: http://jsfiddle.net/4st5e/
Though I'm still not sure why you can't just add a wrapper
div
..我做了这个快速模拟: 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.