html - Bootstrap - 使卡片水平滚动

标签 html css twitter-bootstrap

我正在尝试制作卡片轮播,但在保持内容可读性方面遇到了问题。这是我当前的代码:

.news-article {
    font-family: 'Muli', sans-serif;
    font-size: 0.725rem;
    font-weight: normal;
    line-height: 1.19;
    text-align: left;
    color: #333333;
}

.news-source{
    font-family: 'Muli', sans-serif;
    font-size: 0.675rem;
    font-weight: 800;
    line-height: 1.19;
    text-align: left;
    color: #333333;
}

.white-background{
    background-color: transparent !important;
    border-top: none !important;
}

.card-footer{
    background-color: #fff;
}

@media only screen and (max-width: 813px) {
    .white-background{
        background-color: transparent !important;
        border-top: none !important;
    }


    .article{
        width: 400px !important;
        height: 100px !important;
    }

    .news-box{
        overflow: hidden;
    }
}
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  </head>
  <body>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <div class="pt-3">
        <div class="row news-box no-margin flex-row flex-nowrap">
            <div class="card shadow-sm article col m-2">
                <div class="card-body">
                    <div class="news-article">
                        The Economic Times
                    </div>
                </div>
                <div class="card-footer white-background">
                    <div class="news-source">
                        The Economic Times
                    </div>
                </div>
            </div>
            <div class="card shadow-sm article col m-2">
                <div class="card-body">
                    <div class="news-article">
                        VcCircle
                    </div>
                </div>
                <div class="card-footer white-background">
                    <div class="news-source">
                        VcCircle
                    </div>
                </div>
            </div>
        </div>
    </div>

  </body>
</html>

我如何让它可以滚动并具有相同的卡片宽度?

我尝试为每张卡片使用 col-md-4 col-sm-6 col-xs-6 而不是 col 但它不起作用。

我需要宽度等于浏览器和 PC 中的 col-md-4 但要有 col-xs-6 在手机中。

如有任何帮助,我们将不胜感激。

提前致谢。

最佳答案

我个人建议通过将此代码添加到 .news-article.news-source 来使用省略号

overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;

如果您更愿意使用溢出,简单集 .news-article.news-sourcewhite-space: nowrap;overflow: auto;article

解决方案 01(使用省略号)

.news-article,
.news-source {

    /* Ellipsis */

    overflow: hidden;
    white-space: nowrap;
    -ms-text-overflow: ellipsis;
    text-overflow: ellipsis;

    color: #333333;
    font-family: 'Muli', sans-serif;
    text-align: left;
}

.news-article {
    font-size: 0.725rem;
    font-weight: normal;
    line-height: 1.19;
}

.news-source {
    font-size: 0.675rem;
    font-weight: 800;
    line-height: 1.19;
}

.white-background {
    background-color: transparent !important;
    border-top: none !important;
}

.card-footer {
    background-color: #fff;
}

@media only screen and (max-width: 813px) {

    .white-background {
        background-color: transparent !important;
        border-top: none !important;
    }


    .article {
        width: 400px !important;
        height: 100px !important;
    }

    .news-box {
        overflow: hidden;
    }
}
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    </head>
    <body>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

        <div class="pt-3">
            <div class="row news-box no-margin flex-row flex-nowrap">
                <div class="card shadow-sm article col m-2">
                    <div class="card-body">
                        <div class="news-article">
                            The Economic Times
                        </div>
                    </div>
                    <div class="card-footer white-background">
                        <div class="news-source">
                            The Economic Times
                        </div>
                    </div>
                </div>
                <div class="card shadow-sm article col m-2">
                    <div class="card-body">
                        <div class="news-article">
                            VcCircle
                        </div>
                    </div>
                    <div class="card-footer white-background">
                        <div class="news-source">
                            VcCircle
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </body>
</html>

解决方案02(使用溢出)

.news-article,
.news-source {
    
    /* So that the text remains on one line */
    white-space: nowrap;

    color: #333333;
    font-family: 'Muli', sans-serif;
    text-align: left;
}

.news-article {
    font-size: 0.725rem;
    font-weight: normal;
    line-height: 1.19;
}

.news-source {
    font-size: 0.675rem;
    font-weight: 800;
    line-height: 1.19;
}

.white-background {
    background-color: transparent !important;
    border-top: none !important;
}

.card-footer {
    background-color: #fff;
}

@media only screen and (max-width: 813px) {

    .white-background {
        background-color: transparent !important;
        border-top: none !important;
    }

    .card {
        flex: 0 0 400px !important;
    }

    .news-box {
        overflow: auto;
    }
}
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    </head>
    <body>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

        <div class="pt-3">
            <div class="row news-box no-margin flex-row flex-nowrap">
                <div class="card shadow-sm article col m-2">
                    <div class="card-body">
                        <div class="news-article">
                            The Economic Times
                        </div>
                    </div>
                    <div class="card-footer white-background">
                        <div class="news-source">
                            The Economic Times
                        </div>
                    </div>
                </div>
                <div class="card shadow-sm article col m-2">
                    <div class="card-body">
                        <div class="news-article">
                            VcCircle
                        </div>
                    </div>
                    <div class="card-footer white-background">
                        <div class="news-source">
                            VcCircle
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </body>
</html>

关于html - Bootstrap - 使卡片水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55354584/

相关文章:

html - 在 Bootstrap 网格中添加堆栈之间的间距

javascript - SVG 溢出在 chrome 中可见但在元素后面

javascript - 事件时更改菜单颜色项

html - 如何在现有类上强制执行 css 样式(尤其是在 Bootstrap 中)

html - CSS 动画跳跃

css - 如何在 Firefox 和 Opera 中缩放 HTML 元素?

html - 如何同时使用 css-modules 和 bootstrap?

java - Java不支持的主要次要版本52.0

html - 图像的响应列表

javascript - 我无法在打印预览中看到 div 的背景图像