2017-08-09 94 views
3

這是一項任務,使用Twitter Bootstrap設計一個簡單的響應式網頁,其中可摺疊菜單按鈕僅出現在額外的小設備上,只是在較大的設備上出現品牌名稱。我認爲我實現了這一目標。如何刪除我網頁右側的空白處?

但問題是這個惱人的白色空間在我的網頁右側,跨所有設備(我使用Chrome開發人員工具測試過)。

任何建議刪除空間,因爲它使網頁更緊湊,很好的view.Thanks提前。我附上了一個空白圖像和一個鏈接到我的代碼。

CSS代碼,

/*html 
{ 
    overflow-x: hidden; 
    overflow-y: scroll; 
}*/ 
body{ 
    font-family: 'Comfortaa', cursive; 
} 
/*Header Section*/ 
#brand-name{ 
    position: relative; 
    bottom:20px; 
    left: 1px; 
    text-decoration:none; 
} 
.nav-LLC{ 
    border-radius: 0px; 
} 


} 
#nav-list{ 
    margin-top: 10px; 
} 
#nav-list a { 
    text-align: center; 
    background-color: #e1e6ed; 
    font-size: 1.1em; 
    font-weight: bolder; 
} 
#nav-list a:hover{ 
    background-color: #96d4e8; 
} 
/*End of Header Section*/ 

/* Only for Extra Small Devices*/ 

@media (max-width: 500px){ 
    h1{ 
     font-size: 7vw; 
    } 
    h2{ 
     font-size: 7vw; 
    } 
} 

/*Start of Body Section*/ 
h2{ 
    margin-top: 10px; 
    margin-bottom: 20px; 
    text-align: center; 
    font-weight: bold; 
    font-color: #0b0821; 
} 
.row section{ 

} 
.row section p{ 
    text-align: justify; 
    background-color: #e1e6ed; 
} 
h4{ 
    position: relative; 
    left: 40%;*/ 
    font-weight: bold; 
    color: #0b2730; 
    /*background-color: #e1e6ed; 
    margin: 0px;*/ 
} 
/*End of Body Section*/ 

HTML代碼,

<header> 
    <nav id="nav-LLC" class="navbar navbar-default "> 
     <div class="container"> 
      <div class="navbar-header"> 
       <div class="navbar-brand"> 
        <a id="brand-name" href="index.html" ><h1> Food,LLC </h1></a> 
       </div> 

       <div class="button-class"> 
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#nav-collapsable" aria-expanded="false" > 
        <span class="sr-only"> Toggle Navigation</span> 
        <span class="icon-bar"></span> <!--Icon Bar is three lines in the icon--> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        </button> 
       </div> 

      </div><!-- Navbar-header --> 
      <section class="visible-xs"> 
      <div id="nav-collapsable" class="collapse navbar-collapse" > 
       <ul id="nav-list" class="nav navbar-nav navbar-right"> 
        <li><a href="#chicken"><span>Chicken</span></a></li> 
        <li><a href="#beef"><span>Beef</span></a></li> 
        <li><a href="#sushi"><span>Sushi</span></a></li> 
        <!--<li><a href="#"><span>Cookies</span><br></a></li>--> 
       </ul> 
      </div> 
      </section> 
     </div> 
    </nav> 
</header> 



<div class="container"> 
<h2>Our Menu</h2> 
<div class="row"> 
<section id="chicken" class="col-md-4 col-sm-6 col-xs-12"> 
<h4>Chicken</h4> 
<p>Do qui nulla et ullamco ut quis excepteur nulla amet. Eiusmod dolore tempor deserunt velit in nulla dolore ut duis dolor. Lorem ipsum duis ea ut occaecat cupidatat sint incididunt laborum ut duis ea cillum excepteur nulla velit ut. Veniam adipisicing proident esse tempor aliquip non nulla laboris esse dolore fugiat nostrud eu nulla consequat ut qui ad. Amet ad sint pariatur aliquip tempor mollit labore minim voluptate fugiat non.<a class="visible-xs" href="#nav-LLC" ><br>Back to top</a></p> 
</section> 

<section id="beef" class="col-md-4 col-sm-6 col-xs-12"> 
<h4>Beef</h4> 
<p>Do qui nulla et ullamco ut quis excepteur nulla amet. Eiusmod dolore tempor deserunt velit in nulla dolore ut duis dolor. Lorem ipsum duis ea ut occaecat cupidatat sint incididunt laborum ut duis ea cillum excepteur nulla velit ut. Veniam adipisicing proident esse tempor aliquip non nulla laboris esse dolore fugiat nostrud eu nulla consequat ut qui ad. Amet ad sint pariatur aliquip tempor mollit labore minim voluptate fugiat non.<a class="visible-xs" href="#nav-LLC" ><br>Back to top</a></p> 
</section> 

