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;
}
}
Related
Hi I am trying to align the bottombar elements so that they are in 2 columns on the side of 102. I was wondering if there is a way to fix it as they are all floating on the right at the moment. I am a beginner html css programmer and I am not very experienced yet. Ill appreciate any help you can give me!
CSS
/*bottom navbar*/
.bottomnav{
width: 100%;
background-color: rgb(248, 138, 180);
display: flex;
flex-direction: row;
}
.navbarlogo2{
display: block;
margin-left: auto;
margin-right: auto;
width: 10%;
text-decoration: none;
}
/*bottombar*/
.nav {
display: flex;
flex-direction: row;
}
.left, .right {
flex: 1;
}
HTML
<div class="bottomnav">
<ul class="bottomlogo">
<li class="navbarimg2"><img class="navbarlogo2" src="img/LOGO.png"></li>
</ul>
<div class='nav'>
<div class='left'>
<ul>
<li>About Us</li>
<li>Affiliates</li>
</ul>
</div>
<div class='right'>
<ul>
<li>TOS</li>
</ul>
</div>
</div>
</div>
END RESULT
WANTED RESULT
I made things like that. CSS Grid is one of the new HTML5 standard you should take a look. In your case, use a grid is better choice against flex because you're looking for a table-like structure.
I choosed to split your needs in 2 parts:
Center your logo
Make a 2 columns grid for your links
Centering your logo
We need to center an element and prevent it to interfere with our incoming links grid. So we'll set our container with a position: relative and place the img tag in position: absolute. Note the image's top right bottom left properties are now relative to the first parent positioned as relative.
And so we only need to make some simple maths. Note the calc() function, we don't want to center the top left corner of your logo but the center. So we need to remove the half of the defined logo's width.
navbarlogo2 {
left: calc(50% - 60px);
}
Make a 2 columns grid for your links
In order make a grid, you have to display your container as grid and set its grid-template-columns to 1fr 1fr. You can translate fr with the word fraction. So here, we're asking for a row split in 2 fractions.
Because we want a place for our logo, we're adding a gap (grid-cap) in out container to make some space between our 2 columns.
Learn more about the fr unit here.
body {
margin:0
}
.bottomnav {
width: 100%;
background-color: rgb(248, 138, 180);
position: relative;
}
.navbarlogo2 {
display: block;
margin-left: auto;
margin-right: auto;
width: 120px;
text-decoration: none;
position: absolute;
filter: brightness(10);
top: 15px;
left: calc(50% - 60px) /*center top left corner then remove half logo width (120px)*/
}
/*bottombar*/
.nav {
display: grid;
grid-gap: 120px;
grid-template-columns: 1fr 1fr;
}
.nav ul {
padding-left: 0;
}
.nav ul li {
list-style: none;
text-align: center;
padding-left: 0;
}
.left,
.right {
flex: 1;
}
<div class="bottomnav">
<div class="bottomlogo">
<img class="navbarlogo2" src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg">
</div>
<div class='nav'>
<div class='left'>
<ul>
<li>About Us</li>
<li>Affiliates</li>
</ul>
</div>
<div class='right'>
<ul>
<li>TOS</li>
<li>Fourth </li>
</ul>
</div>
</div>
</div>
EDIT: I did manage to figure it out in flex! But the links still aren't quite taking up the entire top row:
enter image description here
there's still some extra space to the right and left. Is there any way to force the flex to use all the available space?
I'd like the nav bar to automatically adjust width based on how many links there are.
.top-container {
text-align: center;
display: flex;
}
.top-content-box {
width: 90%;
height: 30%;
margin: 30px auto;
background: #fff;
box-shadow: 0px 0px 10px #000;
justify-content: center;
display: flex;
flex-direction: column;
}
.news-and-twitter {
display: flex;
flex-direction: row;
flex-grow: 1;
}
.twitter-min {
width: 300px;
}
.news-box {
text-align: left;
vertical-align: middle;
padding: 30px;
flex-grow: 2;
}
.nav-bar {
text-align: center;
flex-grow: 1;
}
ul.link-list {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
display: inline-block;
}
<div class="top-container">
<div class="top-content-box">
<div class="nav-bar">
<ul class="link-list">
<li class="link-list"><a class="nav-list" href="default.asp">home</a></li>
<li class="link-list"><a class="nav-list" href="news.asp">about</a></li>
<li class="link-list"><a class="nav-list" href="contact.asp">projects</a></li>
<li class="link-list"><a class="nav-list" href="about.asp">portfolio</a></li>
<li class="link-list"><a class="nav-list" href="about.asp">commissions</a></li>
<li class="link-list"><a class="nav-list" href="about.asp">patreon</a></li>
</ul>
</div>
<div class="news-and-twitter">
<div class="news-box">
<h1>current project</h1>
Arena Circus ch 4 | Inserting voices into The Pretenders Guild
<h1>Current Events</h1>
<ul>
<li>The Pretenders Guild voice update is coming 7/15/19!
</li>
<li>Check the 1 year anniversary special offer here!</li>
</ul>
</div>
<div class="twitter-min">
<a class="twitter-timeline" data-width="300" data-height="450" data-theme="light" data-link-color="#343584" href="https://twitter.com/CapMinyan?ref_src=twsrc%5Etfw">Tweets by CapMinyan</a>
<script async src="https://platform.twitter.com/widgets.js"
charset="utf-8"></script>
</div>
</div>
</div>
</div>
screenshot
You have to make the parent element of the list (ul.link-list) a flex and make the children (li.link-list) have flex-grow of 1
.top-container{
text-align:center;
}
.top-content-box {
width: 90%;
height:30%;
margin:30px auto;
background: #fff;
box-shadow: 0px 0px 10px #000;
}
.news-and-twitter{
display:flex;
flex-direction:row;
flex-grow:1;
}
.twitter-min{
width:300px;
}
.news-box{
text-align:left;
vertical-align: middle;
padding:30px;
flex-grow:2;
}
Some may say "use flex box" but I really prefer not to do such methods!
So my suggestion would be position the nav bar left:50%;
And then give it a margin-left: -100px then change -100px to half of the nav's width, so lets say it has a width of 526px you put it to -263px.
And it may change it's width depending on the device's screen size, so I'd add some Javascript to change that when the page loads, so basically check what the width of the nav is then divide it by 2 and put a "-" in front of it then a "px" after it...
I understand if that sounds too complicated for such a simple task but I like using JavaScript to do my, things
Also many would say this is dumb to do, because the less Javascript the better but I strongly disagree, but that's my opinion... Other than the fact that a lot of Javascript will make the page slower, and a few other things...
Alternatively try doing margin: 0 auto could fix the problem, worth a shot!
Hope this helps!
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/
I wanted the nav bar right at the top, to have the class with left on the left side, the class with middle right in the middle, and the class with right in the right side.
* {
padding: 0;
margin: 0;
}
ul {
list-style: none;
}
.flex-container {
width: 100%;
}
.flex-container ul {
display: flex;
align-items: center;
justify-content: center;
}
.flex-container li {
border: 1px solid red;
}
.flex-container nav ul .nytl {
width: 189px;
height: 26px;
}
.flex-container nav ul .first {
justify-content: flex-start;
}
hr {
margin-top: 10px;
}
<div class="flex-container">
<nav>
<ul>
<li class="left">
<a href="#"><img src="https://img.icons8.com/material-outlined/16/000000/menu.png">
</a>
</li>
<li class="left">
<a href="#"><img src="https://img.icons8.com/material-rounded/16/000000/search.png">
</a>
</li>
<li class="left">SPACE & COSMOS
</li>
<li class="middle"><img src="https://lco1220.github.io/nyt_article/images/nyt-logo.png" alt="NewYorkTimes-Logo" class="nytl"></li>
<li class="right"><button>Subscribe</button> .
</li>
<li class="right"><button>Login</button></li>
</ul>
</nav>
<hr>
</div>
Try using auto margins to push the left and right elements away from the middle element.
(Also, since you're using the HTML5 nav element and CSS3 properties, you really don't need a ul to structure your layout. You can simplify your code substantially.)
nav {
display: flex;
align-items: center;
}
nav > * {
border: 1px solid red;
}
.nytl {
margin: 0 auto;
width: 189px;
height: 26px;
}
hr {
margin-top: 10px;
}
* {
padding: 0;
margin: 0;
}
<nav>
<a href="#">
<img src="https://img.icons8.com/material-outlined/16/000000/menu.png">
</a>
<a href="#">
<img src="https://img.icons8.com/material-rounded/16/000000/search.png">
</a>
SPACE & COSMOS
<img src="https://lco1220.github.io/nyt_article/images/nyt-logo.png" alt="NewYorkTimes-Logo" class="nytl">
<button>Subscribe</button>
<button>Login</button>
</nav>
<hr>
Learn more about auto margins here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
Here's another flex method you may find useful:
Aligning Three Divs Horizontally Using Flexbox
You may encounter another problem now: Because flex features such as auto margins, justify-content and align-items work by distributing free space, your middle item may not be perfectly centered. See these posts for more details and solutions:
Keep the middle item centered when side items have different widths
Center and right align flexbox elements
I would utilise the space-between option that flex brings with the justify-content property. You have to be careful of the way the code is listed for SEO purposes as opposed to placing anywhere and have the css reposition it all. It should cascade in natural order first.
.flex-thirds {
display: flex;
justify-content: space-between;
}
.flex-thirds .col {
width: 32%;
}
.nytl {
margin: 0 auto;
width: 189px;
height: 26px;
}
<div class="flex-thirds">
<div class="col">
<a href="#"><img src="https://img.icons8.com/material-outlined/16/000000/menu.png">
</a>
<a href="#"><img src="https://img.icons8.com/material-rounded/16/000000/search.png">
</a>
SPACE & COSMOS
</div>
<div class="col">
<img src="https://lco1220.github.io/nyt_article/images/nyt-logo.png" alt="NewYorkTimes-Logo" class="nytl">
</div>
<div class="col">
<button>Subscribe</button>
<button>Login</button>
</div>
</div>
You can find more about justify-content here at css-tricks
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>