How can I make my webpage icon stay right? [duplicate] - html

This question already has answers here:
How can I align one item right with flexbox?
(5 answers)
Closed 1 year ago.
I have made a navbar, and it is supposed to replace the links with a menu icon on the right when the webpage gets below 630px wide. The problem is, my icon doesn't stay right, it stays as far left as it can go, leaving space for the margins of the hidden links (the elements themselves are gone - I set them to display: none; - but the margins are still there). I am working with the w3 schools article about responsive navbars, but only using it for the appearing icon part.
I have tried to change he display of the icon to flex, and all sorts of justification and alignment, but nothing will work. When it is set to flex it's width goes to 0 and I cant make it get wider again.
I am trying to give only the relevant code, but there is a fair amount of stuff I didn't include so feel free to ask me for more or to specify something.
.navContainer {
position: sticky;
top: 0;
margin: 0;
background-color: #ffffff;
display: flex;
justify-content: left;
align-content: flex-start;
padding-left: 10px;
padding-top: 20px;
padding-bottom: 10px;
}
.navContainer li {
list-style: none;
margin-right: 40px;
}
.navContainer a {
text-decoration: none;
color: black;
font-family: Montserrat-Regular;
font-size: 20pt;
margin: 10px;
}
.navContainer .icon {
display: none;
margin: 0;
}
.navContainer .iconImg {
max-height: 40px;
max-width: 40px;
}
#media (max-width: 630px) {
.navContainer a:not(.logoContainer, .name) {
display: none;
}
.navContainer li.icon {
float: right;
display: block;
}
}
<ul class="navContainer">
<li class="logoContainer"><img href="TriTech-Home.html" class="logo" src="https://via.placeholder.com/80"></img>
</li>
<li>TriTech</li>
<li><a class="link1" href="#">xDesk</a></li>
<li><a class="link2" href="#">Saturn Bikes</a></li>
<li href="javascript:void(0);" class="icon" onclick="myFunction()">
<img class="iconImg" src="Menu-Bars.ico">
</li>
</ul>

Don't float things. That's an outdated method, especially with flexbox. Use auto left margin on the last item.
.navContainer {
position: sticky;
top: 0;
margin: 0;
background-color: #ffffff;
display: flex;
justify-content: left;
align-content: flex-start;
padding-left: 10px;
padding-top: 20px;
padding-bottom: 10px;
}
.navContainer li {
list-style: none;
margin-right: 40px;
}
.navContainer a {
text-decoration: none;
color: black;
font-family: Montserrat-Regular;
font-size: 20pt;
margin: 10px;
}
.navContainer .icon {
display: none;
margin: 0;
}
.navContainer .iconImg {
max-height: 40px;
max-width: 40px;
}
#media (max-width: 2630px) { /* increased for demo */
.navContainer a:not(.logoContainer, .name) {
display: none;
}
.navContainer li.icon {
display: inline-block;
margin-left: auto; /* <-------------------- HERE */
}
}
<ul class="navContainer">
<li class="logoContainer"><img href="TriTech-Home.html" class="logo" src="https://via.placeholder.com/80" />
</li>
<li>TriTech</li>
<li><a class="link1" href="#">xDesk</a></li>
<li><a class="link2" href="#">Saturn Bikes</a></li>
<li href="javascript:void(0);" class="icon" onclick="myFunction()">
<img class="iconImg" src="https://via.placeholder.com/40">
</li>
</ul>

This should work
.nav-container .icon{
width: 100%;
text-align: end;
}

Related

HTML Aligning icons with logo

