Justify inner Flexbox items across full width of flex container - html

I have a Flexbox in use for header navigation, the logo is aligned to the left and the ul items are aligned to the right as in a traditional style. Both the logo and the navigation links are flex items within a full width Flexbox, and I have given them both flex: 50%. The navigation links section is also a Flexbox (an inner Flexbox) to prevent the menu from stacking and instead behaving in a better responsive manner.
When I apply justify-content to that inner Flexbox, there is no change to the links, as if there is an overriding style or the property does not work on an inner text box. I should like the navigation links to equally divide themselves among the 50% of the screen width.
I've toyed with placing flex: auto on the items but can't keep it within the current layout by doing that, and I've tried fiddling with inline elements to see if I can remove any overriding property, but no cigar.
#nav {
display: flex;
flex: 50%;
align-items: center;
}
#logo {
margin-right: auto;
width: 50px;
height: auto;
}
#links {
margin-left: auto;
display: flex;
justify-content: space-between;
}
#links a {
text-decoration: none;
}
<nav id="nav">
<img id="logo" src="https://pngimage.net/wp-content/uploads/2018/06/logo-placeholder-png.png"/>
<ul id="links">
<li><a href="#">Link1<a></li>
<li><a href="#">Link2<a></li>
<li><a href="#">Link3<a></li>
<li><a href="#">Link4<a></li>
</ul>
</nav>

You were pretty close. Important changes I made were to set the width of the #links <ul> to 50% and add justify-content: space-between to the container #nav wrapper. A few other style changes to the ul so it doesnt have default margin and padding and I think it is behaving as you are expecting now..
#nav {
display: flex;
align-items: center;
justify-content: space-between;
}
#logo {
width: 50px;
flex: 0 0 50px;
}
#links {
width: 50%;
display: flex;
justify-content: space-between;
margin: 0;
padding: 0;
list-style: none;
}
#links a {
text-decoration: none;
}
<nav id="nav">
<img id="logo" src="https://pngimage.net/wp-content/uploads/2018/06/logo-placeholder-png.png"/>
<ul id="links">
<li>Link1</li>
<li>Link2</li>
<li>Link3</li>
<li>Link4</li>
</ul>
</nav>

I think you have problem with flex: 50%; CSS deceleration. It's not at proper place. I have re-write the html to use it properly and fixed the CSS according.
Here is the Modified CSS
#nav {
display: flex;
background: #eee;
}
#nav>#logo,
#nav>#links {
flex: 50%;
}
#logo img {
width: 50px;
height: auto;
}
#links {
display: flex;
justify-content: space-around;
list-style-type: none;
}
#links a {
text-decoration: none;
}
<nav id="nav">
<div id="logo"><img src="https://pngimage.net/wp-content/uploads/2018/06/logo-placeholder-png.png" /> </div>
<ul id="links">
<li><a href="#">Link1<a></li>
<li><a href="#">Link2<a></li>
<li><a href="#">Link3<a></li>
<li><a href="#">Link4<a></li>
</ul>
</nav>
Also the code available at codepen https://codepen.io/mobarak/pen/jRjZxB/

Related

Space between menu items

I'm trying to make menu on top bar but I want to make a little bit of space between each element. How can I make some space between each menu item?
.TopMenu {
display: flex;
justify-content: center;
align-items: center;
width: 50%;
height: 40px
}
<div class="TopMenu">
<a class="active" href="#home">Inicio</a>
Tecnologias Que Trabajamos
Labs
Contacto
Legal
</div>
Since you are using a flexbox container you can use the gap attribute on your flex element as follows:
.TopMenu {
gap: 10px;
}
You should also add some styling to the a links. Adding :not(:first-of-type) makes sure this will only happen from the second item and onward
.TopMenu a:not(:first-of-type) {
padding-left: 8px;
}
Just switch justify-content to space-between
.TopMenu{
display: flex;
align-items: center;
width: 50%;
height: 40px;
justify-content: space-between;
}
<html>
<body>
<div class="TopMenu">
<a class="active" href="#home">Inicio</a>
Tecnologias Que Trabajamos
Labs
Contacto
Legal
</div>
</body>
</html>
See it in jsfiddle

sizing a container with an image to fit the size of another container

