How to horizontally align a ul element to center - html

I'm not able to align a menu, contained within a ul, to the horizontal center of its container. how to do that?
See a live demo of the menu on jsFiddle.
<li>AboutUs
<ul class="sub">
<li>About Square Innovations</li>
<li>Our Vision</li>
<li>Our Mission</li>
<li>Trainer Profiles</li>
<li>Fun In Our ClassRooms</li>
</ul>
</li>

You can address the ul element as an inline-level element within the page flow, while retaining its block-level characteristics (using the inline-block display value) — after applying this, it can be simply aligned within its container like any inline-level element, using text-align.
To implement this, add the following rules:
#container {
text-align: center;
}
ul {
display: inline-block;
}
Here's the updated demo.
Reference
display on Mozilla Developer Network
Disclaimer: Support for inline-block is somewhat limited nope! it's actually very wide by now, see the compatibility table on caniuse.com.

There is a very neath, fully cross-browser and dynamic 'trick' to achieve this, as long as the menu stays on one line. It is very well explained here: http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support
The inline-block often suggested for this problem is not very well supported in legacy browsers in my experience. To be honest, I never use it. I always go for the clever method that Matthew James Taylor describes.
Edit:
As requested I will briefly describe the technique.
Your html should look like a normal list of links, with an extra wrapping div around it. Something like this:
<div class="menu-wrapper">
<ul>
<li><a ...>link</a></li>
<li><a ...>link</a></li>
<li><a ...>link</a></li>
</ul>
</div>
Now the rest is css work. The steps are as follows:
Float the wrapper to the left and give it a width of 100% to make it take up the full viewport width
Float both the ul and the li to the left and don't give them any width to make them adjust to their content.
Give the ul a position: relative of left: 50%. This will make it's left edge move to the center of it's parent, and this means the center of the viewport.
Finally you should give your li a position: relative of left: -50%. This will make them move past the left edge of the parent ul and makes the center of the list of li's line up with the left edge of the parent ul. Since we made that edge the center of our viewport in the previous step, the menu is now effectively centered.
As I said before, all credits to Matthew James Taylor, and definitly check out his thorough explanation. The drawings he made make it much easier to understand.
edit
As requested I set up a little fiddle to demonstrate the technique:
http://jsfiddle.net/fDmCQ/

Change the margin on the <ul> to 0 auto and give it a width (~575px or larger).
jsFiddle example
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0 auto;
padding: 0;
width:600px;
list-style: none;
text-align: center;
}

Related

Stretching <a> tags across a sidebar while vertically aligning its contents to the center

I want to put a bunch of clickable links in a sidebar with a hover effect that covers the entire width of the sidebar. Some of these links also include an image that needs to be aligned so that it's vertically centered in relation to the text. Here's what I currently have:
As you can see, the hover effect and the <a> tag don't cover the entire width of the sidebar yet. That's bad because of big link targets are easier to click. I've tried tinkering with horizontally stretched CSS-based table cells, but then the text parts didn't stay aligned properly.
What's the proper way to do it? ~I could post my current HTML if it's helpful, but I was planning to rewrite my markup based on this answer's solution anyway.~
Edit: here's the relevant HTML snippet.
<nav id="sidebar">
<ul>
<li>Home</li>
</ul>
<header>Recently Added</header>
<ul id="recents">
<li><img src="http://media.radiantstreamer.net/stations/q2music.png" alt="Artwork"> <span>Q2 Music</span></li>
<li><img src="http://media.radiantstreamer.net/stations/rtmoclassic.png" alt="Artwork"> <span>Mostly Classical</span></li>
<li><img src="http://media.radiantstreamer.net/stations/rtpitrios.png" alt="Artwork"> <span>Piano Trios</span></li>
</ul>
</nav>
Putting display: block; on the relevant links should make them full width. Or if it doesn't, display: block; width: 100%;. width: 100% on its own doesn't seem to be much use on inline elements.
…And some positioning to fix the alignment, e.g.
ul li a {
display:block;
text-decoration:none;
position:relative;
}
ul li a span {
position:absolute;
top:50%;
}
Have you tried position: absolute; width: 100% (or something like that) on your links? That should make it the parent's full width.
How I solved it
First I made my <a> tags render as a one-row CSS table by setting them to display: table and its children to display: table-cell. You'll need to add width: 100% to the table tag to make it stretch horizontally. But then the text didn't align properly:
Adding a width: 100% to <span> containing the text does the trick:
Uh oh... the 5 pixels of left padding on my links are causing spillover on the right. The fix was fairly easy: wrap the link tags in another <span> tag and adjust the CSS display rules so that the new <span> renders as a table. Bam!
Summary
I've prepared a minimally working HTML5-compliant example for the benefit of future readers.

