CSS - Img Perfectly Centered In Css Box
As a CSS beginner I want to change my few table based web sites to CSS based. Currently I may have a table with a border exactly fitting a graphic image... looks nice. Now I want that same image to fit perfectly in a CSS box in order to take advantage of all the extra features of color and border that comes with CSS.
The problem is... nothing that I do will exactly fit the rectangular image in the CSS box. I have fooled with the dimensions of the box and image but somehow there is ALWAYS about a 4px edge of background at the bottom edge of the image. The left,top,right borders are exactly bordering the image as should be. Is this possible in CSS, I would like to follow the CSS way. #bottom_center { margin: 0px; padding: 0px; background: #ccc; width: 470px; height: 90px; border-style: ridge; } ---- then in the body ---- <div align="center"> <div id="bottom_center"> <img border="1" src="able_appraisal_web_logo.jpg" alt="Able Appraisal, Inc." width="470" height="90"> </div> </div> Similar TutorialsThe problem is fixed now Hello, In the last few days I made this website: http://www.gambiaguesthouse.nl/index.php?site=home I tested it with firefox and once a day i tested it with IE. Now the site works like a charm in Firefox but after the changes I made today It doesn't work under IE. The width of the site is not correct in IE. This is the CSS code of the website, if I remove the "#all..." part that I made bold the width is correct, but then the site gets very ugly. When I put the "#all..." part back the width of the site isn't correct in IE. De W3C CSS checker gives no errors I hope somebody can see what I do wrong, I am working to figure this out for 2 hours now and I can't find out what the problem is Hope you guys can tell me what I did wrong with my CSS code. Code: body { font-family: verdana; background-color: #bbdaf9; margin: 0; padding: 0; } #all { color: #062A4E; font-size: 14px; background-color: #e9f3fe; position: relative; width: 632px; min-height: 609px; margin: 7px auto 30px; padding: 202px 167px 10px 204px; } #head { position: absolute; top: 0; right: 0; width: 1003px; height: 187px; padding-top: 5px; background-image:url(img/titel.jpg); } #footer { position: absolute; top: -500px; } #con { background-color: #f3f7fb; width: 100%; min-height: 620px; } #add { position: absolute; top: 198px; padding-top: 4px; right: 0; width: 165px; height: 605px; } #menu { position: absolute; top: 198px; left: 0; padding-top: 4px; right: 0; width: 200px; height: 150px; } #login { position: absolute; font-size: 14px; top: 348px; left: 0; width: 190px; background-color: #e0eefc; border-style:solid; border-color:#f3f8fe;} #rotating { position: absolute; top: 470px; left: 0; padding-left: 5px; padding-top: 4px; right: 0; width: 200px; height: 350px; } a { color: #c40000; font-size: 10px; text-decoration: none; margin: 0; padding: 0; border-width: 0; cursor: pointer; } a:hover { color: #c40000; } .whitetb { color: white; } img { margin: 0; padding: 0; border-width: 0; } li { list-style:none; float: left; text-align: center; border-style:solid; border-color:#f3f8fe; } ul { border-top: 0px solid #FFF; width: 195px; height: 145px; background: #e0eefc; margin: 0; padding: 0; } a.menuitem { padding-top: 3px; font-size: 16px; color: #062A4E; text-decoration: none; font-weight: bold; display: block; width: 190px; height: 27px; } a.menuitem:hover { background: #DCEFF8; color: #116086; } And this is the HTML source code of my website: PHP Code: <html> <title>GambiaGuesthouse.nl</title> <link href="stylesheet.css" rel="stylesheet" type="text/css"> </head> <body> <div id="all"> <div id="head"></div> <div id="menu"> <ul> <li> <a class="menuitem" href="index.php?site=home" title="Home">Home</a> </li> <li> <a class="menuitem" href="index.php?site=links" title="Links">Links</a> </li> <li> <a class="menuitem" href="index.php?site=toevoegen" title="Guesthouses">Guesthouses</a> </li> <li> <a class="menuitem" href="index.php?site=contact" title="Contact">Contact</a> </li> </ul> </div> <div id="rotating"> <img src="img/rotate1.jpg" id="rotate"></div> <div id="login"> <form target="index.php?site=login" method="POST"> Gebruiker:<br> <input type="text" name="gebruiker" size="27" /><br> Wachtwoord:<br> <input type="password" name="wachtwoord" size="27" /><br /> <input type="submit" value="Log In" /> </div> <div id="add"> add comes here </div> <div id="con"> <b>Gambia</b><br /><br /> Het is een prachtig land, waar je avontuurlijke dingen kan doen. Maar vooral lekker uitrusten op de prachtige stranden. <br>Het is een geweldig gastvrij land.De plaatselijke bevolking bezit een unieke gave.ze kunnen in het nu leven,en genieten van het moment.</p> <p>Dan is er voor je gevoel even geen verleden en ook geen toekomst,alleen het NU en dan kan je echt leven in zijn puurste vorm,je openen en gelukkig zijn,en dit kunnen de mensen in Gambia. <br>Daarom vind ik dit zo een mooi land,het is prachtig om te zien hoe deze mensen kunnen genieten. dankbaar zijn een groot goed is.</p> <p> Op deze website vindt u een zo groot mogelijk overzicht van Guesthouses in the Gambia.</p> <p>Guesthouses zijn huisjes die verhuurd worden meestal door de lokale bevolking en zijn meestal een stuk lager geprijsd van de hotels in Gambia <br>Ook als u met een groot gezelschap van plan bent om naar Gambia te gaan is een Guesthouse de perfecte manier om te verblijven in Gambia.</p></div> </body> </html> I got the following, which is perfectly displayed in IE: Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled</title> <style type="text/css"> #container { overflow: hide; } #container .content { float: left; margin: 0px 0px -2000px 0px; padding: 0px 0px 2000px 0px; } </style> </head> <body> <div style="width:585px;overflow:hidden;" id="container"> <div style="width:585px;height:9px;background-image: url(images/product/mid_top2.gif);background-repeat:no-repeat;padding:0px;float:left"></div> <div class="content" style="width:1px;background-image: url(images/product/mid_back_edge2.gif);background-repeat:repeat-y;clear:both"></div> <!-- div missing in FF ---> <div class="content" style="width:583px;background-image: url(images/product/mid_back2.gif);background-repeat:repeat-x;"> content goes here.. </div> <div class="content" style="width:1px;background-image: url(images/product/mid_back_edge2.gif);background-repeat:repeat-y;float:left"></div> <div style="width:585px;height:7px;background-image: url(images/product/mid_bottom2.gif);background-repeat:no-repeat;clear:both;float:left"></div> </div> </body> </html> But the div at the left-side (see comment) is just not shown in Firefox. Can anyone explain why? Hi everybody, I'm sure there is a very simple solution to this layout problem but I haven't been able to fix it. My design displays perfectly in Firefox but is out of place by 40px in IE. URL The CSS in question is certainly this div: Code: #sub-content ul li a { ... margin-left: -40px; position: relative; } I've tried various IE hacks, such as the ones mentioned on Centricle, e.g: Code: div>#sub-content ul li a { margin-left:0px; } But to no avail. Would a CSS guru mind helping me out? Hi, i'm working on this search box but I can't get the 3 elements perfectly aligned, the bottom of the elements should be aligned. What I have is: Search: <input> <img> Search is <label>, input is a normal input text box, <img> is used instead of submit. The problem is, that the <img> is about 5px higer than the other two. The Search label and input are middle aligned. Does anyone have a solution for this positioning problem? A link with workarounds would be useful. Thanx. Hi, I'm relatively new to web development and I am self taught so please forgive any noobish comments haha. Basically I am building my company's web page and I have it all done except for one small discrepancy. My navigation bar at the top of the page has 4 links to the various sections of the website and they are in the right place in Safari, Chrome, Firefox, and Opera but in IE they are all out of whack. I've been banging my head against the desk trying to figure out whats wrong with either my CSS file or HTML code with no luck. I've validated both my CSS and HTML on the W3C validators and they come back with no errors. Not even a warning. Its all valid code. So I've heard this is common when dealing with compatibility issues that IE is the worst. So could someone please take a look at my code and see if they can find out whats wrong? Here is my CSS... #navbar { font-family: "Frutiger LT 55 Roman", Arial, sans-serif; height: 103px; width: 792px; padding-bottom: 0px; background-color: #0097c6; } #bigpic { margin: 0px 0px 0px 0px; padding: 0px 0px 0px 0px; } #leftcol { color: #585858; font-size: 10pt; width: 365px; padding-top: 15px; padding-left: 5px; padding-right: 7px; margin-bottom: 30px; font-family: Arial, sans-serif; float: left; } #rightcol { color: #585858; font-size: 10pt; width: 365px; padding-top: 24px; padding-left: 5px; padding-right: 5px; margin-bottom: 30px; font-family: Arial, sans-serif; float: right; } #threecol-mid { color: #585858; font-size: 10pt; width: 195px; padding-top: 24px; padding-left: 10px; padding-right: 5px; margin-bottom: 30px; font-family: Arial, sans-serif; float: left; } #threecol-right { color: #585858; font-size: 10pt; width: 195px; padding-top: 24px; padding-left: 5px; padding-right: 5px; margin-bottom: 30px; font-family: Arial, sans-serif; float: right; } #leftcol-nohead { color: #585858; font-size: 10pt; width: 365px; padding-top: 24px; padding-left: 5px; padding-right: 7px; margin-bottom: 30px; font-family: Arial, sans-serif; float: left; } #footer { width: 792px; padding: 0px 0px 0px 0px; margin: 0px 0px 10px 0px; color: #999999; font-family: Helvetica, Verdana, Arial, sans-serif; font-size: 8pt; border-top-style: dotted; border-top-color: #999999; border-bottom-style: dotted; border-bottom-color: #999999; border-width: thin; clear: both; } .logo { padding-top: 18px; padding-left: 10px; padding-right: 10px; float: left; } #navlinks { padding-top: 4px; padding-right: 5px; margin-bottom: -4px; position: relative; float: right; } .allcontent { width: 800px; margin-left: auto; margin-right: auto; } h2, h3, h4, h5 { margin-right: 1px; margin-left: 0px; margin-bottom: 0px; margin-top: 1px; padding-bottom: 0px; padding-top: 0px; font-family: Arial, sans-serif; } .heading1 { font-family: Arial, sans-serif; font-size: 16pt; } .heading2 { font-family: Arial, sans-serif; font-size: 10pt; font-weight: bold; } a { text-decoration: none; color: #0097c6; } img { border: 0; } .disclaimer { font-family: inherit; font-size: xx-small; } The problem lies with the navlinks ID I think. Basically, IE doesn't put the images (which are also links) where I want them while all the other browsers do. Something funky about the way IE reads CSS maybe? I don't know. If more info or code is needed I'll gladly provide. Thanks in advance, Griz I've been working on a layout for my site. It consists of a fixed top menu bar, a fixed left-side menu bar, a fixed right-side bar (possibly used for ads; not sure yet), and a scrollable middle content section. Think a modified Facebook layout. I've been testing with Firefox, and here's what I ended up with (remove the spaces; as a new user, I can't post images, either): htt p://dl.dropbox.com/u/11662651/Graphics/SiteStuff/LayoutGoo d.png That's perfect. It's exactly what I wanted. But then, once I finished it, I tried to test it in IE for compatibility. I almost cried. Almost every aspect of the layout was just completely messed up. Nothing remains fixed when I scroll. Although the menu bar's background color shows it in the right place, all its content shows up below the left-menu instead. In fact, ALL content besides the left-menu shows up below the left-menu instead of in its correct place. And as if that weren't enough, the bullet points on the left-side menu's list (a UL) show up aligned to the left, while the text is aligned to the right (it should be to the right). Besides fonts, not one thing in the layout is correct in IE. But it's all PERFECT in Firefox. I don't usually do this, but because of the scale of how many things are wrong, and because it's only messed up in IE, I'm going to post a download to the .HTML file and ask you to help me figure out what's going on here. So, here's the file (remove the spaces; I'm a new user so I can't post links): htt p:// dl.dropbox.com/u/11662651/Graphics/SiteStuff/index .html?dl=1 Please, please, please, at least get me on the right track to fixing this. And, in related news, I hate IE. Very much. -IMP I have a site that I am working on: test dot choupique dot com The #subpage class is centered in internet explorer 9, but not firefox 5. I can't figure out why for the life of me. Can anyone see why? Thanks Ok, I give up... can't figure this one out... the navbar seems to be centered in IE but not in Firefox. www.moksha.net/index.html I notice that it doesn't seem to matter whether I am inside the header div or not. The navbar is a UL. I don't care how wide it is really... I would love to strip all this down to its most essential. Thanks! #header { padding: 1em; margin: 0px; text-align:center; width:100%; align: center; display:block; } #navbar { color:#FFCC33; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; text-decoration: none; margin:0 auto; } #navbar ul { clear:left; text-align: center; margin: 0 auto; list-style-type: none; text-indent: 0; width: 600px; background-color:#996633;} #navbar li { /* float:left; width:80px; */ padding-right: 1em; height: 3em; line-height:3em; font-size: 20px; display: inline; list-style-type: none; text-align: center; margin:0; } #navbar a { color:#FF9933; text-decoration: none; } #navbar a:hover { color:#FFCC33; } How can i accomplish this with CSS? See image he hxxp://g.imagehost.org/0398/css.png (replace x with t) http://www.lightblu.com/ (It speaks for itself) how come on my test page, http://www.lightblu.com/test.php works fine (ie. the menu is centered correctly), but not on the homepage? What did I do wrong? Hi there, sorry to bother y'all, but I've been fighting with this css for hours now and I can't seem to get it quite right. I'm trying to center a search box and button skinned with an image, which I think should be fairly simple, but I'm not very good with css yet and it's being more evil than usual. I will try my best to provide all the pertinent information, and thank you very much in advance for any assistance you might provide. My top bar is as such, at the moment (the current issue being that I can't get the text to not be on the edges with the image border while keeping the text field and the button flush against each other), although I've tried a number of other solutions, this is the closest as of yet that I have gotten. Code: <div class="header"> <div class="leftAlign"><a href="/Controller?pageRequest=home"><img src="/img/spinner.png"></img></a></div> <div class="rightAlign" id="topRight"> <jsp:include page="AccountLinks.jsp"></jsp:include> </div><br /> <form id="searchForm" action="/SearchServlet"> <input type="text" id="searchText"></input><input type="image" src="/img/searchButton.gif" alt="Search" id="searchButton"></input> </form> </div> and the relevant css is Code: div.header{ width: 1000px; margin: 0 auto; min-height: 60px; display: block; text-align:center; } div.leftAlign{ float: left; text-align: left; } div.rightAlign{ float: right; text-align: right; } #searchForm{ vertical-align: top; margin: 0; line-height: 1; outline:0; } #searchText{ background:url(/img/searchText.gif) no-repeat top center; padding:0px; margin:0; display:inline; border:0; width:159px; height: 31px; } #searchButton { background:url(/img/searchButton.gif) no-repeat top center; padding-top:0px; margin:0; display:inline; border:0; width:42px; vertical-align: top; line-height: 1; height:31px; } Any help that you can provide would be greatly appreciated. Thank you very much. Hey guys, I know that this is a well covered topic. but I am trying to get a container div to sit centered and 100% of the page. my css reads: * { margin:0; padding: 0; } html, body { height: 100%; } #container { position: relative; width: 900px; height: 100%; margin: 0 auto; background: url("imgs/backgroundmain.gif"); } It works fine for the required browsers until the contents of the page goes beyond the size of the screen then the background stops. it literally stops at 100% of window rather than 100% of content. Any ideas??? James MacLeod Hello everyone. I would like to thank anyone who attempts to solve this in advance. I have a css page here . If you need me to post the code let me know, but I temporarily have embedded styles so I figured you could view source easily enough. The entire content of the page centers just fine in Firefox2, but unfortunately is aligned to the left in IE7 on PC. Does anyone know why this might be? Thanks again for your expertise and time, phpkata. having trouble centering a web site what i want to precisely position elements in my site and i thought that i could put all the elements in to one div tag and make that centered. i can center it but the problem is when the browser is resized smaller that the content the site remains centered and center starts diserpering off the left edge. what i want it to do once resized smaller is for a horizontal scroll bar to appear and for content not to disappear on the left edge of the screen. i have half achieved this but now my content dose not stay in the main div check out my example the "center1" div should appear inside the top light gray box but it dose not! http://www.thehopeandanchor.net/nypd/layertest.htm the code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>redeem file</title> <style type="text/css"> <!-- div#center1 { /*position:absolute;*/ margin:0px auto; top:10px; width:600px; height:47px; background-color: #cccccc; } div#center2 { position:absolute; left: 50%; margin-left: -300px; top:80px; width:600px; height:47px; background-color: #cccccc; } div#insidebit { position:absolute; left:50px; top:10px; width:64px; height:26px; background-color: #eeeeee; } --> </style> </head> <body> <div id="center1"> <div id="insidebit">center1</div> </div> <div id="center2"> <div id="insidebit">center2</div> </div> </body> </html> Hi everyone, I am trying to make a horizontal navigation near the top of my website. But I can't seem to make it center but AND horizontal, i've only been able to center it but not horizontal... or have it horizontal but not center. This is my CSS so far. Quote: ul#nav li { display:inline; margin: 0 auto; width: 100%; text-align:center; } ul#nav li a { display:block; height:48px; width:113px; background: transparent url(images/nav.png); margin: 0 auto; text-indent:-9009px; } ul#nav li a:hover {background-position:0 -48px} ul#nav li#photos a { width:139px; margin: 0 auto; background-position:-113px 0; } ul#nav li#photos a:hover {background-position:-113px -48px} My home link and photo link are currently centered and on top of each other in a vertical fashion with this code. Please help! Trying to build a CSS Page Centered Template, where content autostretches containers height, as opposed to setting absolute heights. If one column is longer than the others the others should auto stretch to same height. Also trying to work in a Footer that sits automatically at the bottom. Heres where I am at...as usual different story in every browser. page ---- <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="test.css" rel="stylesheet" type="text/css"> </head> <body leftmargin="0" topmargin="0"><table><tbody ><tr><td><div class="container"> <!--*******page content start*******--> <div class="top">top</div> <div class="main1">main1</div> <div class="main2">main2</div> <div class="main3">main3</div> <!--*******page content end*******--> </div></td></tr></tbody></table></body></html> CSS Sheet ---------- table { width: 100%; height: 100%; } td { text-align: center; vertical-align: top; } div.container { position: relative; margin: 0 auto; width: 780px; text-align: left; height: 100%; } div.top { position: absolute; top: -3px; left: 0px; width: 780px; height: 203px; background-color: #CCC; } div.main1 { position: absolute; top: 200px; left: 0px; width: 200px; height: 100%; background-color: #BBB; } div.main2 { position: absolute; top: 200px; left: 200px; width: 200px; height: 100%; background-color: #DDD; } div.main3 { position: absolute; top: 200px; left: 400px; width: 380px; height: 100%; background-color: #AAA; } This is my rendition of Claire Campbell's "Vertical Buttons". Why is my text "Quick Links" not centered in IE? All the other text is centered? Thanks in advance. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <style type="text/css"> #quikcontainer { position: absolute; top: 120px; left: 30px; height: 650px; width: 143px; background: white; border: orange 2px solid; margin: 0 auto; padding-top: 20px; font-family: Arial, Helvetica, sans-serif; font-size: 13px; text-align: center; color: #1E599F; } ul#quiklist { text-align: center; color: #1E599F; list-style: none; padding: 0; margin: 0 auto; width: 100%; } ul#quiklist li { display: block; margin: 0; padding: 0; } ul#quiklist li a { display: block; width: 100%; padding: 0.5em 1em 0.5em 1em; color: #1E599F; text-decoration: none; background: white; } #quikcontainer>ul#quiklist li a { width: auto; } ul#quiklist li a:hover, ul#quiklist li#active a:hover { color: white; background: #1E599F; border-color: orange; border-bottom: 2px solid; border-top: 2px solid; } </style> <html> <head> <title>Untitled</title> </head> <body> <div id="quikcontainer"> <p><strong>Quick Links</strong></p> <ul id="quiklist"> <li id="active"><a href="#" target="_blank">Aaaaaaa A-A</a></li> <li><a href="#" target="_blank">AA Aaaa aa Aaaaaa Aaaaaaaa</a></li> <li><a href="#">Aaaaaaa A-A Aaaaaaa</a></li> <li><a href="#">Aaa A-A Aaaa</a></li> <li><a href="#">Aaaaaa A-A Aaaaaa Aaaaaaa</a></li> </ul> </div> </body> </html> Struggling to get my <ul> to display itself on the left side of my sidebar navigation. I just want it over maybe 40 pixels to the left. I'm not sure why the ul is aligning itself in the center. I tried floating the elements left, but the ul is stuck centered. I tried adjust margins/padding but all that does is truncate the text to another line. I set absolute position and got the list to move all the way to the left, but it broke the flow of the page and the list ran over top of the headers. Any thoughts? All the CSS structures are imbedded in the STYLE section of the index file. http://www.mgan.net/work/10-20-04/ Bonus question: Anyone know why in internet explorer when you run the mouse of my top navi-bar the footer graphic for the news section disappears? Hi, there. Having looked around the forum for a CSS method of vertically centering an IMG within a DIV, I found the following code which works perfectly in IE but not FF : #full-image { float: right; width: 380px; height: 380px; text-align: center; line-height: 0px; background-color: #F0F5F7; margin-top: 5px; margin-right: 0px; margin-bottom: 10px; margin-left: 10px; } #full-image img { margin-top: expression(( 380 - this.height ) / 2); } That's right - works in IE but not FF, which is most unusual! Any ideas how to make this work cross-browser? Thanks in advance for any assistance. Hello I hope this isnt a stupid questions but here goes. I have a page designed with vertically centered content It works fine but if you view the page in a smaller screen size some of the content disapears in to the menu area of the browser. Its hard to explain but if you use the sorce code below and view it in any broswer. Make it so the browser is not maximized and then change the height of the window until the scroll bar appears ... you will notice that some of the conent goes into neverland before the srollbar appears. Line 1 disapears and can not be reads. This will be an issue for people viewing my site in 800x600 Thoughts? Code: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style> #horizon { background-color: transparent; position: absolute; top: 50%; left: 0px; width: 100%; margin-top: -200px; text-align: center; min-width: 900px; } #wrapper { background-color: #fff; position: relative; text-align: left; width: 900px; height: 380px; margin: 0px auto; } </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <div id="horizon"> <div id="wrapper"> Line 1<br /> Line 2<br /> Line 3<br /> Line 4 </div> </div> </body> </html> |