I'm trying to make a horizontal navbar, and I've got it sitting at the top of the page with each of the three list items filling up a third of the space, all good on that part.
The issue is when I try to make the anchor tag (which is within the list item) fill up all of the list item's space. I set it to display: block;, so it takes up the full width, however I can't get it to fill up vertical space how I want it to. I can use height: 100% and it will fill up the space, however if I do it that way the anchor tag's text rests at the top of the list item's space, however I want it to be vertically centred. Top and bottom padding came to mind for this, so I tried setting padding: 100%;. When this happened, I could no longer see the text of the anchor tag. By dragging the space with my mouse it looks like the tag is taking up more space than is in the list item.
This is an image of the nav bar with height: 100%;:
Note: the red lines surrounding each section are the borders which I set visible to view boundaries
The relevant HTML code is as follows:
<ul class="main_nav">
<li>Home</li>
<li>Test page</li>
<li>Test page 2</li>
</ul>
The relevant CSS code is as follows:
body * {
font-family: Roboto sans-serif;
box-sizing: border-box;
border: solid 2px red;
}
header ul.main_nav {
background-color: gray;
color: #EBEBEB;
margin: 0px;
padding: 0px;
width: 100%;
height: 50px;
list-style-type: none;
list-style-position: inside;
overflow: hidden;
}
header ul.main_nav li {
text-align: center;
float: left;
width: calc(100% / 3);
height: 100%;
}
header ul.main_nav li a {
display: block;
height: 100%;
text-decoration: none;
}
EDIT: I would rather not use fixed sizes to solve this, as I'd largely prefer being able to edit a single value (50px for the ul) to adjust the whole navbar
you can do this using flexbox,
*,
*:before,
*::after {
box-sizing: border-box;
}
body * {
font-family: Roboto sans-serif;
border: solid 2px red;
}
ul.main_nav {
background-color: gray;
color: #EBEBEB;
margin: 0;
padding: 0;
width: 100%;
height: 50px;
list-style-type: none;
list-style-position: inside;
display: flex
}
ul.main_nav li {
flex: 1;
display: flex;
height:100%;
}
ul.main_nav li a {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
background: yellow
}
<ul class="main_nav">
<li>Home
</li>
<li>Test page
</li>
<li>Test page 2
</li>
</ul>
if you are going to have one line only in your menu, yo can use line-height, see below
*,
*:before,
*::after {
box-sizing: border-box;
}
body * {
font-family: Roboto sans-serif;
border: solid 2px red;
}
ul.main_nav {
background-color: gray;
color: #EBEBEB;
margin: 0;
padding: 0;
width: 100%;
height: 50px;
list-style-type: none;
list-style-position: inside;
overflow: hidden;
font-size: 0;
}
ul.main_nav li {
text-align: center;
width: calc(100% / 3);
height: 100%;
display: inline-block;
font-size: 16px
}
ul.main_nav li a {
display: block;
height: inherit;
background: yellow;
text-decoration: none;
line-height: 32px /*this should be the same value has the parent height*/
}
<ul class="main_nav">
<li>Home
</li>
<li>Test page
</li>
<li>Test page 2
</li>
</ul>
Related
I'm trying to align all navigation links, besides the logo, to the right side of the container/navigation. I want to keep 1rem margin on both sides so that the content has some space to breathe.
I've tried using the code below but nothing on the page changes:
.menu:not(:first-child){
text-align: right;
}
<body>
<div class="body-wrap">
<header class="header">
<nav role="navigation">
<ul class="menu">
<li class="home-link"><img src="https://www.nicolefenton.com/_/images/dec/circle-menu.svg" height="12" width="12" alt=""></li>
<li>About</li>
<li>Writing</li>
<li>Speaking</li>
<li>Projects</li>
</ul>
</nav>
</header>
</div>
</body>
* { box-sizing: inherit; margin: 0; padding: 0; }
body {
position: relative;
line-height: 1.5em;
min-width: 320px;
margin: 0 auto;
color: #222222;
border: 30px solid #ffffff;
background-color: #f8f7f3;
}
.body-wrap {
display: flex;
min-height: 100vh;
display: box;
}
.header {
width: 100%;
max-width: 960px;
margin-right: 1rem;
margin-left: 1rem;
}
.menu {
display: flex;
position: absolute;
top: -0.83rem;
width: 100%;
max-width: 960px;
}
.menu:not(:first-child){
text-align: right;
}
li {
flex-grow: 1;
position: relative;
margin-right: 1em;
display: inline-block;
}
I expect all the nav links to align to the right when using the :not(:first-child) selector.
This:
.menu:not(:first-child)
selects class menu items that aren't a first child.
What you want is:
.menu :not(:first-child)
which selects non-first-child elements within a .menu class.
Notice the space.
Or better yet, make it more obvious what you really mean:
.menu li:not(:first-child)
You might just have to change to this if all you are looking to do is align the text to the right.
.menu li:not(:first-child){
text-align: right;
}
I'm new to HTML and CSS.
I'm trying to do a website, and I'm starting by the navbar, but this navbar is not "scalable" for every screen side, when it is on full screen fine but when I minimize it it does not load the part on the right side wich is "About". All of the menus are pointing to the same page and for now that's the objective.
Here's the Code:
body {}
.navbardiv {}
.navbar_ul {
list-style-type: none;
margin: 0;
padding: 0;
/*overflow:hidden;*/
background-color: #333;
border: 5px solid gray;
margin: -8px;
width: auto;
min-width: 600px;
height: 70px;
}
li {
float: left;
padding: 10px 150px;
}
li a {
display: block;
color: white;
text-align: center;
padding: 16px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<!--NAVBAR-->
<div class="navbardiv">
<ul class="navbar_ul">
<li class="navbar_li_Contact">Contact</li>
<li class="navbar_li_WebHosting">Webhosting</li>
<li class="navbar_li_About">About</li>
</ul>
</div>
You are setting the padding for li to 150px which is very high, you need to reduce.
But if you want the links to take the whole width and to be evenlly spaced, then you can use flex box and justify-content: space-between;
see code snippet:
body {}
.navbardiv {}
.navbar_ul {
list-style-type: none;
margin: 0;
padding: 0;
/*overflow:hidden;*/
background-color: #333;
border: 5px solid gray;
margin: -8px;
width: auto;
min-width: 600px;
height: 70px;
display: flex;
justify-content: space-between;
}
li {
float: left;
padding: 10px 20px;
}
li a {
display: block;
color: white;
text-align: center;
padding: 16px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<!--NAVBAR-->
<div class="navbardiv">
<ul class="navbar_ul">
<li class="navbar_li_Contact">Contact</li>
<li class="navbar_li_WebHosting">Webhosting</li>
<li class="navbar_li_About">About</li>
</ul>
</div>
Your navbar is not responsive because of min-width: 600px; part. What this one will do is that when screen resolution is below 600px in width it will keep your navbar at 600 px. Thus it will align it to leftmost part of the screen which will leave you a cropped right edge.min-width:100%; wont work either since it will start to crop when inner elements of the navbar will not fit.
It is easy to fix this. Just change it to width:100%;.
I have a "current" class which highlights the current page. And it works as far as highlighting the current page. With the padding I've added the highlighted area covers the a-element and go towards the top of the page which is what I want it to do. But each highlighted area is the width of the a-element and since each a-element has a different number of characters within, the widths are inconsistent.
To correct this I gave the a-element a display: block. This correction did give a consistent width, but now the highlighted area goes down towards the page as opposed to towards the top of the page as I wanted it.
How can I get a consistent width of highlighted area to go up towards the top of the page?
nav {
margin: 100px auto;
}
.top-nav {
width: 785px;
color: dimgrey;
text-align: center;
margin: 0 auto;
}
.top-nav li {
width: 150px;
float: left;
margin: 0 3px;
}
.top-nav a {
color: dimgrey;
width: 150px;
}
.current {
padding: 150px 0px 5px 0px;
background-color: #fab938;
color: white;
}
span.fa-bath {
font-size: 50px;
margin-top: -45px;
margin-bottom: 10px;
border-radius: 100%;
padding: 20px;
background-color: #fab938;
}
<nav>
<ul class="top-nav">
<li>Quem Somos</li>
<li>O Que Fazemos?</li>
<li><span class="fa fa-bath"></span></li>
<li>Donations</li>
<li>Contact</li>
</ul>
</nav>
I would change the way you build you nav.
Instead of using a float use a inline-block on the LI.
Next to that you can use vertical-align: middle; to align the items from the middle or use top/bottom.
also your nav container is not fitting the size of the 5 Li you are using.
https://jsfiddle.net/wyrscn48/1/
nav {
margin: 100px auto; }
.top-nav {
width: 935px;
color: dimgrey;
text-align: center;
margin: 0 auto;
}
.top-nav li {
width: 150px;
margin: 0 3px;
display: inline-block;
vertical-align: top;
}
.top-nav a {
color: dimgrey;
width: 150px; }
.current {
background-color: #fab938;
color: white; }
span.fa-bath {
font-size: 50px;
margin-bottom: 10px;
border-radius: 100%;
padding: 20px;
background-color: #fab938;
}
After having had troubles trying to display three containers in a row with the middle one being centered on the page and the side ones being of a fixed width I came across the CSS Flexbox model, mentioned in a Stackoverflow question.
Using display: flex instead of float: left or displaying the containers as inline-box whilst messing with margin seems to be working quite well, with way fewer lines of code.
However, I ran into an issue with flexbox that I can't seem to solve on my own:
I want a container #menubar to hold three containers in a row: #logo, nav and #search.
<div id="menubar">
<div id="logo"></div>
<nav>
<ul>
<li>Articles</li>
<li>Images</li>
<li>About</li>
<li>Disclaimer</li>
</ul>
</nav>
<div id="search"></div>
</div>
The #logo-container as well as the #search-container are of a fixed size (width: 80px, height: 80px). One should be placed at the very left side of the #menubar-container and one should be placed at the very right.
The nav-container should be centered within the middle of the #menubar-container. Basically the positioning is working and I get the desired layout:
[#logo left] [nav centered] [#search right]
However, for some reason the #logo-container is being displayed at the specified dimension of 80px width * 80px height while the #search-container is being displayed at 79px width * 80px height, even through the CSS looks like:
header div#menubar div#logo {
width: 80px;
height: 80px;
background-color: orange;
}
header div#menubar div#search {
width: 80px;
height: 80px;
background-color: orange;
}
To confirm I made a screenshot and zoomed in with Photoshop, selecting the container to view its dimensions.
I can't figure out why the #search-container is missing one pixel in width.
Here is a JSFiddle with the HTML and CSS I am using.
Am I using flexbox correctly?
How do I fix it so both side-containers are 80x80 pixel in dimensions?
Am I using flexbox correctly?
Yes and no
Instead of width you should, ideally, be using the flexshorthand property combining, flex-grow, flex-shrink and flex-basis.
header div#menubar div#logo {
flex: 0 0 80px;
height: 80px;
background-color: orange;
}
Alternatively, you can ensure that the element doesn't shrink by using width AND the flex-shrink value of 0
* {
margin: 0;
padding: 0
}
body {
background-color: #dfe3e5;
}
header div#top {
height: 22px;
/*background-image: url('../img/colorbar.png');
background-position: top left;
background-repeat: repeat-x;*/
background-color: gray;
}
header div#menubar {
background-color: #1c2227;
display: flex;
justify-content: space-between;
}
header div#menubar div#logo {
flex: 0 0 80px;
height: 80px;
background-color: orange;
}
header div#menubar nav {
display: table;
text-align: center;
background-color: darkred;
}
header div#menubar nav ul li {
display: inline-block;
margin-left: -4px;
list-style-type: none;
line-height: 80px;
text-align: center;
}
header div#menubar nav ul li a {
outline: 0;
display: block;
text-transform: uppercase;
padding: 0px 20px;
font-size: 1.1em;
font-family: 'Raleway', "Trebuchet MS", Arial, Helvetica, sans-serif;
color: #eee;
text-decoration: none;
}
header div#menubar nav ul li a:hover {
color: #000;
background-color: orange;
}
header div#menubar div#search {
flex: 0 0 80%;
height: 80px;
background-color: orange;
}
<header>
<div id="menubar">
<div id="logo"></div>
<nav>
<ul>
<li>Articles
</li>
<li>Images
</li>
<li>About
</li>
<li>Disclaimer
</li>
</ul>
</nav>
<div id="search"></div>
</div>
</header>
Then you get the right result
I have been struggling with some CSS Style. The problem is that i am not able to position the images properly. Due to some reason the image is not displaying in proper expected flow. And I also want to centralize the whole content. It is not properly centralize when you resize the the browser. You can easily notice all this issue once you copy past my code. Here is my code. Thanks
HTML
body {
font-family: 'Open sans',sans-serif;
}
#content p {
padding: 5px;
font-size: 0.8em;
}
#wrap {
max-width: 900px;
padding: 3%;
margin: 0 auto;
}
ul {
list-style-type: none;
padding: 0;
}
#contact a {
padding-left: 35px;
background-repeat: no-repeat;
background-size: 20px;
}
#content a {
width: 100%;
display: inline-block;
height: 100%;
}
#content li img {
width: 100%;
}
a {
text-decoration: none;
}
#content li {
float: left;
width: 25%;
margin: 3%;
box-sizing: border-box;
background-color: bisque;
}
footer {
clear: both;
text-align: center;
}
footer img {
width: 25px;
}
h1 {
font-weight: normal;
font-family: 'Change one', sans-serif;
color: #fff;
font-size: 2.4em;
padding: 0px;
margin: 0px;
display: inline-block;
}
h2 {
font-size: .9em;
font-weight: normal;
color: #fff;
margin: 10px 0;
}
#content ul {
padding: 0;
margin: 0;
}
<div id="wrap">
<div id="content">
<ul>
<li><a href="img/numbers-01.jpg"><img src="img/numbers-01.jpg">
<p>Experimentation with color and texture</p></a>
</li>
<li><a href="img/numbers-02.jpg"><img src="img/numbers-02.jpg">
<p>Experimentation with color and texture Experimentation with color and texture</p></a>
</li>
<li><a href="img/numbers-06.jpg"><img src="img/numbers-06.jpg">
<p>Experimentation with color and texture</p></a>
</li>
<li><a href="img/numbers-09.jpg"><img src="img/numbers-09.jpg">
<p>Experimentation with color and textureExperimentation with color and texture</p></a>
</li>
<li><a href="img/numbers-12.jpg"><img src="img/numbers-12.jpg">
<p>Experimentation with color and texture</p></a>
</li>
</ul>
</div>
<footer>
<img src="img/twitter-wrap.png">
<img src="img/facebook-wrap.png">
<p>© 2014 Chimed.</p>
</footer>
</div>
The issue is that some of your text items in the <p> elements wrap to 3 lines, and some only wrap to 2 lines. This makes them taller than the others. When the next <li> wraps to the next line, it ends up being positioned to the right of the taller item.
Represented visually:
To fix this, you could try to make all your items the same height. That way they would wrap cleanly around each other.
Two "issues" are in your code, as I can see so far.
You're trying to center your content and doing it right with the #wrap. But the list elements inside the list have a width of 25% each, plus 3% margin to the sides, so 31% in total. So the closest you can get to 100% width of the surrounding element is 93%, leaving a gap on the right side, because of your float: left for the list elements. This should fix the problem:
ul {
width: 93%;
margin: 0 auto;
list-style-type: none;
padding: 0;
}
#content li {
display: inline-block;
vertical-align: top;
width: 25%;
margin: 3%;
box-sizing: border-box;
background-color: bisque;
}
I added a width and centering margin to the ul and you're li elements are now inline-block and aligned at their top line.