I have a header with a nav list and a logo in it. When the screen size shrinks to a certain size, I want the nav list to go away, and have a nav icon appear instead. I am having trouble sizing the image. Here is my html
<header>
<div class="navContainer">
<img src="download.png" alt="Nehemiah Logo" class="logo">
<!--consider adding a div to the drop down for easier styling-->
<nav>
<ul class= "navList">
<li class=navListItem><a class="navTag current"
href="nehemiahUniversity.html">Home</a></li>
<li class=navListItem><a class="navTag"
href="courses.HTML" >Courses</a>
<ul class="navDropList" id="navDropCourses">
<li class="navDropItem"><a class="dropTag"
href="courses.html#lifeSkillsSection">Life Skills</a></li>
<li class="navDropItem"><a class="dropTag"
href="courses.html#jobSkillsSection">Job Skills</a></li>
</ul>
</li>
<li class=navListItem>
<a class="navTag" href="training.html">Training
Material</a>
<ul class="navDropList">
<li class="navDropItem"><a class="dropTag"
href="training.html#productionSection">Production</a></li>
<li class="navDropItem"><a class="dropTag"
href="training.html#warehouseSection">Warehouse</a></li>
<li class="navDropItem"><a class="dropTag"
href="training.html#qualitySection">Quality Control</a></li>
<li class="navDropItem"><a class="dropTag"
href="training.html#blendingSection">Blending</a></li>
<li class="navDropItem"><a
class="dropTag" href="training.html#officeSection">Office</a></li>
</ul>
</li>
<li class=navListItem><a class="navTag"
href="resources.HTML">Resources</a></li>
</ul>
<!--styling the nav list that shows on smaller screens-->
</nav>
<div class=navIconContainer>
<img src="nav.png" alt="nav icon" class="navIcon">
</div>
</div>
</header>
And here is my css
body{
width: 100%;
height: 100%;
margin: 0;
background: #fff;
}
.navContainer {
width: 90%;
display: flex;
margin: 0 auto;
}
/*background color*/
header{
height: 150px;
background:#414141;
}
/* this makes it so the header block still shows even the logo and ul
are floated to the left and right, respectively*/
/*header::after {
content: '';
display: table;
clear: both;
}*/
/*have logo appear on the left and set sizing and round edges*/
.logo {
float:left;
width: 80px;
height: 80px;
border-radius: 50%;
margin-top: 20px;
margin-bottom: 20px;
}
/*maybe put this inside a div and center that div*/
.navIcon{
background-color: rgb(223, 196, 196);
display: flex;
height: 30%;
}
.navIconContainer{
display: none;
}
.navListSmall{
display: none;
}
/*styling nav responsiveness*/
/*create a mobile style nav for when screen is below certain pixel*/
#media screen and (max-width: 1400px) {
.navList {
display: none;
}
}
#media screen and (max-width: 1400px) {
.navIconContainer {
background-color: red;
display: flex;
justify-content: center;
align-items: center;
}
}
I colored the container and the image background to make it easier to
see how they are being manipulated. Can someone help me get the size of
the container (red cube) to fit the header? i ultimately want that icon
to be centered horizontally and floated to the right. Thanks.
In the css, Remove height: 150px from header and add it to .navContainer.
Modify your css code
.navContainer {
width: 90%;
display: flex;
margin: 0 auto;
align-items: center;
justify-content: space-between;
}
.navIcon has height: 30% which is the 30% of the parents height.
.navIconContainer is the parent and it has no height giving a height to it should fix this.
Also instead of floats try adding display: flex and justify-content: space-between to .navContainer
and to center .navIcon try adding display: grid and place-items: center to navIconContainer

I want my nav bar to appear on the opposite side of my logo

I want my header to appear as such, with the logo on one side and the nav bar on the other side:
Image
It currently appears with the logo and nav bar stacked on top of each other, on the left side: Image
I tried using a flex display, and justifying the content but that didn't work. Any ideas? Here's the code pen: Why do I need to have code to link a codepen lol https://codepen.io/jalal_b/pen/ZEpmzzX
This is what you need to add to your styles:
#header { display: flex; }
#nav-bar { flex: 1; }
#nav-bar ul { display: block }
#nav-bar ul li {
display: inline-block;
margin: 0 2em;
}
You might need to fit into your design.
Here is an article about flex-box in CSS if you want to learn more: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Good luck! ✌️
I copied your code to a snippet below.
I simply added this code to your CSS:
#header {
display: flex;
flex-direction: row;
}
Then set display: inline on your list items:
li {
list-style-type: none;
display: inline;
}
You will probably need to adjust your layout a bit to space out the list items a bit more.
#import url('https://fonts.googleapis.com/css2?family=Staatliches&display=swap');
#import url('https://fonts.googleapis.com/css2?family=Montserrat:wght#700&display=swap');
#import url('https://fonts.googleapis.com/css2?family=Raleway&display=swap');
#header {
display: flex;
flex-direction: row;
}
#header-img {
margin-left: 40px;
margin-right: auto;
display: block;
height: 45px;
width: auto;
}
.nav-link {
color: #BEB7A4;
text-decoration: none;
font-family: "Montserrat";
}
li {
list-style-type: none;
display: inline;
}
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<div id="page-wrapper">
<header id="header">
<div class="logo">
<img
id="header-img"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeYAAABoCAMAAAATgKPhAAAAflBMVEX///8AAACnp6f29vadnZ1xcXH5+fmZmZlKSkrq6urc3Nzz8/Pm5uYsLCy4uLgxMTHNzc3U1NTg4OBSUlKCgoJLS0vMzMzo6OgfHx9gYGAkJCQNDQ04ODiOjo4oKChmZmaurq4/Pz/CwsJ2dnaHh4cVFRU2NjZiYmJ0dHRra2tiAXGQAAAQH0l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Mee+0L4PBJ92O/YbYmmHA7v57GbhKvIWnRnY6bbHTtrIV0IfDH/cmxUqIK2nX530kWUc/mV43SBMO/2v2fGCWS+NpNnmfxXn7gfBIPXP/3b/pX6/8MILL7zwwgv/T/F/8/jqGMewQ5wAAAAASUVORK5CYII="
alt="sporks logo"/>
</div>
<nav id="nav-bar">
<ul>
<li><a class="nav-link" href="#about">About</a></li>
<li><a class="nav-link" href="#how-to-use">How To Use</a></li>
<li><a class="nav-link" href="#where-to-buy">Where To Buy</a></li>
</ul>
</nav>
</header>