<section id="sushi" class="col-md-4 col-sm-12 col-xs-12"> 
<h4>Sushi</h4> 
<p>Do qui nulla et ullamco ut quis excepteur nulla amet. Eiusmod dolore tempor deserunt velit in nulla dolore ut duis dolor. Lorem ipsum duis ea ut occaecat cupidatat sint incididunt laborum ut duis ea cillum excepteur nulla velit ut. Veniam adipisicing proident esse tempor aliquip non nulla laboris esse dolore fugiat nostrud eu nulla consequat ut qui ad. Amet ad sint pariatur aliquip tempor mollit labore minim voluptate fugiat non.<a class="visible-xs" href="#nav-LLC" ><br>Back to top</a></p> 
</section> 

</div> 
</div> 


<script src="js/jquery-3.2.1.min.js"></script> 
<script src="js/bootstrap.min.js"></script> 

Link to HTMl and CSS codehttps://jsfiddle.net/w5v6yr9q/

link to the imagehttps://i.stack.imgur.com/ieRok.png

編輯:臨屋你太多人了!今天學到了新東西!

+0

似乎你忘了附件:) –

+0

這是因爲文本雞肉,牛肉和壽司在h4標籤,而不是給左:40%;嘗試text-align:center; – programmingArrow

+0

body元素對大多數瀏覽器都有標準的填充/邊距。添加:body {padding:0;保證金:0; } – Gerard

回答

1

什麼你需要知道Web中是否有HTML元素的默認CSS值,它們在d中隱藏了一些eveloper工具。 https://www.w3schools.com/cssref/css_default_values

爲了您的例子中,你需要添加兩件事情:一是刪除了body默認的保證金。這將刪除視口邊緣的白色間隙。

body { 
    margin: 0; 
} 

其次你h4被拉到因爲left: 40%權,居中他們靈活,對所有視口看看的folliwing代碼:

h4 { 
    position: relative /* remove this */; 
    left: 40%; /* remove this */ 

    text-align: center; /* add this */ 
} 

請記住這是一個更好在像.center-headline這樣的課程上採用風格,並在那裏添加您需要的風格。那麼你只需要複製&粘貼而不用擔心。

+0

感謝您的諮詢!我會記住這一點! –

0

刪除此行的CSS

CSS

h4{ 
left:40%;/*remove this*/ 
} 
0

根據你的jsfiddle,該段下的標籤(雞肉,牛肉,壽司)。你有位置相對的h4 css和「左:40%」。

給你所有的h4標籤一個類(例如「菜單標題」)。 並在下面給出

.menu-title { 
    position: static; 
    text-align: center; 
} 

的CSS希望它可以幫助

+0

謝謝!這工作!但爲什麼會發生? –

+0

@VigneshGunasekaran 「position:relative;」 &「剩下:40%;」意思是「將該元素從父元素總寬度的左側移動40%」。通過將其設置爲「position:static;」它會禁用「頂部,右側,左側,底部」屬性。 參考:https://www.w3schools.com/css/css_positioning.asp –

0

嘗試增加一個div像這樣:

<div class="container"> 
    <div class="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12"><!-- Add this Div--> 
    <h2>Our Menu</h2> 
    <div class="row"> 
     <section id="chicken" class="col-md-4 col-sm-6 col-xs-12"> 
     <h4>Chicken</h4> 
     <!-- following codes --> 

    </div> 
    </div> 
</div> 
0

請參考下面的H4風格:

h4{ 
    position: relative; 
    /* left: 40%;*/ /* this is the issue */ 
    font-weight: bold; 
    color: #0b2730; 
    text-align:center; /* if you want to center the text */ 
    /*background-color: #e1e6ed; 
    margin: 0px;*/ 
} 
+0

工作!非常感謝你!下次我會跟着這個! –

0

在這裏,你有一個解決方案去https://jsfiddle.net/w5v6yr9q/2/

/*html 
 
{ 
 
\t overflow-x: hidden; 
 
\t overflow-y: scroll; 
 
}*/ 
 
body{ 
 
\t font-family: 'Comfortaa', cursive; 
 
} 
 
/*Header Section*/ 
 
#brand-name{ 
 
\t position: relative; 
 
\t bottom:20px; 
 
\t left: 1px; 
 
\t text-decoration:none; 
 
} 
 
.nav-LLC{ 
 
\t border-radius: 0px; 
 
} 
 

 

 
} 
 
#nav-list{ 
 
\t margin-top: 10px; 
 
} 
 
#nav-list a { 
 
    text-align: center; 
 
    background-color: #e1e6ed; 
 
    font-size: 1.1em; 
 
    font-weight: bolder; 
 
} 
 
#nav-list a:hover{ 
 
\t background-color: #96d4e8; 
 
} 
 
/*End of Header Section*/ 
 

 
/* Only for Extra Small Devices*/ 
 

 
@media (max-width: 500px){ 
 
\t h1{ 
 
\t \t font-size: 7vw; 
 
\t } 
 
\t h2{ 
 
\t \t font-size: 7vw; 
 
\t } 
 
} 
 

 
/*Start of Body Section*/ 
 
h2{ 
 
\t margin-top: 10px; 
 
\t margin-bottom: 20px; 
 
\t text-align: center; 
 
\t font-weight: bold; 
 
\t font-color: #0b0821; 
 
} 
 
