使用 CSS,如何在两个浮动 div 之间堆叠两个跨度?

发布于 2024-11-14 16:12:27 字数 391 浏览 3 评论 0原文

在页面顶部,我有两个 div,一个浮动到左侧,一个浮动到右侧。我可以在它们之间放置带有边框的文本,但是,我现在需要在它们之间堆叠两个这样的文本区域。

这是一个说明我的问题的小提琴: http://jsfiddle.net/TcRxp/

我需要下面的橙色框绿色框,每个中心彼此对齐。 “图例”(向右浮动)曾经处于同一水平,但现在向下移动。

我尝试添加另一张桌子,但这没有帮助。

请原谅标记 - 我知道它并不真正光滑。随着时间的推移,有一些人接触过这一点,但我们都不是这方面的专家。

是的,我已经游说一位设计师加入团队,但还没有发生。

谢谢,

保罗

At the top of a page I've got two divs, one floated to the left and one to the right. I can place text with a border between them, however, I now need to stack two such areas of text between them.

Here's a Fiddle illustrating my problem: http://jsfiddle.net/TcRxp/

I need the orange box under the green box, with each center aligned with the other. The "legend" (floated to the right) used to be at the same level but is shifted down now.

I tried adding another table to the mix but that didn't help.

Excuse the markup - it's not real slick, I know. A few people have touched this over time and none of us are gurus at this.

And yes, I have lobbied for a designer to be added to the team but it hasn't happened yet.

Thanks,

Paul

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

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

发布评论

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

