Center ul between floats in container - html

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>

Related

Text Align Middle Vertically CSS

I building a header and I have HTML code below
#header{
height:50px;
border-bottom: 1px solid #000;
}
.left{
display: inline-block;
float: left;
}
.right{
display: inline-block;
float: right;
}
.logo{
display: inline-block;
font-size: 40px;
}
.slogan{
display: inline-block;
vertical-align: middle;
}
.menu{
margin: 0px;
}
.menu li {
display: inline;
}
<div id="header">
<div class="left">
<div class="logo">LOGO</div>
<div class="slogan">SLOGAN HERE</div>
</div>
<div class="right">
<nav>
<ul class="menu">
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
<li>Menu 4</li>
</ul>
</nav>
</div>
</div>
I want Slogan and Menu is middle of header div so I use CSS vertical-align: middle; but it's not working. It only working with vertical-align: top;
How to fix it ?
Thanks you so much.
Change display: inline-block to display: table on element .left, then on .slogan change display: table-cell;to display: inline-block;.
#header{
height:50px;
border-bottom: 1px solid #000;
}
.left{
/*display: inline-block;*/
display: table;
float: left;
}
.right{
display: inline-block;
float: right;
}
.logo{
display: inline-block;
font-size: 40px;
}
.slogan{
vertical-align: middle;
display: table-cell;
/*display: inline-block;*/
}
.menu{
margin: 0px;
}
.menu li {
display: inline;
}
<div id="header">
<div class="left">
<div class="logo">LOGO</div>
<div class="slogan">SLOGAN HERE</div>
</div>
<div class="right">
<nav>
<ul class="menu">
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
<li>Menu 4</li>
</ul>
</nav>
</div>
</div>
You could use line-height to achieve the effect you want. The headers height is 50px, so you should give the list items a line-height of 50px. I think this will solve your problem in this case but it wouldn't work if your text has multiple lines.

Centering Vertically an UL inside a DIV

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>

HTML Navigation positioning

I have pretty simple navigation.
Three divs inside a header (logo, navigation and phone).
I want to make them responsive and stretchable whenever user zooms out.
Example
http://www.zendrive.com/
Can someone give me a simple CSS example on how to achieve this?
.header {
display: table;
position: relative;
width: 100%;
height: 60px;
}
.header > * {
display: table-cell;
position: relative;
height: 100%;
}
.header .navigation ul {
display: inline-block;
position: relative;
list-style: none;
margin: 0;
padding: 0;
}
.header .navigation li {
display: inline-block;
}
<header class="header">
<div class="logo">
<img src="http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png">
</div>
<nav class="navigation">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</nav>
<div class="phone">555.555.5555</div>
</header>

2 column layout that extends the length of the page as content is added to sidebar div

