Center contents of fluid middle div in three column layout - css

I cannot for the life of me get the contents of the center div to align no matter what I try:
HTML
<div class="content-controls">
<div class="content-controls-left">
123
</div>
<div class="content-controls-middle">
<img src="logo.png">
</div>
<div class="content-controls-right">
456
</div>
</div>
CSS
.content-controls {
width:100%;
}
.content-controls-left {
float:left;
width:50px;
}
.content-controls-right {
float:right;
width:35px;
}
.content-controls-middle {
float:left;
margin:auto 0;
}
What I would like to happen is that the image in the middle column is always centred in content-controls-middle - I have tried suggestions in:
CSS: center element within a <div> element
How do I put 3 div's side by side and center the content in each one
Three DIVs next to each other with fluid horizontal width
Align image in center and middle within div
and more but all to no avail - what am I doing wrong?
http://jsfiddle.net/8VS5U/

You'd be better off using a table layout if your middle cell will only contain inline content (which an image is):
/* Set container to display as a table. */
.content-controls {
display: table;
table-layout: fixed;
width:100%;
}
/* Set div elements within container to display as table cells. */
.content-controls div {
display: table-cell;
}
.content-controls-left {
width:50px;
}
.content-controls-right {
width: 35px;
}
/* Centrally align middle cell content. */
.content-controls-middle {
text-align: center;
}
JSFiddle demo.

You can change your CSS like this, it will allow the middle div to take the remainer space so you can just text-align the content.
CSS:
.content-controls {
width:100%;
}
.content-controls-left {
float:left;
width:50px;
}
.content-controls-right {
float:right;
width:35px;
}
.content-controls-middle {
text-align: center;
}
And you must change the HTML like so:
HTML:
<div class="content-controls">
<div class="content-controls-left">123</div>
<div class="content-controls-right">456</div>
<div class="content-controls-middle">ABC</div>
</div>
DEMO HERE

Try this: margin-left:40%; on your .content-controls-middle.
http://jsfiddle.net/8VS5U/7/
As James points out, if you make the window smaller, it won't be central. But this is good enough if you are not making a responsive website. I would prefer James' answer if you want a solid approach, and mine if you want a hacky way to do it :)

Related

I want to keep three div in one row of different width one in left one in center and one in right

I want to keep three div in one row of different width one in left one in center and one in right.
Left div is of 160px
Center Div is of 640px
Right Div is of 160px
All i want is when they open in wide screen All will look separate on in left one in center one in right.
And When User Re-size Browser window or open in smaller resolution they come near to each other and do not collapse(means they acquire atleast 960px = 160 + 640 + 160)
As Far I done this with the help of StackOverflow & Google :
Html :
<div id="main">
<div id="leftDiv">left</div>
<div id="centerDiv">center</div>
<div id="rightDiv">right</div>
</div>
CSS:
#main {
overflow:hidden;
}
#main div {
width:33%;
float:left;
}
#leftDiv
{
width:160px;
}
}
#centerDiv {
text-align:center;
width:640px;
}
#rightDiv {
text-align:right;
width:160px;
}
If you can help me please provide me solution.
Thanks In Advance
Well, by using this approach you have to reorder the elements as:
<div id="main">
<div id="leftDiv">left</div>
<div id="rightDiv">right</div>
<div id="centerDiv">center</div>
</div>
You could keep the centerDiv element at the center of the layout by using auto value for the left/right margin.
Also you could set a minimum width to the container element (#main) in order to prevent collapsing the layout.
#main {
min-width: 960px; /* minimum width: 160px + 640px + 160px */
}
#leftDiv { float: left; width:160px; }
#rightDiv { float: right; width:160px; }
#centerDiv {
width:640px;
margin: 0 auto;
}
WORKING DEMO
For these i would suggest using twitter bootstrap, you just need to do this:
css:
#main {
min-width:960px;
}
/* these are optional, but you do want your columns to scale unless you have a max width */
#leftDiv, #rightDiv {
width: 160px;
}
#centerDiv {
width: 640px;
}
html:
<div id='main' class='row'>
<div class='col-md-2' id='leftDiv'></div>
<div class='col-md-8' id='centerDiv'></div>
<div class='col-md-2' id='rightDiv'></div>
</div>

Left right column divs

I have this HTML and css, I need to make them float next to each other and the right div should auto resized when the left div's with changes and the height of all should open fully on screen, how can I do that please help:
CSS:
.ne-divadmincontent
{
top: 15px;
width: 100%;
height:100%;
}
.ne-leftcontent
{
float:left;
}
.ne-rightcontent
{
float:left;
border-left:dotted 1px #CCC;
}
HTML:
<div class="divadmincontent">
<div class="ne-leftcontent">
l-content
</div>
<div class="ne-rightcontent">
r-content
</div>
</div>
If you don't specify the width of your two inner div tags (ne-leftcontent, ne-rightcontent) they will automatically receive the width of their parent div (divadmincontent)
Therefore a solution for your problem would be to assign some value to your inner div tags
.ne-leftcontent{
width:40% // 560px
float:left;
}
.ne-rightcontent{
width:60% // 400px
float:left;
}

Unable to center text between two images

