Usual CSS centering issue, just not working for me, the problem is that I don't know the finished width px
I have a div for the entire nav and then each button inside, they dont center anymore when there is more than one button. :(
.nav {
margin-top: 167px;
width: 1024px;
height: 34px;
}
.nav_button {
height: 34px;
margin: 0 auto;
margin-right: 10px;
float: left;
}
<div class="nav">
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Home</div>
<div class="b_right"></div>
</div>
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Contact Us</div>
<div class="b_right"></div>
</div>
</div>
Any help would be greatly appreciated. Thanks
Result
If the width is unknown, I did find a way a center the buttons, not entirely happy but doesnt matter, it works :D
The best way is to put it in a table
<table class="nav" align="center">
<tr>
<td>
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Home</div>
<div class="b_right"></div>
</div>
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Contact Us</div>
<div class="b_right"></div>
</div>
</td>
</tr>
</table>
I stumbled across this problem today and I got it to work with
<div style="text-align:center;">
<button>button1</button>
<button>button2</button>
</div>
Consider adding this to your CSS to resolve the problem:
button {
margin: 0 auto;
display: block;
}
Another nice option is to use :
width: 40%;
margin-left: 30%;
margin-right: 30%
The problem is with the following CSS line on .nav_button:
margin: 0 auto;
That would only work if you had one button, that's why they're off-centered when there are more than one nav_button divs.
If you want all your buttons centered nest the nav_buttons in another div:
<div class="nav">
<div class="centerButtons">
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Home</div>
<div class="b_right"></div>
</div>
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Contact Us</div>
<div class="b_right"></div>
</div>
</div>
</div>
And style it this way:
.nav{
margin-top:167px;
width:1024px;
height:34px;
}
/* Centers the div that nests the nav_buttons */
.centerButtons {
margin: 0 auto;
float: left;
}
.nav_button{
height:34px;
margin-right:10px;
float: left;
}
Consider adding this to your CSS to resolve the problem:
.btn {
width: 20%;
margin-left: 40%;
margin-right: 30%;
}
In HTML you can write,
<div id="btn">
<button>Click</button>
</div>
if you work with JS instead of CSS, you can write this to move the button to center.
const bu = document.getElementById("btn");
bu.style.cursor="pointer";
bu.style.justifyContent="center";
bu.style.display="flex";
when all else fails I just
<center> content </center>
I know its not "up to standards" any more, but if it works it works
Related
In this fiddle : http://jsfiddle.net/H4F8H/16/
I'm attempting to center two divs by wrapping an outer div and centering it :
<div style="margin-left:auto;margin-right:auto;">
But the divs are remaining left aligned. How can I center these divs on page ?
fiddle code :
HTML :
<div style="margin-left:auto;margin-right:auto;">
<div id="block">
<img height="50" style="max-width: 50px;background-position: top left;" src="http://socialmediababe.com/wp-content/uploads/2010/12/administrator.jpg" />
<div style="font-size:20px;font-weight:bold;">
Test
</div>
<div>
Google
</div>
</div>
<div id="block">
<img height="50" style="max-width: 50px;background-position: top left;" src="http://socialmediababe.com/wp-content/uploads/2010/12/administrator.jpg" />
<div style="font-size:20px;font-weight:bold;">
Test
</div>
<div>
Google
</div>
</div>
</div>
CSS :
*{
margin:0;
padding:0;
}
#block {
margin-right:100px;
border-width: 2px;
border-color: #4682B4;
background-color: WHITE;
width: 100px;
text-align: center;
line-height:30px;
padding:3px 0;
float:left;
}
img{
float:left;
}
#block:hover {
background-color: #C2DFFF ;
}
div is a block level element by default so it will take up 100% of horizontal space if you do not assign some width to it, so you need to assign some width to your container
<div style="margin-left:auto;margin-right:auto; width: 300px;">
Here, you can just set the width accordingly. Also avoid using inline CSS.
Your CSS is lil sloppy, for example margin-right:100px; is not required, also, you can use shorthand like
margin: 0 auto; = margin-left:auto; margin-right:auto;
Demo (Added a red border just to show the boundaries)
Note: You are floating your elements, so make sure you clear your floats either by using <div style="clear: both;"></div> which I've already done in the demo provided, else you can also use the snippet below to self clear the parent like
.clear:after {
display: table;
clear: both;
content: "";
}
A couple things I want to point out in this post:
You have set Id="block" in two different instances. Id's are meant to be unique. If you want a reusable identifier you should be using classes.
Inline styling should be avoided when possible. In this case there is no need to set inline styling on the parent div.
There is more then one way to center div's
I am going to leave this link here: http://thenewcode.com/723/Seven-Ways-of-Centering-With-CSS
This would be my solution:
html:
<div class="container">
<div class="block">
<span>Test</span>
</div>
<div class="block">
<span>Test 2</span>
</div>
</div>
css:
.container {
display: flex;
justify-content: center;
align-items: center;
}
.block {
display: flex;
background: grey;
width: 30%;
height: 200px;
border: 1px solid #777;
margin: 5px;
}
Give a width to that container.
#outerdiv{
margin-left:auto;margin-right:auto;
width:500px;
}
<div align="center">
<!-- -staff ->
</div>
margin:auto; doesn't work unless the width is specified...
<div style="margin:auto;width:100px;">
your content here. [Replace the width with your choice]
</div>
Giving width and margin auto will centralise the content in specified width.
<div style="margin-left:auto;margin-right:auto;width:400px;">//give variable width here..Normally 1000 to 1018..
<div id="block">
<img height="50" style="max-width: 50px;background-position: top left;" src="http://socialmediababe.com/wp-content/uploads/2010/12/administrator.jpg" />
<div style="font-size:20px;font-weight:bold;">
Test
</div>
<div>
Google
</div>
</div>
<div id="block">
<img height="50" style="max-width: 50px;background-position: top left;" src="http://socialmediababe.com/wp-content/uploads/2010/12/administrator.jpg" />
<div style="font-size:20px;font-weight:bold;">
Test
</div>
<div>
Google
</div>
</div>
</div>
Like this
DEMO
CSS
.container{
width:960px;
margin:0 auto;
text-align:center;
border:1px solid red;
}
I have floated two elements right in the header of my website which are <div id="twitter"> and <div id ="navbar>, but there appearance becomes skewed when I view them in IE6 and IE7. I believe that I either need to clear the floated elements or apply a clearfix but I am unsure as to where.
here is an image of the issue in IE6 and IE7:
This is the desired result as it would appear in modern browsers.
Here is a link to the web page: http://www.bestcastleintown.co.uk/pg/
CSS:
#twitter {
background: red;
float: right;
height: 40px;
margin-bottom: 40px;
width: 200px;
}
#navbar {
font-size: 2.2em;
float:right;
}
HTML:
<div id="main_header">
<div id="inner_main_header">
<div>
<div id="main_logo">
<div class="home_page_logo left">
<img src="PG_awards_logo.gif">
</div>
</div>
<div>
<div id ="twitter" class="padall">
Follow us
</div>
<div id ="navbar" class="right">
<ul class="nav NG">
<li>home</li>
<li>enter</li>
<li>categories</li>
<li>judging</li>
<li>sponsorship</li>
<li>contact</li>
</ul>
</div>
</div>
</div>
</div>
</div>
Change #navbar style to this:
#navbar{
font-size: 2.2em;
float: right;
width: 60%;
overflow: auto;
}
Just give the #navbar some width and overflow other than visible.
Currently having issues trying to get these bordered text boxed to stay aligned with the above images in a way that when I re-size the page they stick with their prospective images. Currently I have them forced to stick under the images with margin css but once I re-size the boxes, the boxes all flee under the images.
EDIT: Updated code - still having similar issues but this looks more promising - attaching photo of current state.
<body>
<div id="container">
<div id="header">
<img class="leaf" src="images/freshleaf.png" height="150px" alt="freshtext"/>
<!--<button class="navbutton"> <strong> TEST </strong> </button>-->
</div>
<div id="body">
<div id="main" class="">
<div class="column">
<img class="smart" src="images/phone1.png" height="500px" alt="phone1"/>
<div class="box1"> <h3>Pie is tasty Mmmmm...</h3> </div>
</div>
<div class="column">
<img class="smart" src="images/phone2.png" height="500px" alt="phone2"/>
<div class="box2"> <h3>Pie is tasty Mmmmm...</h3> </div>
</div>
<div class="column">
<img class="smart" src="images/phone3.png" height="500px" alt="phone3"/>
<div class="box3"> <h3>Pie is tasty Mmmmm...</h3> </div>
</div>
</div>
</div>
</div>
#body{
text-align: center;
}
#header {
padding-bottom: 50px;
text-align: center;
}
#main{
margin: 0 auto;
text-align:left;
width: 770px;
}
.column{
float:left;
padding-left: 10px;
}
.column:first-child {
padding-left: 0px;
}
.box1{
border: dotted;
border-color: gray;
padding: 2px 5px 2px 5px;
max-width: 250px;
text-align: center;
}
img.smart{
margin-left: auto;
margin-right: auto;
}
wrap each image and the coresponding text in one container and set float:left in the css. get rid of the #textboxes div. then center your #main div by setting it to the width of all the images (plus padding - if desired).
see example fiddle with all the code:
http://jsfiddle.net/QLvt7/4/
I'm trying to learn how to put table content into css floats.
My approach has been to use a div at the "table" level, at the "row" level, and the "cell" level. Not sure this is a good strategy.
Anyway, when I set a background style at the "table" or "cell" level I can see the color change. When I set it at the row level it stays white.
Any guesses what's going on? Is there a better way to do this?
<h2>"Tables" work</h2>
<div style="width: 455px; background-color:#a4c4fc">
<div>
<div style="width: 70px; float: left">ID</div>
<div style="width: 220px; float: left">Lemons</div>
<div style="width: 50px; float: left">Horseradish</div>
</div>
<br clear: both>
<div>
<div style="width: 70px; float: left">1<LEFT></div>
<div style="width: 220px; float: left">3</div>
<div style="width: 50px; float: left">4</div>
</div>
</div>
<br>
<h2>"Row" divs do not seem to work</h2>
<div style="width: 455px">
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left">ID</div>
<div style="width: 220px; float: left">Lemons</div>
<div style="width: 50px; float: left">Horseradish</div>
</div>
<br clear: both>
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left">0</div>
<div style="width: 220px; float: left">0</div>
<div style="width: 50px; float: left">1</div>
</div>
</div>
<br>
<h2>Individual cell divs work</h2>
<div style="width: 455px">
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left; background-color:#a4c4fc">ID</div>
<div style="width: 220px; float: left; background-color:#a4c4fc">Lemons</div>
<div style="width: 50px; float: left; background-color:#a4c4fc">Horseradish</div>
</div>
<br clear: both>
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left; background-color:#a4c4fc">0</div>
<div style="width: 220px; float: left; background-color:#a4c4fc">0</div>
<div style="width: 50px; float: left; background-color:#a4c4fc">1</div>
</div>
</div>
This looks like tabular data.
If it is, you should use a HTML table element to display this information.
Too many people make the mistake of hearing "tables are bad", and try and do work arounds using div's, even when tables are most appropriate.
When people say you shouldn't use tables, they are referring to layout structure. It is still fine to use it in this case, they are not evil!
Then you'll be able to do:
<tr style="background-color:#a4c4fc;">
</tr>
If you are certain on using div's your issue is because the "cell" divs are all float:left;. Therefore your "row" div doesn't have a height.
You could add the following to the bottom of the "cell"'s to fix this:
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left">ID</div>
<div style="width: 220px; float: left">Lemons</div>
<div style="width: 50px; float: left">Horseradish</div>
<div style="clear:both;overflow:hidden;height:0;"></div>
</div>
Alternatively, use display:inline-block;
<div style="background-color:#a4c4fc">
<div style="width: 70px; display:inline-block;">ID</div>
<div style="width: 220px; display:inline-block;">Lemons</div>
<div style="width: 50px; display:inline-block;">Horseradish</div>
</div>
http://jsfiddle.net/hRCWk/2/
But don't do that, use a table :)
Clear your floats. Add overflow:hidden; to your row div.
Also, <br clear: both> makes no sense. Remove it or use <div style="clear:both;"></div>
Demo: http://jsfiddle.net/RX8Pq/
The row element isn't stretching because all the sub elements are floated:
http://www.quirksmode.org/css/clearing.html
Instead of floating you DIV elements you could set them to display as inline blocks...
display:inline-block;
then you dont need to clear the DIV floats afterwards.
Of course, you should just use a table anyway
NOTE: Apparently this will not work in IE8 and earlier versions.
I have the following code:
<div class="clearfix">
<div style="float: left; padding-right: 1%;">
<label class="adm">Created</label>
</div>
<div style="float: left; padding-right: 1%;">
<label class="adm">Created</label>
</div>
</div>
<div class="mdl_ftr"></div>
.clearfix:after{
clear: both;
bdy: ".";
display: block;
height: 0;
visibility: hidden;
}
.mdl_ftr {
min-height: 69px;
}
.mdl_ftr {
background: Red;
min-height: 45px;
}
and an example fiddle
I would like the background color of mdl_ftr to start AFTER the labels. Is there a simple way that I can make this happen. Right now the mdl_ftr DIV starts right at the top left corner of the first label. What I want is it to follow the labels and not appear as a background to them.
Help would be much appreciated
It is common, when using floating elements, to have another element to clear content in the layout. Something like :
<div class="clearfix">
<div style="float: left; padding-right: 1%;">
<label class="adm">Created</label>
</div>
<div style="float: left; padding-right: 1%;">
<label class="adm">Created</label>
</div>
</div>
<div class="clearfloat"></div>
<div class="mdl_ftr"></div>
And simply apply the CSS rule to this element like
.clearfloat { clear: both; }
Please never forget overflow:hidden again!!
Replace your full CSS with:
.clearfix {
overflow:hidden
}
.mdl_ftr {
background:red;
min-height:45px
}
Or even better in this case:
HTML:
<div style="float:left; padding-right:1%">
<label class="adm">Created</label>
</div>
<div style="float:left; padding-right:1%">
<label class="adm">Created</label>
</div>
<div class="mdl_ftr"></div>
CSS:
.mdl_ftr {
background:red;
min-height:45px;
clear:both
}
And perhaps use an ID for your footer instead of a class. (I guess you have only one footer on your page?)
If you mean after as in to the right of the labels, then put your mdl_ftr inside of your clearfix and float:left;; otherwise, set a height to your clearfix
Yea its pretty easy. i put a div.clear at the
end of your div.clearfix. And added this to your
css code. .clear { clear: both; height: 1%; }
<div class="clearfix">
<div style="float: left; padding-right: 1%;">
<label class="adm">Created</label>
</div>
<div style="float: left; padding-right: 1%;">
<label class="adm">Created</label>
</div>
<div class="clear"></div>
</div>
<div class="mdl_ftr"></div>