I'm having a bit of an issue where I can't get the icons to correctly align. If I remove the section for all the icons, the Logo & title align perfectly, however when adding the icons, it causes massive issues. Sorry if this is a silly question, html is new to me!
I'm using a base HTML5 template for the design elements: HTML5UP Editorial
HTML & CSS:
/* Header */
#header {
display: -moz-flex;
display: -webkit-flex;
display: -ms-flex;
display: flex;
border-bottom: solid 5px #f56a6a;
padding: 6em 0 1em 0;
position: relative; }
#header > * {
-moz-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
margin-bottom: 0; }
#header .logo {
border-bottom: 0;
color: inherit;
font-family: "Roboto Slab", serif;
font-size: 1.125em; }
#header .icons {
text-align: right; }
#media screen and (max-width: 1680px) {
#header {
padding-top: 5em; } }
#media screen and (max-width: 736px) {
#header {
padding-top: 6.5em; }
#header .logo {
font-size: 1.25em;
margin: 0; }
#header .icons {
height: 5em;
line-height: 5em;
position: absolute;
right: -0.5em;
top: 0; } }
/* Icons */
ul.icons {
cursor: default;
list-style: none;
padding-left: 0; }
ul.icons li {
display: inline-block;
padding: 0 1em 0 0; }
ul.icons li:last-child {
padding-right: 0; }
ul.icons li .icon {
color: inherit; }
ul.icons li .icon:before {
font-size: 1.25em; }
<!-- Header -->
<header id="header">
<div>
<img style="vertical-align:middle; margin-right:0.5em;"; src="images/cat-logo.png"; alt=""; width="64"; height="64";/>
<a class="logo"><strong> Oscat Pets</strong></a>
<ul class="icons">
<li><span class="label">Facebook</span></li>
<li><span class="label">Twitter</span></li>
<li><span class="label">Instagram</span></li>
<li><span class="label">Snapchat</span></li>
<li><span class="label">Medium</span></li>
</ul>
</div>
</header>
Example of Current Broken Alignment
Example of how Alignment should look
Example of Header without icons being generated
Example of HTML Inspect
You have to add below code to your css to get it working.
#header > div {
display: flex;
align-items: center;
}
#header > div > ul.icons {
margin-left: auto;
}
Try the following. I separate into two div and float the logo div to left
<style>
/* Header */
#header {
display: -moz-flex;
display: -webkit-flex;
display: -ms-flex;
display: flex;
border-bottom: solid 5px #f56a6a;
padding: 6em 0 1em 0;
position: relative;
}
#header>* {
-moz-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
margin-bottom: 0;
}
#header .logo {
border-bottom: 0;
color: inherit;
font-family: "Roboto Slab", serif;
font-size: 1.125em;
}
#header .icons {
text-align: right;
}
#media screen and (max-width: 1680px) {
#header {
padding-top: 5em;
}
}
#media screen and (max-width: 736px) {
#header {
padding-top: 0;
}
#header .logo {
font-size: 1.25em;
margin: 0;
border: 0;
}
#header .icons {
height: 5em;
line-height: 5em;
right: -0.5em;
top: 0;
}
}
/* Icons */
ul.icons {
cursor: default;
list-style: none;
padding-left: 0;
}
ul.icons li {
display: inline-block;
padding: 0 1em 0 0;
}
ul.icons li:last-child {
padding-right: 0;
}
ul.icons li .icon {
color: inherit;
}
ul.icons li .icon:before {
font-size: 1.25em;
}
</style>
<!-- Header -->
<header id="header">
<div style="float: left;">
<img style="vertical-align:middle; margin-right:0.5em;" ; src="images/cat-logo.png" ; alt="" ; width="64" ; height="64" ;/>
<a class="logo"><strong> Oscat Pets</strong></a>
</div>
<div>
<ul class="icons">
<li><span class="label">Facebook</span></li>
<li><span class="label">Twitter</span></li>
<li><span class="label">Instagram</span></li>
<li><span class="label">Snapchat</span></li>
<li><span class="label">Medium</span></li>
</ul>
</div>
</header>
The template CSS you showed has some stuff in there that is either very outdated, or just pretty horrible. Because, even though it explicitly uses flexbox, it then uses oldschool positioning and such to do the things that flexbox is actually good at; weird.
Anyway, this should give you all you asked for:
/* reset header CSS to saner defaults */
#header, #header > div, #header div > a, #header .logo, #header ul.icons, #header ul.icons li, #header ul.icons li .icon {
position: static;
display: flex;
flex: 0 0 auto;
height: auto; width: auto;
line-height: normal;
padding: 0;
}
/* now define the header layout */
#header { padding: 1em; }
#header > div { width: 100%; flex-flow: row nowrap; justify-content: flex-start; align-items: center; align-content: center; gap: 1.0em; }
#header ul.icons { flex: 1 1 auto; flex-flow: row wrap; justify-content: flex-end; align-items: center; align-content: center; gap: 0.5em; }
Just place that CSS in your stylesheet (somewhere under/after the template code).
Be aware by the way, your <a ... class="logo"> element contains some odd ;s. You use ; separators inside the style="" argument, but not outside it in the HTML.
Try using position: relative instead of position: absolute.
For further understanding you could try refer this page.
#header .icons {
height: 5em;
line-height: 5em;
position: relative;
right: -0.5em;
top: 0; } }
Hi thanks all for the feedback & advice! Turns out after about 6 hours of trying lots of different things, all that was missing was a simple statement for the ul class:
style="align-self: flex-end;"
Text is now being properly aligned although the base code has changed:
<!-- Header -->
<header id="header">
<a href="index.php" class="logo"><img style="vertical-align:middle; margin-right:0.5em;" src="images/cat-logo.png" alt="" width="64" height="64" />
<strong> Oscat Pets</strong></a>
<ul class="icons" style="align-self: flex-end;">
<li><span class="label"; >Facebook</span></li>
<li><span class="label">Twitter</span></li>
<li><span class="label">Instagram</span></li>
<li><span class="label">Snapchat</span></li>
<li><span class="label">Medium</span></li>
</ul>
</header>
Ultimately, there was nothing wrong with the template, I had just used the wrong functions. Although Raxi's contributions will be taken into consideration, upon further manipulating the project!
Thanks to everyone supporting so quickly on my first post! I really appreciate this, and will be sure to engage fully on this site!

