How to position this <ul> under <h4>? - html

.footer {
display: flex;
position: fixed;
bottom: 0;
width: 100%;
justify-content: space-around;
align-items: center;
min-height: 30vh;
background-color: #a44949;
font-family: 'Open Sans', sans-serif;
}
.footer-list {
display: flex;
justify-content: space-around;
}
<div class="footer">
<h4>Kontakt</h4>
<ul class="footer-list">
<li>Mobilni: 062/329-077</li>
</ul>
<h4>Podaci</h4>
<ul class="footer-list">
<li>PIB: 112295370</li>
<li>Matični broj: 66007057</li>
</ul>
<h4>Lokacija</h4>
<ul class="footer-list">
<li>Oslobođenja 32c, Rumenka</li>
</ul>
</div>
How it looks like:
I want those ul elements placed under the h4 , how do I do it? did I mess something up or? I will add more stuff to it, I planned to add a google map for the location too
EDIT: Another issue
So, I've solved my first and second issue, now here comes the third. Added a map thumbnail, h4 positioning kinda got messed up as you can see here
enter code herehttps://jsfiddle.net/sushmee/nz1txweh/1/
EDIT2: And another issue
How to fix this div with p and text in it, goes under footer instead of expanding the body? I have another line of text, you just cant see it

.footer {
display: flex;
position: fixed;
bottom: 0;
width: 100%;
justify-content: space-around;
align-items: center;
min-height: 30vh;
background-color: #a44949;
font-family: 'Open Sans', sans-serif;
}
<div class="footer">
<div>
<h4>Kontakt</h4>
<ul>
<li>Mobilni: 062/329-077</li>
</ul>
</div>
<div>
<h4>Podaci</h4>
<ul>
<li>PIB: 112295370</li>
<li>Matični broj: 66007057</li>
</ul>
</div>
<div>
<h4>Lokacija</h4>
<ul>
<li>Oslobođenja 32c, Rumenka</li>
</ul>
</div>
</div>

You have a couple of options, the first is to stay with CSS flex layout, and wrap the <h4> elements into a 'wrapping' element with its associated <ul>, and the second – as you want to align across columns and rows – is to use CSS grid layout.
To show the first option, because you're effectively showing a navigation list with associated lists, we wrap the <h4> elements along with its associated <ul> in a wrapper <li> element which, of course, requires either a <ul> or <ol> parent.
I've effectively stripped out all of the styling, so you will need to reintroduce the styles required to position the .footer element, but this should be enough to show you how to visually align the elements:
/* a basic reset so that elements are sized, and
spaced, consistently: */
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
/* removing all list-markers from the <ul> elements
within the .footer element: */
.footer ul {
list-style-type: none;
}
.footer-list {
display: flex;
justify-content: space-between;
}
/* to show how a marker could be added back to the
descendant <li> elements that follow the <h4>: */
h4 + ul ::marker {
/* a right guillemet, using unicode: */
content: '\00BB';
width: 0.5em;
}
h4 + ul li {
/* making space for the marker: */
margin-left: 0.6em;
}
<div class="footer">
<!-- this element serves to wrap each of the content 'columns'
in <li> elements in order that flex-box layout can
position the <h4> along with its associated list of
links; the class-name is used for ease-of-selecting: -->
<ul class="footer-list">
<li>
<h4>Kontakt</h4>
<ul>
<li>Mobilni: 062/329-077</li>
</ul>
</li>
<li>
<h4>Podaci</h4>
<ul>
<li>PIB: 112295370</li>
<li>Matični broj: 66007057</li>
</ul>
</li>
<li>
<h4>Lokacija</h4>
<ul>
<li>Oslobođenja 32c, Rumenka</li>
</ul>
</li>
</ul>
</div>
JS Fiddle demo.
The second option, as mentioned above, is to use CSS grid, though because this does not wrap the relevant <h4> and <ul> within the same parent, this is somewhat brittle, but can be used:
/* a basic reset so that elements are sized, and
spaced, consistently: */
*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
/* removing all list-markers from the <ul> elements
within the .footer element: */
.footer ul {
list-style-type: none;
}
.footer {
display: grid;
/* because we're positioning the elements in pairs,
one h4 to one ul, we use two rows and direct the
layout to place elements in column direction
instead of rows: */
grid-template-rows: repeat(2, min-content);
grid-auto-flow: column;
}
<div class="footer">
<h4>Kontakt</h4>
<ul>
<li>Mobilni: 062/329-077</li>
</ul>
<h4>Podaci</h4>
<ul>
<li>PIB: 112295370</li>
<li>Matični broj: 66007057</li>
</ul>
<h4>Lokacija</h4>
<ul>
<li>Oslobođenja 32c, Rumenka</li>
</ul>
</div>
JS Fiddle demo.
References:
Adjacent-sibling (+) combinator.
display.
grid-auto-flow.
grid-template-columns.
grid-template-rows.
justify-content.
list-style-type.
::marker.
repeat().
Bibliography:
"Adding HTML entities using CSS content."
"Basic concepts of flexbox."
"CSS Grid Layout."
"A Complete Guide to Flexbox."
"A Complete Guide to Grid."

