Geek Fools
Geek Fools

Reputation: 17

How can i fix this top sticking div at top

I am trying to make a portfolio website, as you can see my first div (typing about me) is coming below my second div. I want my first div to be visible in full screen in mobile and desktop.

var typed = new Typed('#type-1', {
  strings: [
    'Hey, <br> This is <strong id="ft1">Geekfool</strong>, <br>web developer!'
  ],
  typeSpeed: 100,
  loop: false,
  showCursor: false,
});
* {
  margin: 0px;
  padding: 0px;
}

* {
  box-sizing: border-box;
}

body {
  background: black;
  color: white;
  font-family: 'IBM Plex Mono', monospace;
}

.main {
  width: 100%;
  padding: 12px;
  margin: 0;
  margin: 0;
  position: absolute;
  top: 50%;
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}

.main h1 {
  font-size: 2.5em;
  font-family: 'Secular One', sans-serif;
}

#ft1 {
  color: #7d5fff;
  text-shadow: 2px 2px 0px #32ff7e;
}

.main h6 {
  animation-name: mini;
  animation-duration: 6s;
  color: white;
}

@keyframes mini {
  0% {
    opacity: 0;
  }
  25% {
    opacity: 0;
  }
  50% {
    opacity: 0;
  }
  75% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

.flex-container {
  display: flex;
  flex-direction: row;
  font-size: 30px;
  text-align: center;
}

.flex-item-left {
  background-color: #f1f1f1;
  padding: 10px;
  flex: 50%;
}

.flex-item-right {
  background-color: dodgerblue;
  padding: 10px;
  flex: 50%;
}


/* Responsive layout - makes a one column-layout instead of two-column layout */

@media (max-width: 800px) {
  .flex-container {
    flex-direction: column;
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@200&family=Secular+One&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="assets/styles.css">
</head>

<body>
  <div class="main">
    <!-- Head Section Name, Slogan -->
    <h1 id="type-1"></h1>
    <h6>Frintend Developer / Backend Developer / WordPress</h6>
  </div>

  <div class="flex-container">
    <!-- Bio Section and Profile Image -->
    <div class="flex-item-left">
      One
    </div>
    <div class="flex-item-right">
      Two
    </div>
  </div>
  <!-- Bio Section and Profile Image END -->

  <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
</body>

</html>

JS Fiddle

Upvotes: 0

Views: 526

Answers (1)

tacoshy
tacoshy

Reputation: 13001

Simply give the first div a height of min-height: 100vh;. Then remove the absolute positioning and use flexbox to center the text instead. See comments within CSS for full explaination.

var typed = new Typed('#type-1', {
  strings: [
    'Hey, <br> This is <strong id="ft1">Geekfool</strong>, <br>web developer!'
  ],
  typeSpeed: 100,
  loop: false,
  showCursor: false,
});
.main {
  min-height: 100vh; /* makes first div as height as the "screen" (viewport) */
  display: flex; /* to align text vertically and horizontally */
  flex-direction: column; /* to have the h6 text below the h1 text (normal block-level-element behavior) */
  justify-content: center; /* centers along main axis (vertically) */
  align-items: center; /* centers along side axis (horizontally */
}


/* orginial css */
* {
    margin: 0px;
    padding: 0px;
}
* {
   box-sizing: border-box;
}

body {
    background: black;
    color: white;
    font-family: 'IBM Plex Mono', monospace;
}
.main {
    width: 100%;
    padding: 12px;
    margin: 0;
    margin: 0;
  /* remove
    position: absolute;
    top: 50%;
    -ms-transform: translateY(-50%);
     transform: translateY(-50%); */
}
.main h1 {
    font-size: 2.5em;
    font-family: 'Secular One', sans-serif;
}
#ft1 {
    color: #7d5fff;
    text-shadow: 2px 2px 0px #32ff7e;
}
.main h6 {
    animation-name: mini;
    animation-duration: 6s;
    color: white;
}
@keyframes mini {
    0% { opacity: 0; }
    25% { opacity: 0; }
    50% { opacity: 0; }
    75% { opacity: 0; }
    100% { opacity: 1; }
}
.flex-container {
  display: flex;
  flex-direction: row;
  font-size: 30px;
  text-align: center;
}

.flex-item-left {
  background-color: #f1f1f1;
  padding: 10px;
  flex: 50%;
}

.flex-item-right {
  background-color: dodgerblue;
  padding: 10px;
  flex: 50%;
}

/* Responsive layout - makes a one column-layout instead of two-column layout */
@media (max-width: 800px) {
  .flex-container {
    flex-direction: column;
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@200&family=Secular+One&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="assets/styles.css">
</head>

<body>
  <div class="main">
    <!-- Head Section Name, Slogan -->
    <h1 id="type-1"></h1>
    <h6>Frintend Developer / Backend Developer / WordPress</h6>
  </div>

  <div class="flex-container">
    <!-- Bio Section and Profile Image -->
    <div class="flex-item-left">
      One
    </div>
    <div class="flex-item-right">
      Two
    </div>
  </div>
  <!-- Bio Section and Profile Image END -->

  <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
</body>

</html>

Upvotes: 1

Related Questions