首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能使浮动项超过父项的宽度

不能使浮动项超过父项的宽度
EN

Stack Overflow用户
提问于 2015-03-19 22:28:13
回答 1查看 75关注 0票数 1

我无法在CSS上获得想要的效果,只有新闻播报器。如果我使用的位置绝对,链接只显示一次与与的链接已清除。如果使用位置相对,则会获得所需的效果(Marquee),但如果项目数超过父项的宽度,则项目将分解为第二行。

为了让它与position:relative一起工作,我尝试了各种各样的方法。我甚至试过display:inline-block; white-space:nowrap;

如何将浮动项保持在一行中,而不考虑父项的宽度?

HTML

代码语言:javascript
复制
Position Absolute<br>
<div id="news-ticker-1" class="marquee-1">
    <span class="news-label">News</span>
    <div class="overflow">
    <a class="item" href="#">First Link</a>
    <a class="item" href="#">Second Link</a>
    <a class="item" href="#">Third Link</a>
    <a class="item" href="#">Fourth Link</a>
    <a class="item" href="#">Fifth Link</a>
    <a class="item" href="#">Sixth Link</a>
    <a class="item" href="#">Seventh Link</a>
    <a class="item" href="#">Eighth Link</a>
    <a class="item" href="#">Ninth Link</a>
    <a class="item" href="#">Tenth Link</a>
    <a class="item" href="#">Eleventh Link</a>
    <a class="item" href="#">Twelfth Link</a>
    </div>
</div>
<br>
<br>
<br>
Position Relative
<div id="news-ticker-2" class="marquee-2">
    <span class="news-label">News</span>
    <div class="overflow">
    <a class="item" href="#">First Link</a>
    <a class="item" href="#">Second Link</a>
    <a class="item" href="#">Third Link</a>
    <a class="item" href="#">Fourth Link</a>
    <a class="item" href="#">Fifth Link</a>
    <a class="item" href="#">Sixth Link</a>
    <a class="item" href="#">Seventh Link</a>
    <a class="item" href="#">Eighth Link</a>
    <a class="item" href="#">Ninth Link</a>
    <a class="item" href="#">Tenth Link</a>
    <a class="item" href="#">Eleventh Link</a>
    <a class="item" href="#">Twelfth Link</a>
    </div>
</div>

CSS

代码语言:javascript
复制
.marquee-1 {
    width: 100%;
    position: relative;
    overflow:hidden;
}
.marquee-1 .overflow {
    position: absolute;
    animation-name: marquee-1;
    animation-duration: 40s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
}
.marquee-1:hover .overflow {
    animation-play-state: paused;
}
.marquee-1 .item {
    display: block;
    float: left;
}
@keyframes marquee-1 {
    from {
        left: 100%;
    }
    to {
        left: -200%;
    }
}
#news-ticker-1 {
    background: rgba(190, 128, 255, 0.6);
    border-right: 3px solid #BE80FF;
    height: 60px;
}
#news-ticker-1 .news-label {
    background: #BE80FF;
    position: absolute;
    top: 0;
    left: 0;
    line-height: 60px;
    z-index: 2;
    padding: 0 1rem;
    color: #fff;
}
#news-ticker-1 a {
    line-height: 60px;
    padding: 0 1rem;
    border-right: 1px solid rgba(0, 0, 0, 0.1);
    color: #fff;
}
#news-ticker-1 a:hover {
    background: rgba(0, 0, 0, 0.1);
}
#news-ticker-1 a:first-child {
    border-left: 1px solid rgba(0, 0, 0, 0.1);
}




.marquee-2 {
    width: 100%;
    position: relative;
    overflow:hidden;
}
.marquee-2 .overflow {
    position: relative;
    animation-name: marquee-2;
    animation-duration: 40s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
}
.marquee-2:hover .overflow {
    animation-play-state: paused;
}
.marquee-2 .item {
    display: block;
    float: left;
}
@keyframes marquee-2 {
    from {
        left: 100%;
    }
    to {
        left: -200%;
    }
}
#news-ticker-2 {
    background: rgba(190, 128, 255, 0.6);
    border-right: 3px solid #BE80FF;
    height: 60px;
}
#news-ticker-2 .news-label {
    background: #BE80FF;
    position: absolute;
    top: 0;
    left: 0;
    line-height: 60px;
    z-index: 2;
    padding: 0 1rem;
    color: #fff;
}
#news-ticker-2 a {
    line-height: 60px;
    padding: 0 1rem;
    border-right: 1px solid rgba(0, 0, 0, 0.1);
    color: #fff;
}
#news-ticker-2 a:hover {
    background: rgba(0, 0, 0, 0.1);
}
#news-ticker-2 a:first-child {
    border-left: 1px solid rgba(0, 0, 0, 0.1);
}

JSFIDDLE:http://jsfiddle.net/n6a5uLao/

注意:代码包括小提琴的两个例子。通过观察小提琴,它能更好地解释问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-19 22:49:08

在您的位置相对

代码语言:javascript
复制
.marquee-2 .item {
    display: block;
    float: left;
}

改为:

代码语言:javascript
复制
.marquee-2 .item {
    display: table-cell;
    white-space: nowrap;
}

片段:

代码语言:javascript
复制
.marquee-2 {
    width: 100%;
    position: relative;
    overflow:hidden;
}
.marquee-2 .overflow {
    position: relative;
    animation-name: marquee-2;
    animation-duration: 40s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
}
.marquee-2:hover .overflow {
    animation-play-state: paused;
}
.marquee-2 .item {
    display: table-cell;
    white-space: nowrap;
}
@keyframes marquee-2 {
    from {
        left: 100%;
    }
    to {
        left: -200%;
    }
}
#news-ticker-2 {
    background: rgba(190, 128, 255, 0.6);
    border-right: 3px solid #BE80FF;
    height: 60px;
}
#news-ticker-2 .news-label {
    background: #BE80FF;
    position: absolute;
    top: 0;
    left: 0;
    line-height: 60px;
    z-index: 2;
    padding: 0 1rem;
    color: #fff;
}
#news-ticker-2 a {
    line-height: 60px;
    padding: 0 1rem;
    border-right: 1px solid rgba(0, 0, 0, 0.1);
    color: #fff;
}
#news-ticker-2 a:hover {
    background: rgba(0, 0, 0, 0.1);
}
#news-ticker-2 a:first-child {
    border-left: 1px solid rgba(0, 0, 0, 0.1);
}
代码语言:javascript
复制
Position Relative
<div id="news-ticker-2" class="marquee-2">
    <span class="news-label">News</span>
    <div class="overflow">
    <a class="item" href="#">First Link</a>
	<a class="item" href="#">Second Link</a>
	<a class="item" href="#">Third Link</a>
	<a class="item" href="#">Fourth Link</a>
	<a class="item" href="#">Fifth Link</a>
	<a class="item" href="#">Sixth Link</a>
	<a class="item" href="#">Seventh Link</a>
	<a class="item" href="#">Eighth Link</a>
	<a class="item" href="#">Ninth Link</a>
	<a class="item" href="#">Tenth Link</a>
	<a class="item" href="#">Eleventh Link</a>
	<a class="item" href="#">Twelfth Link</a>
    </div>
</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29156153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档