I have an inline-block nav element that needs to be centered inside its parent container. But because the width is auto (because I need it to shrink to its content), I can't center it.
And I can't use text-align: center because I don't want the text content to be centered. I just want the mainNavigation element to be centered.
http://jsfiddle.net/vpvzmg3j/3/
How can I center this thing?
*{margin:0;padding:0;}
#mainNavigation
{
display: inline-block;
width: auto;
height: auto;
margin: 0 auto;
background-color: blue;
}
#mainNavigation div
{
width: auto;
float: left;
}
#mainNavigation div p
{
float: left;
width: 100%;
}
#mainNavigation div ul
{
width: auto;
height: auto;
float: left;
}
#mainNavigation div ul li
{
list-style-type: none;
display: inline;
}
<nav id="mainNavigation">
<div id="homeMenuContainer">
<p class="noDisplay">Home</p>
<ul id="homeMenu">
<li>Home</li>
</ul>
</div>
<div id="helpAndSupportMenuContainer">
<p>Help & Support</p>
<ul id="helpAndSupportMenu">
<li>User Guides</li>
<li>Video Tutorials</li>
</ul>
</div>
<div id="myAccountMenuContainer">
<p>My Account</p>
<ul id="myAccountMenu">
<li>Sign In</li>
<li>Register</li>
</ul>
</div>
</nav>
Is this what you are looking for?
The code updates :
#mainNavigation {
text-align: center; /*this does the trick with its child*/
height: auto;
width: 100%;
margin: 0 auto;
}
#mainNavigation div {
/*you said you want them on the left side*/
text-align: left;
background-color: blue;
display: inline-block;
vertical-align: middle;
margin-left: -4px; /*inline-block space remove*/
}
Demo: http://jsfiddle.net/vpvzmg3j/9/
add blow changes, that will center align the nav and same time left align the content:
#mainNavigation{text-align: left;}
.center-div{text-align:center;}
HTML
<div class="center-div"> <nav id="mainNavigation"> .....</nav> </div>
Related
I'm quite new to HTML and CSS. I have a list that serves as a navigation bar for a website, which is inside the div "inner header." I was able to align the "nav" container (in teal) to the right edge of the "inner header" container, but I am struggling to do the same for the items within nav (in red).
Containers Appearance
Essentially, I would like the text to align with the teal on the right side, but I noticed no matter what I do, the red will not expand. I've tried changing width to 100%, but that causes each word to separate into three rows (possible due to the display type?).
If I add width: 98px, it gets closer to my desired outcome, but it doesn't perfectly align with the teal.
Close to desired outcome
Any advice is appreciated!
.inner_header {
width: 1000px;
height: 100%;
display: block;
margin: 0 auto;
background-color: #8ecae6;
display: flex;
justify-content: center;
align-items: center;
}
.nav {
float: right;
height: 100%;
width: 70%;
background-color: teal;
text-align: right;
}
.nav a {
height: 100%;
display: table;
table-layout: fixed;
float: left;
padding: 0px 20px;
background-color: red;
overflow: hidden;
width: 98px;
}
.nav a:last-child {
padding-right: 0px;
}
.nav a li {
display: table-cell;
vertical-align: middle;
height: 100%;
font-family: 'Montserrat';
font-weight: 300;
}
<div class="header">
<div class="inner_header">
<div class="logo_container">
<img src=".png">
<img src=".jpeg">
</div>
<p>Text <br> Text</p>
<ul class="nav">
<a>
<li>Home</li>
</a>
<a>
<li>Data</li>
</a>
<a>
<li>Tool</li>
</a>
</ul>
</div>
</div>
I still don't understand what you wanted, I'm just taking a guess here and tried to get the result closer to your screenshot and based on what you explained.
.inner_header {
width: 1000px;
height: 100%;
display: block;
margin: 0 auto;
background-color: #8ecae6;
display: flex;
justify-content: center;
align-items: center;
}
.nav {
float: right;
height: 100%;
width: 70%;
display: table;
background-color: teal;
text-align: right;
padding:0;
}
.nav a {
height: 100%;
display: table-cell;
padding: 0px 20px;
background-color: red;
overflow: hidden;
width: auto;
}
.nav a:last-child {
padding-right: 0px;
}
.nav a li {
display: table-cell;
vertical-align: middle;
height: 100%;
font-family: 'Montserrat';
font-weight: 300;
}
<div class="header">
<div class="inner_header">
<div class="logo_container">
<img src=".png">
<img src=".jpeg">
</div>
<p>Text <br> Text</p>
<ul class="nav">
<a>
<li>Home</li>
</a>
<a>
<li>Data</li>
</a>
<a>
<li>Tool</li>
</a>
</ul>
</div>
</div>
I have a footer which has a variable height. The largest element is an image which is automatically centered via the padding of the footer. Next to the image there is a little navigation and a copyright statement below. To the far right there is a button to go back to top.
I've included a fiddle where I got all the elements vertically centered but I got the result by adding the height on some elements. Now my question is if there is a way to achieve this result but without adding the height of the biggest element?
My fiddle
.footer {
background: darkgray;
}
.wrap {
position: relative;
width: 960px;
margin: 0 auto;
padding: 30px 0;
}
.wrap:after {
content: "";
display: table;
clear: both;
}
.left {
width: 29%;
float: left;
}
.left img {
height: 160px;
}
.center {
text-align: center;
float: left;
width: 50%;
height: 160px;
}
.center-wrap {
display: inline-block;
vertical-align: middle;
}
.center:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em;
}
.right {
position: relative;
float: left;
width: 20%;
height: 160px;
}
.right a {
position: absolute;
right: 0;
top: 50%;
margin-top: -25px;
}
/* Styles */
nav ul {
list-style: none;
padding: 0;
margin: 0;
}
nav ul li {
display: inline-block;
margin-left: 20px
}
nav ul li:first-child {
margin-left: 0;
}
.right a {
display: block;
width: 50px;
height: 50px;
background: black;
}
<div class="footer">
<div class="wrap">
<div class="left">
<img src="http://placehold.it/500x500" alt="" />
</div>
<div class="center">
<div class="center-wrap">
<nav>
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
</ul>
</nav>
<p>© 2015 copyright COMPANY NAME // All Rights Reserved</p>
</div>
</div>
<div class="right">
top
</div>
</div>
</div>
You can vertically align anything with just 3 lines of CSS.
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
Go nuts.
Don't forget vendor prefixes.
I'm working on my homepage and I'm trying two get a header with 3 columns.
The first column should be a logo, the second a navigation menu and the third a language switcher.
The nav and the lang. switcher should be align to the right and have auto width (so only the width needed by the content).
The img should be floated to the left and should fill the empty width (!but not bigger than 100% of the img width and with max-height!)
If the browser width gets smaller, than the img should be shrinked, but the right contents should be the same size.
.frame {
max-width: 90%;
height: 75px;
margin: 0 auto;
}
img {
max-width: 100%;
height: auto;
}
#top-header {
width: 100%;
height: 100px;
position: fixed;
z-index: 100;
text-transform: uppercase;
font-weight: bold;
background-color: #CCC;
}
#top-header .frame {
position: relative;
top: 10px;
}
#top-header .frame div {
white-space: nowrap;
}
#top-header .frame > div,
nav {
float: right;
}
#top-header .frame > div:first-child {
float: left;
max-width: 60%;
}
#mainmenu {
position: relative;
top: 25px;
}
#mainmenu .moduletable {
display: block;
position: static;
}
#mainmenu ul.nav.menu {
display: flex;
display: -ms-flexbox;
display: -webkit-flex;
margin: 0;
padding: 0;
list-style-type: none;
}
#mainmenu ul.nav.menu li {
font-size: .92rem;
-ms-flex: 1 1 auto;
-webkit-box-flex: 1;
-webkit-flex: 1 1 auto;
flex: 1 1 auto;
position: relative;
}
#mainmenu ul.nav.menu li a {
display: block;
margin: 10px;
}
<body>
<header id="top-header">
<div class="frame">
<div id="logo">
<div class="moduletable">
<div class="custom">
<p>
<img alt="logo" src="http://lorempixel.com/1637/100">
</p>
</div>
</div>
</div>
<div id="language-switcher"></div>
<nav id="mainmenu">
<div class="moduletable">
<ul class="nav menu">
<li class="item-101 current active"> Home
</li>
<li class="item-102 deeper parent"> About us
</li>
<li class="item-103"> XXXXXXXX
</li>
<li class="item-104"> XXXXXX
</li>
</ul>
</div>
</nav>
</div>
</header>
</body>
Here's an example of my current try at jsFiddle
Thanks and have a nice day :)
I think i understand now.
I changed your layout to work similar to a table with
dislpay:table
for the #top-header .frame
and dislpay:table-cell
for the logo and the menu
here is the fixed jsFiddle
I have a div with its content (list inside),
<div id="container">
<ul id="list">
<li>
<div class="title">
something
</div>
<div class="text">
something again
</div>
</li>
<li>
<div class="title">
something
</div>
<div class="text">
something again
</div>
</li>
</ul>
</div>
And my items from list are displayed float:left, and I want to the div container expand in height as its content.
#container{
position: relative;
width: 100%;
padding-bottom: 30px;
}
#list{
position: relative;
padding-top: 80px;
width: 95%;
/*height: 100%;*/
padding-bottom: 10px;
}
#list li{
position: relative;
float: left;
list-style: none outside none;
width: 20%;
height: 170px;
/*border:1px solid black;*/
}
You have several options:
Clearfix on the container:
#container:after {
clear: both;
content: "";
display: table;
}
The use of overflow:
#container {
overflow: hidden; /* or: auto | scroll */
}
Float the container:
#container {
float: left;
}
Using inline-block instead of float for the list items:
#container ul li {
display: inline-block;
float: none;
}
Try this approach...
li {
display: inline-block;
float: none; /* only if you have this set as you mentioned in your post */
}
#container {
height: auto;
}
I have two different elements, a logo and navigation. The logo is 48px but the navigation is 16px. The challenge I am facing is vertically aligning them both and then horizontally aligning them with my webpage content.
<div class="header">
<div class="logo">LOGO</div>
<ul class="nav">
<li>ABOUT</li>
<li>CONTACT</li>
</ul>
</div>
<div class="container">
<div class="wrapper">
CONTENT
</div>
</div>
Then in my CSS:
.header {
height: 10%;
width: 100%;
display: table;
}
.logo {
height: 100%;
display: table-cell;
vertical-align: middle;
}
.nav {
list-style: none;
margin: 0;
padding: 0;
display: table-cell;
vertical-align: middle;
}
.container {
width: 100%;
height: 90%;
display: table;
}
.wrapper {
height: 100%;
display: table-cell;
vertical-align: middle;
}
This code produces an output where both are vertically aligned but I'm not sure how to then align it with the content in the container, since the margin on the container is auto. If someone could give me some direction that would be great.
My overall goal is to align both the logo and the navigation vertically within the header but then say for example my content is 590px wide, align the logo (horizontally) to the left of that and the navigation to the right.
The output should look like
|logo| |nav |
|contentttttttttttt|
Thanks
http://jsfiddle.net/tqBUb/
The following code perfectly centers your different sized text vertically, and the total combined width of those two elements horizontally. Is this what you want? Either way this is probably a good starting point for you. The width of the header element can be changed to anything and it adjusts accordingly.
HTML
<header>
<div class="container">
<div class="logo">
<span>LOGO</span>
</div>
<nav class="main-nav">
<ul>
<li>HOME</li>
<li>ABOUT</li>
</ul>
</nav>
</div>
</header>
CSS
header {
color: #fff;
width: 80%;
background: blue;
}
.container {
display: table;
background: red;
margin: 0 auto;
}
.logo {
display: table-cell;
vertical-align: middle;
}
.logo span {
font-size: 48px;
line-height: 1;
}
.main-nav {
display: table-cell;
}
.main-nav ul {
list-style-type: none;
}
.main-nav ul li {
display: inline-block;
}
|logo|_______|nav|
|_____content____|
To achieve this format at 590px, you will need a media query breakpoint for screens widths of 590 or less. Then... just change the display: table; on .container to display: block; and the display: table-cell; div's to display: block. Then Flow the .logo div left, and float .main-nav right.