2017-04-21 40 views
0

我正在研究一個網站的小設計,網站的主要內容位於網站寬度的大約80%的正方形內,然後通過線路發送頁眉,頁腳和兩個側欄。我有標題,頁腳,行和內容全部正常工作,並且它們都(大部分)在調整大小時保留在正確的位置。然而,我不能在我的生活中弄清楚如何讓中心的方塊繼續填充屏幕中間的部分(沒有重疊,或者沒有滾動條!)響應式的基於形狀的佈局?

I'我附上了我的廣場的代碼和我正在嘗試做的事情以及下面發生的事情的截圖。除了我的innercontent div之外,在側面沒有div,以及上下的頁眉和頁腳div。

*更新:按要求添加代碼。我相信這就是我在屏幕截圖中提供的示例中使用的所有內容,而不是通過某些JS生成的行。我已經包含在這裏太重要的CSS,所以讓我與它的所有鏈接到我的JS小提琴:

/* =================== 
 
    Inital Styles 
 
    =================== */ 
 

 
html { 
 
    color: #fff; 
 
    font-size: 1em; 
 
    background: #3f51b5; 
 
    line-height: 1.4; 
 
    animation: fade 5s 
 
} 
 

 
::-moz-selection { 
 
    background: #6534ff; 
 
    text-shadow: none; 
 
} 
 

 
::selection { 
 
    background: #3f51b5; 
 
    text-shadow: none; 
 
} 
 

 
hr { 
 
    display: block; 
 
    height: 1px; 
 
    border: 0; 
 
    border-top: 1px solid #ccc; 
 
    margin: 1em 0; 
 
    padding: 0; 
 
} 
 

 
audio, 
 
canvas, 
 
iframe, 
 
img, 
 
svg, 
 
