Centering Vertically an UL inside a DIV - html

i am trying to make a navigation menu inside a 200px x 200px square, this navigation list (UL)
changes from square (200px) to square in 2 rows, like a table, it has some transitions and a lot more stuff going on, but i don't think that affects the vertically centering i want to accomplish.
(i've seen the other answes for this question, but they don't fit this specific scenario, line-height doesn't work, etc.)
these multiple menus are the ones i want to be aligned depending on every element i add, if i just add 1 element then it looks like the center square, of course there is a limit of elements too that can fit on the square, i have a maximum already ( 5 elements ) according to my actual HTML - CSS
This is what i want to accomplish
the html markup is something like this
<div id="table" align="center" border="0" cellpadding="5" cellspacing="5" style="width 100%">
<div id="row">
<div>
<div class="nav"><ul><li><a href=link...>Element 1</a></li> <!----- this is the navigation menu that is in top of an image--->
<img alt="" src="image.jpg" style="width: 200px; height: 200px;" /></div>
<div class="nav"><ul><li><a href=link...>Element 1</a></li><li><a href=link...>Element 2</a></li> <!----- this is the navigation menu that is in top of an image--->
<img alt="" src="image.jpg" style="width: 200px; height: 200px;" /></div>
I know i am not giving too much info, but i can't really put the code as it is, it's work for a private company, but i hope you understand my scenario, the menu on top of the img it has an RGB alpha transition that makes it appear on top of on :hover, but again, i think the important thing is to align every button vertically like that, withouth recurring to special "fixes" for every different section using "position: relative; top: 30px;" i could do that, but i want to understand and see if i can do it without too much trouble, and adding the elements i want to and get automatically aligned in perfect center.
Thank you so much for your help.
(english is not my first language so i hope it is understandable)

The trick for centering something vertically includes two simple steps:
Move the content to its top half of its height using transform: translateY(-50%).
Apply top: 50%. The element must be positioned relatively for this to work.
There are ofcourse many ways to do this.
body {
background: #333333;
}
.main-container {
width: 640px;
margin: 0 auto;
}
.container {
display: inline-block;
vertical-align: top;
width: 200px;
height: 250px;
background: #0077A3;
margin: 5px;
}
ul {
position: relative;
padding: 0;
list-style: none;
transform: translateY(-50%);
top: 50%;
}
li {
text-align: center;
height: 25px;
margin: 10px;
}
span {
background-color: #00C430;
padding: 5px 10px 5px 10px;
color: white;
text-transform: uppercase;
font-weight: bold;
}
<div class="main-container">
<div class="container">
<ul>
<li><span>Element 1</span></li>
<li><span>Element 2</span></li>
<li><span>Element 3</span></li>
</ul>
</div>
<div class="container">
<ul>
<li><span>Element 1</span></li>
</ul>
</div>
<div class="container">
<ul>
<li><span>Element 1</span></li>
<li><span>Element 2</span></li>
</ul>
</div>
</div>

You can use the Centering in the Unknown approach.
Use HTML like
<div class="container">
<ul>
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>
</ul>
</div>
To center ul vertically inside .container, use
.container:before {
content: '';
height: 100%;
}
.container:before, ul {
display: inline-block;
vertical-align: middle;
}
body {
background: #333333;
}
.main-container {
width: 610px;
margin: 0 auto;
}
.container {
display: inline-block;
text-align: center;
vertical-align: top;
width: 200px;
height: 250px;
background: #0077A3;
}
.container:before {
content: '';
height: 100%;
}
.container:before, ul {
display: inline-block;
vertical-align: middle;
}
ul {
padding: 0;
list-style: none;
}
li {
margin: 10px;
padding: 5px 10px;
background-color: #00C430;
color: white;
text-transform: uppercase;
font-weight: bold;
}
<div class="main-container">
<div class="container">
<ul>
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>
</ul>
</div>
<div class="container">
<ul>
<li>Element 1</li>
</ul>
</div>
<div class="container">
<ul>
<li>Element 1</li>
<li>Element 2</li>
</ul>
</div>
</div>

Related

items margin top issue [duplicate]

This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Closed 4 years ago.
I have 3 list items with display: block. I want the even element to be positioned a little lower but when I try with margin-top all my elements are lower positioned.
.hero__description-right {
margin-left: 0px;
width: 100%;
height: auto;
margin-bottom: 20px;
}
.description__item {
margin-top: 0px;
display: inline-block;
position: relative;
clear: both;
}
.description__item:last-child {
margin-bottom: 0px;
}
.description__item:nth-child(2n) {
margin-left: 0px;
margin-top: 20px;
}
<div class="hero__description-right">
<li class="description__item">
<h2><span class="hashtag">#</span> Text</h2>
</li>
<li class="description__item test">
<h2><span class="hashtag">#</span> Text</h2>
</li>
<li class="description__item">
<h2><span class="hashtag">#</span> Text</h2>
</li>
</div>
How can I position only the even elements? Thanks in advance !
It seems you only want to visually move the even elements - in that case it'd be better to use transform: translate instead of margins (this is because margin affects other elements in the document flow, whereas transform is only visually moving them), for example:
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
display: inline-block;
}
li:nth-child(even) {
transform: translateY(5px);
}
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
When put in a proper snippet, it mostly works - if I understand your question. The biggest issue seems to be that, li elements do not belong inside a div - they need to be inside a ul or ol element.
.hero__description-right {
margin-left: 0px;
width: 100%;
height: auto;
margin-bottom: 20px;
}
.description__item {
margin-top: 0px
display: inline-block;
position: relative;
clear: both;
}
.description__item:last-child {
margin-bottom: 0px;
}
.description__item:nth-child(2n) {
margin-left: 0px;
margin-top: 200px;
}
<ul class="hero__description-right">
<li class="description__item">
<h2><span class="hashtag">#</span> Text</h2>
</li>
<li class="description__item test">
<h2><span class="hashtag">#</span> Text</h2>
</li>
<li class="description__item">
<h2><span class="hashtag">#</span> Text</h2>
</li>
</ul>

