0

How to move div id="test" to center of id="main" like image ?

<div id="main" 
     style="
            position: relative;
            display: inline-block;
            text-align: center;
            border: 1px solid;
        ">
  <img src="https://i.ytimg.com/vi/SfLV8hD7zX4/maxresdefault.jpg" style="width: 70%;height: auto;border: none;max-width: 100%;">
    <div id="test" style="display: block;position: absolute;top: 0;left: 0;width: 70%;height: 100%;color: #FFF;background: rgba(0, 0, 0, .6);">
    </div>
</div>
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
mamiw
  • 123
  • 4
  • 9

6 Answers6

3

See comments in code. Every change has comment. Everything else is as you had it. (I just don't like inline styles, they make it so hard to read)

#main {
    position: relative;
    display: inline-block;
    text-align: center;
    border: 1px solid;
}

#test {
    position: absolute;
    display: block;
    top: 50%; /* changed from 0 to 50% */
    left: 50%; /* changed from 0 to 50% */
    width: 70%;
    height: 100%;
    color: #FFF;
    background: rgba(0, 0, 0, .6);

    transform: translate(-50%, -50%);  /* added this line */
}

#main img {
    width: 70%;
    height: auto;
    max-width: 100%;
    /* border: none; this is not needed */
    vertical-align: middle; /* add this line */
}
<div id="main">
  <img src="https://i.ytimg.com/vi/SfLV8hD7zX4/maxresdefault.jpg">
  <div id="test"></div>
</div>
caramba
  • 21,963
  • 19
  • 86
  • 127
2

Adding
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
centers to your #test centers it.

Robert S.
  • 126
  • 1
  • 3
1

You can try this

<div id="main" style="position: relative;display: inline-block;text-align: center;border: 1px solid;">
    <img src="https://i.ytimg.com/vi/SfLV8hD7zX4/maxresdefault.jpg" style="width: 70%;height: auto;vertical-align:middle;border: none;max-width: 100%;">
    <div id="test" style="display: block;position: absolute;top: 0;left: 0; right:0; margin:0 auto;width: 70%;height: 100%;color: #FFF;background: rgba(0, 0, 0, .6);">
    </div>
</div>
Ezzuddin
  • 645
  • 5
  • 18
1

        <div id="main" style="
            position: relative;
            display: inline-block;
            text-align: center;
            border: 1px solid;
        ">
            <img src="https://i.ytimg.com/vi/SfLV8hD7zX4/maxresdefault.jpg" style="width: 70%;height: auto;border: none;max-width: 100%;">
            <div id="test" style="display: block;position: absolute;top: 0;left: 0;width: 70%;margin:0 auto;right:0;height: 100%;color: #FFF;background: rgba(0, 0, 0, .6);">
            </div>
        </div>
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
1

No Flexbox solution? I can't believe it. OK so, here goes :

.parent {
  border : blue solid 2px;
  width : 400px;
  height: 300px;

  display : flex;
  align-items : center;
  justify-content : center;
}

.child {
  border : green solid 2px;
  width : 200px;
  height: 200px;
}
<div class="parent">
   <div class="child"></div>
</div>
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
0

Add left: 0; right: 0; margin:0 auto; to your #test that's it.

Sarim
  • 124
  • 4