Related

I just need to center an unordered list, and image in the middle of a blank page

I am trying to center just 2 elements in the center of a page. This is a page with clickable icons for your social links. But I need to find a way to center them in a way that they are centered no matter the device's size. Please bare in mind this is my second day ever developing. Any feedback is highly appreciated!
Here is what it should look like in the end
Here is the code I have
<div class="fresh">
<img width="160" src=logo.svg>
</div>
<div class="icons-inline">
<ul class="icons">
<svg>insert super long svg</svg>
<svg>insert super long svg</svg>
<svg>insert super long svg</svg>
<svg>insert super long svg</svg>
<svg>insert super long svg</svg>
<svg>insert super long svg</svg>
<svg>insert super long svg</svg>
</ul>
</div>
css:
.fresh {
color: #E3EEF8;
text-align: center;
padding-top: 20%;
}
.icons {
text-align: center;
padding-right: 1.5%;
}
I'm currently just using text-align and then setting the padding to 20% on the top text so that it pushes both of them down and appears centered only on MY specific screen. But I want it to work on phones, other monitors, etc.
use flex-box, try this:
You can skip the * {...} since it will override every element in your document, and you'll have to set margin and padding manually
*{
box-sizing: border-box;
padding: 0;
margin: 0;
}
.flex-container{
display: flex;
flex-flow: row wrap; /* can also try column wrap */
align-items: center;
justify-content: center;
}
#main-container{ /* this will be the size of your browser window */
width: 100%;
height: 100vh;
}
html:
<section class="flex-container" id="main-container">
<!-- your things here -->
</section>
if you want an ul to show li elementos horizontally just add flex-container class to it, it will show horizontally, then you can add this rule ul.flex-container li to treat those kind of list better, e. g:
ul.flex-container li{
margin: 15px 0 0 15px;
}
your code could look like this:
<section class="flex-container" id="main-container">
<ul class="flex-container">
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>
</ul>
</section>
you'll have a main container with the size of fhe window with its elements centered, inside youll have a list with its elements horizontally aligned
I like to use this approach:
.pageContainer {
display: grid;
place-items: center;
min-height: 100vh;
}
You can read more about it directly from MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/place-items
Create a container at least as big as the screen, and align the content to the center of the container.
The centering of elements can be handled by Flexbox. You just need to make sure that the container that holds the logo and the list of social media icons is as big as any screen the user might have. You can use viewport-percentage sizes for that, for example min-height: 100vh means “make the height of this element at least 100 percent of the viewport height”, where viewport is the size of the browser window.
Here’s a working solution:
body {
margin: 0;
}
main {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
img {
margin-bottom: 16px;
}
ul {
display: flex;
list-style-type: none;
padding: 0;
}
li:not(:last-of-type) {
margin-right: 16px;
}
<main>
<img src="https://picsum.photos/id/237/280/120" alt="Logo" />
<ul>
<li>Icon 1</li>
<li>Icon 2</li>
<li>Icon 3</li>
<li>Icon 4</li>
<li>Icon 5</li>
<li>Icon 6</li>
<li>Icon 7</li>
</ul>
</main>

My items aren't aligning in a row with flex / not mobile responsive

I need my items in two rows, the h3 and list as one row, and the video in a second row.
The h3 and lists are on the top row and the video below. Also when I inspect it's not mobile responsive on the phone / tablet setting.
.features section {
display: flex;
flex-direction: row;
flex-flow: row;
width: 100%;
}
h3{
position: static;
height: 47px;
left: 0px;
top: 0px;
font-family: Montserrat;
font-style: normal;
font-weight: normal;
font-size: 36px;
line-height: 130%;
display: flex;
align-items: center;
color: #262424;
flex: none;
order: 0;
flex-grow: 0;
margin: 0px 24px
<div class="bg-white overflow-hidden">
<div class="features">
<div class="flex relative max-w-screen-xl mx-auto py-12 px-4 sm:px-6 lg:px-8">
<section>
<h3 class="flex-1"> Features</h3>
<ul>
<li>Easy group conversation </li>
<li>Free movement</li>
<li>Emojo</li>
<li>Private messages</li>
<li>Presenting</li>
<li>Tags</li>
</ul>
<video class="flex-2" width=869px height="346px" autoplay loop muted>
<source src="video.mp4" type="video/mp4">
</video>
</section>
</div>
</div>
</div>
There are a few problems in the code itself...
Check your width attribute on the video (there are no quotes around
the amount, maybe that's affecting something).
Also, your first CSS selector says .features section, but there is
no <section> tag so either add a section tag that you want to
style or remove it from the CSS selector.
Generally, your list items should be inside of an unordered list
element <ul>.
Also, there is an unclosed div in the example code (but I realise
that may just be an example).
In your H3 styling you have display: flex and then flex: none.
I think it would be easier to put the elements that you want on each row inside of their own div that is then styled as flex-row.
You can then choose how to style them within each row.
(unless you're using something like Bootstrap, in which case there is an easier way).
My favourite CSS flexbox cheat sheet is this...
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Also, I popped a suggestion onto codepen, maybe it'll help.
https://codepen.io/petermanchester1987/pen/KKMrOeG
Peter Manchester
Just add a break, this is a simple div with the below CSS properties applied to it. This also forces you to add flex-wrap: wrap to the flex parent.
Anytime you want the next set of elements to be on the new row add <div class="break"></div> before those elements.
You can add justify-content: space-between; to make the video next to the feature list be spaced evenly within your page, although not necessary (it will appear right next to the list without it).
.features section {
display: flex;
flex-wrap: wrap; /* Add this for breaks to work */
width: 100%;
/* justify-content: space-between; Distribute items evenly on each row */
}
.break {
flex-basis: 100%;
height: 0;
}
.video {
background-color: red;
width: 300px
}
<div class="features">
<div>
<section>
<h3>Features</h3>
<div class="break"></div>
<ul>
<li>Easy group conversation </li>
<li>Free movement</li>
<li>Emojo</li>
<li>Private messages</li>
<li>Presenting</li>
<li>Tags</li>
</ul>
<div class="video">VIDEO GOES HERE</div>
<!-- Replace this with your video element -->
</section>
</div>
</div>
Additionally, your video element is missing " " on the width attribute.

aligning the bottom bar elements

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>

Horizontal align for dynamic content (special case)

I have a navigation menu (header menu) in my web page which is actually takes dynamic content. The dynamic menu items are loaded into ul>li>{dynamic_content}. I want this navigation bar (I mean the ul ) in the center of the section, not vertically but horizontally. I can have text-align:center because it has multiple children tags.
Also I can't do the following,
.class{
width:50%;
margin-left:auto;
margin-right:auto;
}
because I can't set a width since this is a DYNAMIC navigation.
You may think why I can't try
.class{
position:absolute;
left:50%;
transform:translateX(-50%);
}
This is also can not be done, because I can't set the position:absolute since it's going to be a fixed header on scroll. It make some position problems in responsive.
I'm looking for an alternative to solve this problem.
You can use justify-content: center from flexbox.
.container {
display: flex;
justify-content: center;
}
<div class="container">
<span>Title 1</span>
<span>Title 2</span>
<span>Title 3</span>
</div>
Using flexbox to center the list on the page will work.
nav {
width: 100%;
display: flex;
justify-content: center;
}
ul {
margin: 0;
display: flex;
list-style: none;
}
li:not(:last-child) {
margin-right: 1rem;
}
<nav>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</nav>
If you are looking for old browsers support, another option would be using display table as it is explained here
.something-semantic {
display: table;
width: 100%;
}
.something-else-semantic {
display: table-cell;
text-align: center;
vertical-align: middle;
}
<div class="something-semantic">
<div class="something-else-semantic">
Unknown stuff to be centered.
</div>
</div>
I think I figured it out a new way to align contents Horizontally.This works nice to me and have no issue with browser compatibility.
Wrapped the navigation with a div and set text-align:center and added display:inline-block to the ul that I wanted to center.
div {
text-align: center;
}
ul {
display:inline-block;
list-style-type: none;
}
ul>li{
float:left;
}
<div>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</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;
}
}