So I've just learnt flexbox, however even after stating justify-content: flex-start; the list is not aligned to the left side of the column like so:
Image of the problem
Essentially I would like to align the list to the left side of the #navigation container so that there is not space present like in the image above. Any ideas what to change?
#navigation {
min-height: 50px;
width: 100%;
background-color: #2F4E6F;
}
.navflex {
display: flex;
flex-direction: row;
margin: 0;
list-style-type: none;
justify-content: flex-start;
}
.navflex a {
text-decoration: none;
text-decoration: none;
font-family: 'Open Sans';
font-size: 25px;
color: #FFFFFF;
display: block;
padding: 5px 25px;
text-align: center;
}
#home_button {
width: 38px;
height: 38px;
}
.tilde {
color: #FFFFFF;
font-size: 22px;
font-weight: bold;
font-family: 'Open Sans';
padding: 7px 0px;
}
#media all and (max-width: 800px) {
.navflex {
flex-direction: column;
flex-wrap: wrap;
}
.tilde {
display: none;
}
}
<div id="navigation">
<ul class="navflex">
<li>
<img src="images/home_button.png" alt="An icon representing a house." id="home_button">
</li>
<li>Phantom of the Opera</li>
<li class="tilde">~</li>
<li>The Lion King</li>
<li class="tilde">~</li>
<li>Wicked</li>
<li class="tilde">~</li>
<li>Bookings</li>
<li class="tilde">~</li>
<li><a href='#'>Location</a></li>
</ul>
</div>
Add padding: 0; to your ul (= .navflex) to avoid the default list padding. And if that isn't close enough to the left, also add padding-left: 0 to the li elements:
#navigation {
min-height: 50px;
width: 100%;
background-color: #2F4E6F;
}
.navflex {
display: flex;
flex-direction: row;
margin: 0;
list-style-type: none;
justify-content: flex-start;
padding: 0;
}
.navflex li {
padding-left: 0;
}
.navflex a {
text-decoration: none;
text-decoration: none;
font-family: 'Open Sans';
font-size: 25px;
color: #FFFFFF;
display: block;
padding: 5px 25px;
text-align: center;
}
#home_button {
width: 38px;
height: 38px;
}
.tilde {
color: #FFFFFF;
font-size: 22px;
font-weight: bold;
font-family: 'Open Sans';
padding: 7px 0px;
}
#media all and (max-width: 800px) {
.navflex {
flex-direction: column;
flex-wrap: wrap;
}
.tilde {
display: none;
}
}
<div id="navigation">
<ul class="navflex">
<li><img src="images/home_button.png" alt="An icon representing a house." id="home_button"></li>
<li>Phantom of the Opera</li>
<li class="tilde">~</li>
<li>The Lion King</li>
<li class="tilde">~</li>
<li>Wicked</li>
<li class="tilde">~</li>
<li>Bookings</li>
<li class="tilde">~</li>
<li><a href='#'>Location</a></li>
</ul>
</div>
Related
I'm creating a website and wanted to put the contact info in the footer. I created the footer using several nested divs: a footer div --> footer-container div --> footer-left div and footer-right div (using display: flex i put them side by side with each other). The footer-left div contains a ul (unordered list) of external links; the footer-right div contains social media icons. I've included the code below:
.footer {
height: 150px;
width: 100vw;
background-color: #7D594F;
}
.footer-container {
display: block;
justify-content: space-between;
align-items: center;
}
.footer-left {
border: 2px solid red;
}
.footer-left ul {
display: flex;
justify-content: space-between;
align-items: center;
list-style-type: none;
border: 2px solid blue;
}
.footer-left ul li {
color: #ffffff;
font-family: montserrat, sans-serif;
text-shadow: none;
text-align: center;
margin: 0px;
width: 150px;
border: 2px solid green;
}
.footer-right {
display: flex;
justify-content: center;
align-items: center;
}
/*to scale down the icons, the #facebook should always be 10px bigger than the #instagram*/
#facebook {
height: 40px;
width: 40px;
}
#instagram {
height: 30px;
width: 30px;
}
.footer p {
color: #ffffff;
font-family: montserrat, sans-serif;
font-size: 10px;
text-align: center;
}
<div class="footer">
<div class="footer-container">
<!--Using flex to push both containers on opposite sides of the footer-->
<div class="footer-left">
<ul>
<li>About</li>
<li>Contact</li>
<li>We Are Hiring</li>
<li>Our Financial Services</li>
<li>Our Legal Services</li>
<li>Kangen Water</li>
</ul>
</div>
<div class="footer-right">
<img id="facebook" src="/images/icons/noun_social media icon_2255034.png" alt="Link to Facebook">
<img id="instagram" src="/images/icons/noun_instagram_3350460.png" alt="Link to Instagram">
</div>
</div>
</div>
But the unordered list is not visible. I created bordered around each element to make it easier to visualize:
body {
margin: 0;
}
.footer {
height: 150px;
width: 100vw;
background-color: #7D594F;
}
.footer-container {
display: flex;
justify-content: space-between;
align-items: center;
}
.footer-left {
border: 2px solid red;
}
.footer-left ul {
display: flex;
/*flex-flow: column nowrap;*/
flex-flow: row wrap;
justify-content: space-around;
align-items: center;
list-style-type: none;
margin: 0;
padding: 0;
border: 2px solid blue;
}
.footer-left ul li {
color: #ffffff;
font-family: montserrat, sans-serif;
text-shadow: none;
text-align: center;
margin: 0px;
padding: .5em 1em;
width: 150px;
border: 2px solid green;
}
.footer-right {
display: flex;
justify-content: around;
align-items: center;
}
/*to scale down the icons, the #facebook should always be 10px bigger than the #instagram*/
#facebook {
height: 40px;
width: 40px;
}
#instagram {
height: 30px;
width: 30px;
}
.footer p {
color: #ffffff;
font-family: montserrat, sans-serif;
font-size: 10px;
text-align: center;
}
<div class="footer">
<div class="footer-container">
<!--Using flex to push both containers on opposite sides of the footer-->
<div class="footer-left">
<ul>
<li>About</li>
<li>Contact</li>
<li>We Are Hiring</li>
<li>Our Financial Services</li>
<li>Our Legal Services</li>
<li>Kangen Water</li>
</ul>
</div>
<div class="footer-right">
<img id="facebook" src="/images/icons/noun_social media icon_2255034.png" alt="Link to Facebook">
<img id="instagram" src="/images/icons/noun_instagram_3350460.png" alt="Link to Instagram">
</div>
</div>
</div>
Hey guys I'm not sure what I'm doing wrong, but I want the 3rd li item (input) to go to the end of the container, when I use the justify-content: space-between; - nothing happens, I've tried aligning them, but still nothing.
nav {
width: 100%;
background-color: black;
padding-left: 30px;
padding-right: 10px;
}
.navContact {
margin-left: auto;
}
.navbar {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.mainNav {
display: flex;
}
.navitem a {
color: white;
text-decoration: none;
border-right: 2px solid rgb(255, 123, 0);
padding: 10px 15px;
}
<hr class="hrNav">
<nav class="navbar">
<ul class="mainNav">
<li class="navitem">About me</li>
<li class="navitem">Contact</li>
<li class="navitem"><input type="text" placeholder="Search..."></li>
</ul>
</nav>
You can set flex: 1 in the ul and the last item of the ul, then set margin-left: auto in the last li inside ul as well
*,
*::after,
*::before {
box-sizing: border-box
}
nav {
width: 100%;
background-color: black;
padding-left: 30px;
padding-right: 10px;
display: flex;
justify-content: space-between;
}
.mainNav {
display: flex;
flex: 1;
}
.navitem a {
color: white;
text-decoration: none;
border-right: 2px solid rgb(255, 123, 0);
padding: 10px 15px;
}
.navitem:last-of-type {
margin-left: auto
}
<hr class="hrNav">
<nav class="navbar">
<ul class="mainNav">
<li class="navitem">About me</li>
<li class="navitem">Contact</li>
<li class="navitem"><input type="text" placeholder="Search..."></li>
</ul>
</nav>
margin-left: auto seems not working
1- Header with an image and a navbar
2- Two unorder list in a navbar
2- Try to get one of the list on the left side and the second in the right side
If I use margin-left: 800px, it's working however, it's not with auto.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
header{
display: flex;
justify-content: flex-start;
align-items: center;
padding: 10px 10px;
}
nav{
font-family: "Montserrat", sans-serif;
font-weight: 500;
font-size: 16px;
margin-left: 15px;
}
.nav__links_R, .nav__links_L, a {
text-decoration: none;
list-style: none;
float: left;
color:rgba(0, 0, 0, .50);
}
.nav__links_R {
margin-left: auto;
}
.nav__links_L li {
display: inline-block;
padding: 0px 20px;
}
.nav__links_R li {
display: inline-block;
padding: 0px 20px;
}
<header>
<img src="../photo/logo_dark.png" alt="logo">
<nav>
<ul class=nav__links_L>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<ul class=nav__links_R>
<li>Register</li>
<li>Login</li>
</ul>
</nav>
</header>
Everything is displaying well as expecting. Only ".nav__links_R" not going to the right side with "auto"
Add the following two style rules to nav:
width: 100%;
display: flex;
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
header{
display: flex;
justify-content: flex-start;
align-items: center;
padding: 10px 10px;
}
nav{
font-family: "Montserrat", sans-serif;
font-weight: 500;
font-size: 16px;
margin-left: 15px;
display: flex;
width: 100%;
}
.nav__links_R, .nav__links_L, a {
text-decoration: none;
list-style: none;
float: left;
color:rgba(0, 0, 0, .50);
}
.nav__links_R {
margin-left: auto;
}
.nav__links_L li {
display: inline-block;
padding: 0px 20px;
}
.nav__links_R li {
display: inline-block;
padding: 0px 20px;
}
<header>
<img src="http://placekitten.com/200/40" alt="logo">
<nav>
<ul class=nav__links_L>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<ul class=nav__links_R>
<li>Register</li>
<li>login</li>
</ul>
</nav>
</header>
This question already has an answer here:
Proper use of flex properties when nesting flex containers
(1 answer)
Closed 4 years ago.
I am trying to get my both ULs align in center vertically(Cross axis). I was doing it in Sass and I may have gotten confuse because there was so much nesting. I am talking about ul that are direct child of nav i.e. "nav-main-list" and "nav-social-list".
This is my HTML code:
* {
margin: 0;
padding: 0;
}
body {
background: #f3c6c6;
}
ul {
list-style-type: none;
}
a {
text-decoration: none;
color: inherit;
}
nav {
background-color: #d42e2e;
height: 3rem;
width: 100%;
display: flex;
flex-direction: row;
align-items: center;
justify-items: center;
justify-content: center;
color: whitesmoke;
}
nav ul {
height: 100%;
width: 50%;
display: flex;
flex-direction: row;
align-items: center;
margin: 0 2rem;
}
nav ul li {
height: 100%;
margin: 0;
margin-top: auto;
padding: 0 0.5rem;
}
nav ul li:hover {
color: #d42e2e;
background: whitesmoke;
}
nav ul li:hover ul {
margin: 1rem 0;
width: 100%;
display: flex;
flex-direction: column;
}
nav ul li:hover ul li {
width: 100%;
background: #d42e2e;
color: whitesmoke;
}
nav ul li:hover ul li:hover {
color: #d42e2e;
background: whitesmoke;
}
nav ul li ul {
display: none;
}
nav .nav-main-list {
text-transform: uppercase;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
font-size: 1.2rem;
}
nav .nav-social-list {
justify-content: flex-end;
font-family: "Courier New", Courier, monospace;
font-size: 1rem;
align-content: center;
align-items: center;
}
<body>
<nav>
<ul class="nav-main-list">
<li>Home</li>
<li>Languages
<ul class="lang-list">
<li>html</li>
<li>css</li>
<li>sass</li>
<li>php</li>
<li>jquery</li>
</ul>
</li>
<li>Frameworks
<ul class="frame-list">
<li>Bootstrap</li>
<li>Laravel</li>
<li>Angular</li>
</ul>
</li>
<li>Tools
<ul class="tools-list">
<li class="a" href="#">Git</li>
<li class="a" href="#">Photoshop</li>
<li class="a" href="#">Github</li>
</ul>
</li>
</ul>
<ul class="nav-social-list">
<li>Fb</li>
<li>Twitter</li>
<li>Git</li>
<li>Codepen</li>
</ul>
</nav>
</body>
The problem is that both ul are on top and not in the center of navbar. I might do it correctly by re-coding from scratch but I want to find error or reason why it's not working.
Thank You and have a Nice Day!!!
I think the issue you are seeing is to do with the padding & margins. I made a few tweaks to those, not sure if it's an improvement.
* {
margin: 0;
padding: 0;
}
body {
background: #f3c6c6;
}
ul {
list-style-type: none;
}
a {
text-decoration: none;
color: inherit;
}
nav {
background-color: #d42e2e;
height: 2.5rem;
width: 100%;
display: flex;
flex-direction: row;
align-items: center;
justify-items: center;
justify-content: center;
color: whitesmoke;
}
nav ul {
height: 100%;
width: 50%;
display: flex;
flex-direction: row;
align-items: center;
margin: 0 2rem;
vertical-align: middle;
}
nav ul li {
height: 100%;
margin: 0;
margin-top: auto;
padding: 0.25rem 0.5rem;
}
nav ul li:hover {
color: #d42e2e;
background: whitesmoke;
}
nav ul li:hover ul {
margin: 0.5rem 0 0.25rem 0;
width: 100%;
display: flex;
flex-direction: column;
}
nav ul li:hover ul li {
width: 100%;
background: #d42e2e;
color: whitesmoke;
}
nav ul li:hover ul li:hover {
color: #d42e2e;
background: whitesmoke;
}
nav ul li ul {
display: none;
}
nav .nav-main-list {
text-transform: uppercase;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
font-size: 1.2rem;
}
nav .nav-social-list {
justify-content: flex-end;
font-family: "Courier New", Courier, monospace;
font-size: 1rem;
align-content: center;
align-items: center;
}
<body>
<nav>
<ul class="nav-main-list">
<li>Home</li>
<li>Languages
<ul class="lang-list">
<li>html</li>
<li>css</li>
<li>sass</li>
<li>php</li>
<li>jquery</li>
</ul>
</li>
<li>Frameworks
<ul class="frame-list">
<li>Bootstrap</li>
<li>Laravel</li>
<li>Angular</li>
</ul>
</li>
<li>Tools
<ul class="tools-list">
<li class="a" href="#">Git</li>
<li class="a" href="#">Photoshop</li>
<li class="a" href="#">Github</li>
</ul>
</li>
</ul>
<ul class="nav-social-list">
<li>Fb</li>
<li>Twitter</li>
<li>Git</li>
<li>Codepen</li>
</ul>
</nav>
(See this great article from CSS Tricks for a complete guide to using flex.)
I'm trying to create a circle border around a number like so:
But so far all my attempts have resulted in an oval:
Codepen to see it live
SCSS code so far
$browser-context: 16;
#function em($pixels, $context: $browser-context) {
#return #{$pixels/$context}em;
}
body {
line-height: 1.6;
display: flex;
flex-direction: column;
min-height: 100vh;
font-family: 'PT Sans', sans-serif;
background: #e5dde1;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
.header-nav {
background-color: #ff4c61;
color: white;
font-family: 'Oswald', sans-serif;
}
.category-nav ul {
background-color: #212d5b;
color: #ff4c61;
display: flex;
justify-content: space-evenly;
}
.category-nav a {
font-size: em(34);
text-decoration: none;
}
.category-nav .circled {
border-radius: 50%;
width: em(12);
height: em(12);
padding: em(4);
border: em(1) solid #ff4c61;
}
HTML:
<header class="header-nav">
<h1> APP</h1>
</header>
<nav class="category-nav">
<ul>
<li>A</li>
<li>B</li>
<li>C <span class="circled">3</span></li>
</ul>
</nav>
I been experimenting different solutions so far and I think the problem comes mostly from the fact that container of the number is an inline element (span). Whenever I switch the class .circled to a div instead or set the display to block, I get a perfect circle but then the number is pushed out and it breaks the flex layout. I wonder if there's any way to get the circle to work with a span?
For your .circled class you need to add the following styles:
.category-nav .circled {
...
line-height: em(12); /* needs to match the height */
display: inline-block; /* needs to not be an inline element */
text-align: center; /* center the character*/
}
You could make it an inline-block. Adding this to your example worked for me:
display: inline-block;
line-height: em(12);
text-align: center;
Check this answer for more information
$browser-context: 16;
#function em($pixels, $context: $browser-context) {
#return #{$pixels/$context}em;
}
body {
line-height: 1.6;
display: flex;
flex-direction: column;
min-height: 100vh;
font-family: 'PT Sans', sans-serif;
background: #e5dde1;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
.header-nav {
background-color: #ff4c61;
color: white;
font-family: 'Oswald', sans-serif;
}
.category-nav ul {
background-color: #212d5b;
display: flex;
justify-content: space-evenly;
}
.category-nav a {
color: #ff4c61;
font-size: em(34);
text-decoration: none;
}
.category-nav .circled {
border-radius: 50%;
width: em(12);
height: em(12);
padding: em(4);
border: em(1) solid #ff4c61;
}
.numberCircle {
display: inline-block;
border-radius: 50%;
behavior: url(PIE.htc);
/* remove if you don't care about IE8 */
width: 1em;
height: 1em;
padding: auto;
background: transparent;
border: 2px solid red;
color: red;
text-align: center;
font: 20px Arial, sans-serif;
}
<header class="header-nav">
<h1> APP</h1>
</header>
<nav class="category-nav">
<ul>
<li>A</li>
<li>B</li>
<li>C <div class="numberCircle">3</div></li>
</ul>
</nav>