CSS Center Element in 3 Column Layout - html

So I have a 3-column layout on my webpage, but I can't get the things in the middle column to be centered. The columns on the left and right are of fixed width, so I created a container for the middle column and set its borders to equal the size of the left and right columns. Then, I used the margin:auto property on the div tag inside the middle container that has everything that I want in the middle column. If possible, I want this to work on all browser window sizes. This is my CSS:
#top
{
width:100%;
background-color:#FF0000;
height:30px;
overflow: auto;
width: 100%
}
#right
{
float:right;
width:100px;
background-color:#CCC;
height:100%;
}
#middleCont
{
margin-left:150px;
margin-right:100px;
}
#middle
{
margin:auto;
text-align:left;
}
#left
{
float:left;
width:150px;
height:100%;
}
And since I can't post html without the browser rendering it for some reason, I uploaded the relevant code in a text file: http://www.mediafire.com/?a89kr1bb4uwb4cf
Thanks in advance for the help.

Yes, for one, under #middle, you said text-align: left rather than text-align: center. The instant I changed this, it worked fine.
I inserted different types of tags into the document, including p, div, blockquote, a list, a table, and an image. The only problem I noticed is that the table did not center. Looking at a number of articles, I found that you had already implemented the recommended way of centering a table in CSS. I think that maybe, the only solution to this problem is either to make a table using the center tag or create a new block of text in your CSS file.
You can do what you like, but the recommended way is this:
#center {
margin: auto;
}
Then make a new table with #center as its ID.
<table id="center">
...
</table>
If you choose to use the center tag, you implement it like this:
<center>
<table>
...
</table>
</center>
Hope I helped a little. Good luck.

I do it this way. Working even for internet explorer 6.
<table border="0" cellspacing="0" cellpadding="0" width="100%" style="width:100.0%;">
<tr>
<td width="50%" valign="top" style="width:50.0%;">
</td>
<td valign="top" style="width:900px;">
<div style="width:900px; border:1px solid white;"></div>
</td>
<td width="50%" valign="top" style="width:50.0%;">
</td>
</tr>
</table>

I suppose you can change the text-align:left to text-align:center for the selector #middle
If you want to use the method margin:0 auto for the #middle (and not margin:auto as you have it) then you have to set a fixed width, something like width:400px; or width:20%;.

If you want a fixed width center column then
#middle
/*this will keep the column width constant wile letting the space on either side expand*/
{
margin:auto;
text-align:left;
width: 600px; /* what ever width you want */
}
or specify fixed margins
#middle
/*this wil keep the space on either side constant wile letting the column expand*/
{
margin-left: 150px; /*to center column just keep the left and right margins equal*/
margin-right: 150px;
text-align:left;
}
I hope that answers your question.
P.S. It would have helped if you had posted the html too.

I am not sure if I follow exactly what you are trying to do - but what about if you use percentages rather than pixels for the width of all four divs?
#top
{
width:100%;
background-color:#FF0000;
height:30px;
overflow: auto;
width: 100%
}
#right
{
float:right;
width:25%;
background-color:#CCC;
height:100%;
}
#middleCont
{
margin-left:36%;
margin-right:26%;
}
#middle
{
margin:auto;
text-align:left;
}
#left
{
float:left;
width:35%;
height:100%;
background-color:#CCC;
}

Related

Creating a responsive 'topbar'

