Vertical-align:middle and display:table issue? - html

So I have user display:table for nav, display:table-row for nav ul, display:table-cell and vertical-align:middle for nav ul li. The menu items still won't align in the middle vertically. Can anyone help?
#main_nav {
display: table;
}
#main_nav ul {
display: table-row;
}
#main_nav ul li {
padding: 10px;
display: table-cell;
vertical-align:middle;
}
<nav id="main_nav">
<ul>
<li>Blog</li>
<li>Videos</li>
<li>Almanac</li>
<li>Snippets</li>
<li>Forums</li>
<li>Shop</li>
<li>Lodge</li>
<li>Jobs</li>
</ul>
</nav>

From your statement The menu items still won't align in the middle vertically.
I presume that you want the table to be in the centre of the page.
The code you have used, vertical-align:middle; is not the same.
Check out vertical-align or this CSS vertical-align Property
Its very neatly explained with working examples.
In order to align in the centre of the page you can run the css as below
.wrapper {
display: table;
text-align: center;
}
.wrapper ul {
display: table-row;
}
.wrapper ul li {
padding: 10px;
display: table-cell;
vertical-align:middle;
}
Here is the JS Fiddle
Or Very simply and easily you could replace the whole code with
<!DOCTYPE html>
<html>
<body>
<table align="center">
<tr><td>Blog</td></tr>
<tr><td>Videos</td></tr>
<tr><td>Almanac</td></tr>
<tr><td>Snippets</td></tr>
<tr><td>Forums</td></tr>
<tr><td>Shop</td></tr>
<tr><td>Lodge</td></tr>
<tr><td>Jobs</td></tr>
</table>
</body>
</html>

Related

why li marker is not aligning center in ul/ol li inside div element?

why li marker is not aligning center in ul/ol li inside div element?
div{
text-align: center;
}
<div>
<ul>
<li>hello world</li>
</ul>
</div>
You can make the markers inside the li space by adding list-style-position: inside to your ul.
div{
text-align: center;
}
ul{
list-style-position: inside;
}
<div>
<ul>
<li>hello world</li>
</ul>
</div>
check the width of your ul, li.
Better use display: flex for your div, set justify-content: center or align-items: center;

Right align element in a div only with relative property

Right now I am practicing on creating a header section, which contains an image which is used as a logo and and a un-ordered list which is used as a navigation menu.
Both of these elements are kept in a div element and I want to align the image to the left in the div element and the un-ordered list to the right of the div element. So how can I do it?
Second, I am willing only to use relative property. Not 'Absolute'! As I want to align the list vertically in the middle of the div element and with position absolute I am unable to vertically align the list with vertical align property.
if I am using float than the same situation is there. I am unable to vertically align the list to the middle, which I want to do.
So, I just want to know how can I position my list to the right of the div element using the relative positioning property.
<style>
.header-section {
background-color:red;
}
ul {
display:inline-block;
list-style:none;
}
ul li {
display:inline-block;
}
</style>
<!DOCTYPE html>
<html>
<body>
<div class="container">
<div class="header-section">
<img src="some-image.jpg">
<ul>
<li>Home</li>
<li>About Us</li>
</ul>
</div>
</div>
</body>
</html>
Use display:flex to parent div and give justify-content: space-between;
.header-section
{
background-color:red;
display: flex;
justify-content: space-between;
}
ul
{
display:inline-block;
list-style:none;
}
ul li
{
display:inline-block;
}
<!DOCTYPE html>
<html>
<body>
<div class="container">
<div class="header-section">
<img src="some-image.jpg">
<ul>
<li>Home</li>
<li>About Us</li>
</ul>
</div>
</div>
</body>
</html>
Flexbox can do that:
Support is IE10 and Up.
.header-section {
background-color: pink;
display: flex;
align-items: center;
}
ul {
list-style: none;
margin-left: auto;
}
ul li {
display: inline-block;
}
<!DOCTYPE html>
<html>
<body>
<div class="container">
<div class="header-section">
<img src="http://lorempixel.com/image_output/abstract-q-c-50-50-8.jpg">
<ul>
<li>Home</li>
<li>About Us</li>
</ul>
</div>
</div>
</body>
</html>
You could use float like this:
ul {
display:inline-block;
list-style:none;
float: right;
}
img {
float: left;
}
Easiest solution will be using float, like the snippet below:
.header-section{
background-color:red;
}
ul{
display:inline-block;
list-style:none;
}
ul li{
display:inline-block;
}
.header-section img{
float: left;
}
.header-section ul {
float: right;
}
.header-section p {
clear: both;
}
<div class="container">
<div class="header-section">
<img src="some-image.jpg">
<ul>
<li>Home</li>
<li>About Us</li>
</ul>
<p></p>
</div>
</div>
Notice I added a paragraph element to clear the floats.
You could do like this
.header-section {
background: red url('http://lorempixel.com/200/200/sports/2/') no-repeat left center;
background-size: contain;
text-align: right;
}
ul {
display:inline-block;
list-style:none;
}
ul li {
display:inline-block;
}
<div class="container">
<div class="header-section">
<ul>
<li>Home</li>
<li>About Us</li>
</ul>
</div>
</div>

How can I align these divs horizontally along the top?

