调整窗口大小和更改位置时图像超出 div

时间:2021-03-19 18:14:42

标签: html css

我正在尝试建立一个个人网站,并且刚开始使用一些 HTML 和 CSS。问题是当我调整浏览器窗口的大小时,山图像链位置和云彩会移动它们的位置,关于如何解决这个问题的任何想法?另外,如果我问问题的方式不正确,我是堆栈的新手,很抱歉。enter image description here

body {
  margin: 0;
  text-align: center;
}

h1 {
  margin-top: 0;
}

.web {
  text-decoration: underline;
}

.top-container {
  background-color: #ccf2f4;
  position: relative;
  padding-top: 100px;
  min-width: 100%;
}

.middle-container {
  background-color: red;
  height: 200px;
  width: 200px;
}

.bottom-container {
  background-color: blue;
  height: 200px;
  width: 200px;
}

.bottom-cloud {
  position: absolute;
  left: 300px;
  bottom: 300px;
}

.top-cloud {
  position: absolute;
  right: 300px;
  top: 50px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Prajwal Timsina</title>
  <link rel="stylesheet" href="css/styles.css">
  <link rel="icon" href="favicon.ico">
</head>

<body>
  <div class="top-container">
    <img class="top-cloud" src="img/cloud.png" alt="top-cloud">
    <h1>I am Prajwal.</h1>
    <p>a aspiring <span class="web">web</span> developer.</p>
    <img class="bottom-cloud" src="img/cloud.png" alt="bottom-cloud">
    <img src="img/mountain.png" alt="mountain">
  </div>

  <div class="middle-container">

  </div>
  <div class="bottom-container">

  </div>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

您应该使用 max-width:100% 作为您的图片。

body {
  margin: 0;
  text-align: center;
}

.bottom-img {
  max-width:100%; /* you should use */
}

h1 {
  margin-top: 0;
}

.web {
  text-decoration: underline;
}

.top-container {
  background-color: #ccf2f4;
  position: relative;
  padding-top: 100px;
  min-width: 100%;
}

.middle-container {
  background-color: red;
  height: 200px;
  width: 200px;
}

.bottom-container {
  background-color: blue;
  height: 200px;
  width: 200px;
}

.bottom-cloud {
  position: absolute;
  left: 300px;
  bottom: 300px;
}

.top-cloud {
  position: absolute;
  right: 300px;
  top: 50px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Prajwal Timsina</title>
  <link rel="stylesheet" href="css/styles.css">
  <link rel="icon" href="favicon.ico">
</head>

<body>
  <div class="top-container">
    <img class="top-cloud" src="img/cloud.png" alt="top-cloud">
    <h1>I am Prajwal.</h1>
    <p>a aspiring <span class="web">web</span> developer.</p>
    <img class="bottom-cloud" src="img/cloud.png" alt="bottom-cloud">
    <img class="bottom-img" src="https://siber.boun.edu.tr/sites/cyber.boun.edu.tr/files/sample6.jpg" alt="mountain">
  </div>

  <div class="middle-container">

  </div>
  <div class="bottom-container">

  </div>
</body>

</html>