Bootstrap - 使卡片可水平滚动

问题描述 投票:0回答:2

我正在尝试制作卡片旋转木马,但我无法保持内容的可读性。这是我目前的代码:

.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,但它无效。

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

任何帮助,将不胜感激。

提前致谢。

html css twitter-bootstrap
2个回答
1
投票

个人我建议使用省略号将此代码添加到.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>

0
投票

您正在使用错误的类属性请将className更改为class。我希望它能解决你的问题。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.