Reputation: 11
#hero-container {
display: flex;
position: relative;
height: 520px;
}
.hero {
position: absolute;
left: 0;
top: 0;
}
#text {
z-index: 100;
position: absolute;
color: white;
font-size: 3vw;
font-weight: bold;
left: 150px;
top: 40px;
}
#text2 {
z-index: 100;
position: absolute;
color: white;
font-size: 2vw;
font-weight: bold;
left: 150px;
top: 70px;
}
#hero-container img {
display: flex;
width: 100%;
}
<html>
<head>
</head>
<body>
<div id="hero-container">
<img class="hero" src="https://vreauperle.ro/images/banners/hero-image.jpg"></img>
<p id="text">
First text here
</p>
<p id="text2">
Secondary
</p>
</div>
</body>
</html>
I have an image with width:100%, in a fixed container. Because my image resizes, the text doesn't follow and stays in the same position, thus moving over the container.
How do I make the text stay in the center of the image (vertically) and to the left, with a few pixels padding (horizontally) without adding 1000 media queries ?
Upvotes: 0
Views: 1554
Reputation: 1650
Code to align element vertically and horizontally center.
p {
position : absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
In your case, since you only want vertically center, remove left:50% and use translateY(-50%).
Upvotes: 0
Reputation: 6537
You can greatly simplify your CSS:
div
and translateY(-50%)
.position:relative
.#hero-container {
position: relative;
}
#center-text {
z-index: 100;
position: absolute;
color: white;
font-weight: bold;
left: 15%;
top: 50%;
transform: translateY(-50%);
}
#text {
font-size: 3vw;
margin: 0;
}
#text2 {
font-size: 2vw;
margin: 0;
}
#hero-container img {
width: 100%;
height: auto;
}
<div id="hero-container">
<img class="hero" src="https://vreauperle.ro/images/banners/hero-image.jpg" />
<div id="center-text">
<p id="text">
First text here
</p>
<p id="text2">
Secondary
</p>
</div>
</div>
This will be fully responsive no matter your resolution and should work in IE10+ (or IE9+ with -ms-transform
prefix), if you use some more standard font-size (instead of vw
).
Upvotes: 2
Reputation: 785
If you apply the left and top css property in terms of '%' instead of 'px' then you will get the result. Fiddle Is Here
#hero-container {
display: flex;
position: relative;
height: 520px;
}
.hero {
position: absolute;
left: 0;
top: 0;
}
#text {
z-index: 100;
position: absolute;
color: white;
font-size: 3vw;
font-weight: bold;
left: 30%;
top: 0;
}
#text2 {
z-index: 100;
position: absolute;
color: white;
font-size: 2vw;
font-weight: bold;
left: 30%;
top: 11%;
}
#hero-container img {
display: flex;
width: 100%;
}
<html>
<head>
</head>
<body>
<div id="hero-container">
<img class="hero" src="https://vreauperle.ro/images/banners/hero-image.jpg"></img>
<div class='txt_wrap'>
<p id="text">
First text here
</p>
<p id="text2">
Secondary
</p>
</div>
</div>
</body>
</html>
Upvotes: 0
Reputation: 22949
You could display the image as a background-image
of #hero-container
.
Then you have no need to use position: absolute
on the text.
You can add padding
to the container in a percentage value - this will scale better than fixed pixels. You can edit the value I have used in the snippet to something that will work for you.
#hero-container {
display: flex;
flex-direction: column;
height: 520px;
background: url(https://vreauperle.ro/images/banners/hero-image.jpg);
background-size: contain;
background-repeat: no-repeat;
padding: 5%;
}
#hero-container p {
color: white;
font-weight: bold;
margin: 0;
}
#text {
font-size: 3vw;
}
#text2 {
font-size: 2vw;
}
<div id="hero-container">
<p id="text">
First text here
</p>
<p id="text2">
Secondary
</p>
</div>
Upvotes: 0