0

I'm trying to position one div in the middle of the page and the other one to the end.

I got this but i'm not able to position the first one on the center.

I've tried setting different aling self to the content_text but none of the thing i've tried work.

Hope you guys can help me. :)

html,body{
  height:100%;
  margin:0;
}

.content {
  align-items: center;
  background: rgb(0, 0, 0); /* Fallback color */
  background: rgba(0, 0, 0, 0.5); /* Black background with 0.5 opacity */
  color: #f1f1f1; /* Grey text */
  display: flex;
  flex-direction: column;
  height: 100vh;
  justify-content: space-between;
  position: absolute; /* Position the background text */
  width: 100%; /* Full width */
}

/* HELPERS */

.img-bg {
  display:block;
  height:100%;
  object-fit: cover;
  width:100%;
}
<!doctype html>
<html class="no-js" lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iraitz Puente</title>
  </head>
  <body>
    <div class="row fullWidth">
      <div class="content">
        <div class="content_text">
          <h1>THIS MUST BE CENTERED</h1>
        </div>
        <div class="content_arrow">
          <h1>END</h1>
        </div>
      </div>
    </div>
  </body>
</html>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Iraitz
  • 31
  • 6

4 Answers4

1

If you set the content to justify-content: flex-end;, then you can apply absolute positioning to the element that needs centering and then center it using a combination of top: 50% and transform: translateY(-50%);:

html,body{
  height:100%;
  margin:0;
}

.content {
  align-items: center;
  background: rgb(0, 0, 0); /* Fallback color */
  background: rgba(0, 0, 0, 0.5); /* Black background with 0.5 opacity */
  color: #f1f1f1; /* Grey text */
  display: flex;
  flex-direction: column;
  height: 100vh;
  justify-content: flex-end;
  position: absolute; /* Position the background text */
  width: 100%; /* Full width */
}

.content_text {
position: absolute;
top: 50%;
transform: translateY(-50%);
}

/* HELPERS */

.img-bg {
  display:block;
  height:100%;
  object-fit: cover;
  width:100%;
}
<!doctype html>
<html class="no-js" lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iraitz Puente</title>
  </head>
  <body>
    <div class="row fullWidth">
      <div class="content">
        <div class="content_text">
          <h1>THIS MUST BE CENTERED</h1>
        </div>
        <div class="content_arrow">
          <h1>END</h1>
        </div>
      </div>
    </div>
  </body>
</html>
0

simply remove the position: absolute style from the .content class

elraphty
  • 630
  • 6
  • 13
0

html,body{
  height:100%;
  margin:0;
}

.content {
  background: rgb(0, 0, 0); /* Fallback color */
  background: rgba(0, 0, 0, 0.5); /* Black background with 0.5 opacity */
  color: #f1f1f1; /* Grey text */
  display: flex;
  flex-direction: column;
  height: 100vh;
  justify-content: center;
  align-items: center;
  width: 100%; /* Full width */
}

/* HELPERS */

.img-bg {
  display:block;
  height:100%;
  object-fit: cover;
  width:100%;
}
<!doctype html>
<html class="no-js" lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iraitz Puente</title>
  </head>
  <body>
    <div class="row fullWidth">
      <div class="content">
        <div class="content_text">
          <h1>THIS MUST BE CENTERED</h1>
        </div>
        <div class="content_arrow">
          <h1>END</h1>
        </div>
      </div>
    </div>
  </body>
</html>
elraphty
  • 630
  • 6
  • 13
-2
<style>
.content {
    align-items: center;
    background: rgb(0, 0, 0);
    background: rgba(0, 0, 0, 0.5);
    color: #f1f1f1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    justify-content: center;
    position: absolute;
    width: 100%;
}
.contact .content_text,.contact .content_arrow{
    margin-top: auto;
}
</style>
user229044
  • 232,980
  • 40
  • 330
  • 338
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57