1
votes

**EDIT: I have already been able to center the div with text and image but I want the background of the div to be full width. I can't make the logo and full width div with background and text align to center of body(responsive)

**EDIT 2: This is what I am trying to achieve: enter image description here

I have tried placing the div inside the logo's div since it is already in center

Snippet:

body {
  background-image: url('https://source.unsplash.com/random');
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  margin: 0;
}

div.container {
  position: absolute;
  top: 50%;
  left: 50%;
  overflow: visible;
}

img.logo {
  position: relative;
  max-width: 100%;
  max-height: 100%;
  margin-top: -50%;
  margin-left: -50%;
}

#text-container {
  background-color: rgba(0, 0, 0, 0.6);
}

#title-text {
  text-align: center;
  padding: 20px;
  color: #f09020;
  font-size: 1.5em;
  font-weight: 600;
  font-family: 'Source Sans Pro', sans-serif;
}

.sub-text {
  color: white;
  font-weight: 100 !important;
  font-size: 15pt;
  font-family: 'Open Sans Condensed', sans-serif;
}
<body>

  <div class="container">
    <img class="logo" src="http://placehold.it/300x200?text=Logo" />
  </div>

  <div id="text-container">
    <p id="title-text">CONSTRUCTION IN PROGRESS <br/><br/>
      <span class="sub-text">WE ARE CURRENTLY BUILDING EXCITING PROJECTS FOR YOU,<br/></span>
      <span class="sub-text">PLEASE CHECK BACK AGAIN SOON FOR OUR NEW AND IMPROVED WEBSITE.</span>
    </p>
  </div>
</body>

I want both logo and div with text on the center of body. Placing div under logo.

4
i didn't get what trying to achieve! please explain more!adel
@adel I am so sorry for that, I edited my question and added an image link as to what I want to achieve.TchMcy

4 Answers

2
votes

You can use flexbox property instead of positioning absolute,relative.

in body flex is used to position the img and text containers horizontally.

body {
   display: flex; // so that it's content can be centered;
   height: 100vh; // To make it fit the whole screen 
}

then I've added center div which contained the containers:

.center { 
      margin: auto;
      width: 100%; // to be full width
    }

body {
background-image: url("https://cdn.pixabay.com/photo/2016/06/17/06/04/background-1462755_960_720.jpg");
      background-position: center center;
      background-repeat: no-repeat;
      background-attachment: fixed;
      background-size: cover;
      margin: 0;
      display: flex;
      height: 100vh;
    }
    .center { 
      margin: auto;
      width: 100%;
    }
    .container { 
      display: flex;
    }

    img.logo {
      position: relative;
      max-width: 128px;
      max-height: 128px;
      margin: auto;
    }

    #text-container {
      background-color: rgba(0, 0, 0, 0.6);
    }

    #title-text {
      text-align: center;
      padding: 20px;
      color: #f09020;
      font-size: 1.5em;
      font-weight: 600;
      font-family: "Source Sans Pro", sans-serif;
    }

    .sub-text {
      color: white;
      font-weight: 100 !important;
      font-size: 15pt;
      font-family: "Open Sans Condensed", sans-serif;
    }
<div class="center">
        <div class="container">
            <img class="logo" src="http://ejad.solutions/cloud/elogo.jpg" />
          </div>
      
          <div id="text-container">
            <p id="title-text">
              CONSTRUCTION IN PROGRESS <br /><br />
              <span class="sub-text"
                >WE ARE CURRENTLY BUILDING EXCITING PROJECTS FOR YOU,<br
              /></span>
              <span class="sub-text"
                >PLEASE CHECK BACK AGAIN SOON FOR OUR NEW AND IMPROVED WEBSITE.</span
              >
            </p>
          </div>
    </div>
0
votes

added .flex container for center the .full-containerwhich contain your logo and text

your flex container should have full screen width and height. here i used width:100vw and height:100vh

*{
  margin:0;
  padding:0;
}
body {
  background-image: url('images/background.jpg');
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  margin: 0;
}
.flex{
  width:100vw;
  height:100vh;
  display:flex;
  align-items:center;
}
.full-container{
  width:100%;
}
img.logo {
    position:relative;
  display:block;
  margin: 0 auto;
  margin-bottom:20px;
    max-width:100%;
    max-height:100%;
}

