HTML CSS Image won't center? - html

I am trying to center an image that is located within a div, which is then located inside of another div. Not to worry, I will post some code so you can all see what the heck I'm talking about here.
HTML:
<div id="container">
<div id="featureimage" style="width: 845px">
<img src="Stylesheets/images/globkey.jpg" alt="" />
</div>
</div>
CSS:
body {
background-color: #383838;
margin: 0; padding: 0;
font-size: small;
color: #383838;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
* html body {
font-size: x-small; /* for IE5/Win */
font-size: small; /* for other IE versions */
}
img {
border-style: none;
}
/* Conatiner */
#container {
background-color: #fff;
width: 845px;
margin: 0 auto;
overflow: hidden;
}
#featureimage{
width: 620px;
padding: 0 0 0 15px;
margin: 20px 0;
/* I have also tried without padding and no margin!*/
}
#featureimage img{
margin-left:50%;
margin-right:50%;
width:360px;
height:360px;
}
I am fresh out of ideas here! Been at this for ever!
Thank you for any help,
Evan

Images are inline elements, to center them, you can use text-align: center; or set them to display: block and set margin: 0 auto;

just for sure add text-align: center to your #container, and then add margin: 20px auto; to your featureImage. If you want img to be centered within featureImage should work (featureImage will inherit text-align: center).

According your provided markup structure it is enough to following way:
img {
display: block;
margin: 0 auto;
}
But above approach will work if container contain single image. Bu if container contain multiple image then above technique will not work anymore then following way would be ideal:
#featureimage {
text-align: center;
}
img {
display: inline-block;
vertical-align:top; // It is recommended when you use css property vertical-align
}

Use:
margin: auto !important;
display: block;
Also try going to Play Store download the app HTML code it will come in handy for you.

#featureimage{
width: 620px;
padding: 0 0 0 15px;
margin: 20px 0;
text-align:center;
}

I would try this css:
#featureimage{
width: 620px;
padding: 0 0 0 15px;
margin: 20px auto 0px auto;
text-align: center;
}
I may be totally off on this one, as I haven't tested it out.

i would use this for sure,, the absolute; will keep it in that spot if you want it to move with the page put fixed; but other than that boom
#featureimage {
width: "change this code to whatever expample..." 555px;
height: "change this code to whatever example..." 96px;
position: absolute;
top: "change this to where ever it centers , you just have to mess with it.. example..." 960px;
right: "same with this i had to put - to get it right as far as i could just play with it..." -945px;
}

It can also be solved by simple put a <center> tag:
<center>
<img src="abc.gif">
</center>
But the best way and html5-like is as mentioned next:
define in the header:
<style>
img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
To make use of this class in the body:
<img src="abc.gif" alt="abc" style="width: 90%; max-width: 300px;>
That's all. The last method is that recomended by w3c.

Related

<center> tag does not work in IE

