Image extending beyond div when resizing window and change position

Viewed 39

I am trying to build a personal site, and just started with some HTML and CSS. The problem is when I resize the browser window, the mountain image chain position and the clouds shift their position, any ideas on how to fix this? Also I am new to stack so sorry if the way I am asking question is incorrect.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 Answers

You should use max-width:100% for your image.

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>

Related