【问题标题】:Div bootstrap background positionDiv bootstrap 背景位置
【发布时间】:2018-09-11 05:07:38
【问题描述】:

issue

我在使用此布局时遇到问题。我想在这些框的中心放置一张应用程序的图片。对于带有文本的部分,图像在背景的顶部和底部下方可见是很重要的。我已经尝试了所有作为背景和普通图像(位置、z-index 等)的东西。 我正在为 col 和 row 使用引导程序。我还需要这个在移动设备上看起来很漂亮。我需要设计来查看附加图片,但图片在背景前面的中间。有什么建议吗?

当前代码和 CSS:

<center> 
<div class="margin50">
<section class="appfeatures section-spacing">
<div class="container_appfeatures">
<div class="row">
    <div class="col-md-3 text-right"><h3 class="white">Headline</h3><br />text text text text text text text text text text text text text text text text text text </div>
    <div class="col-md-1 text-left"><img class="appicon_white" src="img/createtip_white.png" alt="Opret Tip Ikon"></div>
    <div class="col-md-offset-4 col-md-1 text-right"><img class="appicon_white" src="img/rank_white.png" alt="Rank List Ikon"></div>
    <div class="col-md-3 text-left"><h3 class="white">Headline</h3><br />text text text text text text text text text text text text text text text text text text </div>
    <div class="col-md-3 text-right"><h3 class="white">Headline</h3><br />text text text text text text text text text text text text text text text text text text </div>
    <div class="col-md-1 text-left"><img class="appicon_white" src="img/feed_white.png" alt="Live Score Ikon"></div>
    <div class="col-md-offset-4 col-md-1  text-right"><img class="appicon_white" src="img/profile_white.png" alt="Eksperter Ikon"></div>
    <div class="col-md-3 text-left"><h3 class="white">Headline</h3><br />text text text text text text text text text text text text text text text text text text </div>

</div>
</div>
</section>
</div>
</center>


.appicon_white {
width: 60px;
height: 60px;
    }

.appfeatures {

background: rgb(102,204,153);
background: linear-gradient(180deg, rgba(102,204,153,1) 0%, rgba(30,130,76,1) 100%);
opacity: 0.5;
}


.container_appfeatures {
width: 80%;

}



.margin50 {
margin-top: 75px;
margin-bottom: 75px;
width:100%; 
height:100%;
background-image: url("../img/profile.png");
background-repeat: no-repeat;
background-position: center center;
padding-top:95px;
padding-bottom:95px;
}

【问题讨论】:

    标签: html css bootstrap-4


    【解决方案1】:

    不要只使用col-md-offset-4 col-md-4 并且您必须在每一行中使用&lt;div class="row"&gt;。在这里我更新了你的 html 代码:

    <center>
        <div class="margin50">
            <section class="appfeatures section-spacing">
                <div class="container_appfeatures">
                    <div class="row">
                        <div class="col-md-3 col-sm-3 text-right">
                            <h3 class="white">Headline</h3>
                            <br />text text text text text text text text text text text text text text text text text text 
                        </div>
                        <div class="col-md-1 col-sm-1 text-center">
                            <img class="appicon_white" src="img/createtip_white.png" alt="Opret Tip Ikon">
                        </div>
                        <div class="col-md-4"> </div>
                        <div class="col-md-1 col-sm-1 text-center">
                            <img class="appicon_white" src="img/rank_white.png" alt="Rank List Ikon">
                        </div>
                        <div class="col-md-3 col-sm-3 text-left">
                            <h3 class="white">Headline</h3>
                            <br />text text text text text text text text text text text text text text text text text text 
                        </div>
                    </div>
                    <img src="https://path to your iphone image">
                    <div class="row">
                        <div class="col-md-3 col-sm-3 text-right">
                            <h3 class="white">Headline</h3>
                            <br />text text text text text text text text text text text text text text text text text text 
                        </div>
                        <div class="col-md-1 col-sm-1 text-center">
                            <img class="appicon_white" src="img/feed_white.png" alt="Live Score Ikon">
                        </div>
                        <div class="col-md-4"> </div>
                        <div class="col-md-1 col-sm-1 text-center">
                            <img class="appicon_white" src="img/profile_white.png" alt="Eksperter Ikon">
                        </div>
                        <div class="col-md-3 col-sm-3 text-left">
                            <h3 class="white">Headline</h3>
                            <br />text text text text text text text text text text text text text text text text text text 
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </center> 
    

    CSS 代码更新:

    .appicon_white {
        width: 40px;
        height: 40px;
    }
    
    .appfeatures {
        background: rgb(102, 204, 153);
        background: linear-gradient(180deg, rgba(102, 204, 153, 1) 0%, rgba(30, 130, 76, 1) 100%);
        opacity: 0.5;
        margin: 170px 0;
        position: relative;
    }
    
    .container_appfeatures {
        width: 80%;
        margin: 75px auto;
    }
    
    .margin50 {
        width: 100%;
        position: absolute;
        z-index: 5;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        -webkit-transform: translate(-50%, -50%);
        -moz-transform: translate(-50%, -50%);
        -o-transform: translate(-50%, -50%);
    }
    
    .margin50 img {
        max-width: 250px;
        width: 100%;
    }
    
    img.phone-middle {
        position: absolute;
        transform: translate(-50%, -50%);
        -webkit-transform: translate(-50%, -50%);
        -moz-transform: translate(-50%, -50%);
        -o-transform: translate(-50%, -50%);
    }
    

    Jsfiddle:https://jsfiddle.net/marksalvania/94uyLvp1/

    然后在您的移动/较小视图上,只需在您的媒体查询中添加如下内容:

    .col-md-3 {
        text-align:left;
        font-size: 10px; /*just adjust this to your desired size*/
    }
    
    .col-md-1,
    .margin50 {
        display:none; /*this is just optional if you just want hide icons on mobile view. Much better if you will hide the phone image on mobile view for sleek and clean layout.*/
    }
    
    .your-other-styles {
        /*add your other styles here*/
    }
    

    【讨论】:

    • Double .row 不是 Bootstrap 的方式。
    • 没看到,是的,你是对的,我只是在上面编辑了我的代码。谢谢!
    • 谢谢,但我仍然无法将图片置于背景色前面。你对此有什么建议?
    • @htobiassen 查看我上面更新的代码并检查这个小提琴:jsfiddle.net/marksalvania/94uyLvp1 告诉我是否正确。谢谢。
    • 嗨,马克,感谢您的回答。我已尝试使用更新的代码,但现在图片位于页面中间。而不是在 div/section 的中间。我认为问题在于页面还有其他内容,并且您发送的解决方案仅在页面上仅包含内容时才有效。即使页面上有其他内容,您知道如何将其仅放置在部分中间吗?
    猜你喜欢
    • 2015-03-27
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    相关资源
    最近更新 更多