video { 
 
    vertical-align: middle; 
 
} 
 

 
fieldset { 
 
    border: 0; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
textarea { 
 
    resize: vertical; 
 
} 
 

 
.browserupgrade { 
 
    margin: 0.2em 0; 
 
    background: #ccc; 
 
    color: #000; 
 
    padding: 0.2em 0; 
 
} 
 

 
body { 
 
    font: 16px/26px Helvetica, Helvetica Neue, Arial; 
 
} 
 

 
h1, 
 
h2, 
 
h3, 
 
h4 { 
 
    color: #fff; 
 
    font-family: 'Montserrat', sans-serif; 
 
    line-height: normal; 
 
} 
 

 
h1 { 
 
    font-size: 25px; 
 
    letter-spacing: 3.1; 
 
} 
 

 
h2 { 
 
    font-size: 14px; 
 
    font-weight: 500; 
 
    text-align: justify; 
 
} 
 

 
h3 { 
 
    font-family: 'Space Mono', monospace; 
 
    font-weight: 400; 
 
} 
 

 
h4 { 
 
    font-size: 19px; 
 
} 
 

 
.inline { 
 
    display: inline; 
 
} 
 

 
.clear { 
 
    clear: both; 
 
    display: block; 
 
    content: ""; 
 
} 
 

 
.center { 
 
    margin: auto; 
 
    width: 50%; 
 
    padding: 10px; 
 
} 
 

 
.header-container { 
 
    height: 145px; 
 
    position: relative; 
 
} 
 

 
.header-center { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
} 
 

 
.title { 
 
    color: white; 
 
    text-align: center; 
 
    font-size: 40px; 
 
    font-family: 'Space Mono', monospace; 
 
    font-weight: 400; 
 
    line-height: 0px; 
 
    margin-top: 35px; 
 
} 
 

 
.subTitle { 
 
    color: white; 
 
    text-align: center; 
 
    font-size: 14px; 
 
    font-family: 'Space Mono', monospace; 
 
    font-weight: 400; 
 
    font-style: italic; 
 
} 
 

 
.innercontent { 
 
    position: relative; 
 
    height: 3vw; 
 
    width: 80%; 
 
    margin: auto; 
 
    padding-top: 32.5vw; 
 
    margin-top: -28px; 
 
    margin-left: 10.0%; 
 
} 
 

 
.green { 
 
    background: #42ab9e; 
 
} 
 

 

 
/* =================== 
 
    Name Collection 
 
    =================== */ 
 

 
#nameCollection { 
 
    text-align: center; 
 
} 
 

 
#nameText { 
 
    margin-top: -28%; 
 
    margin-left: 15%; 
 
    position: absolute; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    left: 0; 
 
    right: 0; 
 
} 
 

 
.nameSkipButton { 
 
    margin-top: 25px; 
 
    background-size: 210% 100%; 
 
    background-image: linear-gradient(to right, #963019 50%, #16174f 50%); 
 
    -webkit-transition: background-position 0.3s; 
 
    -moz-transition: background-position 0.3s; 
 
    transition: background-position 0.3s; 
 
    border: none; 
 
    font-family: 'Space Mono', monospace; 
 
    color: #fff; 
 
    padding: 10px; 
 
    font-size: 12px; 
 
} 
 

 
.nameSkipButton:hover { 
 
    background-position: 99%, 0; 
 
}
<body> 
 
    <div class="delay"> 
 
    <div class="fade-in delay"> 
 
     <div class="header-container"> 
 
     <header class="header-center"> 
 
      <h1 class="title" onclick="location.reload();" style="cursor: pointer;">COMPUTERIFY.IO</h1> 
 
      <p class="subTitle" onclick="location.reload();" style="cursor: pointer;">Alpha is just a fancy word for unfinished.</p> 
 
      <p class="subTitle"> 
 
      </h1> 
 
     </header> 
 
     </div> 
 
     <div class="main-container"> 
 
     <div id="content" class="innercontent green"> 
 
      <div id="nameCollection"> 
 
      <div id="nameText"> 
 
       <h3>Hello. I'm the PC Generator, or PCG for short. Before we get started, can I just ask...</h3> 
 
       <h1>What's your first name?</h1> 
 
       <div> 
 
       <input type="button" style="display:none" id="btnSearch" value="Search" onclick="getUserName()" /> 
 
       <span class="input input--nameCollection"> 
 
         <input class="input__field input__field--nameCollection" maxlength="19" onkeydown = "if (event.keyCode == 13) document.getElementById('btnSearch').click()" type="text" id="input-25" /> 
 
         <label class="input__label input__label--nameCollection" for="input-25"> 
 
         <span class="input__label-content input__label-content--nameCollection">First Name</span> 
 
       </label> 
 
       </span> 
 
       </div> 
 
       <input class="nameSkipButton" id="skipName" type="button" value="I don't value my name, pick one for me" onclick="confirmSkip();" /> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div>

https://jsfiddle.net/nxyg4a9x/2/

Attached screenshots demonstrating the design when it works (on a 1920x1080 display, and how it looks on resize and on mobile.

+0

歡迎來到StackOverflow,你的問題應該包含[**最小,完整和可驗證的示例**](http://stackoverflow.com/help/mcve)。 如果您希望您的網站具有響應能力,則使用絕對定位進行頁面佈局的FWIW是一個糟糕的選擇。當視口尺寸發生變化時,元素無法相互正確反應。 – hungerstar

+0

道歉,如果你願意,我可以做一個jsfiddle嗎?你會推薦我用什麼來定位我的div,讓它們更具響應性? –

+0

如果可以,創建一個Stack Snippet。建議將遵循。 – hungerstar

回答

0

如果可以的話,我會用flexbox

以下是演示,您可能需要進行調整以適應您的需求。

* { 
 
    box-sizing: border-box; 
 
} 
 

 
body { 
 
    display: flex; 
 
    flex-direction: column; 
 
    margin: 0; 
 
    min-height: 100vh; 
 
    overflow: hidden; 
 
    background-color: #3f51b5; 
 
} 
 

 
header, 
 
main, 
 
footer { 
 
    margin: 0 10%; // Indirect way of making element width: 80%; 
 
    border-left: 2px solid lightgray; 
 
    border-right: 2px solid lightgray; 
 
} 
 

 
header, 
 
footer { 
 
    flex-basis: 100px; 
 
} 
 

 
main { 
 
    position: relative; 
 
    flex-grow: 1; 
 
    background-color: #42ab9e; 
 
} 
 

 
main:before { 
 
    content: ''; 
 
    z-index: -1; 
 
    position: absolute; 
 
    top: -2px; 
 
    right: -100%; 
 
    bottom: -2px; 
 
    left: -100%; 
 
    display: block; 
 
    border-top: 2px solid lightgray; 
 
    border-bottom: 2px solid lightgray; 
 
}
<header></header> 
 
<main></main> 
 
<footer></footer>

  • ,我們做的是使用flex-grow: 1main最重要的事情既解決您的伸展你的內容區域,填補了適當的空間的首要問題。 flex-grow: 1告訴該元素佔用其父元素內的任何剩餘空間。因此,從headerfooter的高度剩下的任何空間都會被記錄下來。
  • 我們使用min-heightbody爲出發點,使main不會落得一個固定的大小(這是什麼會發生,如果我們使用height代替)和佈局初步填充視即使有也不是很多的內容。
  • 您提到使用JS來創建您的線條,這似乎對我來說過分誇張,可以用CSS來完成。線只是邊界,唯一不明顯的一組線將是延伸到main之外的水平線。我用一個僞元素來做這件事,這個僞元素伸展在它的父元素之外。僞元素的頂部和底部被拉到其包含與邊界厚度相等的元素之外。僞元素的左側和右側被拉出足夠寬,總是延伸到視口窗口一點點(我使用了一個相對單位,因此它將與視口一起增長)。我們在body上使用overflow: hidden;來阻止此元素最初創建的水平滾動條。

希望這適用於你,讓我知道如果你有任何問題,歡呼!

+0

嗨。對延遲迴應抱歉。我花了幾個小時重新編寫我的網站,以實現您提供給我的上述代碼,並且它的工作效果非常好,直到達到最佳效果。我很欣賞你說過它可以更好地使用CSS,而且你是對的,它確實如此。但是,我一直在使用JS插件來製作線條。這意味着即使內容沒有,線條仍保持準確的位置。有什麼方法可以對此進行排序,還是我必須丟失動畫? 我已經將我的代碼上傳到了一個git,並且有一個活動版本的鏈接。 [Github](https://github.com/joshcawthorne/pcify.io) –

0

在你.innercontent div,你正在使用代表「視口寬度」的vw度量 - 基本上這意味着你盒子的高度是多少將與用戶正在查看該網站的屏幕/視口的寬度有關。

您需要在除vw以外的其他單位中設置高度,寬度邊距等,因爲它會導致您看到。我把你的小提琴展示出來。

https://jsfiddle.net/550n5wgn/1/

+0

我編輯過我的代碼,現在使用「height:80%;」,但是仍然會出現同樣的問題,並且在調整大小時不會繼續填充80%的屏幕? –

+0

「computerify.io」應該是綠色的還是外部的?如果在裏面,你需要把這些標題放在綠色框的div中。 – kawnah

+0

它應該在外面。如果您查看imgur鏈接,您可以看到我已經離開jsfiddle的那些行,但它基本上是菜單/標題。 –