#text-container{
  background-color: rgba(0, 0, 0, 0.6);
}

#title-text{
  text-align: center;
  padding: 20px;
  color: #f09020;
  font-size: 1.5em;
  font-weight: 600;
  font-family: 'Source Sans Pro', sans-serif;
}

.sub-text{
  color: white;
  font-weight: 100 !important;
  font-size: 15pt;
  font-family: 'Open Sans Condensed', sans-serif;
}
<body>
  <div class="flex">
<div class="full-container"> 
      <img class="logo" src="https://cdn.pixabay.com/photo/2017/03/19/20/19/ball-2157465__340.png"  width="150"/>
    <div id="text-container">
      <p id="title-text">CONSTRUCTION IN PROGRESS <br/><br/>
        <span class="sub-text">WE ARE CURRENTLY BUILDING EXCITING PROJECTS FOR YOU,<br/></span>
        <span class="sub-text">PLEASE CHECK BACK AGAIN SOON FOR OUR NEW AND IMPROVED WEBSITE.</span>
      </p>
    </div>
  </div>
  </div>
  </body>
0
votes

I recommend use transform: translateY(-50%); in container in this case because I think is not a good idea display flex all the body.

You must include img and text-container in the same layer to work together.

Tip: You may not use id for css attributes in HTML

Here you have the result

body, html {
  height: 100%;
}
body {
  background-image: url('http://lorempixel.com/1600/400/abstract/');
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  margin: 0;
}
.container {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  text-align:center;
}
#text-container {
  width:100%;
  background: red;
}
#text-container p{
  text-align: center;
  padding-top:40px;
  padding-bottom:40px;
}
.logo {
  max-width: 200px;
}



#text-container{
  background-color: rgba(0, 0, 0, 0.6);
}

#title-text{
  text-align: center;
  padding: 20px;
  color: #f09020;
  font-size: 1.5em;
  font-weight: 600;
  font-family: 'Source Sans Pro', sans-serif;
}

.sub-text{
  color: white;
  font-weight: 100 !important;
  font-size: 15pt;
  font-family: 'Open Sans Condensed', sans-serif;
}
<body>

    <div class="container">
      <img class="logo" src="http://ejad.solutions/cloud/elogo.jpg" />

    <div id="text-container">
      <p id="title-text">CONSTRUCTION IN PROGRESS <br/><br/>
        <span class="sub-text">WE ARE CURRENTLY BUILDING EXCITING PROJECTS FOR YOU,<br/></span>
        <span class="sub-text">PLEASE CHECK BACK AGAIN SOON FOR OUR NEW AND IMPROVED WEBSITE.</span>
      </p>
    </div>
   </div>
  </body>
0
votes

CSS:

<style type="text/css">
          body{
                background-image: url(https://cdn1.vectorstock.com/i/1000x1000/88/30/gray-checkers-background-empty-transparent-vector-14458830.jpg);
          }
          .ddd{
                margin: auto;
                width: 50%;
                height: 50%;
                border: 3px solid #2f2018;
                padding: 10px;
                background-color: #2f2018;
                text-align: center;
                color: #ffffff; 
          }
           .ddd1{
                margin: auto;
                margin-top: 20px;
                width: 100%;
                border: 3px solid #695547;
                padding: 10px;
                background-color: #695547;
                text-align: center;
                color: #ffffff; 
          }
          .asd1{
            padding-top: 50px;
          }
      </style>

HTML:

    <div class="asd1"> 
        <div class="ddd">
            <h1>LOGO HERE</h1>
        </div>
        <div class="ddd1">
            <h1 style="color: #ed8e32;">CONSTRUCTUION IN PROGRESS</h1>
            <h3>WE ARE CURRENTLYBUILDING EXCITING PROJECTS FPR YOU,<br>PLEASE CHECK BACK AGAIN SOON FOR OUR NEW AND IMPROVED WEBSITE</h3>
        </div>
    </div>