简体   繁体   English

您将如何设置该DIV布局(左对齐div左对齐,右对齐div右对齐)?

[英]How would you setup this DIV layout (Right aligned div on left, Left aligned Div on right)?

I'm trying to have 2 seperate DIV's, one with Right aligned content (the labels) and the other div with Left aligned content (the content for each label). 我正在尝试有2个单独的DIV,一个具有右对齐内容(标签),另一个div具有左对齐内容(每个标签的内容)。

I am hoping to make each label "connected" with it's child content on the right so that if it gets pushed down by content from above they will still remain together. 我希望每个标签都与其右侧的子内容“连接”在一起,这样,如果它被上层内容推下,它们仍将保持在一起。

What would be the best way to approach setting this type of layout that is cross-browser? 设置这种跨浏览器布局的最佳方法是什么? (I have provided in a JPG below). (我在下面的JPG中提供)。

在此处输入图片说明

The alignment is going to be tricky if you actually have the right aligned content in that separate div, especially if the content is variable in nature and the height is prone to change. 如果您实际上在该单独的div中具有正确的对齐内容,则对齐将非常棘手,尤其是如果该内容本质上是可变的并且高度易于更改时。

Very basically, here is what I would do just so that the right hand features always line up with their associated left hand content. 基本上,这就是我要做的,以便右手功能始终与其关联的左手内容对齐。 You're obviously going to have to tweak it to your taste. 您显然必须根据自己的口味进行调整。

CSS: CSS:

ul {
    list-style: none outside none;
    margin-left: 150px;
}

ul li h3{
    position: absolute;
}

ul li span {
    position: relative;
    display: block;
    width: 150px;
    text-align: right;
    left: -150px;
}

HTML: HTML:

<ul>
    <li>
        <h3><span>Feature 1</span></h3>
        <p>Content 1<br />Content 1</p>
    </li>
    <li>
        <h3><span>Feature 2</span></h3>
        <p>Content 2<br />Content 2</p>
    </li>
</ul>

I literally just threw this into an empty file just to make sure it works. 我只是将其扔到一个空文件中,以确保它可以正常工作。 I know it's not exactly what you asked for, but maybe it will give you some ideas. 我知道这并非您真正要求的,但是也许它将给您一些想法。

As I'm being accused of providing an overkill solution, here's a simpler one just using floats: 由于有人指责我提供了一种过大的解决方案,因此下面是一个仅使用浮点数的简单方法:

CSS: CSS:

.left, .right {
    float: left;
}

.left {
    clear: left;
    width: 150px;
    text-align: right;
}

HTML: HTML:

<div class="left">feature</div>
<div class="right">content<br />content</div>

<div class="left">feature</div>
<div class="right">content<br />content</div>

This is semantically a table, and is an appropriate use for the HTML table layout. 从语义上讲,这是一个表,并且适合HTML表布局。 While it's important to move away from the use of tables for non-tabular layout, a table is still a table, no matter how few rows or columns. 尽管重要的是不要使用非表格布局的表格,但是无论行数或列数如何,表格仍然是表格。 A table with two columns and lots of rows is very prevalent in the real world. 具有两列和许多行的表在现实世界中非常普遍。

似乎您也可以使div中的类在父元素中向左或向右对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM