简体   繁体   中英

Twitter Bootstrap: Unable to make website responsive for smaller device

I am developing a project for school and I am pretty new to Bootstrap and I keep having some problems with scaling the website for different resolutions. When I change it to mobile the images go on top of the text. If anybody could help me I would appreciate it. I have tried everything and still cant find a solution.

<body>
  
<div class="container">
    
<nav class="navbar-fixed-top sticky-top navbar" style="width: 100%; background-color: white; box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);"> 
    
    <div class="navbar-header">
    
        <a class="navbar-brand"><img src="transferir.png" alt="" style="height: 65; width: 60px"></a>
        
    </div> 
    
    <div>
        <ul id="menu">
            <li><a href="#home">Home</a></li>
            <li><a href="#sobre">About</a></li>
            <li><a href="#features">Features</a></li>
            <li><a href="#contact">Contacta-nos</a></li>
        </ul>    
    </div>
        
</nav>


    
<div class="site-index">
    
    <div id="home" class="block home-block">
        <div class="container">
            <div class="col-sm-6  left-block">
                <div class="text-centered">
                    <h1>Texter</h1>
                    <p class="info-text">Send text messages, voice messages, video messages or video call with all your friends and family easily, quickly and securely.</p>
                    
                    <p class="Medium-text">Download Em Breve</p>
                    <a href="https://play.google.com/?hl=pt-PT" target="_blank"><img src="playstore.png" alt="Playstore" class="d-img"></a>
                    <a href="https://www.apple.com/pt/ios/app-store/" target="_blank"><img src="appstore.png" alt="Apple App Store" class="d-img"></a>
                </div>
            </div>
            <div class="col-sm-5  right-block">
                <img src="phones.png" style="height: 350px; float: right; vertical-align: middle; width: auto !important; position: relative">            
            </div>   
        </div>
        <hr class="sombra">
    </div>
</div>

Css

html{
   scroll-behavior: smooth;
}

body{
    padding-top: 1%;
    font-family: 'Lato', sans-serif;
}

.block{
    padding: 35px;
}

.home-block{
    min-height: calc(100vh - 90px);
}

#home .container{
    height: 500px;  
}

.left-block{
    text-align: center;
    top: 30%;     
}

.right-block{
    bottom: 35%;
    margin-left: 25%;
}

.container{
    padding-right: 15px;
    padding-left: 15px;
    margin-right: auto;
    margin-left: auto;
}

Desktop

When i squish the page

You can use

<head>
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>

See this and this

First of all, you probably forgot to include <div class="row"></div> wrapper inside your <div class="container">...</div> element, just as it says here .

Secondly, I strongly recommend you to not play too much with CSS properties such as position: relative / absolute , top: ...; left: ...; right: ...; bottom: ... top: ...; left: ...; right: ...; bottom: ... top: ...; left: ...; right: ...; bottom: ... , because most of them break the CSS native document flow and they should be used only when other tools do not help much.

I suggest you reading this series of articles if you have enough time: CSS layout

I turned off most of the properties of that kind and it already looks much nicer:

通过禁用某些 CSS 属性更正了 HTML 布局

This answer would be just be a massive advice if I wouldn't provide some code help, so here it is.

Start by disabling these properties in DevTools:

.home-block{
    /* min-height: calc(100vh - 90px); */
}

#home .container{
    /* height: 500px; */
}

.left-block{
    /* text-align: center; */
    /* top: 30%; */
}

.right-block{
    /* bottom: 35%; */
    /* margin-left: 25%; */
}

Fixing Bootstrap markup:

<div id="home" class="block home-block">
    <div class="container">
        <!-- Added this wrapper, changed .col-* classes to responsive -->
        <div class="row">
            <!-- Removed .left-block class -->
            <div class="col-sm-12 col-lg-6  left-block">
                <div class="text-centered">
                    <h1>Texter</h1>
                    <p class="info-text">Send text messages, voice messages, video messages or video call with all your friends and family easily, quickly and securely.</p>
                    <p class="Medium-text">Download Em Breve</p>
                    <a href="https://play.google.com/?hl=pt-PT" target="_blank"><img src="playstore.png" alt="Playstore" style="height: 40px;"></a>
                    <a href="https://www.apple.com/pt/ios/app-store/" target="_blank"><img src="appstore.png" alt="Apple App Store" style="height: 40px"></a>
                </div>
            </div>
            <!-- Removed .right-block class, added .text-centered class -->
            <div class="col-sm-12 col-lg-6 text-centered">
                <!-- Removed inline styles (bad practice), changed "height" to be an attribute -->
                <img src="phones.png" height="350">            
            </div>
        </div>
    </div>
</div>

Then you would get this picture (no interval between image and the button on the top):

图像和顶部按钮之间没有间隔

This one is solved by applying margin-top: ...px; to the image block, wrapped in @media query at .col-md-* resolutions and lower. For the exact values see Bootstrap grid options . For more info on applying @media queries see MDN docs

As for navigation bar, I first suggest you disabling padding-left on ul#menu element:

#menu {
    padding-left: 0;
}

Although it fixes it on sm resolutions, the navigation menu still wraps under the logo on resolutions less than about 520px . I suggest you imagine what to do with this occasion in your mind or in some markup service like https://app.diagrams.net/ and then develop what you decided to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM