2017-04-15 134 views
0

我可以使用一些幫助,因爲我是html編碼的新手。無論如何, 我已經做了一個帶引導的旋轉木馬。傳送帶本身工作正常。但在旋轉木馬中放置一個按鈕後(使用top:(px)和right:(px),因爲我不知道如何將它放在我想要的地方),但它仍然可以正常工作。直到我開始調整窗口大小。背景變小,但按鈕種類保持不變。所以它開始四處移動,曾經是彼此旁邊的2個按鈕,現在是2個按鈕,它們之間有很多空間。在這裏,看看我的代碼: HTML如何防止在調整窗口大小時移動按鈕

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width = device-width, initial-scale = 1"> 
     <title>Galaxy</title> 
     <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
     <link rel="stylesheet" type="text/css" href="css/bootstrap.css"> 
     <link rel="stylesheet" type="text/css" href="css/style.css"> 
     <style type="text/css"> 
</style> 
    </head> 
     <body> 
      <div id="theCarousel" class="carousel slide" data-ride="carousel"> 
       <ol class="carousel-indicators"> 
        <li data-target="#theCarousel" data-slide-to="0" class="active"></li> 
        <li data-target="#theCarousel" data-slide-to="1"></li> 
       </ol> 
       <div class="carousel-inner"> 
        <div class="item active"> 
         <img src="galaxy3.jpg"> 
         <div class="slide1"></div> 
         <div class="carousel-caption"> 
          <div class="bannertext"> 
           <h1>Santorodesign</h1> 
           <p>A website made by Michael</p> 
           <div class="mobileHide"> <button id="headerbutton-nederlands">Nederlands</button></div> 
           <div class="mobileHide"> <button id="headerbutton-english">English</button></div> 
          </div>        
         </div> 
       </div> 
       <div class="item"> 
        <div class="slide2"></div> 
        <img src="galaxy2.jpg"> 
        <div class="carousel-caption"> 
         <div class="bannertext2"> 
          <h1>Explore the galaxy<h1> 
        </div> 
       </div>   
      </div> 

       <a class="left carousel-control" href="#theCarousel" data-slide="prev"> 
        <span class="glyphicon glyphicon-chevron-left"></span> 
       </a> 

       <a class="right carousel-control" href="#theCarousel" data-slide="next"> 
        <span class="glyphicon glyphicon-chevron-right"></span> 
       </a>    
      </div> 
     </div> 
     <div id="firstrow"> 
      <div class="planet col-lg-4 col-md-4 col-sm-6 col-xs-12"> 
       <img id="mercury" src="mercury.png"> <br> 
       (text) <br> 
       <a href="#"><button type="submit" class="btn-primary btn-lg">Learn more about Mercury</button></a> 
      </div> 
      <div class="planet col-lg-4 col-md-4 col-sm-6 col-xs-12"> 
       <img id="earth" src="earth.png"> <br> 
       (text)<br> 
       <a href="#"><button type="submit" class="btn-primary btn-lg">Learn more about the Earth</button></a> 
      </div> 
      <div class="planet col-lg-4 col-md-4 col-sm-6 col-xs-12"> 
       <img id="venus" src="venus.png"> <br> 
       (text) <br> 
       <a href="#"><button type="submit" class="btn-primary btn-lg">Learn more about Venus</button></a> 
      </div> 
     </div>   
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
     <script src="js/bootstrap.min.js"></script> 
    </body> 
</html> 

CSS

.carousel-control.left, .carousel-control.right { 
    background-image: none 
} 
#firstrow { 
    font-size: 2em; 
    padding: 100px 0 0 0; 
    width: 100%; 
    height: 550px; 
    text-align: center; 
    color: black; 
    background: #DCDCDC; 
    position: relative; 
} 
@media all and (max-width: 900px) { 
    #headerbutton-nederlands { display: none; } 
    #headerbutton-english { display: none; } 
} 
.planet { 
    margin-bottom: 30px; 
    position: relative; 
} 
    .planet img { 
     height: 300px; 
     max-width: none; 
    } 

    .bannertext { 
     font-size: 1.3em; 
     line-height: 15px; 
    } 
    .bannertext h1 { 
     font-size: 2em; 
    } 
    .bannertext2 { 
     font-size: 2em; 
    } 
.mobileShow { display: none;} 
.mobileHide { display: inline;} 
    /* Smartphone Portrait and Landscape */ 
    @media only screen 
     and (max-device-width : 480px){ 
     .mobileShow { display: inline;} 
     .mobileHide { display: none;} 
     .planet img{ 
      height: 200px; 
     } 
     } 
#headerbutton-nederlands { 
    position: absolute; 
    font-weight: bold; 
    bottom: 35px; 
    right: -90px; 
    -webkit-border-radius: 5px; 
    line-height: 50px; 
    color: white; 
    background-color: #778899; 
    width: 13%; 
    text-align: center; 
    border: white 2px solid ;  
} 
#headerbutton-english { 
    position: absolute; 
    font-weight: bold; 
    bottom: 35px; 
    right: 100px; 
    -webkit-border-radius: 5px; 
    line-height: 50px; 
    color: white; 
    background-color: #778899; 
    width: 13%; 
    text-align: center; 
    border: white 2px solid ; 
} 

基本上是我想要的是按鍵+的按鈕中的文本,當窗口變小也調整。就好像按鈕是圖像的一部分一樣。

謝謝您的時間。 -Michael

+0

由2個非常不同的答案來看,它可能用http://codepen.io或http://jsfiddle.net演示來澄清你的問題是個好主意 – ryantdecker

+0

可能與[此問題]重複(http://stackoverflow.com/questions/1495407/maintain-the對的一-DIV與 - CSS -aspect比)。如果你打算將來編寫SCSS而不是純CSS,你也可以檢查[this mixin](https://css-tricks.com/snippets/sass/maintain-aspect-ratio-mixin/)。 – andreim

回答

0

如果您希望相對於屏幕大小縮放按鈕,則需要使用相對單位而不是像素來對其進行設置。您可以使用%將它們相對於其父元素emrem進行尺寸調整,以根據字體大小調整它們的大小,或者 - 最有可能滿足您的需要 - vhvw將它們相對於高度和寬度的視口,分別。

假設您最初的風格上視工作1140px寬(只是一個任意起始點),下面的CSS可以讓你在正確的方向前進:

#headerbutton-nederlands { 
    position: absolute; 
    font-weight: bold; 
    bottom: 3vw; 
    right: -8vw; 
    -webkit-border-radius: .5vw; 
    line-height: 4.3vw; 
    color: white; 
    background-color: #778899; 
    width: 13%; 
    text-align: center; 
    border: white 2px solid ;  
} 
0

您需要將position: relative;屬性添加到.bannertext元素。當子元素完全位於相對位置的父元素內時,它會做的是,它確保子元素與父元素的邊界保持相對。因此,請將您的CSS中的.bannertext更改爲以下版本,它應該可以正常工作。

.bannertext { 
    font-size: 1.3em; 
    line-height: 15px; 
    position: relative; 
} 

您可能需要,但這樣又回到你想再要在正確的地方,以更新topright性能。

相關問題