Adding space between divs in flexbox

I want to ask about good practive in making spacing between two divs which have flexbox. For example:
header {
background-image: radial-gradient(circle, #72d6c9, #54d1ed, #7ac5ff, #bcb2fe, #f29cd9);
height: 80px;
}
.menu-section {
display: flex;
}
.nav-logo {
color: #e00986;
font-size: 25px;
margin: 0;
padding-left: 15px;
line-height: 80px;
}
nav ul {
list-style-type: none;
padding: 0;
margin: 0;
text-align: center;
}
nav ul li {
display: inline-block;
width: 150px;
position: relative;
}
nav ul li a {
text-decoration: none;
line-height: 80px;
padding: 0 10px;
display: block;
color: #e00986;
}
nav ul li a:hover {
color: #FFF;
transition-duration: 2s;
}
<header>
<nav class="menu-section">
<h1 class="nav-logo">Love &#9825 Cookies</h1>
<ul>
<li>Strona główna</li>
<li>Ciastka
<ul>
<li>Torty</li>
<li>Babeczki</li>
<li>Bezy</li>
<li>Ciasta</li>
</ul>
</li>
<li>Kontakt</li>
</ul>
</nav>
</header>
Of course I can give margin-left: 550px to nav ul and it will be okay, but I don't think so that is the good practice. What's your opinion in this topic?
All you gotta do is make the parent of those two div's (the left logo and the right menu items) a display flex and justify-content: space-between or justify-content: space-around like so
.parent-div {
display:flex;
justify-content: space-between; //or space-around
}
.child-left {//doesn't matter what you put in here just so long as its a div}
.child-right {//doesn't matter what you put in here just so long as its a div}
If you have anymore question, this is where I go if I need help figuring out how to use flex: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I guess what you need is to move your navigation menu to right, reading the comments.
So solution to this would be giving space to left for your navigation(menu links). Yes you can definitely use margin-left but you do not need to specify exact pixels.
Better would be giving margin-left:auto, so that remaining space would be left on left side of your Navigation
Solution:
header {
background-image: radial-gradient(circle, #72d6c9, #54d1ed, #7ac5ff, #bcb2fe, #f29cd9);
height: 80px;
}
.menu-section {
display: flex;
}
.nav-logo {
color: #e00986;
font-size: 25px;
margin: 0;
padding-left: 15px;
line-height: 80px;
}
nav ul {
list-style-type: none;
padding: 0;
margin: 0;
text-align: center;
margin-left:auto; /*ADDED HERE - THIS IS THE TRICK :)*/
}
nav ul li {
display: inline-block;
width: 150px;
position: relative;
}
nav ul li a {
text-decoration: none;
line-height: 80px;
padding: 0 10px;
display: block;
color: #e00986;
}
nav ul li a:hover {
color: #FFF;
transition-duration: 2s;
}
<header>
<nav class="menu-section">
<h1 class="nav-logo">Love &#9825 Cookies</h1>
<ul>
<li>Strona główna</li>
<li>Ciastka
<ul>
<li>Torty</li>
<li>Babeczki</li>
<li>Bezy</li>
<li>Ciasta</li>
</ul>
</li>
<li>Kontakt</li>
</ul>
</nav>
</header>
Hope this helps you.
.nav-logo
{
color: #e00986;
font-size: 25px;
margin-right: 100px;
padding-left: 15px;
line-height: 80px;
}
Use margin for outside spacing:)!

Space between navigation bar and screen [duplicate]

This question already has answers here:
Width 100% with white borders around it. WHy?
(2 answers)
Closed 5 years ago.
I faced an issue today when I created the navigation bar I found a space between the screen and the navigation bar,
here's what I'm talking about
I want the navigation bar with full width, no space at all, I tried using width width: 100% but it didn't work.
Here's the code :
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #141414;
height: 80px;
border-radius: 5px;
}
li {
float: right;
display:inline;
list-style-type:none;
}
li a {
font-family: Julius Sans One, Arial;
font-size: 19px;
display: block;
color: white;
text-align: center;
padding: 30px 20px;
text-decoration: none;
}
.logoimg {
height: auto;
margin-left: 150px;
margin-top: 5px;
}
.left {
float: left;
}
<div>
<ul>
<li class="left"><img class="logoimg" src="/images/logo.png"></li>
<li><a>Test 1</a></li>
<li><a>Test 2</a></li>
</ul>
</div>
From the look of it, your navigation bar is full-width. The additional whitespace you are seeing is actually coming from <body>, which adds a margin of 8px by default. You can override this to ensure that your content is flush against the edge of the page:
body {
margin: 0; /* Added */
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #141414;
height: 80px;
border-radius: 5px;
}
li {
float: right;
display:inline;
list-style-type:none;
}
li a {
font-family: Julius Sans One, Arial;
font-size: 19px;
display: block;
color: white;
text-align: center;
padding: 30px 20px;
text-decoration: none;
}
.logoimg {
height: auto;
margin-left: 150px;
margin-top: 5px;
}
.left {
float: left;
}
<div>
<ul>
<li class="left"><img class="logoimg" src="/images/logo.png"></li>
<li><a>Test 1</a></li>
<li><a>Test 2</a></li>
</ul>
</div>
It's important to note that the <body> tag is always present, even when not explicitly written. This can be seen in the snippets here -- note that the original snippet appears to be offset from the edge and the line, whereas this does not, and all I added was an override for body margin.
Hope this helps! :)
What you are experiencing is the default window/page Padding/Margin. You can set this default value to 0 in order to have your full width/height of the page:
body{
margin: 0;
padding: 0;
}
This should correct your problem.

I can't change the position of these elements

I would like to change the position of these buttons inside my navbar, also I would like you to tell me if it's correct what I'm doing in my CSS. the main idea is that I wanted to make a navbar, just I wonder to know if it's right how I did it.
Note: About the position of the buttons, as you can see I set 20 px to the margin button(before I set a position relative), but as you can see nothing happen.I would like to move the buttons more to the top.
Another doubt that I've got now, is how do I have to move, the <a> tags or the <li> or both?
Thanks!
body {
background-color: #fcf2e5;
margin: 0;
}
nav {
background-color: #beb8a4;
height: 120px;
}
.menu {
padding-left: 380px;
}
/* Nav Buttons */
.menu li {
display: inline;
font-family: sans-serif;
font-size: 1.2em;
padding-left: 40px;
text-decoration: none;
}
.navButtons {
text-decoration: none;
color: black;
}
.navButtons:hover {
color: #a52929;
}
/*I don't know if it's correct what I'm doing here below*/
ul,
li,
a {
display: inline;
position: relative;
margin-bottom: 20px;
}
/* Logo */
header nav a img {
margin-top: 20px;
margin-left: 22px;
}
<header>
<nav>
<a href="#">
<img src="Images/logo.png" title="Home" alt="Logo" />
</a>
<ul class="menu">
<li>Quienes Somos
</li>
<li>Muestras y Acabados
</li>
<li>Ubicación
</li>
<li>Contáctenos
</li>
</ul>
</nav>
</header>
Most navbars look a little like this these days. Very little markup and a bit of CSS to make it looks nice.
I've used flexbox for positioning. You can position things at the top bottom or centre using this method.
It's also using media queries, this is so it can cope with a small screen (which the snippet will have) press full screen to swap between its states.
body {
background-color: #fcf2e5;
margin: 0;
}
/* Start of navbar */
nav {
background-color: #beb8a4;
display: flex;
padding: 15px 0;
height: 100%;
align-items: center;
justify-content: space-around;
}
/* Nav buttons */
.navButtons {
font-family: sans-serif;
font-size: 1.2em;
text-decoration: none;
color: black;
}
.navButtons:hover {
color: #a52929;
}
/* Small screen support */
#media only screen and (max-width: 640px) {
nav {
flex-direction: column;
}
.navButtons {
text-align: center;
font-size: 1.6em;
max-width: 180px;
margin-top: 10px;
}
}
<nav>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/7d/BBC_America.svg/100px-BBC_America.svg.png" title="Home" alt="Logo" />
About us
Samples and Finishes
Find us
Contact us
</nav>
Hope you find this helpful.

