I've got a background image in a parent container that covers the full screen width.
I want a child container centered within the parent which will contain some text and have a semi-transparent background color.
No matter how I try to set the top margin of the child, the parent container's top margin moves along with the child's as if the two top edges are "glued" together. The parent's margin follows the child's.
How can I make the contained element (.content) move down while keeping the top edge of the image fixed to the top of the page?
Thanks!
BG image follows box's top margin
/* Header */
.hero {
background: url('../images/hero_laptop_2.jpg') no-repeat center center/cover;
height: 75vh;
position: relative;
color: #fff;
}
.hero .content {
border: solid black 1px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
width: 40%;
height: 60%;
padding: 20px;
z-index: 1;
margin-left: auto;
margin-right: auto;
margin-top: 200px;
}
<header class="hero">
<div id="navbar" class="navbar top">
<h1 class="logo">
<span class="text-primary"><i class="fas fa-desktop"></i> IT</span>-Simplified</h1>
<h1 id="navbar-phone">845-520-1115</h1>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Testimonials</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</nav>
</div>
<div class="content">
<h1>Welcome to IT-Simplified</h1>
<p>We Provide COMPUTER SOLUTIONS... <br>Simply & Affordably.</p>
<p id="call-us">Call Us: 845-520-1115</p>
<!-- <i class="fas fa-chevron-right"></i> Testimonials -->
</div>
</header>
You need to add a position absolute or other positioning to the .hero .content class so that it will float on top of the background, independently of the parent container. Edit: In my solution below I have used position: relative;.
The z-index is irrelevant as the positioning take it out of the normal html flow context, so I removed it. Did a few other changes, take a look (use full screen, in the small snippet area below the content box doesn't display correctly).
Alternatively, you could try to make the .hero container display: flex; so you can position its children within it, but that doesn't seem to be practical here where you want the navigation on top and content below.
/* Header */
.hero {
background: url('https://www.esa.int/var/esa/storage/images/esa_multimedia/images/2017/11/autumn_fireball/17255671-1-eng-GB/Autumn_fireball.jpg') no-repeat center center/cover;
height: 100vh;
color: #fff;
}
.hero .content {
color: white;
position: relative;
border: solid yellow 1px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
width: 40%;
padding: 20px;
margin-left: auto;
margin-right: auto;
}
<header class="hero">
<div id="navbar" class="navbar top">
<h1 class="logo">
<span class="text-primary"><i class="fas fa-desktop"></i> IT</span>-Simplified</h1>
<h1 id="navbar-phone">845-520-1115</h1>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Testimonials</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</nav>
</div>
<div class="content">
<h1>Welcome to IT-Simplified</h1>
<p>We Provide COMPUTER SOLUTIONS... <br>Simply & Affordably.</p>
<p id="call-us">Call Us: 845-520-1115</p>
<!-- <i class="fas fa-chevron-right"></i> Testimonials -->
</div>
</header>
Result:
Related
.header{
min-height: 90vh;
width: 100%;
background-image: linear-
gradient(rgba(4,9,30,0.7),rgba(4,9,30,0.7)),
url(../images/background.jpg);
background-position: center;
background-size: cover;
position: relative;
overflow: hidden;
background-repeat: no-repeat;
background-attachment: fixed;
}
nav{
min-height: 7%;
background-color: white;
opacity: 0.8;
display: flex;
padding: 2% 4%;
justify-content: space-between;
align-items: center;
}
nav img{
width: 140px;
}
.nav-links{
flex: 1;
text-align: right;
}
.nav-links ul li{
list-style: none;
display: inline-block;
padding: 8px 12px;
position: relative;
}
.nav-links ul li a{
color: rgb(0, 0, 0);
text-decoration: none;
font-size: 13px;
}
.nav-links ul li::after{
content: '';
width: 0%;
height: 2px;
background: #f44336;
display: block;
margin: auto;
transition: 1s;
}
.nav-links ul li:hover::after{
width: 100%;
}
nav .fa{
display: none;
}
<section class="header">
<nav>
<img src="../images/logo.png">
<div class="nav-links" id="navLinks">
<i class="fa fa-close" onclick="hideMenu()"></i>
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>LOCATIONS</li>
<li>PRODUCTS</li>
<li>CONTACTS</li>
</ul>
</div>
<i class="fa fa-bars" onclick="showMenu()"></i>
</nav>
<div class="text-box">
<h1>AVEM SALAM</h1>
<p>AICI CEVA DESCRIERE<br>YOU KNOW SHORT AND GOOD</p>
PRODUSELE NOASTRE
</div>
</section>
Also, I need some help related to the size of the navbar, how do I make it smaller in terms of height? I did my best to go with "min-height: 7%;" but it is not enough...
It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.
[![It needs to look smh like this][2]][2]
[![So this how the navbar currently looks][1]][1]
[1]: https://i.stack.imgur.com/UVWJg.jpg
[2]: https://i.stack.imgur.com/BNswb.png
Your DOM order is wrong, try this HTML
<section class="header">
<nav>
<div class="nav-links" id="navLinks">
<i class="fa fa-close" onclick="hideMenu()"></i>
<ul>
<li>HOME</li>
<li>ABOUT US</li>
</ul>
</div>
<img src="../images/logo.png">
<div class="nav-links" id="navLinks">
<i class="fa fa-close" onclick="hideMenu()"></i>
<ul>
<li>LOCATIONS</li>
<li>PRODUCTS</li>
<li>CONTACTS</li>
</ul>
</div>
<i class="fa fa-bars" onclick="showMenu()"></i>
</nav>
<div class="text-box">
<h1>AVEM SALAM</h1>
<p>AICI CEVA DESCRIERE<br>YOU KNOW SHORT AND GOOD</p>
PRODUSELE NOASTRE
</div>
</section>
if you want elements around your logo and Logo to be centered then your code is totally written wrong.
make parent-div with display property:flex
and then make three child div with class nav-left logo and nav-right and give them width of percentage 33.33 %
include ul li in first div with class nav-left
then add your logo with in second div element so that you can control your image responsiveness
and lastly include ul li in third div with class nav-right
ABOUT HEIGHT
remove height from nav and I am assuming you are new to code then you should also do this
ul,li {
padding: 0;
margin : 0;
}
it should be done before you start styling because some html tags have default padding and margins (give this search on Google so you know which tags have this default styling e.g are p tags heading-tags anchor-tags etc,..) you have to remove this and then add your styles. Hope this will help you out :)
I'm very new to coding with html/css. I'm currently working on a project for uni and am stuck with a problem. I want my footer (about me, socials, imprint) to be in the horizontal middle of the page (so just a little bit further on the right). I have tried everything (text-align, justify-content, align-items, ...) - nothing really worked. Has anyone an idea on how to fix it?
I'm thankful for any kind of help :)
Here's my code so far (with a few other problem areas. plus it's probably super messy - sorry!) :
body,
html {
margin: 0;
padding: 0;
overflow-x: hidden;
}
body {
overflow-x: hidden;
font-family: Baskerville, Helvetica, serif;
font-size: 20px;
text-align: center;
letter-spacing: .2em;
background-image: url(https://cdn.shopify.com/s/files/1/1362/2563/products/Kariceramicshandmadepotterybluesixhandceramicdinnerplates_2048x.jpg?v=1591292140);
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
background-position: center;
color: #A5A58D;
}
.header1 {
height: 90px;
padding: 80px 0;
font-size: 68px;
letter-spacing: 12px;
text-transform: uppercase;
}
nav > ul > li {
display: block;
padding-left: 20px;
padding-right: 20px;
position: relative;
}
<head>
<link rel="stylesheet" href="WS 2020 Screendesign.css">
<title> ALINA'S POTTERY </title>
</head>
<body>
<div class="bg-image"></div>
<div class="header1"> Alina's Pottery </div>
<nav class="categoryContainer">
<ul>
<li class="current">Home</li>
<li>Pottery
<ul class="dropdown">
<li>General</li>
<li>Hand-Building</li>
<li>Pottery Wheel</li>
<li>Materials</li>
<li>Temperatures</li>
</ul>
</li>
<li>Tutorials
<ul class="dropdown">
<li>View All</li>
<li>Get Started</li>
<li>Plates</li>
<li>Mugs</li>
<li>Bowls</li>
</ul>
</li>
<li>Shop
<ul class="dropdown">
<li>Products</li>
<li>Contact Form</li>
</ul>
</li>
</ul>
</nav>
<div class="header2"> Welcome to my pottery portfolio! </div>
<div class="text1"> I am very passionate about creating my own ceramics. </div>
<section class="footerContainer">
About Me
Socials
Imprint
</section>
</body>
<div class="background"></div>
</html>
try to use flexbox to align the items in the section modifying the css rule like so:
.footerContainer {
display: flex;
justify-content: center;
bottom: 3%;
position: absolute;
}
In your current code, the best thing to do is to delete the whole:
.footerContainer{
...
}
The footer will appear centered (behind the menu dropdown), because of the text-align:center you put on the body.
If at a certain point you would remove that from the body, I would also suggest to work with:
.footerContainer{
display: flex;
justify-content: center;
}
And overall feedback: avoid using position: fixed/absolute everywhere, it makes everything more complicated as these elements are removed from the document flow.
You should try to leverage display: flex here, it will solve your issue really fast and it will also adapt easily to further changes. My suggestion here is to change your footer to something like this:
<footer class="footer-container">
<a class="footer-link" href="aboutme.html" title="About Me">About Me</a>
<a class="footer-link" href="socials.html" title="Socials">Socials</a>
<a class="footer-link" href="imprint.html" title="Imprint">Imprint</a>
</footer>
As a side-note, it is better to use <footer></footer> instead of <section></section> to create your footer, and it also helps with SEO. (Read more about it here).
And for styling you can simply do this
.footer-container {
display: flex;
flex-wrap: wrap;
/* Change justify-content as you like */
justify-content: space-around;
}
/* If you don't want or like to use justify-content,
just apply some margins on your links */
.footer-container .footer-link {
margin: 0 16px;
}
Try
.footerContainer {
bottom: 3%;
position: absolute;
text-align: center;
width: 100%;
}
I wanted to add a line of text and the background color should be the full with of the website.
I wanted to center a text in the website. I gave the text a specific width which makes it centered. Although the background color is just as wide as the text and I want to extend that
#wrapper {
text-align: center;
background-color: #e6eef2;
padding: 7px;
width: 1222px;
margin: 0 auto;
}
.topbar {
display: flex;
justify-content: space-between;
color: #06507f;
}
<header>
<div id="wrapper">
<div class="topbar">
<span>Lidl-Reisen.de - ... einfach urlaubiger!</span>
<ul>
<li>Verantwortung</li>
<li>Unternehmen</li>
<li>Karriere</li>
<li>Service & Hilfe</li>
</ul>
</div>
</div>
</header>
Here is the website im refering to: https://www.lidl-reisen.de/asd
I want to recreate the topbar
If you want to recreate that navigation bar, you should have used position as absolute, so that you dont get the extra margins, though you can fix that without using that.
Code:
HTML:
<header>
<div id="wrapper">
<div class="topbar">
<span>Lidl-Reisen.de - ... einfach urlaubiger!</span>
<ul>
<li>Verantwortung</li>
<li>Unternehmen</li>
<li>Karriere</li>
<li>Service & Hilfe</li>
</ul>
</div>
</div>
</header>
CSS:
#wrapper {
text-align: center;
background-color: #e6eef2;
width: 100%;
position: absolute;
left:0;
top:0;
padding:10px;
}
.topbar {
display: flex;
justify-content: space-between;
color: #06507f;
}
Check out my js fiddle link : https://codepen.io/djmayank/pen/PooZrKo
I'm writing the CSS for my site. I have text that I am putting on top of my background image. My HTML and CSS is below:
HTML
<header class="site-header">
<div class="site-header__menu-icon">
<div class="site-header__menu-icon__middle"></div>
</div>
<div class="site-header__menu-content">
<div class="site-header__btn-container">
Request A Demo
</div>
<nav class="primary-nav primary-nav--pull-right">
<ul>
<li>Home</li>
<li>Services</li>
<li>Why Us</li>
</ul>
</nav>
</div>
</div>
</header>
<div class="section">
<picture>
<img src="assets\images\pepper.jpg">
</picture>
<div>
<div class="section__text-content">
<h1 class="section__title">Company</h1>
<h3 class="section__sub-title">Company Slogan</h3>
<div class="btn-container">
<a class="btn" href="#">Talk To A Specialist</a>
<a class ="btn btn__white btn__pepper-white" href="#">Get A Quote</a>
</div>
</div>
</div>
</div>
CSS
.section {
position: relative;
max-width: 100%;
&__text-content {
position: absolute;
top: 30%;
width: 100%;
margin-left:
}
&__title {
font-size: 7rem;
font-weight: 300;
color: #ffffff;
margin-bottom: 0;
}
&__sub-title {
font-size: 2.5rem;
font-weight: 300;
margin-top: 3%;
margin-bottom: 2%;
color: #ffffff;
}
}
The problem arises when I try to add a margin-left to the .section class in my CSS, because then a blank which space to the right of my screen appears with a horizontal scroll bar in direct proportion to the amount of margin I specified to move to the left.
I know that I could use a simple "background-image" for my css, but I'd prefer to do it this way for responsive imaging (it's how I learned to do it and I'm on a bit of a time crunch).
Any suggestions?
FIXED
My problem was I was writing my margin in relative terms by doing:
.section__text-content {
position: absolute;
margin-left: 10%
}
Which is wrong, because the &__text-content is set to position: absolute
Percentage is relative position, with an absolutely positioned image you need absolute margins (i.e. pixels)
I have a sidenav which has a container with social media context that is meant to stick to the bottom of the sidenav which spans the entire view-port height like so:
sidenav {
height: 100%;
background: #007979;
left: 0;
position: fixed;
text-align:
width: 25rem;
}
The sidenav immediate child is a container which has it's position property set to relative to I can then assign a position property and value to the social media container to stick to the bottom, like so:
sidenav-inner {
position: relative;
height: 100%;
}
My issue is that if I set the social media containers position property to either absolute or relative, if the view-port height is adjusted slightly, the social media container overlaps the navigation list that is at the top, as it is positioned in correspondence to the sidenav-inner container.
Question
How do I position the social media container at the bottom of the sidenav, without using the position proeprty? Or how do I use the position property without it overlapping other content in the sidenav?
<nav class="sidenav">
<div class="sidenav-inner">
<div class="sidenav-header">
<div class="row">
<div class="col-md-12">
<img src="/">
</div>
</div>
</div>
<ul class="nav">
<li><a>Item</a></li>
<li><a>Item</a></li>
<li><a>Item</a></li>
</ul>
<div class="side-nav-social">
<div>
//social media content
</div>
//more social media content
</div>
</div>
</nav>
You can easily solve this by defining .sidenav-inner as a flexbox and then having the header and footer not grow, but the ul grow to take up all the space. It will thus push the social media to the bottom of the box.
.sidenav {
height: 100%;
background: #007979;
left: 0;
position: fixed;
text-align:
width: 25rem;
}
.sidenav-inner {
position: relative;
height: 100%;
display: flex;
flex-direction: column;
}
.sidenav-header {
flex: 0 1 auto;
}
.sidenav-inner ul {
flex: 1 0 auto;
}
.side-nav-social {
flex: 0 1 auto;
}
https://jsfiddle.net/d2u85u4q/
You can do it like this:(be aware of margins)
html
<nav class="sidenav">
<div class="sidenav-inner">
<div class="sidenav-header">
<div class="row">
<div class="col-md-12">
header
<img src="/">
</div>
</div>
</div>
<ul class="nav sidenav-body">
<li><a>Item</a></li>
<li><a>Item</a></li>
<li><a>Item</a></li>
<li><a>Item</a></li>
<li><a>Item</a></li>
<li><a>Item</a></li>
<li><a>Item</a></li>
<li><a>Item</a></li>
<li><a>Item</a></li>
</ul>
<div class="side-nav-social">
<div>
//social media content
</div>
//more social media content
</div>
</div>
</nav>
css:
.sidenav {
height: 250px;
background: #007979;
left: 0;
position: fixed;
width: 200px;
}
.sidenav-inner {
position: relative;
height: 90%;
}
.sidenav-header{
height:10%;
}
.sidenav-body{
overflow:auto;
height:70%;
}
.side-nav-social{
position:absolute;
bottom:0;
height:10%;
}
plnker code