Reputation:
How can i set background inside responsive div?
I want to have inside that div plus sign (font awesome).
Problem is next:
I need to set height
if I want to set background image, otherwise, div is invisible (when I set height: auto
;);
Second thing, if I set a fixed height
(let's say, 200px), div is not responsive, logicaly;
And third thing, if I fix that problem somehow, how can I set plus sign inside div verticaly and horizontaly in center?
Any ideas?
Thanks.
Upvotes: 2
Views: 73
Reputation: 14345
If you want the + centered in a square, responsive box, here's how you could do that:
div {
width: 20%;
padding-bottom: 20%;
background: url(http://lorempixel.com/output/nature-q-g-640-480-4.jpg) no-repeat;
background-size: cover;
position: relative;
}
p {
font-size: 4em;
color: white;
margin: 0;
padding: 0;
font-weight: bold;
position: absolute;
top:50%;
left:50%;
-webkit-transform:translate(-50%, -50%);
-moz-transform:translate(-50%, -50%);
-ms-transform:translate(-50%, -50%);
-o-transform:translate(-50%, -50%);
transform:translate(-50%,-50%);
}
<div><p>+</p></div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
div {
width: 20%;
padding-bottom: 20%;
background: url(http://lorempixel.com/output/nature-q-g-640-480-4.jpg) no-repeat;
background-size: cover;
position: relative;
}
p {
font-size: 4em;
color: white;
margin: 0;
padding: 0;
font-weight: bold;
position: absolute;
top:50%;
left:50%;
-webkit-transform:translate(-50%, -50%);
-moz-transform:translate(-50%, -50%);
-ms-transform:translate(-50%, -50%);
-o-transform:translate(-50%, -50%);
transform:translate(-50%,-50%);
}
</style>
</head>
<body>
<div><p>+</p></div>
</body>
</html>
Upvotes: 1
Reputation: 38252
In your case you can use %
values on padding and width to keep the aspect ratio relative to the parent, and use some absolute position for the Plus sign:
div {
width:50%;
padding:25% 0;
background:url('http://placehold.it/500') no-repeat;
background-size:cover;
position:relative;
}
div > span {
position:absolute;
top:50%;left:50%;
transform:translate(-50%,-50%);
height:30px;line-height:30px;
background:red;color:white;
width:50%;text-align:center;
}
<div><span>PlusSign</span></div>
Upvotes: 3