I need to center html content inside a div class="partners" (top div with 2 images). As you can see from the image below (it floats left instead of center of the div):
This is my html code:
<div id="partners">
<div class="wrap clearfix">
<h2>Partnertnerzy serwisu:</h2>
<ul>
<li><img width="56" height="16" alt="Parnter bar wika" src="/as/partners/wika.png"></li>
<li></li>
</ul>
<a class="linkClose" href="/firmy?clbp=1">Zamknij </a>
</div>
</div>
Image:
CSS:
#partners, #top {
position: relative;
z-index: 100;
}
#partners {
margin: 12px 0 3px;
text-align: center;
}
.clearfix:after, .row:after {
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
}
#partners .wrap {
width: 655px;
}
.wrap {
margin: 0 auto;
position: relative;
width: 990px;
}
#partners h2 {
color: #A6A5A5;
float: left;
font-weight: normal;
margin: 2px 15px 0 0;
}
#partners ul {
float: left;
}
ul {
list-style-position: outside;
list-style-type: none;
}
To center a div, set it's width to some value and add margin: auto.
#partners .wrap {
width: 655px;
margin: auto;
}
EDIT, you want to center the div contents, not the div itself. You need to change display property of h2, ul and li to inline, and remove the float: left.
#partners li, ul, h2 {
display: inline;
float: none;
}
Then, they will be layed out like normal text elements, and aligned according to text-align property of their container, which is what you want.
There are many ways to center any element. I listed some
Set it's width to some value and add margin: 0 auto.
.partners {
width: 80%;
margin: 0 auto;
}
Split into 3 column layout
.partners {
width: 80%;
margin-left: 10%;
}
Use bootstrap layout
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-4">Your Content / Image here</div>
</div>
You just need
.parent-div { text-align: center }
Try using flexbox. As an example, the following code shows the CSS for the container div inside which the contents needs to be centered aligned:
Depending on the axis of the flexbox, you will need to align or justify items, read more at MDN
.absolute-center {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-align: center;
-webkit-align-items: center;
-webkit-box-align: center;
align-items: center;
justify-content: center;
}
in parent div
parentDiv: {
display:flex;
height:100%;
width:100%;
justify-content:center;
align-items:center;
}
You just do CSS changes for parent div
.parent-div {
text-align: center;
display: block;
}
The problem is that you assigned a fixed width to your .wrap DIV. The DIV itself is centered (you can see that when you add a border to it) but the DIV is just too wide. In other words the content does not fill the whole width of the DIV.
To solve the problem you have to make sure, that the .wrap DIV is only as wide as it's content.
To achieve that you have to remove the floating in the content elements and set the display property of the block levels elements to inline:
#partners .wrap {
display: inline;
}
.wrap { margin: 0 auto; position: relative;}
#partners h2 {
color: #A6A5A5;
font-weight: normal;
margin: 2px 15px 0 0;
display: inline;
}
#partners ul {
display: inline;
}
#partners li {display: inline}
ul { list-style-position: outside; list-style-type: none; }
do like this :
child{
position:absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
After 2022.
Here is your class to center content both vertically and horizontally.
.center {
display: flex;
flex-wrap: wrap;
align-content: center;
justify-content: center;
}
Related
This is my code and tried everything I could think of to place my image in the top middle and links on the top right corner.
Whatever I try doesn't align them in the same div and makes things wacky.
Here is my code:
#header img{
display: block;
margin: 0 auto;
width: 150px;
height: 90px;
}
.login a{
float: right;
text-decoration: none;
font-size: 18px;
padding: 10px;
}
<div id="header">
<img src="img/raystratos.png"/>
<div class="login">
Sign Up
Log In
Shopping Cart
</div>
</div>
Try absolute positioning:
#header { position: relative; }
.login { position: absolute; top: 0; right: 0;}
You can use flex to do this and also if you want to divide this row into 3 equal width columns you add another div inside the container and then add flex:1 property to each child element inside the #header.
#header {
display: flex;
justify-content: space-between;
}
#header img{
display: block;
margin: 0 auto;
width: 150px;
height: 90px;
flex: 1;
}
.equalFlex {
flex: 1;
}
.login a{
text-decoration: none;
font-size: 18px;
padding: 10px;
flex: 1;
}
.login{
flex: 1;
}
<div id="header">
<div class="equalFlex"></div>
<img src="img/raystratos.png"/>
<div class="login">
Sign Up
Log In
Shopping Cart
</div>
</div>
The jsfiddle for the code can be found here: http://jsfiddle.net/c4rz0hk1/
The nav-wrapper at the bottom should be centered below the dividing line div at the bottom of the page. The works correctly in Firefox, but in Chrome it placed to the r-hand side of that same line. This is strange as the wrapper is set to 1024px anyways...
Here's a snippet of the css code:
footer {
text-align: center;
width: 1024px;
}
#nav-wrapper {
display: -moz-inline-stack;
display: inline-block;
text-align: center;
width: 900px;
*display: inline;
}
Because footer has floated children, a overflow:hidden should be applied to it so it properly contains them, also your bottom nav needs to have floats cleared as it's siblings are floating causing it to 'go with the flow' (lame joke, sorry I couldn't stop myself :P).
To center the nav simply apply a margin: 0 auto; to it after clearing the floats.
Here's the updated css that I believe works according to your requirements -
footer {
overflow: hidden;
text-align: center;
width: 1024px;
}
#nav-wrapper {
clear: both;
display: block;
margin: 0 auto;
padding: 10px 0; /* Some padding to give the footer some vertical breathing space */
text-align: center;
width: 900px;
}
Updated fiddle: http://jsfiddle.net/c4rz0hk1/1/
Try this:
#-moz-document url-prefix() {
#nav-wrapper {
display: -moz-inline-stack;
text-align: center;
width: 900px;
}
}
#nav-wrapper {
display: inline-block;
text-align: center;
width: 900px;
}
I'm making a responsive site, so most of the elements I'm working with have unknown heights. Here's what I got...
<div class="main">
<div class="submenu">
<ul>
<li>Lorem</li>
<li>Lorem</li>
<li>Lorem</li>
<li>Lorem</li>
</ul>
</div>
</div>
CSS:
.main {
background: #0e0e0e;
width: 80%;
margin: auto;
position: relative;
}
.main .submenu {
background: #e62e7a;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 15%;
}
.submenu ul {
background: pink;
float: left;
height: 100%;
}
.submenu ul li {
display: block;
float: left;
}
.submenu ul li a {
background: rgba(0,0,0,0.5);
padding: 8px;
}
Okay. So I have div with a proportional size inside a div with a changing height and width. I have two problems:
1) How do I vertically center the anchors at the end of the code inside the .submenu div? Apparently percentage line height doesn't work, and I'm not sure why, but the table display trick does not work either.
2) Also in the anchors, the padding escapes its parent boundaries. The text is perfectly at top, but the padding transparently black background of the anchors is escaping the parent. Why is that?
P.S. as for .main height I added a div inside with a padding-top of 50% to set its height relatively to its width.
This article may be able to help: http://css-tricks.com/centering-in-the-unknown/
Basically, by using display: table-cell;, you can use vertical-align: middle; on your content.
Here's a JSFiddle updated to use this method:
http://jsfiddle.net/yVn7Z/
Updated CSS:
html, body {
margin: 0;
height: 100%;
width: 100%;
display: table;
}
.main {
height: 100%;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.main .submenu {
background: pink;
margin: 0 auto;
display: table;
}
.submenu ul {
margin: 0;
padding: 0;
list-style: none;
}
.submenu ul li {
display: table-cell;
}
.submenu ul li a {
background: rgba(0, 0, 0, 0.5);
padding: 8px;
display: block;
}
I'm working on the header of a website. I've looked around stackoverflow for instructions on how to center the header (includes logo & navigation bar).
I'm using Dreamweaver CC and when I click the preview button, it shows up on the browser centered, but the right has more white space than the left.
My current CSS:
.container {
width: 1000px;
margin-right: auto;
margin-left: auto;
text-align: center;
}
.header_left {
float: left;
width: 300px;
}
.navi {
float: right;
width: 600px;
}
.navi li {
list-style: none;
display: inline;
}
My Current HTML:
<body id="home">
<div id="header">
<div class="container">
<div class="header_left">
<img src="../images/bestfoodservicesweb_04.jpg" width="208" height="69"/>
</div>
<div class="header_right">
<ul class="navi">
<li><img src="../images/bestfoodservicesweb_07.jpg" width="88" height="56"/></li>
<li><img src="../images/bestfoodservicesweb_09.jpg" width="88" height="56"/></li>
<li><img src="../images/bestfoodservicesweb_11.jpg" width="88" height="56"></li>
<li><img src="../images/bestfoodservicesweb_13.jpg" width="88" height="56"></li>
</ul>
<div style="clear: both"></div>
</div>
</div>
EDIT: Sample of what it looks like
Trying to understand the problem. The header as a whole is centered. The elements inside have margin issues due to specifying width on the images and then giving the class a different width as well. You can remove the width in the class and it will push each floated element flush to the their specified sides. Then add margin to push them the distance you would like:
body, html {
width: 100%;
height: auto;
margin: 0;
}
.container {
background: #333;
width: 1000px;
margin: auto;
text-align: center;
}
.header_left {
float: left;
margin-left: 70px;
margin-top: 12px;
}
.navi {
float: right;
margin-right: 60px;
}
.navi li {
list-style: none;
display: inline;
}
http://jsfiddle.net/derekstory/zz2Dy/3/
text-align:center and float don't make good friends :)
test this : setting ul as inline-block element and not floatting: http://jsfiddle.net/zz2Dy/2/
.container {
width: 1000px;
margin-right: auto;
margin-left: auto;
text-align: center;
background:#333;
}
.header_left {
float: left;
}
.navi {
display:inline-block;
padding:0;
margin:0;
}
.navi li {
list-style: none;
display: inline;
}
The right header element has the property text-align: center, and it doesn't occupy the entire width of the element, so it ends up with more white space on the right. If you add to your fiddle the class:
.header_right {
text-align: right;
}
That should remove the white space on the right.
(if I understood your issue properly)
I believe this is what you are looking for:
.container {
margin: 0 auto;
text-align: center;
}
.header_left {
display: inline-block;
}
.header_right {
display: inline-block;
}
.navi {
display: inline-block;
padding: 0;
}
.navi li {
list-style: none;
display: inline;
}
Demo
Basically, I've removed floats and widths and padding, used display: inline-block;.
<img src="../images/bestfoodservicesweb_07.jpg" style=" display: block;
margin-left: auto;
margin-right: auto;"/>
I am having difficulty with centering the navigation bar on this page.
I tried nav { margin: 0 auto; } and a bunch of other ways, but I still can't center it.
#nav ul {
display: inline-block;
list-style-type: none;
}
It should work, I tested it in your site.
Add some CSS:
div#nav{
text-align: center;
}
div#nav ul{
display: inline-block;
}
If you have your navigation <ul> with class #nav
Then you need to put that <ul> item within a div container. Make your div container the 100% width. and set the text-align: element to center in the div container. Then in your <ul> set that class to have 3 particular elements: text-align:center; position: relative; and display: inline-block;
that should center it.
Just add :
*{
margin: 0;
padding: 0;
}
nav{
margin: 0 auto;
text-align: center;
}
The best way to fix it I have looked for the code or trick how to center nav menu and found the real solutions it works for all browsers and for my friends ;)
Here is how I have done:
body {
margin: 0;
padding: 0;
}
div maincontainer {
margin: 0 auto;
width: ___px;
text-align: center;
}
ul {
margin: 0;
padding: 0;
}
ul li {
margin-left: auto;
margin-right: auto;
}
and do not forget to set doctype html5
Your nav div is actually centered correctly. But the ul inside is not. Give the ul a specific width and center that as well.
You could also use float and inline-block to center your nav like the following:
nav li {
float: left;
}
nav {
display: inline-block;
}