Aligning images in footer

I'm trying to create a simple footer with images to each side and some text in the middle.
Problem is the images aren't the same size and therefore the alignment is from top to bottom , i know their is a way to align to middle - I've tried to use
vertical-align:middle
But it didn't work.
Here is what I've done so far - if you have more tips for me regarding doing footer right i'll be glad to hear.
Fiddle
Use the flexbox module with justify-content: space-between. This will push the child nodes of your container away from each other so the left and right images sit against the edges.
footer {
display: flex;
justify-content: space-between;
text-align: justify;
}
<footer>
<img>
<span>text</span>
<img>
</footer>
display: flex; align-items: center; justify-content: center; on the footer ul will align the items in the footer vertically and horizontally. Also removed the fixed height from your footer and am applying top/bottom padding instead which will ensure even spacing on the top/bottom. And you have a random stray </p> that needs to be removed.
img {
width: 120px;
}
.container-footer {
margin: auto;
width: 100%;
text-align: center;
}
#footer {
background-color: #01b3d0;
padding: 1em 0;
}
#footer-images ul {
padding: 0;
}
#footer-images li {
list-style: none;
margin: 0 10px;
display: block;
}
#footer-images ul {
display: flex;
justify-content: center;
align-items: center;
}
<div class="container-footer">
<div id="footer">
<div id="footer-images">
<ul>
<li class="pull-left">
<img src="http://www.essai-automobile.com/actualites/photos-logos/jaguar-logo.png" class="pull-left img-responsive">
</li>
<li class="pull-center">©QBS LAB - ©TCWD 2017</li>
<li class="pull-right">
<img src="https://s-media-cache-ak0.pinimg.com/originals/9e/0d/0d/9e0d0d29921036c2ff5e78d891573f45.png" class="pull-right img-responsive">
</li>
</ul>
</div>
</div>
</div>

how to properly align list items vertically?