The issue is to align the image at the center of the div. This is working properly in chrome, except for IE
<DIV id="content"><P>Internal resources </P>
<DIV class="containertop">
<P>External resources </P>
</DIV>
<DIV class="contentcontainer">
<DIV class="containerImg">
<img height="286" width="381" src="http://www.bestwestern.com/img/bg-groups-meetings.png" /alt="Banner Image" title="Banner Image"></img>
</DIV>
</DIV>
</DIV>
CSS:
.containerImg Img {
border: 0 none;
padding: 0px !important;
margin: 0px;
width: 368px;
height: 277px;
margin-left: auto;
margin-right: auto;
display: block;
}
#content .contentcontainer {
float:left;
padding: 5px 10px 0 0;
margin: 1px 0 0 0px;
}
#content .containertop {
padding-top: 15px;
color: #999;
}
div#content {
font: normal 12px/1.6em arial;
font-size: 12px;
color: #666;
width: 471px;
padding: 10px 10px 0 10px;
margin: 0;
background-color: #fff;
min-height: 100%;
height: 100%;
background-color: #fff !important;
min-height: 100% !important;
height: 100%;
}
I removed tag and tried above CSS and that is also not working.
Here is the fiddle : https://jsfiddle.net/nf5hghqy/10/ (Open in IE)
Can we resolve this?
<center> tag is deprecated (see here)
This tag has been deprecated in HTML 4 (and XHTML 1) in favor of the
CSS text-align property, which can be applied to the element or
to an individual . For centering blocks, use other CSS properties
like margin-left and margin-right and set them to auto (or set margin
to 0 auto).
To get img aligned center you have to set it display:block ( because img is by default an inline element) and margin:auto
Another mistake, img is a self-closing tag, so you can't do this <img></img>
See snippet below:
div {
border: 1px solid red /*demo purposes */
}
img {
display: block;
margin: auto;
}
<div class="containerImg">
<img src="http://www.bestwestern.com/img/bg-groups-meetings.png" alt="Banner Image" title="Banner Image" />
</div>
UPDATE - Based on OP's new fiddle:
your issue is here:
#content .contentcontainer {
float:left;
padding: 5px 10px 0 0;
margin: 1px 0 0 0px;
}
simply remove float:left, like this:
#content .contentcontainer {
padding: 5px 10px 0 0;
margin: 1px 0 0 0px;
}
First check you have added doctype
center tag doesn't work in IE ,you can use CSS as text-align: center , and auto for margin , then it will work ok in IE. Fine more here
From http://www.w3.org/Style/Examples/007/center.en.html#block:
.containerImg img{
display:block;
margin-left:auto;
margin-right:auto;
}
The important part is to make sure the image is a block element; this way it has width. The margin-left and margin-right set to auto automatically set margins based on the width of the block element image, making the image center itself.
try this.
.containerImg img{
margin:auto;
display:block
}
You need to remove the tags and add some css either as separate file or in line and use margin:auto; instead of your margin-left and margin-right

HTML - Putting text in some field

I'm trying to create a single sentence in some kind of a field I created, and every time I just make the font bigger it pops out of the field, and I gotta lower the font-size to put it inside again.
Can I make the font size bigger and keep it in the field at the same time?
My code:
HTML:
<div id="wrapper">
<h1 style=""> Nothing Created Yet </h1>
</div>
CSS:
#wrapper {
width: 1000px;
height: 120px;
margin-top: 100px;
border: 5px solid gray;
border-radius:500px;
margin-left: auto;
margin-right: auto;
font-family: Arial;
font-size:40px;
background-color: #F0EEF3;
border-color:red;
}
What I get:
You firstly need to remove the browser-default margin styling on your h1 element:
#wrapper h1 {
margin: 0;
}
Then you should ideally give your #wrapper element a line-height equal to its height:
#wrapper {
...
height: 120px;
line-height: 120px;
}
JSFiddle demo.
try this DEMO
#wrapper {
width: 1000px;
height: 120px;
margin-top: 100px;
border: 5px solid gray;
border-radius:500px;
margin-left: auto;
margin-right: auto;
font-family: Arial;
font-size:40px;
line-height:10px;
background-color: #F0EEF3;
border-color:red;
text-align:center;
}
The reason why this happens is you set fixed width and height for the DIV and when you increase the font size, it could no longer fit inside the DIV. So, the answer is it is impossible in fixed size DIV like this.
or do
-added class to the header and put the margin to 0 and center the text
(jsfiddle.net/6GRGH/)

color body set to auto change margin

Here's my style sheet.
I'm trying to get the margin around my body to have a different color from the body itself. Here's my beginning code. Is a reset section really necessary? Can someone tell me what I'm doing wrong...
#charset "utf-8";
html {
color: #d9a552;
}
body {
display: table;
margin: auto;
background: white ;
width: 960px;
margin-top: 20px;
max-width: 1050px;
height: 100%;
font-weight: 300;
}
What you're looking for is a border. If you want to make the margin around your body red, simply use this:
body {
margin: 0;
border: 10px solid red;
/*and this is to make it stretch all the way*/
height: 100%;
box-sizing: border-box;
}
html {height:100%;}
as can be seen in this demo.
You may want to try adding padding-top: 20px; to the html instead of margin-top: 20px; with the body:
<html>
<head>
<style>
html {
padding-top: 20px;
background: #d9a552;
}
body {
display: table;
margin: auto;
background: white;
width: 960px;
max-width: 1050px;
height: 100%;
font-weight: 300;
}
</style>
</head>
<body>
</body>
</html>
I believe you also want to change color: #d9a552; to
background: #d9a552;
or
background-color: #d9a552;
Here's my solution: I added section tag immediately following the body tag. I gave it a class id of 'mainBody'. Then I adjusted the code as follows:
I know I can't give myself credit, but I thought I would share my solution for someone else to use. I will give both you guys credit for steering me in the right direction.
Thank you...

