【问题标题】:CSS centered image with dynamic left and right div with lines带有动态左右 div 的 CSS 居中图像和线条
【发布时间】:2014-05-15 02:47:46
【问题描述】:

我想要一个固定宽度的图像在中间。
没问题。
但我希望它旁边的左右 div 是 2 行。

示例。

我在中间有一个 100px * 100px 的图像,并且希望 2 个 div 的宽度为 50% - 50px,而不使用 calc。 2个div应该有一条线,这也没有问题。

我只是用

 { background: white; margin-top: 48px; margin-bottom: 48px; }

现在的问题是让这 2 个 div 填充图像的左右空间。 整个屏幕的宽度不用说是动态的。

有没有办法只使用 CSS 来做到这一点?

【问题讨论】:

  • 到目前为止您的代码尝试过什么?

标签: css html dynamic center


【解决方案1】:

这是我为左右 div 使用包装器的解决方案,它们的宽度为 50%,并包含其他 div,其边距为图像宽度的一半。图像本身使用绝对定位定位在包装器中。

HTML

<div class="wrapper">
    <div class="left"><div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.</div></div>
    <div class="right"><div>Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</div></div>
    <img src="your_image.png" alt="" />
</div>

CSS

.wrapper {
    position: relative;
    overflow: hidden;
    height: auto; /* match the height of the floating divs */
}

.wrapper > div > div {
    min-height: 100px; /* at least as high as the image */
}

.wrapper > .left {
    float: left;
    width: 50%;
}

.wrapper > .left > div {
    margin-right: 50px;
    background: blue;
}

.wrapper > .right {
    float: right;
    width: 50%;
}

.wrapper > .right > div {
    margin-left: 50px;
    background: red;
}

.wrapper > img {
    position: absolute;
    width: 100px;
    height: 100px;
    top: 0;
    left: 50%;
    margin-left: -50px;
}

这是一个 JSFiddle:http://jsfiddle.net/j84LB/

【讨论】:

  • 谢谢它似乎确实有效。现在我只需要找出如何让蓝色和红色 div 在 48px 下变成白色 4px 高线;)
  • 这可以通过 3.level div 和 .wrapper > div > div > div { max-height: 4px;最小高度:4px;背景:白色;位置:相对;顶部:48px; }
  • 我想,你的意思是position: absolute;,不是吗?我没有意识到,您希望左右 div 垂直居中。我猜这也可以用那些二级 div 来完成。但它也可以这样工作,这里描述的方法应该更通用。
【解决方案2】:

您可以尝试对其进行变形,并将两侧的 div 设置为 25%,将图像设置为 50%。
这与值为图像宽度的 50% 的 div 相同。

CSS

.wrapper {width: 200px;}
.left {width: 25%;}
.right {width:25%;}
.image {width: 50%;}

HTML

<div class="wrapper">
    <div class="left"></div>
    <div class="image">
        <img src="" />
    </div>
    <div class="right"></div>
</div>

【讨论】:

  • 图片的固定宽度为 100px。不是上例中的百分比。因此,如果屏幕为 300 像素,则 2 个 div 应为 100 像素,但如果屏幕为 400,则 2 个 div 应为 150 像素
【解决方案3】:

使用 display:table/display:table-cell 似乎很容易

不需要额外的 div 等。

JSfiddle Demo

HTML

<body>
    <div class="wrapper">
        <div class="left">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor
        </div>
        <div><img src="http://lorempixel.com/output/abstract-q-c-100-100-6.jpg" alt="" /></div>
        <div class="right">Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
        </div>

    </div>
</body>

CSS

.wrapper {
    position: relative;
    overflow: hidden;
    height: auto;
    display:table;
}

.wrapper > div {
    display:table-cell;
    vertical-align: middle;
    padding: 1em;
}

.wrapper > div:nth-child(2) {
    min-height: 100px;
    width:100px;
    padding:0;

}

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 2021-02-09
    • 1970-01-01
    • 2015-05-06
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2021-09-29
    相关资源
    最近更新 更多