I wanted to create a list of items by displaying a name, a list of properties and an image. Although this seems like quite a common and easy problem, I am struggling to get it right.
After having changed the markup a dozen of times, I chose to represent the list by a ul in which each li consists of a h3(name), a ul(properties) and a img(image).
In order to make it fill the page a bit more, I used CSS's flexbox in order to put the image and the properties next to each other in a responsive way.
img {
max-width: 100px;
}
#example > ul > li {
display: -webkit-flex;
display: flex;
-webkit-justify-content: space-around;
justify-content: space-around;
-webkit-align-items: center;
align-items: center;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
}
h3 {
width: 100%;
text-align: center;
}
div > ul {
border-left: 2px solid red;
}
<section id="example">
<ul>
<li>
<h3>Bulbasaur</h3>
<div>
<span>Properties</span>
<ul>
<li>green</li>
<li>seed</li>
<li>grass</li>
<li>poison</li>
</ul>
</div>
<img src="http://cdn.bulbagarden.net/upload/2/21/001Bulbasaur.png" />
</li>
<li>
<h3>Charmander</h3>
<div>
<span>Properties</span>
<ul>
<li>orange or some kind of red, I am not completely sure</li>
<li>lizard</li>
<li>fire</li>
</ul>
</div>
<img src="http://cdn.bulbagarden.net/upload/7/73/004Charmander.png" />
</li>
<li>
<h3>Squirtle</h3>
<div>
<span>Properties</span>
<ul>
<li>blue</li>
<li>tiny turtle</li>
<li>water</li>
</ul>
</div>
<img src="http://cdn.bulbagarden.net/upload/3/39/007Squirtle.png" />
</li>
</ul>
</section>
This looks pretty nice when the properties for all elements are equally long, but it kind of looks messy when this is not the case (the property-lists are not properly aligned as indicated by the red lines in the above snippet). I know I could get all the content in a table, causing every table element to be aligned nicely under each other, but then I don't know how I can have my names in a different line than the properties and the image...
My question could thus be formulated as:
How can I align the properties nicely under each other in such a way that they are displayed next to the image (to fill the space on the screen)? Additionally I would like that the image is displayed under the properties when the screen becomes too small (i.e. responsive design) and a separate line for the name.
Any help will be greatly appreciated
Update:
As it turned out that my question is not that clear, I tried to make it more clear by adding the vertical red lines in the snippet. I manage to get the desired result when using a table, but then I have to omit the names (as shown in the attached image) and the responsiveness...
You can just create a simple item element, something like this:
HTML
<li class="item">
<h2>Charmander</h2>
<div class="content">
<h3>Properties</h3>
<ul>
<li>orange or some kind of red, I am not completely sure</li>
<li>lizard</li>
<li>fire</li>
</ul>
</div>
<div class="image">
<img src="http://cdn.bulbagarden.net/upload/7/73/004Charmander.png" />
</div>
</li>
I simply divided the element in three main sections: title, properties and the image.
As you can see the properties are still inside a <ul> because they are used like a enumeration.
CSS
#example > ul {
padding: 0;
}
.item {
width: 100%;
background: #CCC;
padding: 20px;
box-sizing: border-box;
/* Padding will be inside the element (will not affect the width/height) */
margin: 20px 0;
overflow: hidden;
/* Used to keep the floated element inside the flow */
}
.item h2 {
text-align: center;
}
.item .content {
width: 60%;
float: left;
padding-left: 20px;
box-sizing: border-box;
}
.item .image {
width: 200px;
float: left;
}
.item img {
width: 100%;
}
.item .content ul {
border-left: 2px solid red;
margin-bottom: 20px;
}
With the first selector (#example > ul) I reset the default padding it has.
The text of the properties will just start on a new-line if it is too long (you can test this by resizing the window).
You can just edit the padding-left of the .content element, to move the properties a little bit more to the right or to the left.
Example JsFiddle
This is just to give you an example of how you want to approach this.
Hope it was helpful!
I have just been so stupid. As an alternative to the helpful answer of nkmol, it could also be as simple as changing the justify-content property to space-between and correct it by setting width and auto-margins.
img {
max-width: 100px;
}
#example > ul > li {
width: 80%;
margin: auto;
display: -webkit-flex;
display: flex;
-webkit-justify-content: space-between;
justify-content: space-between;
-webkit-align-items: center;
align-items: center;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
}
h3 {
width: 100%;
text-align: center;
}
li > div > ul {
border-left: 2px solid red;
}
<section id="example">
<ul>
<li>
<h3>Bulbasaur</h3>
<div>
<span>Properties</span>
<ul>
<li>green</li>
<li>seed</li>
<li>grass</li>
<li>poison</li>
</ul>
</div>
<img src="http://cdn.bulbagarden.net/upload/2/21/001Bulbasaur.png" />
</li>
<li>
<h3>Charmander</h3>
<div>
<span>Properties</span>
<ul>
<li>orange or some kind of red, I am not completely sure</li>
<li>lizard</li>
<li>fire</li>
</ul>
</div>
<img src="http://cdn.bulbagarden.net/upload/7/73/004Charmander.png" />
</li>
<li>
<h3>Squirtle</h3>
<div>
<span>Properties</span>
<ul>
<li>blue</li>
<li>tiny turtle</li>
<li>water</li>
</ul>
</div>
<img src="http://cdn.bulbagarden.net/upload/3/39/007Squirtle.png" />
</li>
</ul>
</section>
PS: I'm sorry for my awful question...
You need to break out your items from the primary UL
You can think of it as though you were building a table, but instead, use divs and then use a UL just to list the properties. This way, you can style each of the individual elements as needed.
look here: https://jsfiddle.net/oq04f6pm/2/
<section id="example">
<div class="section-title">Bulbasaur</div>
<div class="section-list">
<span>Properties</span>
<ul>
<li>green</li>
<li>seed</li>
<li>grass</li>
<li>poison</li>
</ul>
</div>
<div class="section-image">
<img src="http://cdn.bulbagarden.net/upload/2/21/001Bulbasaur.png" />
</div>
</section>
img {
max-width: 100px;
}
.section-title {
width: 100%;
text-align: center;
font-weight: bold;
}
.section-list, .section-image {
width: 50%;
float: left;
}
.section-image {
text-align: center;
}
#media screen and (max-width: 400px) {
.section-list, .section-image {
width: 100%;
}
.section-image {
text-align: left;
}
}