I am trying to center 4 div boxes in a straight vertical line using translate method you use when centering objects in the middle of the screen, this is the code I used:
.body-component {
position: relative;
margin: 10px;
color: #000;
display: inline-block;
vertical-align: top;
background-color: #ff6d00;
overflow: auto;
border: 1px solid #D0D3D4;
}
.width-medium {
width: 500px;
}
.height-medium {
height: 400px;
}
.code-snippet {
position: relative;
width: 95%;
height: 95%;
background-color: #000;
}
.snippet-title {
position: absolute;
color: #248b98;
font-family: "Open Sans", sans-serif;
padding: 15px;
text-decoration: underline;
z-index: 1;
}
.center {
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
margin: 0px;
}
.boxes {
position: relative;
width: 100%;
height: 100%;
}
.box1 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 20%;
left: 50%;
transform: translate(-20%, -50%);
}
.box2 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 40%;
left: 50%;
transform: translate(-40%, -50%);
}
.box3 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 60%;
left: 50%;
transform: translate(-60%, -50%);
}
.box4 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 80%;
left: 50%;
transform: translate(-80%, -50%);
}
<div class="body-component width-medium height-medium">
<span class="snippet-title">Box loading animation</span>
<div class="code-snippet center">
<div class="boxes">
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</div>
</div>
</div>
I tried multiple methods to fix this, but I would not like to centre them using pixels because I am using this on a responsive website.
If absolute positioning, you can use left: 50% with a negative translateX of 50%.
.body-component {
position: relative;
margin: 10px;
color: #000;
display: inline-block;
vertical-align: top;
background-color: #ff6d00;
overflow: auto;
border: 1px solid #D0D3D4;
}
.width-medium {
width: 500px;
}
.height-medium {
height: 400px;
}
.code-snippet {
position: relative;
width: 95%;
height: 95%;
background-color: #000;
}
.snippet-title {
position: absolute;
color: #248b98;
font-family: "Open Sans", sans-serif;
padding: 15px;
text-decoration: underline;
z-index: 1;
}
.center {
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
margin: 0px;
}
.boxes {
position: relative;
width: 100%;
height: 100%;
}
.box {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
left: 50%;
transform: translateX(-50%)
}
.box1 {
top: 20%;
}
.box2 {
top: 40%;
}
.box3 {
top: 60%;
}
.box4 {
top: 80%;
}
<div class="body-component width-medium height-medium">
<span class="snippet-title">Box loading animation</span>
<div class="code-snippet center">
<div class="boxes">
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
</div>
</div>
</div>
That said, you could use flexbox to achieve this layout without having to know the number of the boxes in advance for the purpose of vertical spacing.
html,
body {
margin: 0;
padding: 0;
color: #248b98;
font-family: "Open Sans", sans-serif;
}
.body-component {
background-color: black;
height: 100vh;
border: 10px solid #ff6d00;
box-sizing: border-box;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: space-betwen;
align-items: center;
min-height: 500px;
}
.snippet-title {
flex: 0 1 auto;
text-decoration: underline;
padding: 10px;
}
.code-snippet {
flex: 1 1 auto;
display: flex;
}
.boxes {
width: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
padding: 15px;
}
.box {
width: 30px;
height: 30px;
background-color: #056ab3;
}
<div class="body-component">
<span class="snippet-title">Box loading animation</span>
<div class="code-snippet">
<div class="boxes">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
</div>
Try this... add transform: translate(-50%, -50%); to box classes
.body-component {
position: relative;
margin: 10px;
color: #000;
display: inline-block;
vertical-align: top;
background-color: #ff6d00;
overflow: auto;
border: 1px solid #D0D3D4;
}
.width-medium {
width: 500px;
}
.height-medium {
height: 400px;
}
.code-snippet {
position: relative;
width: 95%;
height: 95%;
background-color: #000;
}
.snippet-title {
position: absolute;
color: #248b98;
font-family: "Open Sans", sans-serif;
padding: 15px;
text-decoration: underline;
z-index: 1;
}
.center {
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
margin: 0px;
}
.boxes {
position: relative;
width: 100%;
height: 100%;
}
.box1 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 20%;
left: 50%;
transform: translate(-50%, -50%);
}
.box2 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
}
.box3 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 60%;
left: 50%;
transform: translate(-50%, -50%);
}
.box4 {
position: absolute;
width: 30px;
height: 30px;
background-color: #056ab3;
top: 80%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="body-component width-medium height-medium">
<span class="snippet-title">Box loading animation</span>
<div class="code-snippet center">
<div class="boxes">
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</div>
</div>
</div>
Related
I am trying to create an overlay for the website, it should have the form of three boxes stacked on top of each other. Currently, they are situated so that the first two are on the first row, and the third one is on the bottom. To make them all be on a separate row, I am trying to add single parameter to css: width: 100%, which worked for the third row, but ruins everything if I do it to either of the ones left.
Here is the code snippet, It will work on its own:
* {
box-sizing: border-box;
}
#box {
width: 70vw;
height: 80vh;
margin: 10px;
padding: 10px;
border-radius: 25px;
}
.column4 {
height: 39vh;
width: 50%;
float: left;
padding: 50px;
text-align: center;
color: black;
font-size: 20px;
line-height: 1.3;
/*width: 100%;*/ /* here is the culprit*/
}
.column5 {
height: 39vh;
width: 100%;
float: left;
padding: 10px;
text-align: center;
color: black;
font-size: 20px;
line-height: 1.3;
}
#overlay {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.8);
z-index: 2;
cursor: pointer;
}
#text {
position: absolute;
top: 50%;
left: 50%;
font-size: 50px;
color: white;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
#boxt3 {
font-size: 25px;
}
#boxt1 {
font-size: 20px;
}
#boxt2 {
font-size: 20px;
}
<div id="overlay" style="z-index: 100; display: block;" onclick="off()">
<div id="text">
<div id="box" style="background:#fff">
<div class="row">
<div id="boxt1" class="column4" style="padding-top: 0; overflow: auto">Facebook, Inc.<br>NASDAQ<br>US<br>Internet Content & Information<br>Communication Services<br>some nice info</div>
<div id="boxt2" class="column4" style="padding-top: 0; overflow: auto">Price: 341.00 <br>mCap: 965.39B<br>Beta: 1.295305<br>volAvg: 14142012<br>P/E: NA</div>
<div class="column5" style="padding-top: 5; overflow: auto">
<h2 id="boxt3" stle="">Key features:<br> Net income: 6969% year-to-year<br> Undervalued on Very% by P/S<br>We love your data -Mark<br><br> Estimated probability of success: 0<br></h2>
</div>
</div>
</div>
</div>
</div>
I understand that there is probably some dumb mistake, but I can't find it at all (I have just started learning :) ). What is the issue here?
The issue occurs because you have set the height for the child element which is too high (39vh + 39vh + 39vh) for the parent element (80vh) . Try fitting the height of the child elements into parent's height. It will fix your problem.
* {
box-sizing: border-box;
}
#box {
width: 70vw;
height: 80vh;
/*margin: 10px;
padding: 10px;*/
border-radius: 25px;
}
.column4 {
height: 20vh;
width: 50%;
float: left;
padding: 50px 50px 10px;
text-align: center;
color: black;
font-size: 20px;
line-height: 1.3;
width: 100%;
}
*{box-sizing:border-box;}
.column5 {
height: 40vh;
width: 100%;
float: left;
padding: 10px;
text-align: center;
color: black;
font-size: 20px;
line-height: 1.3;
}
#overlay {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.8);
z-index: 2;
cursor: pointer;
}
#text {
position: absolute;
top: 50%;
left: 50%;
font-size: 50px;
color: white;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
#boxt3 {
font-size: 25px;
}
#boxt1 {
font-size: 20px;
}
#boxt2 {
font-size: 20px;
}
<div id="overlay" style="z-index: 100; display: block;" onclick="off()">
<div id="text">
<div id="box" style="background:#fff">
<div class="row">
<div id="boxt1" class="column4" style="padding-top: 0; overflow: auto">Facebook, Inc.<br>NASDAQ<br>US<br>Internet Content & Information<br>Communication Services<br>some nice info</div>
<div id="boxt2" class="column4" style="padding-top: 0; overflow: auto">Price: 341.00 <br>mCap: 965.39B<br>Beta: 1.295305<br>volAvg: 14142012<br>P/E: NA</div>
<div class="column5" style="padding-top: 5; overflow: auto">
<h2 id="boxt3" stle="">Key features:<br> Net income: 6969% year-to-year<br> Undervalued on Very% by P/S<br>We love your data -Mark<br><br> Estimated probability of success: 0<br></h2>
</div>
</div>
</div>
</div>
</div>
Now I have set the child element's height to 20vh + 20vh + 40vh sums up to the height of the parent's height 80vh
You must remove width: 100% from column5 class
* {
box-sizing: border-box;
}
#box {
width: 70vw;
height: 80vh;
margin: 10px;
padding: 10px;
border-radius: 25px;
}
.column4 {
height: 39vh;
width: 50%;
float: left;
padding: 50px;
text-align: center;
color: black;
font-size: 20px;
line-height: 1.3;
/*width: 100%;*/ /* here is the culprit*/
}
.column5 {
height: 39vh;
float: left;
padding: 10px;
text-align: center;
color: black;
font-size: 20px;
line-height: 1.3;
}
#overlay {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.8);
z-index: 2;
cursor: pointer;
}
#text {
position: absolute;
top: 50%;
left: 50%;
font-size: 50px;
color: white;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
#boxt3 {
width : 50px;
font-size: 25px;
}
#boxt1 {
width : 30px;
font-size: 20px;
}
#boxt2 {
width : 50px;
font-size: 20px;
}
<div id="overlay" style="z-index: 100; display: block;" onclick="off()">
<div id="text">
<div id="box" style="background:#fff">
<div class="row">
<div id="boxt1" class="column4" style="padding-top: 0; overflow: auto">Facebook, Inc.<br>NASDAQ<br>US<br>Internet Content & Information<br>Communication Services<br>some nice info</div>
<div id="boxt2" class="column4" style="padding-top: 0; overflow: auto">Price: 341.00 <br>mCap: 965.39B<br>Beta: 1.295305<br>volAvg: 14142012<br>P/E: NA</div>
<div class="column5" style="padding-top: 5; overflow: auto">
<h2 id="boxt3" stle="">Key features:<br> Net income: 6969% year-to-year<br> Undervalued on Very% by P/S<br>We love your data -Mark<br><br> Estimated probability of success: 0<br></h2>
</div>
</div>
</div>
</div>
</div>
I'm trying to place Text and button on an image like this. Can someone help with this.
this is what I have tried. The thing is it does render properly in outlook email
CSS:
.container {
position: relative;
width: 100%;
max-width: 400px;
}
.container img {
width: 100%;
height: auto;
}
.container .btn {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #55A646;
color: white;
font-size: 16px;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
}
.container .btn:hover {
background-color: #55A646 ;
}
HTML:
<div class="container">
<img src="http://image.com" alt="" style="width:100%">
<button class="btn">Button</button>
</div>
Folow The link: https://www.w3schools.com/howto/howto_css_image_text.asp
/* Container holding the image and the text */
.container {
position: relative;
text-align: center;
color: white;
}
/* Bottom center text */
.bottom-center {
position: absolute;
bottom: 8px;
left: 45%;
}
/* Centered text */
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="container">
<img src="https://cdn.searchenginejournal.com/wp-content/uploads/2019/07/the-essential-guide-to-using-images-legally-online-760x400.png" alt="Snow" style="width:100%;">
<div class="bottom-center">
<button>Bottom Center</button>
</div>
<div class="centered">Centered</div>
</div>
try this instead,
.container {
position: relative;
width: 100%;
max-width: 400px;
}
.container img {
width: 100%;
height: 100%
}
.container .btn {
position: absolute;
bottom: 10px;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
background-color: #55A646;
color: white;
font-size: 16px;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
}
.container .btn:hover {
background-color: #00cc00 ;
}
h4{
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
color: #fff;
background:#111;
padding: 7px;
}
<div class="container">
<img src="https://i.stack.imgur.com/bf3jO.png" alt="" style="width:100%">
<h4>The table Fan</h4>
<button class="btn">Fan</button>
</div>
I was trying to participate in a CSS challenge when this occurred. Everything seems working as expected, however when clicking on the plus circle to display div#card the div.container loses it's top margin and I cannot understand why. Please if anyone can help me with this, I'd be really grateful - And maybe we can all learn from it ;)
Thanks!
Codepen: https://codepen.io/albertrf147/pen/LMKKeK
HTML
html,
body {
width: 100%;
height: 100%;
}
body {
margin: 0px;
overflow: hidden;
background: lightblue;
}
.container {
width: 400px;
height: 400px;
margin: 20px auto !important;
position: relative;
display: flex;
flex-wrap: wrap;
background: white;
padding: 2px;
}
.square {
box-sizing: border-box;
padding: 2px;
width: 50%;
height: 50%;
position: relative;
background: white;
}
.square>img {
height: 100%;
width: 100%;
object-fit: cover;
display: block;
margin: auto;
}
.onhover {
box-sizing: border-box;
width: 100%;
height: 100%;
position: absolute;
cursor: pointer;
transition: all .6s ease-in-out;
}
.onhover:hover {
background: rgba(0, 0, 0, 0.5);
}
.circle-aux {
position: relative;
width: 100%;
height: 100%;
transition: all .6s ease-in-out;
}
.circle-aux:hover .circle {
visibility: visible;
opacity: 1;
transform: scale(0.2);
transition: all .6s ease-in-out;
}
.circle {
visibility: hidden;
opacity: 0;
position: absolute;
border-radius: 50%;
width: 100%;
height: 100%;
background: salmon;
}
.circle:before {
content: "";
background: white;
height: 50%;
width: 6px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.circle:after {
content: "";
background: white;
height: 6px;
width: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.container-card {
visibility: hidden;
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.container-card>img {
width: 100%;
height: 60%;
object-fit: cover;
}
.container-card:target {
visibility: visible;
}
.avatar {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
box-shadow: 0px 0px 20px black;
width: 25%;
height: 25%;
background: white;
z-index: 2;
text-align: center;
font-family: calibri;
font-weight: bold;
color: white;
font-size: 18px;
}
.avatar>img {
box-sizing: border-box;
width: 100%;
height: 100%;
object-fit: cover;
border-radius: inherit;
padding: 4px;
}
.mini-circle {
display: inline-block;
width: 15%;
height: 15%;
border: 1px solid white;
border-radius: 50%;
margin-top: 12px;
}
.mini-circle:hover {
background: white;
cursor: pointer;
}
footer {
box-sizing: border-box;
position: absolute;
bottom: 0;
left: 0;
background: salmon;
height: 50%;
width: 100%;
z-index: 1;
padding: 2px;
}
.close {
position: absolute;
border-radius: 50%;
width: 8%;
height: 8%;
background: black;
transform: rotate(45deg);
right: 10px;
top: 10px;
cursor: pointer;
}
.close:before {
content: "";
background: white;
height: 60%;
width: 2px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.close:after {
content: "";
background: white;
height: 2px;
width: 60%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<main>
<div class="container">
<div class="square">
<div class="onhover">
<div class="circle-aux">
<a class="circle" href="#card"></a>
</div>
</div>
<img src="https://images.pexels.com/photos/305241/pexels-photo-305241.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
</div>
<div class="square">
<div class="onhover">
<div class="circle-aux">
<div class="circle"></div>
</div>
</div>
<img src="https://images.pexels.com/photos/1546711/pexels-photo-1546711.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
</div>
<div class="square">
<div class="onhover">
<div class="circle-aux">
<div class="circle"></div>
</div>
</div>
<img src="https://images.pexels.com/photos/1800433/pexels-photo-1800433.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
</div>
<div class="square">
<div class="onhover">
<div class="circle-aux">
<div class="circle"></div>
</div>
</div>
<img src="https://images.pexels.com/photos/1757111/pexels-photo-1757111.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
</div>
<div id="card" class="container-card">
<a class="close" href="#"></a>
<img src="https://images.pexels.com/photos/1769331/pexels-photo-1769331.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
<div class="avatar">
<img src="https://images.pexels.com/photos/769772/pexels-photo-769772.jpeg?auto=compress&cs=tinysrgb&h=650&w=940" />
<span>David Craig</span>
<span>
<div class="mini-circle"></div>
<div class="mini-circle"></div>
<div class="mini-circle"></div>
</span>
</div>
<footer></footer>
</div>
</div>
</main>
Welcome to SO!
As the appearing container is position: absolute no margin takes effect.
You need to work with the top attribute to place it properly.
Check this out: https://codepen.io/anon/pen/pGzVyL
For more informations click here: https://www.w3schools.com/css/css_positioning.asp
I'm trying to replicate this, essentially:
So basically two 50% <div>'s side-by-side, with some form of absolute positioning (I assume) to achieve the left box to go over the top of the right box (the red line is just representing the middle of the viewport)
Any hints? Thanks :)
.container {
width: 500px;
height: 300px;
border: 1px solid black;
position: relative;
overflow: hidden;
}
.box1 {
width: 100%;
height: 100%;
background-color: blue;
transform: skewX(-20deg) translateX(-40%);
position: absolute;
z-index: 10;
}
.box2 {
width: 100%;
height: 100%;
background-color: red;
z-index: 0;
}
Should be pretty simple with CSS3.
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
I offer a version without the transformation, using pseudoelement. It is faster and does not distort the text.
.container {
width: 500px;
height: 300px;
border: 1px solid black;
position: relative;
overflow: hidden;
}
.box1 {
width: 50%;
height: 100%;
background-color: blue;
position: absolute;
left: 0;
}
.box1::after{
background: linear-gradient(to bottom right, blue 50%, transparent 0);
content: " ";
width: 20%;
height: 100%;
position: absolute;
left: 100%;
z-index: 1;
}
.box2 {
width: 50%;
height: 100%;
background-color: red;
position: absolute;
right: 0;
}
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
Try this
.wrapper {
overflow-x: hidden;
}
.outer {
position: absolute;
width: 2000px;
left: 50%;
bottom: 0;
margin-left: -1000px;
display: flex;
justify-content: center;
align-items: center;
}
.left__inner {
background: goldenrod;
padding: 24px 48px;
flex: 1;
transform: skew(45deg);
display: flex;
justify-content: flex-end;
}
.right__inner {
background: #222;
padding: 24px 48px;
flex: 1;
transform: skew(45deg);
}
.left__text,
.right__text {
transform: skew(-45deg);
span {
font-weight: 200;
font-size: 36px;
text-transform: uppercase;
}
}
.left__text {
color: #3c3c3c;
}
.right__text {
color: Goldenrod;
}
<div class="wrapper">
<div class="outer">
<div class="left__inner">
<div class="left__text">
<span> so skewy</span>
</div>
</div>
<div class="right__inner">
<div class="right__text">
<span>span much angle</span>
</div>
</div>
</div>
</div>
I would do it like this
this is just an example, not a ready-made solution ))
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
.container {
display: flex;
}
.container div {
width: 50%;
height: 200px;
position: relative;
}
.container .left:before {
content: '';
position: absolute;
right: 0;
top: 0;
height: 100%;
transform: skewY(-1.5deg);
background: inherit;
}
we have the following div structure, where we would like to horizontally center both the image and the text within the container div. However, the image is left aligned and only the title is centered.You could find the code below:
.QHeader {
position: absolute;
margin-top: 96px;
margin-left: 0px;
width: 800px;
height: 415px;
background-image: url('../img/bg_blue_rect.png');
background-repeat: no-repeat;
background-position: left top;
}
#QHeaderImg01 {
position: absolute;
display: block;
margin: 0 auto;
margin-top: 72px;
width: 263px;
height: 221px;
background-color: #0F0;
}
.QHeaderTitle {
position: absolute;
margin-top: 324px;
margin-left: 0;
width: 800px;
height: auto;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
font-size: 30px;
font-weight: bold;
color: #000;
}
<div class="QHeader">
<img id="QHeaderImg01" src="img/q_header_img_01.png" />
<div class="QHeaderTitle">
TITLE
</div>
<!--QHeaderTitle-->
</div>
<!--QHeader-->
Just remove your position: absolute; or change it to position: relative;, as you will not need absolute positioning to horizontally center your elements:
.QHeader {
width: 800px;
height: 415px;
background-image: url(http://placehold.it/800x415);
background-repeat: no-repeat;
background-position: left top;
}
#QHeaderImg01 {
display: block;
margin: 0 auto;
width: 263px;
height: 221px;
background-color: #0F0;
}
.QHeaderTitle {
margin-top: 50px;
width: 800px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
font-size: 30px;
font-weight: bold;
color: #000;
}
<div class="QHeader">
<img id="QHeaderImg01" src="http://placehold.it/263x221/0c0" />
<div class="QHeaderTitle">
TITLE
</div>
<!--QHeaderTitle-->
</div>
<!--QHeader-->
just change
#QHeaderImg01 {
position:absolute;
}
to
#QHeaderImg01 {
position:relative;
}
and of cource, remove the big margin-top
div.container {
height: 10em;
position: relative }
div.container p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
}
IMG.displayed {
display: block;
margin-left: auto;
margin-right: auto;
}
<body>
<div class=container>
<IMG class="displayed" src="http://labs.qnimate.com/slider/1.jpg" alt="...">
<p>Centered!
</div>
or
IMG.displayed {
display: block;
margin-left: auto;
margin-right: auto;
}
div.container {
height: 10em;
position: relative }
div.container p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
}
<IMG class="displayed" src="http://labs.qnimate.com/slider/1.jpg" alt="...">
<div class=container>
<p>Centered!
</div>