Here is my CSS:
.header
{
background-image:url(Images/head.png);
background-position: center top;
background-repeat:no-repeat;
width:1010px;
height:269px;
}
This is my HTML:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>xSky Software - Most likely the only software on Clickbank that exists.</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="header"></div>
</div>
</body>
</html>
I am trying to get my image centered on the X-axis, and top'ed on the Y-axis. However my CSS class .header wont do that. Can you see what I am doing wrong?
EDIT: Turning the top center around does not work either
Your background-position values are around the wrong way. It should be:
background-position: 50% 0; /* Short values FTW! */
Don't forget if you want it to stay centered, you'll also need to do something like wrap the content in a container and use margin:0 auto on it to keep everything centered.
HTML:
<div class="wrap">
<div class="header"></div>
</div>
CSS:
.wrap {
width:1010px;
margin:0 auto;
}
.header {
width:1010px;
height:265px;
background:transparent url('//placehold.it/1010x265') no-repeat;
}
Demo: jsfiddle.net/UGDxU/show (or edit it)
The order of background-position is left top. Try flipping the values.
You'll also need background-repeat: no-repeat.
jsFiddle.
Related
I am trying to put background image at end of page ( at bottom bar) but key word bottom does not work, if I use like 900px or 55em it does work but in inspect element shows in middle of page.
<!DOCTYPE html>
<html>
<head>
<title>Test Website</title>
<style type="text/css">
body{
background: url('google2.png') no-repeat center bottom;
}
</style>
</head>
<body>
</body>
</html>
The body doesn't seem to take up the full height. So in your CSS, add this:
html, body {
height: 100%;
}
why is my form div at bottom of parent div? it wants me to say more, but the question has been asked and it's pretty clear... why is the form div appearing at the bottom of my header div? i know i can hack it and give it a negative top margin, but I KNOW that's not proper form. what gives? thx.
html:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div id="header">
<div id="logo"></div>
<div id="login">
<input type="text" placeholder="EMAIL ADDRESS" value="" name="email" id="user">
</div><!-- end login div -->
</div><!--end header div -->
</body>
</html>
css:
#charset "utf-8";
/* CSS Document */
body {
margin: 0px;
padding: 0px;
}
input {
float: right;
margin-left:15px;
}
#header {
background-image:url(images/headerGradient.png);
background-repeat: repeat;
margin:0px;
padding:0px;
width:auto;
height:72px;
display:block;
}
#logo {
background-image:url(images/logo.png);
width: 182px;
height: 66px;
vertical-align:middle;
margin-left: 60px;
}
It's because your logo div has no floating style set.
Try to add
float:left
to #logo div
Demo: http://jsfiddle.net/eyMJa/1/
Using float in Css I have found becomes really annoying and makes getting layout how you want it kinda difficult, Unless you can watch a fair few youtube video's until you master it.
I'm trying to use:
position:absolute;
instead, and then setting
top:Number of Pixels or percentage/em;
right:Number of Pixels or percentage/em;
bottom:Number of Pixels or percentage/em;
left:Number of Pixels or percentage/em;
also
position:relative;
Seems to add blank space in the dimensions of the div again messing up the layout, where Absolute acts kind of like float, but without the annoying interactive or page jerking of the other two methods.
If you do use float for whatever reason, remember to add Clear float after each time you use it unless you specifically need to keep it.
Hope this helps
This question already has answers here:
Why does this CSS margin-top style not work?
(14 answers)
Closed 8 years ago.
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#a
{
background:#33F;
width:1000px;
height:1000px;
}
#b
{
width:500px;
height:500px;
background:#F00;
}
#c
{
margin-top:50px;
height:200px;
width:200px;
background:#FF0;
}
</style>
</head>
<body>
<div id="a">
<div id="b">
<div id="c">
</div>
</div>
</div>
</body>
</html>
Why div#c get margin-ed from the top of the browser instead of getting distanced from the div#b.
CSS has a concept called margin collapse.
Two margins are adjoining if and only if:
...
both belong to vertically-adjacent box edges, i.e. form one of the following pairs:
top margin of a box and top margin of its first in-flow child
you can add position: relative; to the #b element and position: absolute; to the #c element
You are percieving this incorrectly. It is doing exactly what you want it to -when the divs have contents.
I've inserted non breaking spaces to achieve what you were looking for.
http://jsbin.com/ifofix/2/
Below is the other fellows suggestion
http://jsbin.com/ifofix/3/
I am making a website thats 960px wide but I want images on both sides of the header that you can see if you have a bigger screen.
because I want to keep the site 960px wide I need these extra side images to not be counted by the browser, I can get it to work on the left
see here:
http://www.wireframebox.com/test/sideimages/index_leftworks.html
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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
body { margin: 0; padding: 0; border: 0; background-color:#096 }
img { border: 0; }
#main {
width:960px;
height:216px;
background-image:url(main.jpg);
position:relative;
top:0; margin: 0 auto;
}
#left {
width:170px;
height:216px;
background-image:url(left.jpg);
float:left;
left:-170px;
position:relative;
}
#right {
width:170px;
height:216px;
background-image:url(right.jpg);
float:right;
left:170px;
position:relative;
}
</style>
</head>
<body>
<div id="main">
<div id="left"></div>
<div id="right"></div>
</div>
</body>
</html>
if you make your window thinner the left red image disappears off the site without causing the browser window to get a bottom scroll bar, however when I try and do the same thing to the right side it doesn't work
see here
http://www.wireframebox.com/test/sideimages/
Code is equal, only <div id="right"></div> is missing
the css is in the source.
you can also see it being used on this site to show the date sticking out the left of the page, without impacting the overall sites width
http://www.tequilafish.com/2009/04/22/css-how-to-pin-an-image-to-the-bottom-of-a-div/
why does this work on the left but not the right?
See the below fiddle for output...
Fiddle: http://jsfiddle.net/C2j6G/4/
Demo: http://jsfiddle.net/C2j6G/4/embedded/result/
see below image -
It's better if you can combine those two images & give in the background of body. like this:
HTML
<div id="main"></div>
CSS
body {
margin: 0;
padding: 0;
background:#096 url(http://imgur.com/JHXDv.png) no-repeat top center;
}
#main {
width:960px;
height:216px;
background-image:url(http://www.wireframebox.com/test/sideimages/main.jpg);
margin:0 auto;
}
Check this http://jsfiddle.net/PVWzA/1/
If you want your website page to be 960px wide, then you should change your width of the main image to 960 - 170(left) - 170(right). Changing the width of main.jpg to 620px should fix your issue.
HTH
Put the image in a div or image tag that is larger than your center div and make it the child of the center content div. Also make sure that it's positioning will take it out of the flow(\absolute). If you then add a negative margin you can pull the image outside of the content div without disrupting its placement.
#center div.top{
width:1200px;
height:170px;
margin: 0px -170px;
position:absolute;
background:url("randombackground.png") no-repeat;
}
The html will be kinda like this:
<div id="center">
<div class="top"></div>
Content content content
</div>
view on http://www.eveo.org
download site for easy modification:
http://www.eveo.org/backup/eveo.rar
http://www.eveo.org/backup/eveo.zip
As you can see right now, it is centered horizontally and vertically using an easy table method:
<table cellpadding="0" cellspacing="0">
<tr>
<td align="left">
*put anything in here and it will be aligned horizontally and vertically
</td>
</tr>
</table>
accompanying CSS:
table
{
height: 100%;
width: 100%;
vertical-align: middle;
}
However, in my document I did not set a doctype, I only have:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
If I add a standard doctype such as the following:
<!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" lang="en" xml:lang="en">
my table method is no longer valid and does not work. So I need a new way to center my webpage vertically and horizontally regardless of browser window size.
There are some cross-browser solutions that don't require Javascript or hacking.
One good example is here
Have a look also on this one.
For some learning, check this excellent example of gtalbot about horizonal align in CSS.
good luck >)
HTML
<div id="container"></div>
CSS
div#container {
width: 200px;
height: 200px;
margin-left: -100px; /* Half of width */
margin-top: -100px; /* Half of height */
position: absolute; left: 50%; top: 50%;
}
You can do this with CSS/HTML, but the method I'm going to use will work best if your height is known, or you can use JavaScript to grab the height.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Centered Page</title>
</head>
<body>
<div class="container">
<!-- My Content will be 500px tall -->
</div>
</body>
</html>
CSS:
.container { height:500px; margin:-250px /* Half the height container */ auto; position:absolute; top:50%; width:960px; }
JavaScript (jQuery): If you don't know the height or if it changes.
(function() {
var $container = $('.container'),
height = $container.outerHeight();
$container.css({
'marginTop': (height/2) * -1
});
})();
There is an easy way to center a page using just CSS using inline-blocks: http://jsfiddle.net/CUd8G/
It works when there are a very little content: http://jsfiddle.net/CUd8G/1/
When there are a lot of content: http://jsfiddle.net/CUd8G/2/
With fixed width (however, it works with unknown width/height): http://jsfiddle.net/CUd8G/3/
This is the minimum you need to do it in only html and css without javascript
<!doctype html><html><head><style>
table.inner{width:100%;}
table.outer{text-align:center; width:100%; height:100%; position:absolute; top:0px; bottom:0px; left:0px; right:0px;}
</style></head><body>
<table class='outer' cellspacing='0px' cellpadding='0px'><tr><td></td></tr><tr><td>
<table class='inner' cellspacing='0px' cellpadding='0px'><tr><td></td></tr><tr><td>
INSPIRATIONAL
CELEBRATIONAL
MUPPETATIONAL
</td></tr><tr><td></td></tr></table>
</td></tr><tr><td></td></tr></table>
</body></html>