I'm currently having difficulty creating a responsive 'topbar' while fiddling around.
I've created it normally and works fine when using a standard laptop sized display, but when I try to resize the screen down to a mobile size, i'm having trouble figuring out how to keep my content drifting from the view.
I've currently used 2 divs for the example.
top-bar-holder is a container div and top-bar is used to keep content to a certain max-width.
I've also used a two tuple table which i've just used images in each for this example. The problem can be seen by trying to resize the viewport the images are disappearing to the left instead of staying on the screen.
I've attached a fiddle for better explanation.
Please ask if i've not been detailed enough in this question.
.top-bar-holder {
position:fixed;
width:100%;
height:70px;
background-color:#384452;
z-index:1;
}
.top-bar {
position:relative;
max-width:1100px;
width:100%;
border:1px solid orange;
height:70px;
left:50%;
margin-left:-550px;
}
.top-bar table tr th {
border:1px solid green;
height:65px;
max-width:50%;
}
<div class="top-bar-holder">
<div class="top-bar">
<table style="width:100%">
<tr>
<th> <img src="company-logo.png" width:"120" height="55" /> </th>
<th> <img src="company-logo.png" width:"120" height="55" /> </th>
</tr>
</table>
</div>
</div>
before I answer - you should avoid using tables for layout (if you have questions about this a quick google search will do good)
your positioning of .top-bar is causing the issue
.top-bar {
left:50%;
margin-left:-550px;
}
This is your current css - margin-left will always stay at 550px. If your display is less than that you end up with no content. Generally, the agreed on way to horizontally center things is by setting the horizontal margins to auto.
change the css above to this
.top-bar {
margin:0 auto;
}
This is the same as using
margin-top: 0;
margin-bottom: 0;
margin-left: auto;
margin-right: auto;
and will center your header content the way I think you intended; as well as fixing your screen issue

Overlay Fixed Size Form Field Over Fixed Size Image

I have an image that fixes itself to 100% of the page width. It stays the same size (on the screen) no matter what resizing the user does in their browser.
I now need to add a single Enter Your Name form field on top of the image. However, I want the text and form field sizing and position to stay the same no matter what the user does to resize their browser window. This is important because I need to overlay the form field table just perfectly on top of the image or else it will look bad.
Here's my current CSS:
.fixedimage img{
width:100% !important;
height:auto;
display:block;
}
Here's my current HTML:
<TABLE WIDTH="100%" CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD WIDTH="100%">
<div class="fixedimage">
<center><img src="FixedImage.jpg"></center>
</div>
</TD>
</TR>
</TABLE>
How would I overlay the table in a particular spot on top of the FixedImage.jpg? And how would I keep it being the same size when a user resizes or zooms in/out in their browser window?
Thanks for your help with this!!!
See this fiddle:
http://jsfiddle.net/nV7tP/1/
CSS:
.fixedimage{
width:100% !important;
height:auto;
display:block;
background:url(http://www.hdwallpaperspick.com/wp-content/uploads/2013/06/beautiful-autumn-scenery-723-2.jpg);
background-position:50% 50%;
background-size:cover;
}
.fixedimage form{
width:100%;
color:white;
font-size:40px;
text-align:center;
}
.fixedimage form input{
width:250px;
height:30px;
}
Response to the comment below:
Try this fiddle to show the entire background image:
http://jsfiddle.net/nV7tP/2/
.fixedimage{
position:absolute;
width:100% !important;
height:auto;
display:block;
font-size:40px;
}
.fixedimage img{
position:absolute;
}
.fixedimage form{
position:absolute;
width:100%;
color:white;
text-align:center;
}
.fixedimage form input{
width:250px;
height:30px;
}
Note : It would be better if you use the CSS of first posted fiddle.Its the type of CSS mostly used in present popular sites.
Set the image width to 100% .This will give the output you asked:
See this fiddle: http://jsfiddle.net/nV7tP/5/
or if you want a container with minimum width see this http://jsfiddle.net/nV7tP/6/ .

Overflow DIV content without fix height

I have two divs: left and right. In the left there is a long text. In the right there are some annotations about the text (more divs). If the text of the left is longer than the annotations I'm like it. But when the annotations are bigger/longer then the left div, I want to make the right div's content overflow.
With other words: two divs without fix height, make overflow the right one.
The code is above or JSFiddle
<div id="container">
<div id="left">Some long-long text, allways to show</div>
<div id="right">Some divs not necessarily show all</div>
</div>
css:
#container {
background-color:white;
float:left;
}
#left {
width: 79%;
float:left;
}
#right {
width: 19%;
float:right;
overflow: hidden;
}
But it's not working. :(
As Jan suggested in his last comment, I think you need to use javascript or jQuery to accomplish this.
This question outlines an approach using javascript that was accepted by the OP, though the OP made no comments on his process of execution.
I've modified a js fiddle from this answer to a similar question.
It uses the following:
CSS
#main{
width:auto;
}
#one{
height:auto;
width:200px;
display:inline-block;
float:left;
}
#two{
height:100%;
width:200px;
float:left;
display:inline-block;
overflow: auto;
}
div{
border:1px solid black;
}
Javascript
$(document).ready(function() {
$("#main").css("height",$("#one").height());
});
And I believe addresses your desired outcome.
You have to use overflow: hidden on #left, and not on #right.

