I'm having issues trying to align my list to the center of my page so that it'll match with the rest of my content. I've tried other ways but it either doesn't work or aligns to far to the right.
HTML:
<p>Digital Media:</p>
<div id="list">
<ul>
<li>Tablet</li>
<li>Tablet Pen</li>
<li>Phone + my finger</li>
</ul>
</div>
<p>Tradtional Media:</p>
<div id="list">
<ul>
<li>Notebook</li>
<li>Pens</li>
<li>Pencils</li>
<li>Colored Pencils</li>
<li>Fine-Tip Markers</li>
<li>Canvas</li>
<li>Watercolor Paint</li>
<li>Acrylic Paint</li>
<li>Various-Sized Paintbrushes</li>
</ul>
</div>
CSS:
ul {
margin: 0 auto;
width: 1200px;
padding-left: 0;
font-size: 0;
justify-content: center;
}
li {
font-size: 18px;
list-style-type: none;
width: 150px;
height: 50px;
line-height: 50px;
margin: 15px 25px;
box-sizing: border-box;
text-align: center;
}
You can change code:
li {
...
margin: 15px auto;
...
}
And in a page, don't should multi element with same ID
<div id="list"> => <div class="list">
You can try wrapping the lists in a div element and centering that. See w3schools’s article on css margins.
<style>
.center {
margin: 30px auto;
}
p {
text-align: center;
}
ul {
margin: 0 auto;
width: 150px;
padding-left: 0;
font-size: 0;
justify-content: center;
}
li {
font-size: 18px;
list-style-type: none;
width: 150px;
height: 50px;
line-height: 50px;
margin: 15px 25px;
box-sizing: border-box;
text-align: center;
}
</style>
<div class="center">
<p>Digital Media:</p>
<ul>
<li>Tablet</li>
<li>Tablet Pen</li>
<li>Phone + my finger</li>
</ul>
<p>Tradtional Media:</p>
<ul>
<li>Notebook</li>
<li>Pens</li>
<li>Pencils</li>
<li>Colored Pencils</li>
<li>Fine-Tip Markers</li>
<li>Canvas</li>
<li>Watercolor Paint</li>
<li>Acrylic Paint</li>
<li>Various-Sized Paintbrushes</li>
</ul>
</div>
Related
I have this HTML code:
<body>
<header id="masthead">
<div id="container">
<!-- logo -->
<img src="logo.png" alt="" width="200px" class="logo">
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>About Developers</li>
<li>History</li>
<li>Economy</li>
<li>Why Study in Dublin?</li>
<li>People and Culture</li>
</ul>
</nav>
</div>
</header>
And this CSS code:
.container {
width: 80%;
margin: 0 auto;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
header::after {
content : '';
display: table;
clear: both;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 2px;
position: relative;
padding-right: 0.1rem;
}
nav a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
However I want to make my nav bar to the left from the logo, but not down below. How can I do it using the given initial code that i hav pointed ? As you can see, align: right and align: left has been used, but had not helped me
Like on photo (Used arrows to point it )
Create two columns. In one column, place your logo, and in the second column, place your navigation bar.
<div class="row">
<div class="column" style="background-color:#aaa; width:20%;">
<!--pLACE Logo here--->
</div>
<div class="column" style="background-color:#bbb; width:80%">
<!--Place Navbar code here-->
</div>
</div>
Remember Adjust your css accordingly
Give your div with id container a display property of flex, direction property of row and then align or justify items as per your liking
#container{
display:flex;
flex-direction:row;
justify-content:space-between;
}
Also in your HTML code you've given tags ids but you're using class selectors in your CSS
Some resources that'll help you:
A Complete Guide to Flexbox
Basic Concepts of Flexbox
Flexbox Cheatsheet
You will have to change your CSS as shown below:
/*add this line to adjust paddings on the columns and remove default margin padding for all the html elements*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/*change class to # it's ID and not class*/
#container {
width: 80%;
margin: 0 auto;
}
/*recommended to add width in percentage in css and remove fix width from <img width='200px' /> tag*/
.logo {
float: left;
width:20%;
padding: 10px 0;
}
/*add width 80% for <nav> tag*/
nav {
float: right;
width: 80%;
margin-top: 10%;
}
nav li {
display: inline-block;
/* margin-left: 70px; */ /*remove*/
/* padding-top: 2px; */ /*remove*/
position: relative;
/* padding-right: 0.1rem; */ /*remove*/
padding: 0 5px; /*instead add this for space between li content*/
}
I would recommend you to use CSS FLEXBOX.
I used flexbox to do this. your id="container" was not the same as the CSS so I changed it to class="container"
I added some simple 1px borders just to illustrate what is where on the page.
This is likely not a finished solution and only a starting point.
.container {
width: 90%;
margin: 0 auto;
display: flex;
flex-direction: row;
flex: space-between font-size: 16px;
justify-content: center;
align-items: center;
}
.logo {
padding: 10px 0;
height: 3em;
border: 1px solid lime;
}
header::after {
content: '';
display: table;
clear: both;
}
nav {
border: 1px solid cyan;
justify-content: center;
align-items: center;
}
nav ul li::before {
content: "\200B";
}
nav ul {
display: flex;
flex-direction: row;
border: 1px solid blue;
list-style-type: none;
justify-content: center;
list-style-position: inside;
margin-left: 0;
padding-left: 0;
}
nav ul li {
padding-top: 0.2em;
padding-right: 0.1rem;
border: 1px solid pink;
margin-left: 0;
padding-left: 0;
}
nav li a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 0.875em;
margin-left: 1em;
margin-right: 1em;
}
<header id="masthead">
<div class="container">
<img src="logo.png" alt="logo png" width="200px" class="logo">
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>About Developers</li>
<li>History</li>
<li>Economy</li>
<li>Why Study in Dublin?</li>
<li>People and Culture</li>
</ul>
</nav>
</div>
</header>
My a href inside a div is not working I saw that the width and height was 0px, so I tried to increase size, but it doesn't get increased.
Each dot is supposed to send you to another page in the onepage scroller I'm making
.dotstyle-scaleup{
float: right;
margin-right: 3%;
}
.dotstyle-scaleup li{
background-color: #eeeeee;
width: 15px;
height: 15px;
border-radius: 50%;
margin: 80px 0 0 0;
list-style: none;
}
.dotstyle-scaleup #current1{
background-color: #54a59f;
width: 20px;
height: 20px;
border-radius: 50%;
margin: 80px 0 0 0;
list-style: none;
}
<div id="wrapper">
<!-- Landings -->
<div class="section" data-anchor="page1" style="background-color: red">
<div class="dotstyle-scaleup">
<ul>
<li id="current1"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
As the elements in question contain no content, declaring height and width properties won't be enough.
Consider declaring a display property, in addition to specifying a height and width property for the nested anchor elements (a) e.g:
.dotstyle-scaleup li a {
width: 100%;
height: 100%;
display: block;
}
Code Snippet Demonstration:
.dotstyle-scaleup {
float: right;
margin-right: 3%;
}
.dotstyle-scaleup li {
background-color: #eeeeee;
width: 15px;
height: 15px;
border-radius: 50%;
margin: 80px 0 0 0;
list-style: none;
}
.dotstyle-scaleup #current1 {
background-color: #54a59f;
width: 20px;
height: 20px;
border-radius: 50%;
margin: 80px 0 0 0;
list-style: none;
}
/* Additional */
.dotstyle-scaleup li a {
width: 100%;
height: 100%;
display: block;
}
<div id="wrapper">
<!-- Landings -->
<div class="section" data-anchor="page1" style="background-color: red">
<div class="dotstyle-scaleup">
<ul>
<li id="current1">
</li>
<li>
</li>
<li>
</li>
<li>
</li>
<li>
</li>
</ul>
</div>
</div>
I have a HTML5 header setup. It uses <ul> and <li> elements for the links.
This is the HTML:
<header>
<nav>
<ul>
<li id="logoli"><img src="/assets/logo.png" id="logo"></li>
<li>Home</li>
<li class="5px">Roulette</li>
<li>Free Stuff</li>
</ul>
</nav>
</header>
And the CSS:
header {
height: 100%;
width: 100%;
}
nav {
width: 100%;
height: 65px;
font-size: 14px;
font-weight: 700;
text-transform: uppercase;
padding-left: 10px;
background-color: rgba(0, 0, 0);
color: #0077C5;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
float: left;
}
li {
width: 100px;
line-height: 65px;
vertical-align: middle;
text-align: center;
margin: 0;
text-align: center;
display: inline;
list-style: none;
float: left;
}
#logo {
height: 50px;
}
#logoli {
padding-top: 7px;
width: 250px;
}
For some reason this happens: https://image.prntscr.com/image/w0LAMn5qRMq0OqIAfDTsHw.png
If you look at the nav bar you can easily see that the first two elements have a normal amount of spacing in-between. But the seccond and third have weirdly small spacing. I used inspect element on the <li>'s but they are all the same height. Can someone help me?
You need some HTML and CSS Fixes, Wrap the anchors in li elements and the issue was you have mentioned a width to the li, which causes this wierd spacing.
HTML
<header>
<nav>
<ul>
<li id="logoli">
<img src="/assets/logo.png" id="logo"></li>
<li>Home</li>
<li class="5px">Roulette</li>
<li>Free Stuff</li>
</ul>
</nav>
</header>
CSS
header {
height: 100%;
width: 100%;
}
nav {
width: 100%;
height: 65px;
font-size: 14px;
font-weight: 700;
text-transform: uppercase;
padding-left: 10px;
background-color: rgba(0, 0, 0);
color: #0077C5;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
float: left;
}
li {
line-height: 65px;
vertical-align: middle;
text-align: center;
margin:0px 10px;
text-align: center;
display: inline;
list-style: none;
float: left;
}
#logo {
height: 50px;
}
#logoli {
padding-top: 7px;
width: 250px;
}
You should put the <a href>'s inside the <li> tags like this:
<header>
<nav>
<ul>
<li id="logoli"><img src="/assets/logo.png" id="logo"></li>
<li>Home</li>
<li>Roulette</li>
<li>Free Stuff</li>
</ul>
</nav>
</header>
Also - you have a class="5px" on one of the <li>'s, but that class isn't in your CSS. You should make your <li>'s a fixed width and center the text, or use a standard padding on the left and right of the <li> text to maintain a uniform spacing rather than injecting classes to push single elements around.
This is happening because you're explicitly defining a width on your list items. Remove the width and separate the list items with padding instead. Optionally, remove any padding on the last list item, since you won't need it.
li {
/* width: 100px; */
padding-right: 1em;
line-height: 65px;
vertical-align: middle;
text-align: center;
margin: 0;
text-align: center;
display: inline;
list-style: none;
float: left;
}
li:last-child {
padding-right: 0;
}
https://jsfiddle.net/eulloa/yx7vkt79/
Also, restructure your list items so that they are the parent element of your anchors, not the other way around.
<li>Home</li> <!-- structure your list items this way -->
<li>Home</li>
How to center ul inside of div.
A common advice are
left:50%
some variations of display:inline
but it does not work. How to fix this?
body {
margin: 0;
background: #f4f5f5;
}
.menu {
text-align: center;
margin: 0 auto;
width: 1023px;
background: #f4f5f5;
}
.picture {
margin: 0 auto;
width: 1023px;
height: 255px;
background: green;
}
ul {
list-style: none;
margin: 0 auto;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
li a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
<div class="content">
<div class="menu">
<ul>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
</ul>
</div>
<div class="picture">
</div>
</div>
set text-align:center and to ul and inline-block to li(removing float:left)
Tweaked your code, by removing duplicated code.
body {
margin: 0;
background: #f4f5f5;
}
.content {
margin: 0 auto;
width: 1023px;
}
.picture {
height: 255px;
background: green;
}
ul {
list-style: none;
background: #f4f5f5;
padding: 0;
margin: 0;
text-align: center;
}
li {
display: inline-block
}
li a {
display: block;
padding: 14px 16px;
text-decoration: none;
}
<div class="content">
<ul>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
</ul>
<div class="picture">
</div>
</div>
This should solve the problem
body{
margin:0;
background:#f4f5f5;
}
.menu {
text-align: center;
margin: 0 auto;
width: 1023px;
background: #f4f5f5;
}
.picture {
margin:0 auto;
width: 1023px;
height: 255px;
background: green;
}
ul {
list-style: none;
margin: 0 auto;
padding: 0;
overflow: hidden;
display: inline-block;
}
li{
float:left;
}
li a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
<div class="content">
<div class="menu">
<ul>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
</ul>
</div>
<div class="picture">
</div>
</div>
Use a CSS3 Layout mode called Flexbox.
.menu {
display: flex; // <-- necessary to activate flexbox
justify-content: center; // <-- centers horizontally
// align-items: center; // <-- would center vertically
margin: 0 auto;
width: 1023px;
background: #f4f5f5;
}
learn more:
http://flexboxin5.com/
http://flexboxfroggy.com/
http://jsfiddle.net/rctdzdsm/
I'm trying to align the menu to the logo.
Do I have to manually margin it till I align correctly?
Or is there a better/proper way of doing this?
Because manually margining till I align it correctly, feels kinda kinda dumb.
* {
margin: 0;
padding: 0;
}
header {
background-color: #e58b1a;
}
header .logo {
float: left;
width: 400px;
height: 53px;
font-family: 'Arial';
color: white;
font-size: 50px;
position: relative;
top: 20px;
left: 50px;
}
.menu {
float: right;
font-family: arial;
}
.menu li {
/** Float to the left, helps it become horizontal */
float: left;
/** Marigin-right allows you to space your links */
margin-right: 30px;
}
<header class="show-when-loaded" style="height: 108px;">
<div class="logo">
<span class="text"> Daz Me Lulz </span>
</div>
<div class="menu">
<nav>
<ul>
<li> About Us
</li>
<li> Projectos De Lulz
</li>
<li> Get Involved
</li>
</ul>
</nav>
</div>
</header>
This is not a very flexible way of doing this. What if the height of your header changes?
Than you would manually have to align your padding and margins. If you are looking for a more accurate way of doing this, take a look at the different possibilities of displaying elements and aligning them. Even if your menu lives inside an unordert -list. Think of your list being a table and the list items being the table cells. If your elements inherit the height from your header you could easily align them vertically in the middle. Here is a snippet to give you an idea of what i mean.
html, body {
margin: 0;
padding: 0;
}
header {
height: 108px;
background-color: orange;
}
header:after {
clear: both;
}
nav {
height: 100%;
}
nav ul {
margin: 0;
padding: 0;
float: left;
display: table;
height: inherit;
}
nav li {
display: table-cell;
height: inherit;
vertical-align: middle;
}
nav a {
text-decoration: none;
color: white;
font-family: 'Arial';
}
.logo a {
font-size: 50px;
padding: 0 0 0 20px;
}
nav .nav-bar {
float: right;
}
nav .nav-bar li {
padding-right: 20px;
}
<header>
<nav role='navigation'>
<ul class="logo">
<li>
Daz Me Lulz
</li>
</ul>
<ul class="nav-bar">
<li>Home</li>
<li>About</li>
<li>Clients</li>
<li>Contact Us</li>
</ul>
</nav>
</header>
Have fun.
I removed the position: relative. because its not needed.
Added the some margins for being aligned better.
Tried to make the li elements wrap, ended up with a none wrapping solution instead.
Made the links text direction from right to left. So they stick to the right side of the screen, without breaking the flow, as float: right does.
html, body {
margin: 0;
padding: 0;
}
header {
white-space: nowrap;
background-color: #e58b1a;
min-width: 650px;
}
header .logo {
display: inline-block;
font-family: 'Arial';
color: white;
font-size: 50px;
padding: 20px 0px 20px 20px;
}
.menu {
display: inline-block;
direction: rtl;
font-family: arial;
min-width: 350px;
width: calc(100% - 350px);
margin-top: 40px;
}
.menu ul {
display: inline;
direction: rtl;
padding: 0;
}
.menu ul li {
display: inline;
list-style: none;
margin-right: 10px;
}
<header class="show-when-loaded">
<div class="logo">
<span class="text"> Daz Me Lulz </span>
</div>
<nav class="menu">
<ul>
<li> About Us
</li>
<li> Projectos De Lulz
</li>
<li> Get Involved
</li>
</ul>
</nav>
</header>
Looks like CSS tables will help here.
header {
background-color: #e58b1a;
display: table;
vertical-align: middle;
width: 100%;
}
header .logo {
display: table-cell;
height: 53px;
font-family: 'Arial';
color: white;
font-size: 50px;
vertical-align: middle;
}
.menu {
display: table-cell;
font-family: arial;
text-align: right;
vertical-align: middle;
}
* {
margin: 0;
padding: 0;
}
header {
background-color: #e58b1a;
display: table;
vertical-align: middle;
width: 100%;
}
header .logo {
display: table-cell;
height: 53px;
font-family: 'Arial';
color: white;
font-size: 50px;
vertical-align: middle;
}
.menu {
display: table-cell;
font-family: arial;
text-align: right;
vertical-align: middle;
}
.menu li {
display: inline-block;
}
<link rel="stylesheet" type="text/css" href="style.css">
<title>Daz Me Lulz - Providng Renewable Energy to the children of Latin America</title>
<body>
<header class="show-when-loaded" style="height: 108px;">
<div class="logo">
<span class="text"> Daz Me Lulz </span>
</div>
<div class="menu">
<nav>
<ul>
<li> About Us
</li>
<li> Projectos De Lulz
</li>
<li> Get Involved
</li>
</ul>
</nav>
</div>
</header>
</body>