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;
}
Related
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>
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>
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.
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?
I am not sure if this is the right way to do this but I am trying to align a number of ULs beside each other and should drop the third UL when the screen size is smaller. I just need help with the CSS because for some reason, they keep stacking on top of one another even though I already changed the width to 50%. I already created the '#media'.
HTML:
<ul>
<li>Content 1</li>
</ul>
<ul>
<li>Content 2</li>
</ul>
<ul>
<li>Content 3</li>
</ul>
CSS:
ul {
display: block;
width: 100%;
float:left;
}
#media (max-width: 767px){
ul {
width: 50%;
}
}
You need to remove the display: block, and width: 100%. And make display: inline-block
ul {
display: inline-block;
float:left;
overflow: auto;
}
Since making width: 100% will cover up the whole width, you are getting the uls one down another
You don't need the #media.
You need to use display:inline on your lists.
Have a look here: EXAMPLE
This is all you need.
HTML
<ul>
<li>Content 1</li>
</ul>
<ul>
<li>Content 2</li>
</ul>
<ul>
<li>Content 3</li>
</ul>
CSS
ul {
display:inline-block;
float:left;
}
You can see it over here : http://codepen.io/anon/pen/GwBak
Try changing the window size .
CSS code
{
display: block;
color: #FFF;
background-color: #036;
width: 9em;
padding: 3px 12px 3px 8px;
text-decoration: none;
border-bottom: 1px solid #fff;
font-weight: bold;
}
#navcontainer a:hover
{
background-color: #369;
color: #FFF;
}
In html code
<div id="navcontainer">
<ul>
<li>Milk
<ul>
<li>Goat</li>
<li>Cow</li>
</ul>
</li>
<li>Eggs
<ul>
<li>Free-range</li>
<li>Other</li>
</ul>
</li>
<li>Cheese
<ul>
<li>Smelly</li>
<li>Extra smelly</li>
</ul>
</li>
</ul>
</div>