Center ul between floats in container

I want to center a ul between a float left and float right. They are inside a container which is with a fixed width and is also centered.
<header>
<div class="conatiner">
<div class="left"><img></div>
<div class="right"><img></idv>
<ul class="center">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
<header>
I want them to look like this:
[ [[logo] [Item 1 Item 2 Item 3] [options]] ]
My css is something like this:
header {
width: 100%;
}
.container {
max-width: 1200px;
}
.left {
float: left;
}
.right {
float: right;
}
.center {
text-align: center;
}
However the .center ul is centered between the .left and .right and because .left has a larger width than .right it gets shifted a little to the right. What I want to achieve is to make it centered no matter how big .left and .right are.
In this case you can use position: absolute on ul and transform: translateX(-50%) to center.
With position: absolute you remove ul from elements flow so width of images doesn't affect position of ul and it will always stay in center of window.
header {
width: 100%;
position: relative;
}
.container {
max-width: 1200px;
}
.left {
float: left;
}
.right {
float: right;
}
.center {
text-align: center;
padding: 0;
position: absolute;
margin: 0;
left: 50%;
top: 0;
transform: translateX(-50%);
}
<header>
<div class="conatiner">
<div class="left"><img src="" alt="Lorem ipsum dolor."></div>
<div class="right"><img src="" alt="right"></div>
<ul class="center">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</header>
you can reset BFC for ul , so it can center itself in between floatting elements without laying under it.
Diplay:table; would be appropriate since container will also shrink on its content:
header {
width: 100%;
background:linear-gradient(to left, gray 50%, lightgray 50%);
}
.container {
max-width: 1200px;
}
.left {
float: left;
margin-right:50px;/* cause it is 50px less wide than the other one */
}
.right {
float: right;
}
.center {
display:table;
margin:auto;
padding:0;
border-spacing:0.25em;
}
.center li {
display:table-cell;
border:1px solid;
}
<header>
<div class="container">
<div class="left"><img src="http://lorempixel.com/50/50"></div>
<div class="right"><img src="http://lorempixel.com/100/50"></div>
<ul class="center">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
<header>
or flex:
header {
width: 100%;
background:linear-gradient(to left, gray 50%, lightgray 50%);/* see center */
}
.container {
max-width: 1200px;
display:flex;
}
.left {
order:-1;
margin-right:-50px;/* if know wich is biiger and how much bigger an equal negative margin of that extra size may swallow the difference .. */
}
.right {
order:1;
}
.center {
display:flex;
margin:auto;/* instead : justify-content:space-between; on parent (given into another answer ) */
padding:0;
order:0
}
.center li {
list-style-type:none;
margin:0.25em;
border:solid 1px;
}
<header>
<div class="container">
<div class="left"><img src="http://lorempixel.com/100/50"></div>
<div class="right"><img src="http://lorempixel.com/50/50"></div>
<ul class="center">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
<header>
The simplest and easiest way is flexbox. Like this:
.container {
display: flex;
justify-content: space-between;
}
<header>
<div class="container">
<div class="left"><img src="http://design.ubuntu.com/wp-content/uploads/ubuntu-logo32.png"></div>
<ul class="menu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<div class="right"><img src="http://design.ubuntu.com/wp-content/uploads/ubuntu-logo32.png"></div>
</div>
<header>
Try this method:
.inline-block { display: inline-block; }
.floatright { float: right; }
.floatcenter { margin-right: auto; margin-left: auto; }
.floatleft { float: left; }
<div style="text-align:center;">
<div class="inline-block floatleft">Logo</div>
<div class="inline-block floatcenter">Menu</div>
<div class="inline-block floatright">Options</div>
</div>