I'm using the technique in Stack Overflow question CSS centering text between two images but am unable to make the text center.
I would like the text "0 of 0" centered in this markup (as a fiddle):
HTML:
<div id="invoiceImageContainer">
<img src="http://i.imgur.com/8QT8u.png" id="invoiceImage">
<div id="invoiceNav">
<img title="Next" src="http://i.imgur.com/oZb7r.png" id="nextInvoice">
<img title="Previous" src="http://i.imgur.com/aKi11.png" id="prevInvoice">
<span id="invoiceCount">0 of 0</span>
</div>
</div>
CSS:
#invoiceImageContainer{
width:420px;
margin: 0 auto;
}
#invoiceImage {
height:600px;
}
#invoiceNav {
color:black;
font-size:10pt;
}
#prevInvoice {
float:left;
padding-left:100px;
}
#nextInvoice {
float:right;
padding-right:100px;
}
#invoiceCount {
text-align:center;
}
What am I doing wrong?
You are using a span for the text container which is an inline element. Therefore its width is the same as the width required for its content, changing it to a p (or changing display to block) will allow for horizontal centering. If you want to center vertically then set the line-height equal to the height of the images and set vertical-align: middle.
Updated fiddle: http://jsfiddle.net/W5jQd/3/.
Good old quick hack:
#invoiceImageContainer{
width:420px;
margin: 0 auto;
}
#invoiceImage {
height:600px;
}
#invoiceNav {
color:black;
font-size:10pt;
text-align:center;
}
#prevInvoice {
float:left;
padding-left:100px;
}
#nextInvoice {
float:right;
padding-right:100px;
}
#invoiceCount
{
line-height: 35px;
}
In your original markup you had #invoiceCount set to text-align:center. This is wrong because you can't center spans in that way, so I moved it to your container div.
The quick hack is the line-height, set to approximately the known size of your image. This technique is good and safe when you are doing a single line of text and the size of the elements involved is known.
This will fix it:
/* should be block level element */
#invoiceCount{
display: block;
}
See here: http://jsfiddle.net/W5jQd/5/
You could add the following to the CSS of #invoiceNav
text-align: center;
line-height: 32px;
the first centers the <span> element and the second centers the text vertically in the <div> (same height as the images)
The problem is caused by the fact that doesn't support "width", since it's an inline block. The following changes will do the trick: in HTML change <span id="invoiceCount">0 of 0</span> to <div id="invoiceCount">0 of 0</div> and in CSS add width to the #invoiceCount like this:
#invoiceCount {
text-align:center;
width: 100%;
}

How do I give the same background color to all elements in a container?

Thanks for the very quick replies to my previous question.
"The first thing on the page, right after , I want a sort of banner, containing some text which is left aligned, and an image which is right aligned. It should occupy te full width of the page."
I forgot to mention that I would like the entire "banner" to have the same background colo(u)r. The text, the image and everything in between.
Something like this:
<div style="height:100px;width:100%;background:url(yourimage.png);background-position:right;">Yourtext</div>
.banner{ color:blue;
background-color:blue;
background:url(yourimage.png);
background-position:right;
width:100%
}
in addition, you can have create a new class so that the div for for banner can inherit the properties, this way you can seperate the div for the image and text
Something like this?
HTML
<div class="divwrap">
<div class="div1">text</div>
<div class="div2"><img src="http://farm4.static.flickr.com/3170/2724062433_68f2af7af7_m.jpg"></div>
<div class="divclear"></div>
</div>
CSS
.divwrap
{
background-color: #CCC;
}
.div1
{
float: left;
}
.div2
{
float: right;
}
.div2 img
{
display: block;
}
.divclear
{
clear: both;
}

DIVs anchored to top and bottom of parent div

This is probably a very dummy question, don't throw your shoes at me :)
Consider having HTML like this:
<div class="container">
<div class="header">
</div>
<div class="body">
</div>
<div class="footer">
</div>
</div>
I want 'header' and 'footer' to be anchored to the parent's top and bottom respectively, and 'body' to grow easily to fit all available space.
What would the CSS look like to achieve this?
EDIT: Maybe I'm saying this wrong (i'm not exactly a web developer :) ), but what I need is to have some part of a div always attached to its bottom. So when div grows this part (which might have a fixed size) would go lower with the div's lower end. But all this doesn't mean attaching a div to the bottom of browser's window.
If I understand your question correctly, you require some really basic css.
body { background: black; }
.container { width: 960px; }
.header { height: 100px; background: #ddd; }
.content { padding: 10px; }
.footer { height: 100px; background: #ddd; }
Your div's are not floated, so will stack on top of each other like pancakes.
If you want the footer to be "sticky", see here for a solution...
http://ryanfait.com/sticky-footer/
Here you go:
Example page - footer sticks to bottom
this will have the content right
between the footer and the header.
no overlapping.
HTML
<header>HEADER</header>
<article>
<p>some content here (might be very long)</p>
</article>
<footer>FOOTER</footer>
CSS
html{ height:100%; }
body{ min-height:100%; padding:0; margin:0; position:relative; }
body:after{
content:'';
display:block;
height:100px; // compensate Footer's height
}
header{ height:50px; }
footer{
position:absolute;
bottom:0;
width:100%;
height:100px; // height of your Footer (unfortunately it must be defined)
}
Try this: Set position: relative on the parent div. Set position: absolute on the inner div(s) and set both the top and the bottom properties; don't set height. The inner div(s) should stretch vertically with the parent, as required. (Doesn't work in IE6 and below unfortunately).

Resources