I have a navbar inside a header tag. the header tag is flex. the navbar too. in the navbar i have three a tags with the display value inline-block; and a padding in the height and width. So far so good. When i hover the links the hover effect is shown over the whole height.
The problem: If I add an image to the first link, I can't make the image higher than 10 px because the padding affects the entire navbar height. What I do not want.
Question: How can I add the image to the link without it affecting the height of the navbar?
My code
body {
margin: 0;
padding: 0;
}
header {
display:flex;
justify-content: space-between;
align-items: center;
background: green;
position: fixed;
top: 0;
overflow: hidden;
width:100%;
}
.logo {
background: yellow;
display: flex;
align-items: center;
}
.navbar {
background-color: #333;
display: flex;
align-items: center;
}
.navbar a:not(:first-child) {
display: inline-block;
}
.navbar a {
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 12px;
}
.navbar a:hover {
background: #ddd;
color: black;
}
.flex {
gap:10px;
display: flex;
align-items: center;
font-size: 12px;
}
.main {
margin-top: 180px;
color: color;
height:50vh;
background: black;
}
<div>
<header>
<div class="logo">
<img src="https://via.placeholder.com/40">Logo
</div>
<nav class="navbar">
<a href="#home">
<div class="flex">
<div>
<img src="https://via.placeholder.com/30">
</div>
<div>10000</div>
</div>
</a>
News
Contact
</nav>
</header>
<main class="main">
text
</main>
</div>
expected
body {
margin: 0;
pading: 0;
}
header {
display:flex;
justify-content: space-between;
align-items: center;
background: green;
position: fixed;
top: 0;
overflow: hidden;
width:100%;
}
.logo {
background: yellow;
display: flex;
align-items: center;
}
.navbar {
background-color: #333;
display: flex;
align-items: center;
}
.navbar a:not(:first-child) {
display: inline-block;
}
.navbar a {
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 12px;
}
.navbar a:hover {
background: #ddd;
color: black;
}
.flex {
gap:10px;
display: flex;
align-items: center;
font-size: 12px;
}
.main {
margin-top: 180px; /* Add a top margin to avoid content overlay */
color: color;
height:50vh;
background: black;
}
<div>
<header>
<div class="logo">
<img src="https://via.placeholder.com/40">Logo
</div>
<nav class="navbar">
<a href="#home">
10000
</a>
News
Contact
</nav>
</header>
<main class="main">
xca
</main>
</div>
Use flex behavior to align and stretch instead of additional markups
In the code snippet below, I removed the extra markup in the first a element that contains the img. Instead, I made all a tags display: inline-flex and removed vertical padding in the first one. Then, using the parent nav element's align-items property, I ensured each a tag has the same full height for consistent hover effect.
body {
margin: 0;
padding: 0;
}
header {
display:flex;
justify-content: space-between;
align-items: center;
background: green;
position: fixed;
top: 0;
overflow: hidden;
width:100%;
}
.logo {
background: yellow;
display: flex;
align-items: center;
}
.navbar {
background-color: #333;
display: flex;
align-items: stretch;
}
.navbar a {
display: inline-flex;
}
.navbar a {
color: #f2f2f2;
justify-content: center;
align-items: center;
padding: 14px 16px;
text-decoration: none;
font-size: 12px;
}
.navbar a:first-of-type {
padding-top:0;
padding-bottom:0;
}
.navbar a:hover {
background: #ddd;
color: black;
}
.flex {
gap:10px;
display: flex;
align-items: center;
font-size: 12px;
}
.main {
margin-top: 180px;
color: color;
height:50vh;
background: black;
}
<div>
<header>
<div class="logo">
<img src="https://via.placeholder.com/40">Logo
</div>
<nav class="navbar">
<a href="#home">
<img src="https://via.placeholder.com/30">
10000
</a>
News
Contact
</nav>
</header>
<main class="main">
text
</main>
</div>
Related
This question already has answers here:
CSS child selector higher precedence than class selector?
(3 answers)
Closed 1 year ago.
Ok, sorry in advance for the question for being silly and very specific, but I just cannot figure this out.
I am simply trying to style two <li> elements that are positioned as such: <nav><ul><li>text</li></ul></nav>. Also, I want to style the footer to be white.
I don’t know what is wrong in my code, what is preventing it from happening.
#import url('https://fonts.googleapis.com/css2?family=Roboto:wght#400;700&display=swap');
html,
body {
height: 100vh;
font-family: 'Roboto', sans-serif;
color: #ffffff;
background-color: #0e2a47;
}
.Titlebanner {
display: block;
width: 50%;
margin-left: auto;
margin-right: auto;
text-align: center;
border-color: #48ffd5;
border-radius: 1em;
}
.Titlebanner h1>a {
color: #ffffff;
font-weight: 700;
font-size: 2em;
text-decoration: none;
border-bottom: 2px solid #48ffd5;
}
ul {
display: flex;
flex-direction: row;
justify-content: center;
list-style: none;
padding: 0;
}
nav li {
list-style: none;
display: flex;
margin: 2em;
padding: 1em;
border-radius: 1em;
border: 2px solid #48ffd5;
}
/* --------------Here is what I’ve tried to modify but doesn’t work-------*/
a:hover,
a:visited,
a:link,
a:active {
color: white;
text-decoration: none;
}
/*---------------------------------------*/
.body {
display: flex;
flex-direction: row;
}
.ranking {
width: 8%;
height: 4em;
background-color: grey;
align-items: center;
position: -webkit-sticky;
position: sticky;
top: 0.5em;
}
.ranking>ul {
padding: 0;
display: flex;
flex-direction: column;
}
.ranking>ul>li {
margin: auto
}
.Analysis {
display: flex;
flex-direction: row;
margin: auto;
width: 80%;
justify-content: center;
text-align: justify;
}
/*---------------------- and here for the footer -------*/
footer>a {
display: block;
width: 30%;
margin-left: auto;
margin-right: auto;
text-align: center;
color: white;
}
/*-----------------------------------------------*/
<body>
<header>
<div class="Titlebanner">
<h1>Impact of Privacy Policies on Users’ Lives</h1>
</div>
<!------------- here is the part I’m trying to style----->
<nav>
<ul>
<li>Results</li>
<li>Analysis</li>
</ul>
</nav>
<!----------------------------------------------------->
</header>
<div class="body">
<div class="ranking" id="ranking">
<ul>
<li>First Place</li>
<li>Second Place</li>
</ul>
</div>
<div class="Analysis">
text
</div>
</div>
</body>
<!----------------- and the footer I’m trying to style as well------->
<footer>
<span class="">
About us
</span>
</footer>
<!------------------------------------------------------------->
You are trying to access the a tag that is a direct child in your footer use footer a or footer > span > a instead
I am not sure why your header styling is not working, I guess it has to do with specificity try simplifying your selector header > h1 > a to header a and change a:hover to header a:hover
also try not to use capital symbols when naming classes
When I create a new div, the second one, whether it be an image or text. It overlaps the first div container. I figured it may be something to do with the display: flex;, but I'm not sure. Also I am super new so sorry if this is elementary.
* {
font-family: Helvetica;
font-size: 22px;
color: seashell;
background-color: black;
opacity: 0.9;
}
.container {
height: 69px;
border-bottom: 1px solid seashell;
position: fixed;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
}
.container li {
display: inline;
text-decoration: underline;
justify-content: flex-end;
margin-right: 20px;
}
.header-image {
height: 50px;
margin-left: 10px;
}
.container li:hover {
text-decoration: none;
cursor: pointer;
color: darkgray;
}
<div class="container">
<img class="header-image"
src="https://content.codecademy.com/courses/freelance-1/unit-4/img-tea-cozy-logo.png">
<ul>
<li>Mission</li>
<li>Featured Tea</li>
<li>Locations</li>
</ul>
</div>
<div>
<p>Hello</p>
</div>
Thank you.
It's because your container has positioned: fixed; assigned to it. If you add a margin to your second div and apply top: 0 to your container you can push it down without affecting your container element like below:
* {
font-family: Helvetica;
font-size: 22px;
color: seashell;
background-color: black;
opacity: 0.9;
}
.container {
height: 69px;
border-bottom: 1px solid seashell;
position: fixed;
top: 0rem; /* force container to the top of the page */
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
}
.container li {
display: inline;
text-decoration: underline;
justify-content: flex-end;
margin-right: 20px;
}
.header-image {
height: 50px;
margin-left: 10px;
}
.container li:hover {
text-decoration: none;
cursor: pointer;
color: darkgray;
}
div:nth-of-type( 2 ) {
margin-top: 4rem; /*give the second div top margin to push it down */
}
<div class="container">
<img class="header-image"
src="https://content.codecademy.com/courses/freelance-1/unit-4/img-tea-cozy-logo.png">
<ul>
<li>Mission</li>
<li>Featured Tea</li>
<li>Locations</li>
</ul>
</div>
<div>
<p>Hello</p>
</div>
just remove position: fixed form .container or if you wants it to be fixed you can add z-index: 1
Your second div ('Hello' block) overlaps first div(header container), because first div has position: fixed style.
When you apply position: fixed or position: absolute to element, the element is become removed from the normal document flow, and no space is created for the element in the page layout(https://developer.mozilla.org/en-US/docs/Web/CSS/position).
Since div is removed from document flow it doesn't take any space in document, imagine that div is flying on another separate layer.
If you want to keep position: fixed property you need to compensate it's height by applying:
padding-top: 69px to div's parent -> body.
and
top: 0 to .container to make header stick to the top of the screen;
This way all next elements will have right position from beginning and you will not need to offset each of them.
* {
font-family: Helvetica;
font-size: 22px;
color: seashell;
background-color: black;
opacity: 0.9;
}
/* HERE */
body {
padding-top: 69px;
}
.container {
height: 69px;
border-bottom: 1px solid seashell;
/* HERE */
top: 0;
position: fixed;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
}
.container li {
display: inline;
text-decoration: underline;
justify-content: flex-end;
margin-right: 20px;
}
.header-image {
height: 50px;
margin-left: 10px;
}
.container li:hover {
text-decoration: none;
cursor: pointer;
color: darkgray;
}
<div class="container">
<img class="header-image"
src="https://content.codecademy.com/courses/freelance-1/unit-4/img-tea-cozy-logo.png">
<ul>
<li>Mission</li>
<li>Featured Tea</li>
<li>Locations</li>
</ul>
</div>
<div>
<p>Hello</p>
</div>
<div>
<p>Second Hello</p>
</div>
I want to make a logo which looks like this:
but I can't get it to work, this is what I have at the moment:
I want the logo on the left to be the same as the first picture I uploaded, does anyone know how to do this?
I tried with vertical-align: middle which doesn't work, and I tried display: flex and then align: center but that doesn't work either.
Here is my attempt:
header {
padding-top: 24px;
padding-bottom: 24px;
background: #1b1b2e;
width: 100%;
border-bottom: 1px solid black;
align-items: center;
display: grid;
grid-template-columns: 1fr 3fr;
}
header a {
text-decoration: none;
display: flex;
justify-content: space-between;
color: white;
}
header img {}
#nav {
display: flex;
justify-content: space-around;
}
#logo {
display: flex;
justify-content: space-around;
}
<header>
<div id="Logo"> <img src="img/logo_shape.png" alt="Logo">
<h1>Dublin apps</h1>
<h3>Ipad applications</h3>
</div>
<div id="nav">
Home Ipad Apps
Demonstrations
Leogards Media
connect us
</div>
</header>
As already mentioned Rickard Elimää your CSS targets #logo but your id says "Logo".
We must also put
flex-direction: column;
and some other adjustment to make it work.
I also created the graphic element in the css logo as it is very simple, if you want an image instead of the pseudo element you can put it inside h1 like this:
<h1><img src="path_to.png" alt="i">Dublin apps</h1>
and remove the css rules for the pseudo element.
#Logo h1:before
You will need to change the logo font to match the one in the image
header {
padding-top: 24px;
padding-bottom: 24px;
background: #1b1b2e;
width: 100%;
border-bottom: 1px solid black;
align-items: center;
display: grid;
grid-template-columns: 1fr 3fr;
}
header a {
text-decoration: none;
display: flex;
justify-content: space-between;
color: white;
}
#nav {
display: flex;
justify-content: space-around;
}
#Logo {
display: flex;
justify-content: space-around;
color: white;
flex-direction: column;
}
#Logo h1, #Logo h3 {
margin: 0;
}
#Logo h1 {
font-size: 24px;
display: flex;
align-items: center;
font-weight: 300;
}
#Logo h3 {
font-size: 14px;
padding-left: 24px;
font-weight: 300;
}
#Logo h1:before {
content: "i";
display: inline-block;
font-size: 14px;
color: black;
background-color: white;
width: 20px;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 50% 0 50% 50%;
margin-right: 4px;
}
<img src="https://i.stack.imgur.com/NPCDE.png" alt="">
<header>
<div id="Logo">
<h1>Dublin apps</h1>
<h3>Ipad applications</h3>
</div>
<div id="nav">
Home Ipad Apps
Demonstrations
Leogards Media
connect us
</div>
</header>
Try this answer.
header {
padding: 10px 20px;
background: #1b1b2e;
width: 100%;
border-bottom: 1px solid black;
align-items: center;
display: grid;
grid-template-columns: 1fr auto;
}
header img {
width: 25px;
margin-right: 5px;
}
#nav {
display: flex;
justify-content: space-around;
}
#nav a {
text-decoration: none;
color: white;
padding: 3px 5px;
margin: 0 2px;
}
#nav a.active {
background-color: #fff;
color: #111;
border-radius: 2px;
}
#logo {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: flex-start;
}
.logo-text {
display: inline-flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
}
.logo-text h1, .logo-text h3 {
margin: 0;
color: #fff;
}
.logo-text h1 {
font-size:16px;
}
.logo-text h3 {
font-size:14px;
}
<header>
<div id="logo">
<img src="https://d1nhio0ox7pgb.cloudfront.net/_img/o_collection_png/green_dark_grey/512x512/plain/leaf.png" alt="Logo">
<div class="logo-text">
<h1>Dublin apps</h1>
<h3>Ipad applications</h3>
</div>
</div>
<div id="nav">
Home Ipad Apps
Demonstrations
Leogards Media
connect us
</div>
</header>
enter image description hereUse this css
#logo img{
margin:0 auto;
display:table;
}
I'm having trouble displaying my child div tags side-by-side while the parent div tag is display flex, if anyone can show me how to get this to work, that would be great! Here's my code sample:
css
.wrapper{
z-index: +1;
display: flex;
flex-direction: column;
height: 100vh;
align-items: baseline;
justify-content: space-around;
}
.wrapper .logo{
outline: 1px solid #fff;
width: 400px;
height: 150px;
}
.wrapper .logo img{
width: 100%;
}
.wrapper .discription{
width: 320px;
outline: 1px solid #fff;
}
.wrapper .discription h1{
color: #fff;
padding: 0;
margin: 0;
text-transform: uppercase;
}
.wrapper .links{
outline: 1px solid #fff;
}
.wrapper .links nav ul{
list-style-type: none;
list-style: none;
padding: 0;
margin: 0;
}
.wrapper .links nav ul li{
display: block;
}
.wrapper .links nav ul li a{
font-size: 20px;
color: #fff;
padding: 10px;
display: block;
text-transform: uppercase;
text-decoration: none;
}
html
<div class="wrapper">
<div class="logo">
<img src="images/fire-engine.png" draggable="false">
</div>
<div class="discription">
<header>
<h1>Best non-host mw2 menu cheat engine [cex/dex]</h1>
</header>
</div>
<div class="links">
<nav>
<ul>
<li>home</li>
<li>pricing</li>
<li>support</li>
<li>login</li>
</ul>
</nav>
</div>
</div>
what I'm trying to do, similar to this:
Just keep display: flex and remove flex-direction: column in your .wrapper because flex-direction by default is row when you have display: flex.
.wrapper{
z-index: +1;
display: flex;
height: 100vh;
align-items: baseline;
justify-content: space-around;
}
https://jsfiddle.net/nL27af3z/1/
I’m trying to practice my HTML5 and CSS3 skills by making a navbar for my page, but I’m having a lot of trouble. I'm trying to use HTML5 semantic tags to make it, but none of them are looking how I want them to and it is a huge positioning mess. Could someone please tell me how I could make a functional navbar?
Here’s my HTML code for the whole navbar and header:
body {
margin:0;
}
#nav-plus-head-wrapper {
background: blue;
height: 100px;
position: absolute;
top: 0px;
left:0px;
right:0px;
}
#topheader {
}
#topnav {
background: yellow;
position: relative;
top: 0px;
right: 0px;
}
.navli {
display: inline;
}
a:link {
text-decoration: none;
color: red;
}
a:hover {
text-decoration: none;
color: orange;
}
a:visited {
text-decoration: none;
color: white;
}
<section id="nav-plus-head-wrapper">
<!--CODE FOR WEBSITE NAME-->
<header id="topheader">
<h1>Site</h1>
</header>
<!--CODE FOR TOP NAVBAR-->
<nav id="topnav">
<ul id="topnav-ul">
<li class="navli">Home</li>
<li class="navli">About</li>
<li class="navli">Services</li>
<li class="navli">Contact</li>
</ul>
</nav>
</section>
There is more than one way to make a functional nav bar. you can buttons, divs, links, lists, or even a mix of them. I usually go with the simplest way.
here is a simple way with simple code that you can check and read to learn from it.
I hope this will help.
* {
scroll-behavior: smooth;
margin: 0;
padding: 0;
text-decoration-line: none;
scroll-padding-top: 100px;
}
.header {
width: 100%;
height: 100px;
position: fixed;
top: 0;
background: #666699;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
align-content: center;
}
.navbar {
width: 50%;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
align-content: center;
}
.navBtn {
width: 80px;
height: 40px;
margin: 0 10px;
font-size: 20px;
background: white;
display: flex;
justify-content: center;
align-items: center;
}
.navBtn a {
color: #000;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.section {
width: 100%;
height: 90vh;
z-index: -1;
display: flex;
color: white;
font-size: 40px;
justify-content: center;
align-items: center;
align-content: center;
}
.homeSect {
background: #0066ff;
margin-top: 100px;
height: 100vh;
}
.workSect {
background: #33cc33;
}
.aboutSect {
background: #ffcc00;
}
</head>
<header class="header" id="header">
<nav class="navbar">
<button class="navBtn homeBtn" id="homeBtn">
Home
</button>
<button class="navBtn workBtn" id="workBtn">
Work
</button>
<button class="navBtn aboutBtn" id="aboutBtn">
About
</button>
</nav>
</header>
<body class="body">
<div class="onscrolldiv" id="onscrolldiv"></div>
<section class="homeSect section" id="homeSect">Home section</section>
<section class="workSect section" id="workSect">Work section</section>
<section class="aboutSect section" id="aboutSect">About section</section>
first you need to reset all your html like this:
*, *:after, *:before {
outline: none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
margin: 0;
}
then you float your <header id="topheader"> left maybe it s a logo
#topheader {
float:left;
color:white;
}
and you <nav id="topnav"> right the main nav
#topnav {
position: relative;
top: 20px;
float:right;
}
now you can set you items like this
.navli {
float:left;
list-style:none;
width:24%;
height:100%;
padding:20px;
margin:0 0 0 1%;
background-color:#ccc;
}
demo: http://jsfiddle.net/AvHKT/1/