javascript - 如何用CSS模拟多个<br/>标签

标签 javascript html css

当我想出一个有趣的解决方案但没有真正最终确定时,我花了一些时间在一个问题上。

参见 Fiddle 并尝试删除 <br/>标签。

这个想法也得到了同样的效果(显示红色div)但是没有使用这个解决方案相对可怕。

所以这是我的问题:我如何模拟 <br/>带有 CSS 或最终 Js 的标签?

只是有些困难:你不能触摸 wrapper 。

这是 HTML 代码:

<div class="wrapper">
    <p>Some text and descriptions</p>
    <div class="widget box-wrap">
        <div class="box"><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/></div>
    </div>
    <p>Some more text and description and some more offcourse and a bit more, and just a tiny bit more</p>
</div>

这是 CSS:

.wrapper { 
    width:300px; 
    display:block; 
    position:relative;
    overflow:hidden;
    background-color:gray;
}
.widget {
    width:300px;
    height:300px;
    display:block;
    position:relative;
    background-color:green;
}
.box {
    background-color:red;
    visibility:visible;
}

.box-wrap {
   overflow: hidden;
   height: 0;
   padding-bottom: 60%;
}
.box-wrap div {
   max-width: 100%;
}

最佳答案

您可以使用绝对位置拉伸(stretch) .box,因为它是相对于父级的。这样您就可以确保它占用父容器的整个空间。

.wrapper {
    width:300px;
    display:block;
    position:relative;
    overflow:hidden;
    background-color:gray;
}
.widget {
    width:300px;
    height:300px;
    display:block;
    position:relative;
    background-color:graeen;
}
.box {
    background-color:red;
    visibility:visible;
    position: absolute;
    top: 0; bottom: 0;
    left: 0; right: 0;
}
.box-wrap {
    overflow: hidden;
    height: 0;
    padding-bottom: 60%;
}
.box-wrap div {
    max-width: 100%;
}
<div class="wrapper">
    <p>Some text and descriptions</p>
    <div class="widget box-wrap">
        <div class="box"></div>
    </div>
    <p>Some more text and description and some more offcourse and a bit more, and just a tiny bit more</p>
</div>

关于javascript - 如何用CSS模拟多个<br/>标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31605710/

相关文章:

html - IE6 文本在 dl 标签中丢失

javascript - 在没有 onclick 的情况下运行 Javascript 函数

javascript - 使用正则表达式在 JavaScript 中查找最长的重复子字符串

html - Ubuntu Firefox 64 位有时会错误地加载 css

javascript - 单击和模糊时的 Angular js

jquery - 双向自动图像拉伸(stretch)/挤压/裁剪

javascript - Meteor/Node 中的同步方法

javascript - 使用 Mongoose 插入文档

html - 如何让基于 CSS <li> 的导航栏居中?

php - 使用 PHP 将 <a> 标记替换为 <b> 标记