How to center a table of the screen (vertically and horizontally)

I have these code block:
<table border="1px">
<tr>
<td>
my content
</td>
</tr>
</table>
I'd like to show my table in the center of the screen (vertically and horizontally).
Here is a demo.
How can I do that?
Horizontal centering is easy. You just need to set both margins to "auto":
table {
margin-left: auto;
margin-right: auto;
}
Vertical centering usually is achieved by setting the parent element display type to table-cell and using vertical-align property. Assuming you have a <div class="wrapper"> around your table:
.wrapper {
display: table-cell;
vertical-align: middle;
}
More detailed information may be found on http://www.w3.org/Style/Examples/007/center
If you need support for older versions of Internet Explorer (I do not know what works in what version of this strange and rarely used browser ;-) ) then you may want to search the web for more information, like: http://www.jakpsatweb.cz/css/css-vertical-center-solution.html (just a first hit, which seems to mention IE)
This fixes the box dead center on the screen:
HTML
<table class="box" border="1px">
<tr>
<td>
my content
</td>
</tr>
</table>
CSS
.box {
width:300px;
height:300px;
background-color:#d9d9d9;
position:fixed;
margin-left:-150px; /* half of width */
margin-top:-150px; /* half of height */
top:50%;
left:50%;
}
View Results
http://jsfiddle.net/bukov/wJ7dY/168/
I think this should do the trick:
<table border="1px" align="center">
According to http://w3schools.com/tags/tag_table.asp this is deprecated, but try it. If it does not work, go for styles, as mentioned on the site.
For horizontal alignment (No CSS)
Just insert an align attribute inside the table tag
<table align="center"></table
I've been using this little cheat for a while now. You might enjoy it. nest the table you want to center in another table:
<table height=100% width=100%>
<td align=center valign=center>
(add your table here)
</td>
</table>
the align and valign put the table exactly in the middle of the screen, no matter what else is going on.
One way to center any element of unknown height and width both horizontally and vertically:
table {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
See Example
Alternatively, use a flex container:
.parent-element {
display: flex;
justify-content: center;
align-items: center;
}
This guy had the magic wand we were looking for, guys.
To quote his answer:
just add "position:fixed" and it will keep it in view even if you scroll down. see it at http://jsfiddle.net/XEUbc/1/
#mydiv {
position:fixed;
top: 50%;
left: 50%;
width:30em;
height:18em;
margin-top: -9em; /*set to a negative number 1/2 of your height*/
margin-left: -15em; /*set to a negative number 1/2 of your width*/
border: 1px solid #ccc;
background-color: #f3f3f3;
}
I tried above align attribute in HTML5. It is not supported. Also I tried flex-align and vertival-align with style attributes. Still not able to place TABLE in center of screen.
The following style place table in center horizontally.
style="margin:auto;"

CSS Layout Question

