I have a css problem. Im trying to vertically center the text in a div, which is overlaying another div, but the text won't budge.
EDIT: Here's teh JSFiddle url: http://jsfiddle.net/wgSEw/3/
The html is as follows:
<div id="footer-top">
<div id="footer-top-left">
<div id="footer-logo">
</div>
</div>
<div id="footer-top-transition"></div>
<div id="footer-top-right"></div>
<div id="footer-top-bullets">
<div id="site-map" class="footer-bullet">
<img src="<?php echo BASE_IMG_URL . 'bulletlight.png'; ?>" alt="some_text">
<span class="footer-bullet-text">Site Map</span>
</div>
<div id="report-issue" class="footer-bullet">
<img src=<?php echo BASE_IMG_URL . 'bulletlight.png'; ?> alt="some_text">
<span class="footer-bullet-text">Report an Issue</span>
</div>
<div id="submit-professor" class="footer-bullet">
<img src=<?php echo BASE_IMG_URL . 'bulletblack.png'; ?> alt="some_text">
<span class="footer-bullet-text">Submit Professor</span>
</div>
<div id="submit-school" class="footer-bullet">
<img src=<?php echo BASE_IMG_URL . 'bulletblack.png'; ?> alt="some_text">
<span class="footer-bullet-text">Submit a School</span>
</div>
</div>
</div>
and the current css is:
#footer-top{
position: relative;
width: 960px;
height: 63px;
float: left;
background-image:url('midtilefooter.png');
}
#footer-top-left{
width: 466px;
height: 63px;
float: left;
}
#footer-logo{
width: 265px;
height: 63px;
float: left;
background-image:url('leftlogo.png');
}
#footer-top-transition{
width: 27px;
height: 63px;
float: left;
background-image: url('midblacktransition.png');
}
#footer-top-right{
width: 467px;
height: 63px;
float: left;
background-color: black;
}
#footer-top-bullets{
position: absolute;
float: left;
width: 960px;
height: 63px;
margin-left: 265px;
}
.footer-bullet{
float: left;
height: 63px;
width: 173px;
color: white;
}
.footer-bullet-text{
height: 63px;
color: white;
top: 50%;
margin-top: -31px;
vertical-align: middle;
}
This essentially, creates a basic background for the top part of the footer, then overlays a div of bullets onto that background, so that it overlays without obscuring or messing with the background. The bullets are displaying in the correct places and, the text is correctly placed horizontally, but I can't get it to center vertically at all. Any help would be appreciated, as well as any general advice on css, Im pretty new to it, and it's giving me a run for my money. Thansk a lot!
to center a div vertical
html
<div id="parent">
<div id="child">Content here</div>
</div>
css option 1
#parent {position: relative;}
#child {
position: absolute;
top: 50%;
left: 50%;
height: 30%;
width: 50%;
margin: -15% 0 0 -25%;
}
We begin by positioning both parent and child divs. Next we set the
top and left values of the child to be 50% each, which would be the
center of the parent. However this sets the top left corner to be in
the center so we’re not done.
We need to move the child up (by half its height) and to the left (by
half its width) so it’s center is what sits in the center of the
parent element. This is why we need to know the height (and here the
width) of the child element.
To do that we give the element a negative top and left margin equal to
half its height and width.
Unlike the first 2 methods this one is meant for block level elements.
It does work in all browsers, however the content can outgrow its
container in which case it will disappear visually. It’ll work best
when you know the heights and widths of the elements.
css option 2
#parent {position: relative;}
#child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 50%;
height: 30%;
margin: auto;
}
The idea with this method is to try to get the child element to
stretch to all 4 edges by setting the top, bottom, right, and left
vales to 0. Because our child element is smaller than our parent
elements it can’t reach all 4 edges.
Setting auto as the margin on all 4 sides however causes opposite
margins to be equal and displays our child div in the center of the
parent div.
Unfortunately the above won’t work in IE7 and below and like the
previous method the content inside the child div can grow too large
causing it to be hidden.
css option 3
#parent {
padding: 5% 0;
}
#child {
padding: 10% 0;
}
In the css above I’ve set top and bottom paddings on both elements.
Setting it on the child will make sure the contents in the child will
be vertically centered and setting it on the parent ensures the entire
child is centered within the parent.
I’m using relative measurements to allow each div to grow dynamically.
If one of the elements or it’s content needs to be set with an
absolute measurement then you’ll need to do some math to make sure
things add up.
For example if the parent was 400px in height and the child 100px in
height we’d need 150px of padding on both the top and bottom.
150 + 150 + 100 = 400
Using % could throw things off in this case unless our % values
corresponded to exactly 150px.
This method works anywhere. The downside is that depending on the
specifics of your project you may need to do a little math. However if
you’re falling in line with the idea of developing flexible layouts
where your measurements are all relative you can avoid the math.
Note: This method works by setting paddings on the outer elements. You
can flip things and instead set equal margins on the inner elements. I
tend to use padding, but I’ve also used margins with success. Which
you choose would depend on the specifics of your project.
source
Related
just got a question regarding relative & absolute positioning and applying clearfix to the main container cos I've written the code and it's not behaving as I expected.
Structure-wise this is a simple page about product history. nav-bar with drop-down menu at the top across the screen, then a big hero image across the screen, followed by a few paragraphs and a simple footer, that's it.
here's my problem:
I need to put 3 components in the hero image area - the hero image itself, one title word on the top left corner and one logo on the top right corner. What I've done is: I created a div and used the hero image as background image. I set the position value of the div to relative. I created another div to hold the title word and set the position to absolute, using top and left to give it a location. Following the same logic, I created another div to hold the logo and set it to float right, with position set to absolute and top and right to give a location. I've applied clearfix to the main div and everything looks ok on my screen (resolution 1280 x 1024) until I saw it on the wide screen(1680 x 1050) --- the logo is not on the hero image! It's to the right side of the hero image.
What caused this? I thought by putting 2 divs inside the main div and applying clearfix, the three will "get together" and act as one and won't separate... Is it because I haven't written any code for responsive layout? Or was it because I shouldn't have used the hero image as the background? Would this problem be solved if I used z-index instead to specify the stack order of hero image, logo and title word?
Below is my code and any help would be much appreciated!
<div id="history-content" class="clearfix">
<div id="history-image-text">HISTORY</div>
<div id="stamp">
<img src="./images/logo.png">
</div>
</div>
#history-content {
background-image: url('./images/heroimage.jpg');
min-height: 307px;
background-repeat: no-repeat;
position: relative;
}
#history-image-text {
color: #fff;
position: absolute;
top: 20px;
left: 50px;
font-size: 20px;
font-weight: bold;
}
#stamp img {
width: 10%; /*not sure I'm doing the right thing here either*/
height: 40%; /*not sure I'm doing the right thing here either*/
float: right;
position: absolute;
right: 100px;
top: 20px;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
height: 0;
line-height: 0;
}
Few things:
Absolutely positioned elements are taken out of normal flow, hence doesn't affect the size of their parent.
Since they're out of normal flow, float has no effect on them (as far as i know)
Absolutely positioned elements shrink wraps to fit it's contents unless width and height is set explicitly or stretched using the top, right, bottom & left properties.
Now your parent div #history-content doesn't have any height set, and all of it's content of are absolutely positioned, So it's not visible (height 0)
applying a proper height for the parent seems to fix the issues for me.
Side note: unlike what you think, you don't have two absolutely positioned<div>'s, #stamp img absolutely positions the <img> inside div#stamp, for the same reason mentioned above, div#stamp is also invisible (height 0) you'll get the same result with and without it. And without floats
As others have said, float doesn't have an effect on absolute positioned elements, and so technically you don't need clearfix in this case.
I'm not exactly sure why your logo is positioned outside the outermost container #history-content, but you could try to put a border around the #history-content to further troubleshoot.
EDIT: Maybe check your hero image dimension, is it smaller than 1608px in width?
<div id="history-content">
<div id="history-image-text">HISTORY</div>
<div id="stamp">
<img src="./images/logo.png">
</div>
</div>
I've changed your CSS below
#history-content {
background-image: url('./images/heroimage.jpg');
min-height: 307px; /*set whatever minimum height you wish*/
background-repeat: no-repeat;
position: relative;
}
#history-image-text {
color: #fff;
position: absolute;
top: 20px;
left: 50px;
font-size: 20px;
font-weight: bold;
}
#stamp {
display: block;
position: absolute;
right: 100px;
top: 20px;
width: 10%; /*set width of image in containter instead*/
height: auto;
}
#stamp img {
max-width: 100%; /*image width will not stretch beyond 100% of container*/
height: auto;
}
JSFiddle: http://jsfiddle.net/5L9WL/3/
I have a div with dynamic text content. The amount of text varies between one word and five or ten words (with large font). Right now, it's absolutely positioned some amount from the bottom and the right of its relatively positioned parent.
However, since the content is dynamic, it looks awkward when sometimes there is more text and the text goes further into the main area of the parent. This is because right now, the reference point of the div is its bottom right corner. Is it possible to have it positioned with the center as the reference point, as depicted above?
The parent container is just styled as normal, with position: relative; and 100% width and height
CSS for the child container is also fairly standard:
position: absolute;
bottom: 33%;
right: 33;
I've tried playing with width, max-width, and min-width, but the result is still not desirable
How about this? Compare these two fiddles using the CSS below fiddle1 & fiddle2
HTML
<div id="parent">
<div id="anchor">
<div id="child">
<h1>Some text</h1>
</div>
</div>
</div>
CSS
#parent {
position: relative;
width: 100%;
height: 100%;
}
#anchor {
position: absolute;
right: 33%;
bottom: 33%;
}
#child {
padding: 10px;
margin-right: -50%;
float: right;
}
The final ancestor div in my page needs a margin on all four sides, to give it a panel effect. Here is my code:
CSS:
html, body {
height: 100%;
}
#wrapper {
width: 100%;
height: 100%;
}
#bibletree {
width: 20%;
float: left;
height: 100%;
}
.inner { /*this is the div that I need a margin around, so it is by 10px of the #bibletree div on all sides, including the bottom.*/
overflow: auto;
}
HTML:
<div id="wrapper">
<div id="bibletree">
<div class="inner">my content here, both short and long</div>
</div>
</div>
As you probably guessed, there is a lot more going on here than what is written. I have several columns with divs that all need this margin for the panel effect on the .inner div. Thanks for any help.
BTW, I have tried absolute positioning and it only positions based on the window, not on the parent element, even if I set the parent to position: relative.
If you set .inner to width 100% and add a margin, it will be wider than its container. You can set a padding or a border instead. For example, you can add a white or transparent border of 10px.
Another option is to make #bibletree position relative, then make .inner position absolute and specify top, bottom, right and left:
.inner {
bottom: 10px;
top: 10px;
left: 10px;
right: 10px;
position: absolute;
}
This will make it the same size as #bibletree, minus 10px on every side.
Margin:10px is working right?? you need not no specify the width for inner div, as div is already has block option. check here updated demo http://jsfiddle.net/QShRZ/5/
Horizontally aligning a div-element within another div-element can be achived with margin: 0 auto; as long as they both have a width-property other than auto, but this does not apply for vertical alignment.
How can you vertically align a div within another div?
There are a number of different approaches to this, based on various ideas. Given that the element has a fixed height (in px, % or what have you), the best solution I've found so far is based on the following principle:
Give the parent div position: relative; and the child div position: absolute;, to make the child absolutley positioned in relation to the parent.
For the child, set top, bottom, left and right to 0. Given that the child also has a fixed width and height that is less than the size of the parent, this will push the browser into an impossible situation.
In comes margin: auto; on the child, as the browsers savior. The browser can now add enough margin on all sides to let the child-element keep its size, yet still fill out the entire parent as forced by top, bottom, left and right set to 0.
TADAAA! The element gets vertically and horizontally aligned within the parent.
Markup
<div class="parent">
<div class="child"></div>
</div>
CSS
.parent {
width: 200px;
height: 200px;
position: relative;
}
.child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 100px;
height: 100px;
}
A working example
http://jsfiddle.net/sg3Gw/
I find it easiest to use display:table-cell; vertical-align:middle; here's a jsfiddle
<style>
.a {
border:1px solid red;
width:400px;
height:300px;
display:table-cell;
vertical-align:middle;
}
</style>
<div class="a">
<div>CENTERED</div>
</div>
I want to align a div to the bottom of the PAGE, not to the bottom of the screen. When I do this:
#contact-block{
position: absolute;
bottom: 0; left: 0;
}
, the div is placed in the bottom area of the screen. When my page is long, I have to scroll down and the div which should have been at the bottom, floats somewhere in the middle.
There might be a simple solution to this, but I'm just not seeing it.
Here's my HTML:
<div id="left">
<div id="submenu"> <span class="menutitle">Services</span>
<ul>
</ul>
</div>
<div id="contact-block">
<span class="contacttitle">Contact</span></div>
</div>
<div id="content">
</div>
I've also added a little image to illustrate what I mean:
The red div is the contact div.
Edit:
I've found a solution with jQuery and CSS. This might not be the best solution, but hey, it works.
jQuery:
var offset= $(document).height()-$("#contact-block").height()- $("#footer").height()-60;
$("#contact-block").css("top", offset);
$("#contact-block").css("left", $("#wrapper").position().left);
CSS:
#contact-block {
position : absolute;
width:216px;
height:100px;
background:url(../img/contact-bg.jpg) repeat-x #5c5c5c;
}
You could absolute-position the your divs in place. This technique requires a #wrapper element, which I'm not a fan of, but hey, you gotta do watcha gotta do.
In this example I removed the #left div entirely as it was only required for layout purposed and is no longer necessary.
HTML:
<div id="wrapper">
<div id="submenu">This is services</div>
<div id="contact-block">This is contact</div>
<div id="content">This is content</div>
</div>
CSS:
#wrapper {
position: relative;
width: 960px;
}
#submenu {
position: absolute;
left: 0;
top: 0;
width: 320px;
height: 320px;
}
#contact-block {
position: absolute;
left: 0;
bottom: 0;
width: 320px;
height: 160px;
}
#content {
position: relative;
left: 320px;
right: 0;
top: 0;
width: 640px;
height: 640px;
}
//#content position is relative for the #wrapper to stretch.
//The left property is equal to the width of the #submenu or #contact-block element
A good point of this technique is that it gives you cleaner HTML. I believe it will be easier to make a mobile version of your version if the need arise.
The jsfiddle
Additional thought:
The #wrapper element could easily be removed in favor of you body element, which is a great step towards semantic HTML. Check this out!
The position of your absolute positioned element depends on the first ancestor-element, which is not positioned static ( which is the default, so you have to explicitely set it to relative(or absolute) ).
So, make sure, your enclosing #left container has 100% document-heigth and position:relative, and everything is well.
I would suggest putting the red div inside the right long div and at the end of it. Then use position: relative and negative left margins on the red div to push it out to the left. This way, as your right div expands, your red div always stays at the bottom of it.