div 并排浮动并溢出?

发布于 2024-12-20 18:17:50 字数 389 浏览 3 评论 0原文

我有一个 div 包装器,在它里面,我希望 div 并排放置。我希望包装纸侧面是固定的,这样当添加更多的 div 时,它们会水平溢出!

另外,我希望包装纸宽度是固定的!所以我确实希望里面的帖子溢出到包装纸内!

我想使用一个类,这样我就有了类似

 <div id="wrapper">
      <div class='post'>Post 1</div>
      <div class='post'>Post 2</div>
      <div class='post'>Post 3</div>
 </div>

我该怎么做?! :p

干杯!!

I have a div wrapper, and inside it, i want divs to be side by side. I want the wrapper side to be fixed, so that as more divs are added, they overflow horizontally!!

Also, I want the wrapper width to be fixed! So I do want the posts inside to overflow inside the wrapper!

I want to use a class, so that I have something like

 <div id="wrapper">
      <div class='post'>Post 1</div>
      <div class='post'>Post 2</div>
      <div class='post'>Post 3</div>
 </div>

How do I do that?! :p

Cheers!!

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

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

发布评论

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

评论(5

合久必婚 2024-12-27 18:17:50

您正在寻找这样的东西吗?
这利用了包装器内的第二个 div:包装器本身具有固定宽度,并且 overflow-x 设置为滚动:

#wrapper {
    margin: 20px;
    padding: 20px;
    width: 300px;
    overflow-x: auto;
    background: #eee;
    border: 1px solid #333;
}

#wrapper>div {
    width: 600px;
}

.post {
    background: #fff;
    border: 1px solid #e4e4e4;
    float:left;
    margin-left: 20px;
    padding: 40px;
}

.post:first-of-type {
    margin-left: 0;
}

Are you after something like this?
That makes use of a second div inside your wrapper: the wrapper itself has a fixed width, and overflow-x set to scroll:

#wrapper {
    margin: 20px;
    padding: 20px;
    width: 300px;
    overflow-x: auto;
    background: #eee;
    border: 1px solid #333;
}

#wrapper>div {
    width: 600px;
}

.post {
    background: #fff;
    border: 1px solid #e4e4e4;
    float:left;
    margin-left: 20px;
    padding: 40px;
}

.post:first-of-type {
    margin-left: 0;
}
盛夏已如深秋| 2024-12-27 18:17:50
#wrapper {
display: block;
width: 600px;
height: 100px;
overflow-x: auto;
overflow-y: hidden;
background: #900;
white-space: nowrap;}

.post {
display: inline-block;
width: 250px;
height: 100px;
background: #c00;
margin: 0 5px; }

此处为 Jfiddle 示例

#wrapper {
display: block;
width: 600px;
height: 100px;
overflow-x: auto;
overflow-y: hidden;
background: #900;
white-space: nowrap;}

.post {
display: inline-block;
width: 250px;
height: 100px;
background: #c00;
margin: 0 5px; }

Jfiddle sample here

抠脚大汉 2024-12-27 18:17:50

将包装器 div 设置为具有溢出:用于滚动的自动宽度,用于调整大小的自动宽度(尽管您需要绝对定位才能正常工作,我相信 - 请记住将父级设置为相对位置以获得准确的顶部:x 和左侧:x在子 div 中)然后 float:left .post 类。

Set the wrapper div to have overflow:auto for scrolling, auto width for resizing (although you'll need to position absolute for that to work correctly I believe- remember to set the parent to position relative for accurate top:x and left:x in sub div's) then float:left the .post class.

空名 2024-12-27 18:17:50

据我了解,您希望您的帖子水平滚动并并排放置。

为了实现这一点,您需要添加一个额外的包装器,如下所示:

<div id="wrapper">
 <div id="contentWrapper">
      <div class='post'>Post 1</div>
      <div class='post'>Post 2</div>
      <div class='post'>Post 3</div>
      <div class='post'>Post 4</div>
      <div class='post'>Post 5</div>
      <div class='post'>Post 6</div> 
      <div class='post'>Post 7</div>
      <div class='post'>Post 8</div>
 </div>

这是实现所需效果的 css:

#wrapper {
    width:400px;
    height:200px;
    overflow: auto;
}
#contentWrapper {
    float: left;
    margin-right: -30000px;
}
.post {
     width:100px;
     height:100px;
     display:inline-block;
}

工作示例可以在此处找到:
http://jsfiddle.net/QNXmk/1/

As far as I understand you want your posts to be scrolled horizontally and sitting side by side.

To achieve this you will need to add an additional wrapper like so:

<div id="wrapper">
 <div id="contentWrapper">
      <div class='post'>Post 1</div>
      <div class='post'>Post 2</div>
      <div class='post'>Post 3</div>
      <div class='post'>Post 4</div>
      <div class='post'>Post 5</div>
      <div class='post'>Post 6</div> 
      <div class='post'>Post 7</div>
      <div class='post'>Post 8</div>
 </div>

Here's the css to achieve the desired effect :

#wrapper {
    width:400px;
    height:200px;
    overflow: auto;
}
#contentWrapper {
    float: left;
    margin-right: -30000px;
}
.post {
     width:100px;
     height:100px;
     display:inline-block;
}

A working example can be found here :
http://jsfiddle.net/QNXmk/1/

傲娇萝莉攻 2024-12-27 18:17:50

我想补充一点,您将不知道内容容器的总宽度。使总宽度基于帖子总宽度总和的唯一解决方案是使用 javascript。

下面是一个执行此操作的小脚本示例:

/* adjust the size (width) of the posts container
 * this depends on all its posts widths
 */

function setWrapper(){
    var finalW = 0;
    itemsWrapper = $(".posts-container");
    itemsWrapper.find('.post').each(function(i){
        var $post = $(this);
        finalW+=$post.width();
    });
    itemsWrapper.css('width',finalW+'px');
    console.log("Your total width is: "+finalW);
}
setWrapper();

通过此脚本,您可以将帖子容器设置为正确的大小,而无需在 css 中显式传递它。

I´d like to add that you will not know the total width of your content container. The only solution for having the total width based in the sum of the total of your posts widths is using javascript.

Here is an example of a little script that will do this:

/* adjust the size (width) of the posts container
 * this depends on all its posts widths
 */

function setWrapper(){
    var finalW = 0;
    itemsWrapper = $(".posts-container");
    itemsWrapper.find('.post').each(function(i){
        var $post = $(this);
        finalW+=$post.width();
    });
    itemsWrapper.css('width',finalW+'px');
    console.log("Your total width is: "+finalW);
}
setWrapper();

With this, you will set your posts container to the correct size without passing it explicitly in the css.

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