评论(5

素手挽清风 2024-11-21 16:12:27

更新:合并@Jeremy B的建议

是否必须通过CSS更改?处理此类场景时,您需要注意 HTML 元素的定义顺序。

看看这里的修改: http://jsfiddle.net/TcRxp/8/

我能够通过更改三个 DIV 的顺序并使用 @Jeremy B 的 CSS 建议来实现您所需的效果

本质上,布局的逻辑是

  1. 绘制右浮动内容
  2. 绘制左浮动内容
  3. 绘制中间内容(因为它现在将渲染到左浮动内容的右侧。

UPDATE: Incorporating @Jeremy B's suggestion

Does it have to be via CSS changes? When dealing with scenarios like this, you need to be careful of the order in which the HTML elements are defined.

Look at the modification here: http://jsfiddle.net/TcRxp/8/

I was able to acheive what you needed by changing the order of the three DIVs and using the CSS suggesion from @Jeremy B

Essentially, the logic for the layout is

  1. Draw the float-right content
  2. Draw the float-left content
  3. Draw the content in the middle (as it will now render to the right of the float-left content.
清醇 2024-11-21 16:12:27

首先让你的顶部跨度成为一个块元素来堆叠它们:

Status:< /code>

然后将中间的 div 向左浮动:

将 float:left 添加到 css 中的#headmiddle

First make your top span a block element to stack them:

<span class="color status active bold" style="display:block">Status:</span>

then float the middle div left as well:

add float:left to #headmiddle in your css

各空 2024-11-21 16:12:27

当您将 CSS 和表格布局结合起来时,总是很难获得所需的结果。

我建议简化您的 HTML:

<div id="headleft">a little search form here</div>
<div id="headmiddle">
    <div class="active"><strong>Status:</strong> Active</div>
    <div class="search">Search results displayed</div>
</div>
<div id="headright">
    <dl>
        <dt>Legend:</dt>
        <dd>Status numero uno</dd>
        <dd>Status two</dd>
    </dl>
</div>

和 CSS:

div { padding: 2px; }
strong { font-weight: bold; }
#headleft { float: left; font-size: 0.8em; }
#headmiddle { float: left; font-size: 0.8em; }
#headmiddle div { border: 1px solid #000; margin-bottom: 3px; }
.search { background: orange; }
.active { background: #8ed200; }
#headright { float: right; font-size: 0.8em; }
dt { float: left; font-weight: bold; }
dd { margin-left: 4.5em; }

结果是语义上正确的 HTML,更易于阅读,因此将来更容易修改。 支持小提琴

It's always going to be difficult to get the desired results when you're combining CSS and tables-for-layout.

I would suggest simplifying your HTML:

<div id="headleft">a little search form here</div>
<div id="headmiddle">
    <div class="active"><strong>Status:</strong> Active</div>
    <div class="search">Search results displayed</div>
</div>
<div id="headright">
    <dl>
        <dt>Legend:</dt>
        <dd>Status numero uno</dd>
        <dd>Status two</dd>
    </dl>
</div>

and your CSS:

div { padding: 2px; }
strong { font-weight: bold; }
#headleft { float: left; font-size: 0.8em; }
#headmiddle { float: left; font-size: 0.8em; }
#headmiddle div { border: 1px solid #000; margin-bottom: 3px; }
.search { background: orange; }
.active { background: #8ed200; }
#headright { float: right; font-size: 0.8em; }
dt { float: left; font-weight: bold; }
dd { margin-left: 4.5em; }

The result is semantically correct HTML, easier to read and therefore easier to modify in the future. Supporting fiddle.

熊抱啵儿 2024-11-21 16:12:27

如果您需要使用 CSS 来完成此操作,请参阅我的更改: Fiddle

我添加了以下内容:

#headmiddle span.status { display: block }

这将导致你的跨度要“堆叠”。

If you need to do it with CSS, see my changes: Fiddle

I added the following:

#headmiddle span.status { display: block }

This will cause your spans to "stack".

↙温凉少女 2024-11-21 16:12:27

我通过整合许多不同的来源得到了它。亚历克斯·科尔斯的解决方案是最接近的,但中间并不居中。它也比我的烂摊子干净得多。我从 中的代码开始这篇文章

<style type="text/css">
.leftit {
    float: left;
}
.rightit {
    float: right;
}
.centerit {
    width: 30%;
    margin-right: auto;
    margin-left: auto;
    text-align: center;
}
.centerpage {
    width: 80%;
    margin-right: auto;
    margin-left: auto;
}
</style>
</head>
<body>
<div class="centerpage">
<div class="leftit">Hello Left</div>
<div class="rightit">Hello Right</div>
<div class="centerit">Hello Middle</div>
</div>

小提琴

我采用了亚历克斯清理的元素,这让我更接近我的目标,但中心色块太宽了。从这个问题我了解了“max-width”,结束了是我需要的最后一件作品……至少我是这么想的。

编辑: max-width 在 IE7 怪异模式下不起作用(我必须支持),所以来自 此页 我学习了如何调整我的 CSS 以在 IE7 怪异模式、IE8 和 FF 下工作。

最终代码(fiddle):

.leftit {
    float: left;
    font-size: 0.8em;
}
.rightit {
    float: right;
    font-size: 0.8em;
}
.centerit {
    width:220px;
    margin-right: auto;
    margin-left: auto;
    font-size: 0.8em;
}
#headmiddle div {
    border: 1px solid #000; 
    margin-bottom: 3px;
}
.centerpage {
    margin-right: auto;
    margin-left: auto;
    text-align: center;
}

strong { font-weight: bold; }

.search { background: orange; }
.active { background: #8ed200; }

dt { float: left; font-weight: bold; }
dd { margin-left: 4.5em; }

<div class="centerpage">
    <div class="leftit">a little search form here</div>
    <div class="rightit">
        <dl>
            <dt>Legend:</dt>
            <dd>Status numero uno</dd>
            <dd>Status two</dd>
        </dl>
    </div>
    <div class="centerit" id="headmiddle">
        <div class="active"><strong>Status:</strong>
            Active</div>
        <div class="search">Search results displayed</div>
    </div>
</div>

感谢所有出色的答案 - 我从这个问题中学到了很多东西。

保罗

I got it by putting together many different sources. Alex Coles' solution was closest right off the bat but the middle wasn't centered. It was much cleaner than my mess too. I started with the code from this post:

<style type="text/css">
.leftit {
    float: left;
}
.rightit {
    float: right;
}
.centerit {
    width: 30%;
    margin-right: auto;
    margin-left: auto;
    text-align: center;
}
.centerpage {
    width: 80%;
    margin-right: auto;
    margin-left: auto;
}
</style>
</head>
<body>
<div class="centerpage">
<div class="leftit">Hello Left</div>
<div class="rightit">Hello Right</div>
<div class="centerit">Hello Middle</div>
</div>

(fiddle for above)

I took the elements Alex cleaned up which got me even closer to my goal, but the center color blocks were way too wide. From this question I learned about "max-width", which ended up being the final piece I needed...or so I thought.

Edit: max-width doesn't work in IE7 quirks mode (which I have to support) so from this page I learned how to tweak my css to work in IE7 quirks mode, IE8, and FF.

The final code (fiddle):

.leftit {
    float: left;
    font-size: 0.8em;
}
.rightit {
    float: right;
    font-size: 0.8em;
}
.centerit {
    width:220px;
    margin-right: auto;
    margin-left: auto;
    font-size: 0.8em;
}
#headmiddle div {
    border: 1px solid #000; 
    margin-bottom: 3px;
}
.centerpage {
    margin-right: auto;
    margin-left: auto;
    text-align: center;
}

strong { font-weight: bold; }

.search { background: orange; }
.active { background: #8ed200; }

dt { float: left; font-weight: bold; }
dd { margin-left: 4.5em; }

<div class="centerpage">
    <div class="leftit">a little search form here</div>
    <div class="rightit">
        <dl>
            <dt>Legend:</dt>
            <dd>Status numero uno</dd>
            <dd>Status two</dd>
        </dl>
    </div>
    <div class="centerit" id="headmiddle">
        <div class="active"><strong>Status:</strong>
            Active</div>
        <div class="search">Search results displayed</div>
    </div>
</div>

Thanks to all the great answers - I learned a lot from this question.

Paul

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