Each div contains a list of varying lengths and because I'm using inline-block. I think it's causing it to align along the bottom of the div with the most height.
Is there a way I can align these div along the top or will I need to give each div a unique id and style them each accordingly?
I made a fiddle here:
http://jsfiddle.net/Bezzzo/dcqmh2g2/1/
HTML
<!-- footer -->
<div id="footer" >
<!--Community div-->
<div>
<h3>Community</h3>
<ul>
<li>Facebook</li>
<li>Twitter</li>
<li>Tumbler</li>
<li>Google plus</li>
</ul>
</div>
<!--Contact support div-->
<div>
<h3>Contact support</h3>
<ul>
<li>support#supportsupport.com.au</li>
</ul>
</div>
<!--Legal div-->
<div>
<h3>Legal</h3>
<ul>
<li>Terms and conditions</li>
<li>Refund policy</li>
<li>Privacy Policy</li>
</ul>
</div>
</div>
CSS
#footer {
position: relative;
width:1033px;
height: 160px;
margin:auto;
border-style:dashed;
}
#footer div {
display:inline-block;
position: relative;
margin-left: 150px;
border-style:dashed;
}
#footer ul {
list-style-type: none;
padding:0px;
margin:0px;
left:0px;
right:0px;
}
Thank you
Just add vertical-align: top; to your #footer div:
#footer div {
border-style: dashed;
display: inline-block;
margin-left: 150px;
position: relative;
vertical-align: top;
}
JsFiddle: http://jsfiddle.net/ghorg12110/dcqmh2g2/3/
As an alternative you can also display: table; — Really useful if you want to make use of vertical align. It may not be best for your current situation but it has it's merits.
#footer {
position: relative;
width:1033px;
height: 160px;
margin:auto;
border-style:dashed;
/* added */
display: table;
/* -- */
}
#footer div {
/* added */
display: table-cell;
vertical-align: top;
/* -- */
position: relative;
border-style:dashed;
}
Working example
Just float left all the div and specify them a width but you need to clear div for this. If you want to use inline block then you don't need to specify width and clear left. In this case you can use vertical-align:top to footer div
JsFiddle : Using float left
JsFiddle : Using inline block
remove display:inline-block from #footer div and use
#footer ul li { display:inline-block;float: left;}
or just
#footer ul li {display:inline-block;}
in your style

How to align floated buttons in same row

I am trying to align the button and links in floated UL list. However it seems they are not getting properly aligned. [Note: we have one link and other is form button].
Following is code and link to codepen:
<div>
<ul>
<li>
<button>Hello</button>
</li>
<li>
Delete
</li>
</ul>
</div>
div{
background:yellow;
overflow:hidden;
}
ul li{
float:right;
list-style:none;
}
a, button{
padding: 4px;
border:1px solid green;
margin-left:5px;
font-size:12px;
text-align:center;
}
The reason your li elements aren't getting aligned properly is because of what's inside them.
You have one button and one a-tag. Buttons are by default displayed as a block-element while a-tags are displayed as an inline-element.
This results in your button and your link having different padding because top/bottom padding doesn't show on inline elements.
You can easily solve this by adding display: block; or display: inline-block; to your css rule.
div{
background:yellow;
overflow:hidden;
}
ul:after{content: '.'; visibility:hidden; height:0; display:block; clear:both;}
ul li{
float:right;
list-style:none;
}
a, button{
padding: 4px;
border:1px solid green;
margin-left:5px;
font-size:12px;
display: inline-block;
}
<div>
<ul>
<li>
<button>Hello</button>
</li>
<li>
Delete
</li>
</ul>
</div>
use display:inline-block instead of float
the issue was because of float:right by using display:inline-block element adjusts to the width of its children and you can align it by adding text-align
div {
background: yellow;
overflow: hidden;
text-align:right;
}
ul li {
display: inline-block;
list-style: none;
}
a,
button {
padding: 4px;
border: 1px solid green;
margin-left: 5px;
font-size: 12px;
}
<div>
<ul>
<li>
<button>Hello</button>
</li>
<li>
Delete
</li>
</ul>
</div>
pls give display:block for a(anchor) tag.if you use padding or margin(etc..) in anchor tag.we need to use display.

Positioning social icons on a navigation bar

This is what my page currently looks like: Here
I want the social icons to position in line with the rest of the navigation content. At the moment they are beneath the content. I thought float right would fix things. Is it because of my browser size? How can I fix this then? Here is my code:
HTML:
<div id="Nav">
<div id="NavContent">
<ul>
<li id="Title">PavSidhu.com</li>
<li>Home</li>
<li>Web Design</li>
<li>Graphic Design</li>
<li>How it Works</li>
<li>Pay</li>
<li>Contact</li>
</ul>
<img src="Images/Twitter.png" class="Social"/>
<img src="Images/Pinterest.png" class="Social"/>
</div>
</div>
CSS:
#Nav {
position:fixed;
width:100%;
background-color:#f26522;
}
#NavContent {
margin:0 auto;
width:90%;
}
ul {
margin:0;
padding:0;
}
li {
font-family: Bebas;
color:#FFF;
list-style-type:none;
margin:0;
padding:0 1%;
display:inline;
vertical-align:middle;
font-size:20px;
}
#Title {
font-size: 35px;
}
.Social {
height:35px;
float:right;
}
Thanks guys :)
The <ul> is a block element, so it wants to be 100% width by default. If you make it an inline element with display: inline; instead, there will be space for the icons to sit next to the rest of the nav bar.
ul {
margin:0;
padding:0;
display: inline;
}
You mean you want the social-media-icons higher? Next to the menu-items instead?
Try using
display: inline-block;
for your ul.
set ul to display: inline-block
As explained earlier, ul is a block element that will take 100% of the width of the parent element, so the floated elements will start on the next line.
To fix this, you can use:
ul {
margin:0;
padding:0;
border: 1px solid blue; /*for demo only */
display: inline-block;
width: inherit;
}
See demo at: http://jsfiddle.net/audetwebdesign/tAjW8/
You need to set width: inherit or else the computed width will be narrower than you might expect.