How to center navigation that has icons so text is aligned to icons?

I am trying to create the navigation as per the picture below, where the text is centered aswell as the icon, but the text is left aligned to the icon. The link needs to be the width of its container, this also needs to be responsive:
How do you go about this?
CODEPEN DEMO HERE
HTML
<ul class="mobile-home-section">
<li>PROPERTY</li>
<li>FUTURE PLANNING</li>
<li>COMMERICAL</li>
</ul>
CSS
.mobile-home-section {
margin: 0;
padding: 0;
list-style-type: none;
width: 100%;
background: #163158
}
.mobile-home-section li {
background-image: url('http://s18.postimg.org/m26o71ohx/icon_future_hover.png');
background-repeat: no-repeat;
background-position: 0 10px;
padding-left: 50px;
}
.mobile-home-section li a {
color: white;
text-decoration: none;
padding: 20px 0;
display: block;
border-bottom: 1px solid white;
}
EDIT Changed the code to make it responsive
If i was you, I would not place the icon for the link as a background image. I would place the icon as an image inside of the link tag.
HTML
<div class="container">
<ul class="mobile-home-section">
<li>
<a href="#">
<span><img src="img2.jpg">PROPERTY</span>
</a>
</li>
<li>
<a href="#">
<span><img src="img2.jpg">FUTURE PLANNING</span>
</a>
</li>
<li>
<a href="#">
<span><img src="img3.jpg">COMMERICAL</span>
</a>
</li>
</ul>
</div>
CSS
.container {
background: #273F87;
}
ul.mobile-home-section {
display: block;
list-style-type: none;
padding: 0px;
}
ul.mobile-home-section li:nth-child(2) {
border-top: 2px solid #fff;
border-bottom: 2px solid #fff;
}
ul.mobile-home-section li a {
display: block;
color: #fff;
padding: 0px;
font-family: serif;
margin: 0px auto;
text-decoration: none;
}
ul.mobile-home-section li a span img {
width: 50px;
height: 50px;
padding-right:20px;
display: inline-block;
vertical-align: middle;
}
ul.mobile-home-section li a span {
padding: 10px 0px;
width: 230px;
display:block;
margin: auto;
}
Updated JSfiddle
As others have said, it's going to be easier using an image. The most important thing to know is that not all display: values support vertical alignment - my go-to is display: inline-block, but if you're using flexbox it might be more convenient to use display: flex
Depending on context, I typically use one of these three solutions:
Adjacent inline blocks
CSS tables
A pseudoelement with a background image
Here are examples of each of those.
Notes:
For the table solution, the <li> loses its bullet and gets an auto width (it's as wide as its contents) - depending on your context, you might want to add on a width: 100% to #or-another li
For the background image solution, if the image isn't necessarily the same size as the container you'll want to use background: no-repeat center center; -webkit-background-size: cover; background-size: cover; background-image:url(...);
In all three cases, we're accounting for the possibility that either of the elements could be the taller one. If you know the image is always going to be taller than the text, you could target #or-another span instead of #or-another li > *, and you could drop #one-more li span {...} entirely
#one-way li > * {
display: inline-block;
vertical-align: middle;
}
#or-another li {
display: table;
}
#or-another li > * {
display: table-cell;
vertical-align: middle;
}
#one-more li span {
display: inline-block;
vertical-align: middle;
}
#one-more li:before {
content:'';
width: 200px;
height: 200px;
display: inline-block;
vertical-align: middle;
background:url('https://placehold.it/200x200');
}
<ul id="one-way">
<li>
<img src="https://placehold.it/200x200" alt="" />
<span>1</span><!-- or div or p or what have you -->
</li>
</ul>
<ul id="or-another">
<li>
<img src="https://placehold.it/200x200" alt="" />
<span>2</span>
</li>
</ul>
<ul id="one-more">
<li>
<span>3</span>
</li>
</ul>
Add a text-align: center; in the .mobile-home-section class to make them align to the center and remove the padding-left: 50px; under .mobile-home-section li class. And these two CSS will become:
.mobile-home-section {
margin: 0;
padding: 0;
list-style-type: none;
width: 100%;
background: #163158;
text-align: center;
}
.mobile-home-section li {
background-image: url('http://s18.postimg.org/m26o71ohx/icon_future_hover.png');
background-repeat: no-repeat;
background-position: 0 10px;
}
You can see the demo here.
Try just editing the paddings and background-position.
.container {
width: 600px;
}
.mobile-home-section {
margin: 0;
padding: 0;
list-style-type: none;
width: 100%;
background: #163158
}
.mobile-home-section li {
background-image: url('http://s18.postimg.org/m26o71ohx/icon_future_hover.png');
background-repeat: no-repeat;
background-position:50% 15px;
padding-left: 50px;
}
.mobile-home-section li a {
color: white;
text-decoration: none;
padding: 20px 50%;
display: block;
border-bottom: 1px solid white;
text-align:left;
}
UPDATE try this
.mobile-home-section li a {
color: white;
text-decoration: none;
padding: 20px 0 20px 50%;
display: block;
border-bottom: 1px solid white;
text-align: left;
}
Update the padding of .mobile-home-section li a
The selectors could use some love, but here is my codepen:
<div class="container">
<ul class="mobile-home-section">
<li><span><img src="http://s18.postimg.org/m26o71ohx/icon_future_hover.png" /><span>PROPERTY</span></span>
</li>
<li><span><img src="http://s18.postimg.org/m26o71ohx/icon_future_hover.png" /><span>FUTURE PLANNING</span></span>
</li>
<li><span><img src="http://s18.postimg.org/m26o71ohx/icon_future_hover.png" /><span>COMMERICAL</span></span>
</li>
</ul>
.container {
width: 600px;
}
.mobile-home-section {
margin: 0;
padding: 0;
list-style-type: none;
width: 100%;
background: #163158
}
.mobile-home-section li {
margin: 0;
padding: 0;
}
.mobile-home-section li a {
display: block;
height: 3em;
}
.mobile-home-section li a > span {
display: block;
width: 55%;
padding: 0;
margin: 0 auto;
text-align: left;
}
.mobile-home-section li a > span > span {
line-height: 2em;
font-size: 1.4em;
position: relative;
top: -0.4em;
}
.mobile-home-section li span img {
margin-right: 0.75em;
height: 2.6em;
position: relative;
top: 0.2em;
}
.mobile-home-section li a {
border-bottom: 1px solid white;
color: white;
display: block;
font-family: 'Times New Roman';
font-size: 1.2em;
padding: 0;
text-align: center;
text-decoration: none;
}
http://codepen.io/anon/pen/PZwvZz