unordered list is not in center

i am making business company website, there is a menu bar at top of the page. Now this menubar contains four li tags (below code), these li sticks at left of the page. i want to make it to distribute equally horizontal. Help!
<div id="wrapper-menu">
<div id="menu">
<ul>
<li>home</li>
<li>about us</li>
<li>news</li>
<li>contact us</li>
<li>links</li>
</ul>
</div>
</div>
You need to make the li inline-block items, and don't forget to apply a text-align to the ul:
li {
display: inline-block;
}
ul {
text-align: center;
}
example here:
http://jsfiddle.net/evmDv/
I'm assuming with no other example that your problem is that you want them to be horizontal instead of vertical (you say they are all on the left). If this is not the problem then this answer will not apply.
You probably want something in your css along the lines of:
li {display: inline-block;}
You may want to look up exactly what inline blocks do but in a nutshell it means the block is not the full width of the screen but just wide enough and the blocks act like inline elements meaning you don't start a new line with each one.
You will of course want further styling to make it look good but this should do the trick I think.
http://jsfiddle.net/NqgZ4/ for a jsfiddle example.
As a followup to your comment to have them spread out then the easiest way is to apply a width to them.
li
{
display: inline-block;
width: 19%;
}
Note that I use 19% instead of 20% to avoid rounding issues that may cause the width to exceed the pixels (eg if the width available is 999 px then 20% would make each of them 200 pixels which would then add up to too much).
If you have a dynamic number of menu items then its a bit trickier and I'd start thinking about a bit of script to equalise them (by setting the widths dynamically) though there may be a pure CSS method that will work with variable number of items.
Updated fiddle: http://jsfiddle.net/NqgZ4/1/

HTML/CSS: Keeping Elements Positioned with Window Change

I'm very new to CSS and I spend quite a while trying to research how to position elements "in thirds" so that even when the window changes dimensions the elements stay "in thirds". The first picture below is when the browser is fully opened and the second is when it is horizontally contracted. I am looking for this method but I don't know the name of the technique or what to do (like I said I'm really new). I'm aware that the tabs are using what is called "closing doors" but I'm more interested in their positioning.
For example if I wanted three elements spaced out unevenly
to contract as follows, how would I go above achieving this?
instead of:
check this solution. This is flexible and if more menu comes it will accommodate it automatically.
http://jsfiddle.net/geymU/
<div id="nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Address</li>
<li>Phone</li>
</ul>
</div>​
CSS
#nav{
background:#c1c1c1;
}
#nav ul{
display:table;
width:100%;
}
#nav li{
padding:5px;
display:table-cell;
background:#333;
border:1px solid;
color:#fff;
}​
Without seeing actual code, I would recommend that you declare your horizontal widths, margins and padding with relative units. That would be % or ems, not pixels.
So, in your example,
#element_2 {
margin-left: 45%;
}
#element_3 {
margin-left: 5%;
}
With relative units, you will have to do a little more trial-and-error to get elements to place how you want.
In your pictures it looks like the backgroundimage (i assume it's an image, since it doesnt stretch over the whole button) is only lacking a
background-repeat: repeat-x;
However what I see in your second example can be easily archived if you set the first button to
float: left;
and the remaining buttons to
float: right;
Just don't forget to
clear: both;
after them, incase you don't have any more elements removed from the document flow (which is what float does).

Aligning social media sharing buttons to look nice together

I just put back the social sharing buttons to my home page and they look really terrible together :)
You can take a look at it here on the bottom of the page: http://www.comehike.com
How do people usually make them fit so natural together? Mine look so disordered and unprofessional. Is there anything that can be done with styling?
Thanks!
Looking at your code you'll likely have to add individual classes to each one, as they are all in different elements. There are a few approaches, absolute positioning, display:inline but I'd recommend floats
add a ul element and each of your 'share buttons' as an li child element of that ul
<ul class='shareLinks'>
<li>facebook here</li>
<li>twitter here</li>
</ul>
then your css can look something like this
ul.shareLinks { overflow: auto; }
ul.shareLinks li { list-style: none; float: left; margin-right: 5px; }
you can obviously edit the css how ever you wish, the main point is the float: left the overflow: auto; on the ul is a fix for the floated elements not having a real height.