I'm working on building a page with a 2 column layout, header, and footer. I'm using div's, html, and css.
The Problem I'm Experiencing is the left column that is created by div#sideBar extends over the footer when content is added.
The Solution I'm Looking For is I'd like the left column to 'push' the footer down and extend the length of the div#contentWrapper when more content is added to the div#sideBar.
I've looked through several tutorials, but I can't seem to figure it out. Can someone please direct me to a tutorial that will solve this problem or help me modify the code below so the page it creates will extend (push down footer) as content is added to the div#sideBar?
The ScreenShot below shows the result of the code below.
<style type="text/css">
#wrapper {
width: 900px;
margin: auto;
}
#header {
background-color: #0F0;
}
#contentWrapper {
background-color: #FF0;
}
#footer {
background-color: #00F;
}
#sideBar {
float: left;
width: 200px;
height: 100%;
color: #F00;
}
#content {
width: 700px;
margin-left: 200px;
background-color: #FFF;
}
p {
margin: 0px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="header">
Home | Page One | Page Two </div>
<div id="contentWrapper">
<div id="sideBar">
<p>
<ul>
<li>Home</li>
<li>Page One</li>
<li> Page Two</li>
</ul>
</p>
<p>hgfds</p>
<p>kgjkfhghf</p>
<p>jkfhgjdffgfhj</p>
<p>ljkfhgjdf</p>
<p>;klgjhg</p>
<p>lkgjhfg</p>
<p>lgkjhfg</p>
<p> </p>
</div>
<div id="content">
<p>Content for id "content" Goes Here</p>
<p> </p>
<p> </p>
<p> </p>
</div>
</div>
<div id="footer">
<p>CopyRight 2014</p>
<p>Home | Page One | Page Two</p>
</div>
</div>
</body>
</html>
#footer {
background-color: #00F;
clear:both;
}
You need to clear the float used in the sideBar, which you can do in the footer.
#sideBar {
float: left;
width: 200px;
height: 100%;
color: #F00;
}
Unless you also need to expand the background of in which case you need to add a tag inside the wrapper that clears the floats
</div> <!-- content div -->
<div style="clear:left"></div>
</div> <!-- wrapper div -->
<div id="footer">
Easiest way would be to use display:table; on a container that the sidebar and main content area both share and set the two child divs to display:table-cell;
HTML:
<div class="container">
<div class="sidebar">
<ul>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
<li>item 1</li>
</ul>
</div>
<div class="main">
main content main content main content main content main content main content main content main content main content main content main content main content main content main content main
</div>
</div>
CSS:
.container {
width:100%;
display:table;
}
.sidebar, .main {
width:50%;
display:table-cell;
}
.main {
background:#eee;
}
.sidebar {
background:#666;
}
http://jsfiddle.net/3gbfnpua/3/
You can solve this by adding this to your css:
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
Then add class="clearfix" to any element you want to extend.
There's a lot of information about Clearfix on StackOverflow (and elsewhere on the internet). Try this question, for example.
#wrapper {
width: 900px;
margin: auto;
}
#header {
background-color: #0F0;
}
#contentWrapper {
background-color: #FF0;
}
#footer {
background-color: #00F;
}
#sideBar {
float: left;
width: 200px;
height: 100%;
color: #F00;
}
#content {
width: 700px;
margin-left: 200px;
background-color: #FFF;
}
p {
margin: 0px;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
<div id="wrapper">
<div id="header">
Home | Page One | Page Two</div>
<div id="contentWrapper" class="clearfix">
<div id="sideBar">
<p>
<ul>
<li>Home</li>
<li>Page One</li>
<li>Page Two</li>
</ul>
</p>
<p>hgfds</p>
<p>kgjkfhghf</p>
<p>jkfhgjdffgfhj</p>
<p>ljkfhgjdf</p>
<p>;klgjhg</p>
<p>lkgjhfg</p>
<p>lgkjhfg</p>
<p> </p>
</div>
<div id="content">
<p>Content for id "content" Goes Here</p>
<p> </p>
<p> </p>
<p> </p>
</div>
</div>
<div id="footer">
<p>CopyRight 2014</p>
<p>Home | Page One | Page Two</p>
</div>
</div>

How to fill up the height within an object

I want a page that has two fixed-width columns on the left and a main column that fills up the rest of the width of <body>. Within each column, I want to place some <div> object on the top and a <ul> object that fills up the rest of the height. I tried the following, but could not make the height of the <ul> objects automatically adjust to fill in the height. Rather, it overflows to the bottom as much as the height of the <div> above them.
<html>
<style>
body{
margin:0;padding:0;
overflow: hidden;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color:red;
}
.column{
float: left;
background-color:blue;
}
.column>ul{
height: 100%;
width: 100;
overflow-y: scroll;
overflow-x: hidden;
background-color:green;
}
#main-column>ul{
height: 100%;
overflow-y: scroll;
overflow-x: hidden;
background-color:brown;
}
</style>
<body>
<div class="column">
<div>Column 1 Title</div>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
<div class="column">
<div>Column 2 Title</div>
<ul>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</div>
<div id="main-column">
<div>Main Column Title</div>
<ul>
<li>Item 5</li>
<li>Item 6</li>
</ul>
</div>
</body></html>
How can I make it so that height of the <ul> objects are automatically adjusted to fill up the remaining height?
​
This is what I get so far. The scroll bar is cut off and extends below the window border, which indicates that its height is overflowing.
If you give the column titles a fixed height, you can use that along with position: absolute on the uls to get the layout you want. Demo: http://jsbin.com/ecilob/1/edit
HTML:
<body>
<div class="column fw">
<div class="title">Column 1 Title</div>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
<div class="column fw">
<div class="title">Column 2 Title</div>
<ul>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</div>
<div class="column main">
<div class="title">Main Column Title</div>
<ul>
<li>Item 5</li>
<li>Item 6</li>
</ul>
</div>
</body>
CSS:
body {
margin: 0;
padding: 0;
}
.column .title {
height: 30px;
border-bottom: 1px dashed #333;
}
.column {
background: lightblue;
float: left;
position: absolute;
top: 0;
bottom: 0;
}
.column + .column {
background: coral;
left: 120px;
}
.column + .column + .column { background: goldenrod; }
.column.fw {
width: 120px;
}
.column.main {
left: 240px;
right: 0;
}
.column ul {
position: absolute;
margin: 0;
top: 30px;
bottom: 0;
left: 0;
right: 0;
overflow-y: scroll;
}