I'm having trouble defining the CSS styles necessary in order to achieve the following layout:
Ideally, I'd like to have the left two divs be of width 200px. div#image will always have a height of 100px. However, I would like div#sidebar and div#mainContent to have lower borders which lie on the same horizontal level. Their sizes should be large enough to contain their respective content, which is determined when the page is being served. Hence, the one with more content will cause the other div to extend down to the same distance.
The problem is that with absolute positioning, the div#sidebar and div#mainContent elements don't seem to acknowledge the flow of their child elements. Perhaps I don't fully understand absolute positioning. Also, it seems like bad form to use Javascript in order to set the inline style of elements on the page. Is there a way of accomplishing this solely with CSS?
I've also tried floating the div#image and div#sidebar, and setting a margin-left property on div#mainContent, but wasn't able to get it to work...
Any help will be much appreciated!
Andrew
demo: http://jsfiddle.net/TRa35/
html
<div id="wrapper">
<div id="div-image">div image</div>
<div id="div-maincontent">
<div id="div-sidebar">
div sidebar
</div>
div maincontent
<button>click to add content</button>
<br />
<span></span>
</div>
</div>
css
html, body {
margin:0;
padding:0;
}
#wrapper {
position:relative;
}
#div-image {
position:absolute;
left:0;
top:0;
width:200px;
height:100px;
background-color:#cef;
}
#div-sidebar {
position:absolute;
left:-200px;
top:100px;
bottom:0;
width:200px;
background-color:#efc;
}
#div-maincontent {
position:absolute;
left:200px;
right:0;
top:0;
background-color:#fce;
min-height:300px;
}
This almost solves the problem. In fact, to be more precise, it does solve the problem in Google Chrome and Firefox, but IE 9 seems to have problems recognizing the height of cells and/or rows. I can't really mark it as an answer because of this, but I'm just posting it in case anyone can use something from it. It uses an html table element.
CSS:
#mainContentCell
{
background-color: Blue;
}
#imageCell
{
width: 200px;
height: 100px;
background-color: Yellow;
}
#sidebarCell
{
background-color: Red;
}
HTML:
<table id="layoutTable" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td id="imageCell">
Image
</td>
<td id="mainContentCell" rowspan="2">
Main Content
</td>
</tr>
<tr>
<td id="sidebarCell">
Sidebar
</td>
</tr>
</table>
Also, if anyone can make this work in IE 9, I'll gladly mark their response as the answer.
Note: This has been abandoned as "unsolvable", a reasonable answer is given below, but the original problem question remains without a definite solution.
JS Fiddle: http://jsfiddle.net/steve/gHrce/
Does almost everything, may help put you on the right track.
CSS:
#img {
background:green;
float:left;
height:100px;
width:200px;
}
#sidebar {
background:red;
clear:both;
float:left;
width:200px;
}
#mainContent {
background:yellow;
margin-left:200px;
}
HTML:
<div id='img'>
IMG
</div>
<div id='sidebar'>
SIDEBAR
<br />
<br />
<br />
</div>
<div id='mainContent'>
MAIN CONTENT
<br style='clear:both' />
</div>
That extra <br /> at the bottom forces the height of the main content to whatever the sidebar height is.
The <br /> tags in the sidebar are just to provide some extra height for demonstration purposes.
Of course, you can add Javascript pretty easily to expand the sidebar's height, but it smells strongly of hack:
if($('mainContent').offsetHeight > $('sidebar').offsetHeight + 100) {
$('sidebar').style.height = $('mainContent').offsetHeight - 100 + 'px';
}
#Andrew
for my suggestion, you should use 960 grid system CSS. pls check on this link http://960.gs/
I think you may more easier to develop and maintain.
Whew! After searching tons of articles. I hope this could help you.
<style type="text/css">
#wrapper{
background:blue;
position:relative;
min-height:400px;
}
#img{
background:green;
position:absolute;
left:0;
height:100px;
width:200px;
}
#some-panel{
background:orange;
position:absolute;
width:200px;
top:100px;
left:0;
bottom:0;
}
#main-content{
background:yellow;
margin-left:200px;
}
</style>
<div id="wrapper">
<div id="img">img img</div>
<div id="some-panel">some panel</div>
<div id="main-content">
main content main
</div>
</div>