-1

I am having trouble centering content of one div inside of another because the content doesn't appear.

#searchbkg {
  postition: relative;
  width: 100%;
  height: 700px;
  background-color: #85e085;
}
#searchcentre {
  position: absolute;
  width: 50%;
  margin: 0 auto;
}
<div id="searchbkg">
  <div id="searchcentre">Test</div>
</div>

The green box appears but there is no text inside of it.

j08691
  • 204,283
  • 31
  • 260
  • 272
Louis C.
  • 31
  • 5

3 Answers3

1

Your text is appearing fine, but it won't be centered because you have position: absolute; on the inside div. Change it to position: relative; and it will center horizontally. If you need the text to be centered within the div, you can also apply a text-align: center;.

#searchbkg {
  position: relative;
  width: 100%;
  height: 700px;
  background-color: #85e085;
}
#searchcentre {
  position: relative;
  width: 50%;
  margin: 0 auto;
  text-align: center;
  border: 1px solid red;
}
<div id="searchbkg">
  <div id="searchcentre">This is a centered div!</div>
</div>
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
1

You need to make following 3 changes to make your content in center;

  1. You have typo in one css property inside styles of #searchbkg. There is postition while it should be position.
  2. Remove position: absolute from #searchcentre if not needed (Absolute positioning should be used only if you wants to place one element over another).
  3. Add text-align: center in #searchcentre.

#searchbkg{
  position: relative;
  width: 100%;
  height: 700px;
  background-color: #85e085;
}

#searchcentre{
  text-align: center;
  background: orange;
  width: 50%;
  margin: 0 auto;
}
<div id="searchbkg">
  <div id="searchcentre">Test</div>
</div>
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
0

try this:

#searchbkg{
  postition: relative;
  width: 100%;
  height: 700px;
  background-color: #85e085;
  text-align:center;
}

#searchcentre{
  display: inline-block;
  width: 50%;
}
<div id="searchbkg">
    <div id="searchcentre">Test</div>
  </div>
hmak.me
  • 3,770
  • 1
  • 20
  • 33