Reputation: 45
I got and issue how to center my element in css.
I want set my header with background image width 100% and height 100% and some navigation, text/image div in my header as below.
@import url('https://fonts.googlrapis.com/css?family=poppins:400,500,700');
html,
body {
height: 100%;
width: 100%;
font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, "sans-serif";
}
.navbar {
background-color: rgba(38, 38, 38, 0.30);
}
.nav-background {
background-image: url(http://localhost/newsystem/images/background.jpg);
repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
width: 100%;
height: 100%;
object-fit: cover;
}
.header-contain {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
position: relative;
border: 1px solid red;
}
.contain-one {
padding: 5px;
background-color: rgba(0, 116, 217, 0.50);
position: absolute;
color: #fff;
top: 50%;
left: 50%;
transform: translate(-50%);
}
.header-contain img {
position: absolute;
bottom: 0;
height: 50%;
}
<body>
<div class="nav-background">
<nav class="navbar navbar-expand-lg">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
</div>
</nav>
<div class="header-contain">
<img src="images/umbrella.png">
<div class="contain-one">
<h5>Let's start with us</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam, est.</p>
</div>
</div>
</div>
</body>
The issue is why my .header-container height start below my navigation. I want .header-container
same size as my .nav-background
image. So I can make my .header-container
img fix to bottom of my div elements of nav-background.
Below is the image issue I facing now.
Upvotes: 3
Views: 249
Reputation: 58422
You can use flex on your container and make the header contain grow to fill the rest of the space instead of using height 100%:
@import url('https://fonts.googlrapis.com/css?family=poppins:400,500,700');
* {
box-sizing: border-box; /* add this */
}
html,
body {
height: 100%;
width: 100%;
font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, "sans-serif";
margin:0; /* add this */
}
.navbar {
background-color: rgba(38, 38, 38, 0.30);
}
.nav-background {
background-image: url(http://localhost/newsystem/images/background.jpg);
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
width: 100%;
height: 100%;
object-fit: cover;
display: flex; /* add this */
flex-direction: column; /* aligns child items in a column */
}
.header-contain {
width: 100%;
margin: 0;
padding: 0;
position: relative;
border: 1px solid red;
flex-grow: 1; /* add this */
}
.contain-one {
padding: 5px;
background-color: rgba(0, 116, 217, 0.50);
position: absolute;
color: #fff;
top: 50%;
left: 50%;
transform: translate(-50%);
}
.header-contain img {
position: absolute;
bottom: 0;
height: 50%;
}
<body>
<div class="nav-background">
<nav class="navbar navbar-expand-lg">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
</div>
</nav>
<div class="header-contain">
<img src="images/umbrella.png">
<div class="contain-one">
<h5>Let's start with us</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam, est.</p>
</div>
</div>
</div>
</body>
Upvotes: 1