This question already has answers here:
How can I horizontally center an element?
(133 answers)
How do I center floated elements?
(12 answers)
How do I center list items inside a UL element?
(14 answers)
How to horizontally align ul to center of div?
(5 answers)
Closed 4 years ago.
I create a fiddle here: http://jsfiddle.net/WayDf/27/
How can I get the four white boxes which is contained within a ul to be horizontally centered within the red bar? I've tried many things to footer #social ul I just cannot figure it out. I have ready through the many posts on this topic and have tried many things which have been suggested before, none which I can get to work on my particular issue. What am I missing?
footer {
width: 100%;
position: relative;
background-color: #C5C5C5;
display: block;
overflow: hidden;
}
footer section#footer-content {
max-width: 1440px;
min-height: 50px;
margin: 30px auto;
padding: 0 .694444%;
/* 10px / 1440px */
position: relative;
display: block;
overflow: hidden;
}
footer #social {
height: 30px;
margin: 0 auto;
background-color: red;
width: 600px;
position: relative;
}
footer #social ul {
list-style: none;
width: 100%;
margin: 0 50%;
}
footer #social li {
float: left;
margin: 0 .3em;
position: relative;
display: block;
width: 30px;
height: 30px;
overflow: hidden;
}
footer #social a {
text-decoration: none;
color: #CC7D29;
background: #ffffff;
display: block;
text-align: center;
height: 30px;
}
<footer>
<section id="footer-content">
<div id="social">
<ul>
<li>
</li>
<li>
</li>
<li>
</li>
<li>
</li>
</ul>
</div>
</section>
</footer>
Remove the margin on the <ul> and replace that with text-align:center; and padding:0;. The on the list items, remove the float and padding, and change the display to inline-block.
footer {
width: 100%;
position: relative;
background-color: #C5C5C5;
display: block;
overflow: hidden;
}
footer section#footer-content {
max-width: 1440px;
min-height: 50px;
margin: 30px auto;
padding: 0 .694444%;
/* 10px / 1440px */
position: relative;
display: block;
overflow: hidden;
}
footer #social {
height: 30px;
margin: 0 auto;
background-color: red;
width: 600px;
position: relative;
}
footer #social ul {
list-style: none;
width: 100%;
xmargin: 0 50%;
text-align:center;
padding:0;
}
footer #social li {
xfloat: left;
margin: 0 .3em;
position: relative;
display: inline-block;
width: 30px;
height: 30px;
overflow: hidden;
}
footer #social a {
text-decoration: none;
color: #CC7D29;
background: #ffffff;
display: block;
text-align: center;
height: 30px;
}
<footer>
<section id="footer-content">
<div id="social">
<ul>
<li>
</li>
<li>
</li>
<li>
</li>
<li>
</li>
</ul>
</div>
</section>
</footer>
Related
the layout I'm trying to achieve
So Im trying to achieve a layout of header shown in the picture below using HTML and CSS. I've started with doing the header and put the logo in the centre and the list business but I'm struggling with putting the logo in the centre and the list business in the corner. I've tried using text align but it hasnt worked and im sure its the float but im not sure the code i should use.
Heres my code:
body {
margin: 0;
font-weight: 800;
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #ffe9e3;
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
text-align: center;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 23px;
position: relative;
}
<header>
<div class="container">
<h1 class="logo"><i>LOGO</i></h1>
<nav>
<ul>
<li>List Your Business</li>
</ul>
</nav>
</div>
</header>
Could make the header a flexbox and use justify and align to center the logo. Then make the nav absolute positioned to put it up in the top right corner.
body {
margin: 0;
font-weight: 800;
}
.container {
width: 80%;
height: 100%;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;
}
header {
background: #ffe9e3;
height: 100px;
}
.logo {
text-align: center;
margin: 0;
display: block;
}
nav {
position: absolute;
right: 0;
top: 0;
padding: 10px;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
}
<header>
<div class="container">
<h1 class="logo"><i>LOGO</i></h1>
<nav>
<ul>
<li>List Your Business</li>
</ul>
</nav>
</div>
</header>
There are a couple of ways you can achieve this, but without your actual source code it is hard to work with. If you aren't using flex or grid, then I would say to set the parent element (The header container) to position relative, and then set the logo container itself to position absolute. You can then change the positioning as shown.
#hContainer {
position: relative;
width: 100%;
height: 150px;
background: skyblue;
}
#logo {
position: absolute;
color: white;
font-size: 2em;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
border: 2px solid white;
padding: 10px 12px;
}
<div id="hContainer">
<div id="logo">Logo</div>
</div>
I'm trying to align all navigation links, besides the logo, to the right side of the container/navigation. I want to keep 1rem margin on both sides so that the content has some space to breathe.
I've tried using the code below but nothing on the page changes:
.menu:not(:first-child){
text-align: right;
}
<body>
<div class="body-wrap">
<header class="header">
<nav role="navigation">
<ul class="menu">
<li class="home-link"><img src="https://www.nicolefenton.com/_/images/dec/circle-menu.svg" height="12" width="12" alt=""></li>
<li>About</li>
<li>Writing</li>
<li>Speaking</li>
<li>Projects</li>
</ul>
</nav>
</header>
</div>
</body>
* { box-sizing: inherit; margin: 0; padding: 0; }
body {
position: relative;
line-height: 1.5em;
min-width: 320px;
margin: 0 auto;
color: #222222;
border: 30px solid #ffffff;
background-color: #f8f7f3;
}
.body-wrap {
display: flex;
min-height: 100vh;
display: box;
}
.header {
width: 100%;
max-width: 960px;
margin-right: 1rem;
margin-left: 1rem;
}
.menu {
display: flex;
position: absolute;
top: -0.83rem;
width: 100%;
max-width: 960px;
}
.menu:not(:first-child){
text-align: right;
}
li {
flex-grow: 1;
position: relative;
margin-right: 1em;
display: inline-block;
}
I expect all the nav links to align to the right when using the :not(:first-child) selector.
This:
.menu:not(:first-child)
selects class menu items that aren't a first child.
What you want is:
.menu :not(:first-child)
which selects non-first-child elements within a .menu class.
Notice the space.
Or better yet, make it more obvious what you really mean:
.menu li:not(:first-child)
You might just have to change to this if all you are looking to do is align the text to the right.
.menu li:not(:first-child){
text-align: right;
}
I am trying to center nav tag within the main-container div, but it seems to have a bit too much space on the right. I tried margin: 0 auto method as well as display: inline/inline-block and text-align on the child element - but it does not work.
body {
background: #000;
}
.main-container {
margin: 0 auto;
width: 90%;
overflow: hidden;
background: #fff;
}
.main-container nav {
width: 100%;
display: block;
margin: 10px 0;
}
.main-container nav ul {
list-style-type: none;
}
.main-container nav ul li {
width: 48%;
display: block;
float: left;
height: 300px;
margin: auto auto 10px 10px;
}
.main-container nav ul li.one {
background: url('http://media.npr.org/images/picture-show-flickr-promo.jpg');
background-size: cover;
overflow: hidden;
}
a {
display: block;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
text-align: center;
}
a span {
position: relative;
top: 40%;
font-size: 2em;
z-index: 1000;
}
<div class="main-container">
<nav>
<ul>
<li class="fruits"><span> One </span></li>
<li class="vegetables"><span> Two </span></li>
<li class="carbohydrates"><span> Three </span></li>
<li class="proteins"><span> Four </span></li>
<li class="junk-food"><span> Five </span></li>
<li class="health-tips"><span> Six </span></li>
</ul>
</nav>
</div>
Hi now used to this
.main-container nav ul{padding:0;}
or used to always css reset
Demo
The problem:
ul element has padding and margin by default. You have to set those property to 0.
Jsfiddle
.main-container nav ul {
list-style-type: none;
padding: 0;
margin: 0;
}
I've a div container which is named headline. In this div there are two elements, a menu bar of type unordered list and a div container. I'll centering horizontally the menu bar, the other div container should dock on the right display side with a margin of 5%. How I can do this, has someone an idea?
Okay here is my litte example from jsfiddle: http://jsfiddle.net/nchm3gyj/
HTML
<div class="headline">
<ul class="navbar">
<li>Home</li>
<li>Team</li>
<li>Info</li>
<li>Downloads</li>
</ul>
<img class="facebook" src="" />
</div>
CSS
* {
margin: 0px;
padding: 0px;
}
.headline {
height: 60px;
width: 100%;
background-color: black;
margin-top: 10px;
}
.headline .navbar{
margin: 0px;
padding: 0px;
padding-left: 10px;
padding-right: 10px;
float: left;
height: 60px;
width: auto;
background-color: yellow;
list-style: none;
}
.headline .navbar li{
display: inline;
}
.headline .navbar li a {
text-decoration: none;
line-height: 60px;
padding-left: 10px;
padding-right: 10px;
}
.headline .facebook {
width: 60px;
height: 60px;
margin-right: 5%;
float: right;
}
#clear {
clear: both;
}
If you want your navigation bar centered in the parent block, here is one way of doing it.
Apply display: inline-block to the .navbar and text-align: center to .headline.
Assuming that you want the navigation bar centered with respect to the full
width of the parent block, you need to take the image out of the content flow.
You can do this by applying position: absolute to the .facebook element.
.headline {
height: 60px;
width: 100%;
background-color: black;
margin-top: 10px;
text-align: center;
position: relative;
}
.headline .navbar{
margin: 0px;
padding: 0px;
padding-left: 10px;
padding-right: 10px;
height: 60px;
width: auto;
display: inline-block;
background-color: yellow;
list-style: none;
}
.headline .navbar li{
display: inline;
}
.headline .navbar li a {
text-decoration: none;
line-height: 60px;
padding-left: 10px;
padding-right: 10px;
}
.headline .facebook {
position: absolute;
top: 0;
right: 5%;
width: 60px;
height: 60px;
}
<div class="headline">
<ul class="navbar">
<li>Home</li>
<li>Team</li>
<li>Info</li>
<li>Downloads</li>
</ul>
<img class="facebook" src="http://placehold.it/60x60" />
</div>
I think you might need to position: absolute the facebook image and display: inline-block your menu bar (being centered by the .headline):
http://jsfiddle.net/nchm3gyj/32/
I'm a bit unsure of what you're trying to do, is this it? Applied text-align: center to .headline and display: inline-block to .navbar then position: absolute to .facebook?
http://jsfiddle.net/nchm3gyj/42/
I'm having some trouble with a fixed nav bar at the top of my page. It's supposed to be flush with the top of the page, but isn't. Here's my HTML:
<nav>
<a href="#">
<div id="logo">
lorem
</div></a>
</nav>
<ul>
*enough li's to go past the bottom of the screen*
</ul>
and my CSS:
body {
margin: 0px;
padding: 0px;
}
nav {
position: fixed;
display: block;
color: white;
margin: 0 auto;
padding: 0;
width: 100%;
height: 60px;
background-color: #4d4d4d;
}
#logo {
padding-left: 1%;
padding-right: 1%;
color: #75cc83;
width: 180px;
height: 100%;
background-color: #333333;
font-size: 3em;
font-family: candara, sans-serif;
}
It seems like there are only problems with the fixed nav once I put content in there (the list items, in this case)
Add top:0 to you nav's rules:
nav {
position: fixed;
display: block;
color: white;
margin: 0 auto;
padding: 0;
width: 100%;
height: 60px;
background-color: #4d4d4d;
top:0;
}
jsFiddle example