css vertical and horizontal alignment issues

I'd like to align my menu items and logo-picture both horizontally and vertically in the red DIV. How could I achieve that? I've tried to set margin left and right to auto as well as vertical-align to center, but that didn't work. Thanks for your help
See: http://jsfiddle.net/gm2wzL6z/
HTML:
<div id="nav-container">
<nav>
<ul id="navlist">
<li id="active">Item one
</li>
<li>Item two
</li>
<li>Item three
</li>
<li>
<img src="http://lorempixel.com/output/fashion-q-g-227-148-4.jpg">
</li>
<li>Item four
</li>
<li>Item five
</li>
</ul>
</nav>
</div>
CSS:
#nav-container {
height: 300px;
background: red;
}
#navlist li {
display: inline;
list-style-type: none;
padding-right: 20px;
}
#nav-container {
height: 300px;
background: red;
}
#navlist li {
display: inline-block; /* this for personal preference :) */
vertical-align: middle; /* this for alignment*/
list-style-type: none;
padding-right: 20px;
}
<div id="nav-container">
<nav>
<ul id="navlist">
<li id="active">Item one
</li>
<li>Item two
</li>
<li>Item three
</li>
<li>
<img src="http://lorempixel.com/output/fashion-q-g-227-148-4.jpg">
</li>
<li>Item four
</li>
<li>Item five
</li>
</ul>
</nav>
</div>
Use display:table/table-cell with vertical-align:
#nav-container {
height: 300px;
background: red;
display:table;
width:100%;
}
#nav-container nav{
display: table-cell;
height: 300px;
vertical-align: middle;
text-align:center;
}
#nav-container nav li{
display:inline-block;
vertical-align: middle;
}
<div id="nav-container">
<nav>
<ul id="navlist">
<li id="active">Item one
</li>
<li>Item two
</li>
<li>Item three
</li>
<li>
<img src="http://lorempixel.com/output/fashion-q-g-227-148-4.jpg">
</li>
<li>Item four
</li>
<li>Item five
</li>
</ul>
</nav>
</div>
JSFiddle
In order for this to work you need 3 things.
1)A main div to act as the container
2)A div inside the container with the vertical aligh and a width of 100% of your container as you have to do relative positioning in order to do vertical align.
3)A final container to do your margin-left, margin-right auto. It has to have a set width thats smaller than the previous two containers
4)All your content sits inside the horizontal div
5)Dont set the height of the main container as this will limit the vertical align from going beyond that.
#nav-container {
background: red;
width: 100%
}
#vertical-align {
position: relative;
top: 50%;
width:100%
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
#horizontal-align{
width: 100%
max-width: 700px;
margin-left: auto;
margin-right: auto;
}

Prevent Div from moving