.row section{ 
 
\t 
 
} 
 
.row section p{ 
 
\t text-align: justify; 
 
\t background-color: #e1e6ed; 
 
} 
 
h4{ 
 
\t position: relative; 
 
\t font-weight: bold; 
 
\t color: #0b2730; 
 
\t /*background-color: #e1e6ed; 
 
\t margin: 0px;*/ 
 
} 
 
/*End of Body Section*/
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<header> 
 
\t <nav id="nav-LLC" class="navbar navbar-default "> 
 
\t \t <div class="container"> 
 
\t \t \t <div class="navbar-header"> 
 
\t \t \t \t <div class="navbar-brand"> 
 
\t \t \t \t \t <a id="brand-name" href="index.html" ><h1> Food,LLC </h1></a> 
 
\t \t \t \t </div> 
 
\t \t \t \t \t 
 
\t \t \t \t <div class="button-class"> 
 
\t \t \t \t \t <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#nav-collapsable" aria-expanded="false" > 
 
\t \t \t \t \t <span class="sr-only"> Toggle Navigation</span> 
 
\t \t \t \t \t <span class="icon-bar"></span> <!--Icon Bar is three lines in the icon--> 
 
\t \t \t \t \t <span class="icon-bar"></span> 
 
\t \t \t \t \t <span class="icon-bar"></span> 
 
\t \t \t \t \t </button> 
 
\t \t \t \t </div> 
 
\t \t 
 
\t \t \t </div><!-- Navbar-header --> 
 
\t \t \t <section class="visible-xs"> 
 
\t \t \t <div id="nav-collapsable" class="collapse navbar-collapse" > 
 
\t \t \t \t <ul id="nav-list" class="nav navbar-nav navbar-right"> 
 
\t \t \t \t \t <li><a href="#chicken"><span>Chicken</span></a></li> 
 
\t \t \t \t \t <li><a href="#beef"><span>Beef</span></a></li> 
 
\t \t \t \t \t <li><a href="#sushi"><span>Sushi</span></a></li> 
 
\t \t \t \t \t <!--<li><a href="#"><span>Cookies</span><br></a></li>--> 
 
\t \t \t \t </ul> 
 
\t \t \t </div> 
 
\t \t \t </section> 
 
\t \t </div> 
 
\t </nav> 
 
</header> 
 

 

 

 
<div class="container"> 
 
<h2>Our Menu</h2> 
 
<div class="row"> 
 
<section id="chicken" class="col-md-4 col-sm-6 col-xs-12"> 
 
<h4 class="text-center">Chicken</h4> 
 
<p>Do qui nulla et ullamco ut quis excepteur nulla amet. Eiusmod dolore tempor deserunt velit in nulla dolore ut duis dolor. Lorem ipsum duis ea ut occaecat cupidatat sint incididunt laborum ut duis ea cillum excepteur nulla velit ut. Veniam adipisicing proident esse tempor aliquip non nulla laboris esse dolore fugiat nostrud eu nulla consequat ut qui ad. Amet ad sint pariatur aliquip tempor mollit labore minim voluptate fugiat non.<a class="visible-xs" href="#nav-LLC" ><br>Back to top</a></p> 
 
</section> 
 

 
<section id="beef" class="col-md-4 col-sm-6 col-xs-12"> 
 
<h4 class="text-center">Beef</h4> 
 
<p>Do qui nulla et ullamco ut quis excepteur nulla amet. Eiusmod dolore tempor deserunt velit in nulla dolore ut duis dolor. Lorem ipsum duis ea ut occaecat cupidatat sint incididunt laborum ut duis ea cillum excepteur nulla velit ut. Veniam adipisicing proident esse tempor aliquip non nulla laboris esse dolore fugiat nostrud eu nulla consequat ut qui ad. Amet ad sint pariatur aliquip tempor mollit labore minim voluptate fugiat non.<a class="visible-xs" href="#nav-LLC" ><br>Back to top</a></p> 
 
</section> 
 

 
<section id="sushi" class="col-md-4 col-sm-12 col-xs-12"> 
 
<h4 class="text-center">Sushi</h4> 
 
<p>Do qui nulla et ullamco ut quis excepteur nulla amet. Eiusmod dolore tempor deserunt velit in nulla dolore ut duis dolor. Lorem ipsum duis ea ut occaecat cupidatat sint incididunt laborum ut duis ea cillum excepteur nulla velit ut. Veniam adipisicing proident esse tempor aliquip non nulla laboris esse dolore fugiat nostrud eu nulla consequat ut qui ad. Amet ad sint pariatur aliquip tempor mollit labore minim voluptate fugiat non.<a class="visible-xs" href="#nav-LLC" ><br>Back to top</a></p> 
 
</section> 
 

 
</div> 
 
</div> 
 

 

 

 
<script src="js/jquery-3.2.1.min.js"></script> 
 
<script src="js/bootstrap.min.js"></script>

刪除左:40%;h4CSS

而是使用bootstrap文本中心HTMLh4標籤。這是一個乾淨的方法。