Stretching <a> tag to fill entire <li>

Here's a simple menu structure:
<ul id="menu">
<li>Home</li>
<li>Test</li>
</ul>
I want the <a> to be stretched so that it fills the entire <li>. I tried using something like width: 100%; height: 100% but that had no effect. How do I stretch the anchor tag correctly?
The "a" tag is an inline level element. No inline level element may have its width set. Why? Because inline level elements are meant to represent flowing text which could in theory wrap from one line to the next. In those sorts of cases, it doesn't make sense to supply the width of the element, because you don't necessarily know if it's going to wrap or not. In order to set its width, you must change its display property to block, or inline-block:
a.wide {
display:block;
}
...
<ul id="menu">
<li><a class="wide" href="javascript:;">Home</a></li>
<li><a class="wide" href="javascript:;">Test</a></li>
</ul>
If memory serves, you can set the width on certain inline level elements in IE6, though. But that's because IE6 implements CSS incorrectly and wants to confuse you.
Just style the A with a display:block;:
ul#menu li a { display: block;}
display:flex
is the HTML5 way.
See Fiddle
Useful to hack frameworks buttons, or any other element, but you may need to remove their padding first, and set them to the desired height.
In this case, angular-material tabs, which are kind of tricky to make them work as a "standard" website nav.
Notice that the pointer changes as soon as you enter the tab : the < a > are now stretched to fit their parent dimensions.
Out of topic, notice how flawless angular-material displays the ripple effect, even on a "large surface".
.md-header{
/* THIS IS A CUSTOM HEIGHT */
height: 50vh !important; /* '!important' IS JSFIDDLE SPECIFIC */
}
md-tab{
padding: 0 !important; /* '!important' IS JSFIDDLE SPECIFIC */
}
a{
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
UPDATE 2018
AngularJS Material throws a (gentle) warning when using flex on button elements, so don't assume all HTML elements/tags can handle display:flex properly, or have a homogeneous behaviour across browsers.
Remember to consult flexbugs in case of unexpected behaviour in a particular browser.
A different approach:
<ul>
<li>
<a></a>
<img>
<morestuff>TEXTEXTEXT</morestuff>
</li>
</ul>
a {
position: absolute;
top: 0;
left: 0;
z-index: [higher than anything else inside parent]
width:100%;
height: 100%;
}
This is helpful if the link's container also has images and such inside of it, or is of potentially different sizes depending on image / content size. With this, the anchor tag itself can be empty, and you can arrange other elements inside of anchor's container however you want. Anchor will always match the size of the parent, and will be on top, to make the entire li clickable.
I used this code to fill the width and height 100%
HTML
<ul>
<li>
<a>I need to fill 100% width and height!</a>
</li>
<ul>
CSS
li a {
display: block;
height: 100%; /* Missing from other answers */
}
Just changing the display property to block didn't work for me.
I removed the padding of li and set the same padding for a.
li a {
display:block;
padding: 4px 8px;
}
<!doctype html>
<html>
<head>
<style type="text/css">
#wide li a {
display:block;
}
</style>
</head>
<body>
<ul id="menu">
<li>Home</li>
<li>Test</li>
</ul>
</body>
</html>
You should try to avoid using a class on every tag so your content remains easy to maintain.
Use line-height and text-indent instead of padding for li element, and use display: block; for anchor tag
I wanted this functionality only on tab view i.e below 720px, so in media query I made:
#media(max-width:720px){
a{
display:block;
width:100%;
}
}
If your <li>s had to have a specific height then your <a>s would only stretch to the <li>'s width and not the height anymore.
One way I solve this is to use CSS display:block and add paddings to my <a>s
OR
wrap the <a>s around the <li>s
<ul id="menu">
<li>Home</li>
<li>Test</li>
</ul>