Unwanted 48px margin over body?

I was building a static html page for creating a wordpress theme.but now i notice that 48px margin is above the body element(I found it with chrome developer tools).i can fix it by just adding a -48px margin but what exactly is causing this problem,can someone help me.
My CSS
body{
font-size:18px;
line-height:1.72222;
color:#34495e;
font-family:Ubuntu;
margin:0
}
aside {
background: #31373d;
height: 100%;
position: fixed;
width: 20%;
font-weight: normal;
color:#fff;
}
.main {
margin-left: 20%;
}
.content{
width: 65%;
max-width: 760px;
margin: 3rem auto;
}
Look at this live JSfiddle Demo - http://jsfiddle.net/aq96b/1/embedded/result/
It's the line
margin: 3rem auto;
in your .content that's causing this (if I properly understand the problem). Unchecking/removing that margin will move the content back up to the top left of your .main div.
To maintain a similar effect with the content position, you could add padding to the .main of the same amount ie
padding: 3em;
Remove the margin: 3rem auto; from .content.
DEMO HERE
It's coming from the div .content. To correct this you should add overflow:hidden to .main
Example
.main {
margin-left: 20%;
overflow: hidden;
}
Alternatively you can set .content to inline-block. This will also correct the issue.
Example
.content {
display: inline-block;
}

Centering two transparent columns using CSS

So, right to the point, here's what I want (minus the poor quality)...
http://www.hbperspective.com/alt3/site.jpg
And here's what I've got...
http://www.hbperspective.com/alt3/
I'm trying to get those two transparent columns to be centered as they are in the pic. With this CSS layout I'm having a heck of a time figuring out how to do that without causing all kinds of other problems. Here is my styling...
* {
padding: 0;
margin: 0;
}
body {
background: #000000 url('background_div.png') repeat-y center top;
color: #ffffff;
font-family: Arial, Helvetica, sans-serif;
margin: 0 auto;
}
#wrapper {
background: url('background_header_transparent.png') no-repeat center top;
width: 100%;
display: table;
overflow: hidden;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100%;
background: #000000;
filter:alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
}
.container {
float: left;
position: relative;
margin-top: 100px;
}
.content {
position: relative;
float: left;
}
#contentColumn{
width: 540px;
}
#sidebarColumn {
width: 190px;
margin-left: 20px;
float: left;
display: inline;
}
#contentColumn .content {
width: 500px;
padding: 10px;
}
#sidebarColumn .content {
width: 170px;
padding: 10px;
}
* html #contentColumn .overlay { height: expression(document.getElementById("contentColumn").offsetHeight); }
* html #sidebarColumn .overlay { height: expression(document.getElementById("sidebarColumn").offsetHeight); }
The markup is pretty simple, probably be just easier to look at it from the link provided. So, like I said I'm not really sure what to do at this point to get it working the way I want. Any ideas?
div#container {
width:500px; /* Same width as both columns */
margin:auto; /* Will center the container */
}
div#col1 {
float:left; /* allows side-by-side columns */
width:250px;
}
div#col2 {
float:left;
width:250px;
}
div.clear {
clear:both; /* Stops columns from extending past border of container */
}
<div id="container">
<div id="col1"></div>
<div id="col2"></div>
<div class="clear"></div>
</div>
And for extra credit, avoid using expressions :) Instead, perform any needed logic like that with javascript, via a framework like jQuery.
There are so many gotchas creating CSS columns I would suggest using a framework instead of rolling your own. There are lots of gotchas which are browser defendant and that you may not see unless you check in IE, FF, Safari, Opera, etc.
A few good frameworks are:
YUI Grids
Blueprint CSS
Blocks (new experimental)
Rules for centering things in CSS:
The thing you're centering must be assigned a width
The margins on that block must be assigned to auto
I have it working on your site in FF3 and IE7 using
div#wrapper {
width:800px;
margin: auto;
}
div#contentColumn
{
margin-left: 20px;
}
If you want to fix up the logo (see top right) then add an extra container div immediately inside the wrapper, and apply the above width/margin to the container (as suggested by Jonathan Sampson.)