I am working with a website with css drop down menus within the header div. When the drop down menu appears it resizes the header div therefore shoving down the content div.
In the body of my index.php:
<div class="top"></div>
<div class="container">
<?php include_once("header.php"); ?>
<div class="content"></div>
</div>
The header.php
<div class="header">
<div style="display: table-cell; width: 250px; text-align: center;">
LOGO
</div>
<div style="display: table-cell;">
<br><br><br>
<ul>
<li>Link 1
<ul>
<li>Monkey 1</li>
<li>Monkey 2</li>
<li>Monkey 3</li>
<li>Monkey 4</li>
<li>Monkey 5</li>
<li>Monkey 6</li>
<li>Monkey 7</li>
</ul>
</li>
<li>Link 2
<ul>
<li>Monkey 1</li>
<li>Monkey 2</li>
</ul>
</li>
<li>
Link 3
<ul>
<li>Monkey 1</li>
<li>Monkey 2</li>
<li>Monkey 3</li>
</ul>
</li>
<li>Link 4</li>
<li>Link 5</li>
<li>Link 6</li>
</ul>
</div>
</div>
And main.css
ul{
padding: 0;
list-style: none;
}
ul li{
float: left;
width: 100px;
text-align: center;
}
ul li a{
display: block;
padding: 5px 10px;
color: #333;
background: #f2f2f2;
text-decoration: none;
}
ul li a:hover{
color: #fff;
background: #939393;
}
ul li ul{
display: none;
}
ul li:hover ul{
display: block; /* display the dropdown */
}
.top{
background: #1b2d3c;
width: 100%;
height: 40px;
}
.container{
width: 1100px;
height: 1000px;
}
.header{
display: table-row;
width: 1100px;
height: 130px;
}
.content{
position: absolute;
background: #fff;
width: 1100px;
height: 500px;
}
* {
background: #87a0b4;
margin: 0px;
padding: 0px;
margin-left: auto ;
margin-right: auto ;
}
I apologize for the lengthy post. How can I prevent the problem I am having? I want to be sure I am doing things correctly before I get too deep into the project.
You need to change position of ul li:hover ul to absolute, and add some other properties like this JSFiddel (Source)
ul li:hover ul {
display: block;
position: absolute;
width: 100px;
z-index: 100;
}
Hope this will help you ..
As was said in the previous answer, you need to set position: absolute. I'd like to add a bit of information as to why.
According to MDN:
Elements that are positioned relatively are still considered to be in the normal flow of elements in the document. In contrast, an element that is positioned absolutely is taken out of the flow and thus takes up no space when placing other elements. The absolutely positioned element is positioned relative to nearest positioned ancestor. If a positioned ancestor doesn't exist, the initial container is used.
Basically, by giving an element absolute positioning, it is no longer being taken into account when positioning the rest of the page. It will take up its alloted space in its set position no matter what.
In your case, the div was moving relative to the elements surrounding it. By using position: absolute, you are ommiting and relativity.

How to expand rows containing floated elements expand to 100% width?

I'm working on a nav bar style. I want the border to expand to 100% width of the area the menu is taking up, but I want the elements in the rows to float to the right of this 100% width area. Unfortunately, if I get everything floated to the right then it doesn't expand to full width.
Here's an image to check out: http://i.imgur.com/EKd3cZY.png
You can see what width it should be and what width things actually are.
Here's my HTML:
<section class="row">
<nav class="top-bar-navigation">
<div class="main-menu-holder">
<ul class="main-menu">
<li><a class="active" href="#">Active</a></li>
<li>Page 2</li>
<li>Page 3</li>
<li>Page 4</li>
<li>Page 5</li>
</ul>
<button>CTA BUTTON</button>
</div>
<ul class="secondary-menu">
<li>Support</li>
<li>Docs</li>
<li>Why</li>
<li>Social</li>
<li>Link</li>
</ul>
</nav>
</section>
Here's the SASS I've created for the nav element. There are some other styles for the default elements like typography but I don't think it's relevant to this issue:
.top-bar-navigation {
width: 100%;
clear: both;
.main-menu-holder {
border-bottom: 2px solid $pale-grey;
overflow: hidden;
}
.main-menu-holder,
.secondary-menu {
float: right;
}
span,
button,
.main-menu {
display: inline-block;
}
li {
display: inline-block;
}
a {
color: black;
font-weight: 300;
}
a.active {
font-weight: 700;
}
}
Here is the Row style:
.row {
width: 90%;
margin: auto;
max-width: 1100px;
margin-bottom: 5px;
}
.row:after {
content: ".";
visibility: hidden;
display: block;
height: 0;
clear: both;
}
Have you tried using a <hr /> or creating a new div with a class on it to style it to create the same effect?