當我在我的項目中實現bootstrap時,移動視圖和div的位置移動得很厲害。考慮我的代碼
table {
border-collapse: collapse;
margin: auto;
position: absolute;
width: 60%;
height: 60%;
table-layout: fixed;
}
td {
/* width: 50px;
height: 50px; */
text-align: center;
vertical-align: middle;
font-size: 24px;
font-weight: bold;
border: 1px solid #fff;
width: calc(100% / 8);
height: calc(100% / 8);
}
th {
font-family: 'Press Start 2P', cursive, "Arial", "sans-serif";
width: 60px;
height: 60px;
font-size: 24px;
font-weight: bold;
text-align: center;
border: 1px solid #fff;
color: #fff;
}
.k{
background-color: #5f4419;
opacity: 0.9;
}
#countDiv {
margin-top: 1rem;
display: flex;
align-items: center;
text-align: center;
justify-content: flex-start;
border: 0.5rem solid darkcyan;
padding: 1.5rem;
border-radius: 1rem ;
box-shadow: 0 0 0.625rem #006d82ff;
/* font-family: Arial, sans-serif; */
font-family: 'Press Start 2P', cursive, "Arial", "sans-serif";
font-size: 1.125rem;
background-color: #5f4419;
color: #fff;
border-style: inset;
text-shadow: 3px 3px 0 #363434;
}
/* Set the container to fill the entire viewport */
.container {
justify-content: center;
display: flex;
flex-direction: row;
height: 100%;
background-color: rgba(0, 109, 130, 1);
z-index: 1;
position: relative;
/* Task : for mobile view relative and for web view absolute */
width: 100%;
max-width: auto;
margin: 0 auto;
background-size: cover;
}
/* Set the game section to take up 50% of the width */
.game {
flex: 1;
height: 100%;
/* border: 1px solid black; */
justify-content: end;
position: relative;
width: 100%;
max-width: 100%; /* adjust as needed */
margin: auto;
background-size: cover;
}
.gameContainer {
margin-left: 20%;
margin-top: 21%;
position: relative;
width: 100%;
padding-bottom: 80%; /* set to the same value as width to create a square */
overflow: hidden;
z-index: 1;
position: relative;
}
.background-image {
position: absolute;
top: 0;
left: 0;
background-image: url('../images/TreassureIsland.png');
background-repeat: no-repeat;
background-size: cover;
width: 100%;
height: 100%;
z-index: 0;
}
/* stepsAway text image */
.text-layer {
font-family: 'Press Start 2P', cursive, "Arial", "sans-serif";
position: absolute;
top: 30%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 24px;
font-weight: bold;
color: rgb(255, 248, 246);
text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5);
}
/* Set the info section to take up 50% of the width */
.info {
flex: 1;
height: 100%;
/* border: 1px solid black; */
}
.hintContainer {
position: relative;
float: left;
flex: 1;
height: 100%;
/* margin-left: 5rem; */
margin-top: 5rem;
/* margin-bottom: 5rem; */
margin-left: 5rem;
/* position: absolute; */
/* padding-bottom: 80%; */
width: 70%;
z-index: 2;
}
.hint {
margin-top: 1rem;
display: flex;
align-items: center;
justify-content: flex-start;
border: 0.2rem solid darkcyan;
padding: 1.5rem;
border-radius:1rem;
box-shadow: 0 0 0.625rem #006d82ff;
/* font-family: Arial, sans-serif; */
font-family: 'Press Start 2P', cursive, "Arial", "sans-serif";
font-size: 1.125rem;
background-color: #006d82ff;
color: #fff;
border-style: inset;
text-shadow: 3px 3px 0 #363434;
}
.rules {
}
#foundTreasure {
text-align: center;
margin-top: 1rem;
/* margin-bottom: 150%; */
background-color: #cca669;
align-items: center;
justify-content: flex-start;
border: 0.3rem solid darkcyan;
border-radius: 1rem;
box-shadow: 0 0 0.625rem #006d82ff;
font-family: 'Press Start 2P', cursive, "Arial", "sans-serif";
border-style: inset;
text-shadow: 3px 3px 0 #363434;
color: #fff;
}
#stepsAway {
font-weight: bold;
margin: 0 0.3125rem;
}
/* #################################################################### */
/* Set the layout for smaller screens */
@media screen and (max-width: 1100px) {
.container {
flex-direction: column;
width: 100vh;
height: 100%;
justify-content: center;
/*
display: flex;
flex-direction: row;
background-color: rgba(0, 109, 130, 1);
z-index: 1; */
/* Remove position: relative and add min-height: 100vh */
min-height: 170vh;
max-width: 170%;
/* Task : for mobile view relative and for web view absolute */
/* width: 100%;
max-width: auto;
margin: 0 auto;
background-size: cover; */
}
.game {
flex: 0 0 auto;
height: 100%;
width: 100%;
}
.info {
flex: 0 0 auto;
height: 150%;
width: 110%;
}
.hint{
font-size: 2rem;
}
#foundTreasure {
font-size: 2rem;
}
}
/* Media query for screens up to 768px wide */
@media screen and (max-width: 768px) {
.container {
max-width: 90%;
}
.game, .info {
max-width: 90%;
}
}
/* Media query for screens up to 576px wide */
@media screen and (max-width: 576px) {
.container {
max-width: 100%;
padding: 0 10px;
}
.game, .info {
max-width: 100%;
padding: 0 10px;
}
}
/* button */
.play-again {
font-family: 'Press Start 2P', cursive, "Arial", "sans-serif";
border: none;
background-color: white;
color: #085d1b;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease-in-out;
position: relative;
border: 1px solid grey;
border-radius: 15px;
margin-bottom: 2rem;
box-shadow: 0 0 9px rgb(173, 173, 173);
}
.play-again:hover {
background-color: #006d82ff;
color: #fff;
box-shadow: 0 0 3px darkcyan;
}
.play-again::before {
content: "\f01e";
font-family: 'Press Start 2P', cursive, "Arial", "sans-serif";
font-weight: 900;
position: absolute;
top: 50%;
left: 20px;
transform: translateY(-50%);
transition: all 0.3s ease-in-out;
opacity: 0;
}
.play-again:hover::before {
transform: translateY(-50%) rotate(360deg);
opacity: 1;
}
<!DOCTYPE html>
<html>
<head>
<title>TreasureHunt</title>
<!-- <meta name="viewport" content="width=device-width, user-scalable=no"> -->
<!-- <meta name="viewport" content="width=device-width, initial-scale=1"> -->
<!-- custom css link -->
<link rel="stylesheet" href="./css/game_style.css">
<!-- google font -->
<link rel="stylesheet">
</head>
<body>
<div class="container">
<div class="info">
<div class="hintContainer">
<div class="rules hint">
<ul>
<h3> Rules for Treasure Hunt</h3><br>
<li>There is treasure hidden in one of the cells.</li><br>
<li>You can pick any cell</li><br>
<ul>
<li>if lucky, you land on the treasure.</li><br>
<li>if not, I'll tell you how far the treasure is away.</li>
</ul><br>
<li>The number in the square shows the number of steps to reach the treasure along the shortest path.</li><br>
<li> You can move up,down,left or right.You can't move diagonally. </li><br>
<h4> Can you find the hidden treasure?</h4>
</ul>
</div>
<div id="foundTreasure" style="display: none;" >
<h3 >Yay, you Found the Treasure! <br><span><img src="./images/Treasure.png" height='65' width='65' alt="treasure"></span> </h3>
</div>
<div class="hint" id="hint" style="display: none;">
<h3>INSTRUCTION </h3>
<p id="stepsAway" style="font-size: larger;"></p><br>
</div>
<div id="countDiv" style="display: none;">
<p id="count"></p>
<button class="play-again" onclick="window.location.href='../index.html'">Go Back</button>
<button class="play-again" onclick="location.reload()">Play Again</button>
</div>
</div>
</div>
<div class="game">
<div class="gameContainer">
<table>
<thead>
<tr>
<th class="k"></th>
<th class="k">A</th>
<th class="k">B</th>
<th class="k">C</th>
<th class="k">D</th>
<th class="k">E</th>
<th class="k">F</th>
<th class="k">G</th>
<th class="k">H</th>
<th class="k"></th>
</tr>
</thead>
<tbody>
<script src="./js/game_logic.js"></script>
</tbody>
<tfoot>
<tr>
<th class="k"></th>
<th class="k">A</th>
<th class="k">B</th>
<th class="k">C</th>
<th class="k">D</th>
<th class="k">E</th>
<th class="k">F</th>
<th class="k">G</th>
<th class="k">H</th>
<th class="k"></th>
</tr>
</tfoot>
</table>
</div>
<div class="background-image"></div>
</div>
</div>
</body>
</html>
你需要使用CSS媒體查詢來獲得一個有響應的網站。我建議在網上搜索并閱讀它們,但這可能是一個好的開始:https://CSS-tricks . com/a-complete-guide-to-CSS-media-queries/
但是,這很簡單。基本上,最好的精確方法是設計你的頁面,在你需要的最小屏幕上默認你想要的樣子。然后,隨著屏幕變大,你可以改變一些東西,以便在那些尺寸下工作得更好。
我看到你現在在你的代碼中反其道而行之,你將默認值設置為大屏幕,然后添加媒體查詢,當屏幕變小時,使用@media screen和(max-width: 1100px)覆蓋這些查詢
這里有一個例子:
/*Default styles for small screens*/
html, body {
padding: 0;
margin: 0;
}
#container {
background: #CCC;
height: 100vh;
width: 100%;
}
@media screen and (min-width: 550px){
/*Styles for screens that are 550px wide or larger*/
#container {
width: 500px;
margin: 0 auto;
}
}
@media screen and (min-width: 768px){
/*Styles for screens that are 768px wide or larger*/
#container {
width: 800px;
}
}
<div id="container"></div>
Bootstrap具有內置的響應樣式類,稱為斷點,使用12列布局來定義事物的外觀。這使您可以更改不同部分在不同屏幕尺寸上的外觀。例如,您可以使用& ltdiv class = & quotcol-12 col-MD-6 col-LG-4 col-XL-3 & quot;& gt...& lt/div & gt;在小屏幕上有一個100%寬度的div,在中等屏幕上有50%寬度的div,等等。
在你開始做真正新奇的事情之前,不需要媒體的詢問;這些實用程序類應該滿足大多數基本需求。
呼應@FiniteLooper的另一個答案,首先為移動屏幕設計你的網站,然后逐步調整到每個斷點,看看你需要調整布局的地方。向上移動要比向下移動容易得多。Pr o提示,iPhone屏幕(標準尺寸,不是plus/xl)是375px寬,所以我使用窗口中的Inspect Element/Developer工具并調整其大小,以便網頁正好是我想要的寬度(見下文):
你可以讓你的頁面有反應,你只需要使用媒體查詢來讓應用程序有反應。媒體查詢是基于顯示內容的設備特征(如屏幕大小、方向和分辨率)應用樣式的CSS規則。通過為不同的設備定義不同的樣式,您可以確保您的應用程序在所有設備上都很好看
我寫了一個例子給你檢查一下:
.container {
`background-color: rgba(0, 109, 130, 1);`
position: relative;
width: 100%;
}
.game {
background-color: #ffffff;
}
.hintContainer {
background-color: #ffffff;
margin-top: 1rem;
}
@media (min-width: 576px) {
/* For screens wider than 576px */
.game {
margin-left: 20%;
margin-top: 21%;
width: 80%;
}
.hintContainer {
margin-top: 5rem;
margin-left: 0;
padding-left: 2rem;
width: 30%;
}
}
@media (min-width: 992px) {
/* For screens wider than 992px */
.game {
width: 60%;
}
.hintContainer {
margin-top: 0;
margin-left: 5rem;
padding-left: 5rem;
width: 35%;
}
}