Centering and correct element (div) order - css

Please forgive my lack of design knowledge but I am confused by a bit of div positioning. I have a header div. Within that header I want two divs, one for the logo, and below that another for some content. I have labeled them logo and card respectively. However, when I try to put them both into the html, calling them in proper order, the card div simply lays atop the logo div rather than beneath it. I have tried using pretty much every variation of "clear: xxx" both in the css and inline within the html but they have no effect whatsoever. Can someone explain why this isn't working? Posting relevant css and html below.
#header {
height:440px;
width: 100%;
margin-top: 0px;
background:url(/assets/header-tail.gif) 0 0 repeat-x #f7f7f7
}
#header .logo {
position:absolute;
top: 3px;
left: 50%;
margin-left: -198px;
}
#header .card {
position: absolute;
left: 50%;
margin-left: -500px;
height: 367px;
width: 999px;
background:url(/assets/hback.png) 0 0 no-repeat;
clear: left;
}
And the HTML:
<div id="header">
<div class="logo"><%= link_to image_tag("srlogo.png",alt:"Logo"), 'index.html' %></div>
<div class="card">Some text here</div>
</div>
Any help is greatly appreciated.
Edit: So yes, clearly I am an idiot for trying to use "clear" with no floated elements. I understand that now. So how do I get one division below, rather than on top of, the other?

Get rid of all your absolute positioning. It's rubbish.
http://jsfiddle.net/2BpfF/1/
If you want the .logo DIV to be centered on the page and you know it's width you can do this:
#wrapper {
width: 999px;
margin: 0 auto;
}
#header {
height:416px;
margin-top: 0px;
}
#header .logo {
margin: 0 auto;
width: 333px;
}
#header .card {
background-image: url(http://lorempixel.com/999/367/);
background-repeat: no-repeat;
height: 367px;
}
HTML
<div id="wrapper">
<div id="header">
<div class="logo"><img src="http://lorempixel.com/333/49/" /></div>
<div class="card">Some text here</div>
</div>
</div>
margin: 0 auto; adds 0px margins to the top and bottom of the DIV while calculating the left and right amount for you so it will center. This will only work with a known width.
It seems like you want to center everything. So I would start with a wrapper DIV and center that. I did this with #wrapper.
Remember that source order matters and that by default your .logo DIV will display before your .card DIV without any CSS.
You can also remove the width: 100%; from your #header DIV as all DIVs by default are block level elements. Block level elements always take up the full width of their containing element unless told otherwise.
As for the opacity of the background image I think the best solution would to do this for your image file and not with CSS as I don't think opacity is very versatile yet. What I mean my this is if you set opacity: 0.5; to a DIV, then everything in that div is 50% opaque. I'm not a guru on opacity so you'd have to dig into that a little deeper. But I would just set the opacity in your image editor to 50% and output a PNG file so the alpha(opacity) chanels will be there. JPG files do not have alpha channels for transparency.

You don't have any floated elements, so clear has absolutely no effect.
Absolute positioning removes the element from the document flow, so floating and clearing will have no effects on such positioned elements, since floating adjusts the element within its content flow.

Avoid absolute positioning. Same thing you can achieve using something like this :
#header {
height:440px;
width: 100%;
margin-top: 0px;
background:url(/assets/header-tail.gif) 0 0 repeat-x #f7f7f7
}
#header .logo {
padding: 5px;
text-align:center;
}
#header .card {
margin-left: auto;
height: 367px;
width: 999px;
background:url(/assets/hback.png) 0 0 no-repeat;
clear: both;
padding: 5px;
}

Related

Css position absolute leaves white space at the top

I want to absolute position a div but it is not sticked to the top but has a blank space. Container has position:relative and inner block has position:absolute css rules. I tried to play with the code and noticed that changing background-position has some effect and I have no idea why.
<header>
<div class="header-wrapper">
<div class="header-slogan-1 text-center">Base info</div>
<div class="header-info">Info</div>
</div>
</header>
What I want is to have the green block at the top (see fiddle).
Here is the fiddle
Please can anyone explain the behaviour and answer why the block is shifted from the top?
It is shifted from the top, because it is relative to its parent .header-wrapper, that has a top margin. In order to get the desired result, you have to remove position: relative from its parent, therefore it will be relative to the viewport and will be placed at the top.
Edit: I realised, that he margin is actually applied to the child of the wrapper, causing margin collapsing. In order to fix this, you need apply overflow: auto to the parent element. By doing that, you can still have a position: relative on the wrapper, as it is not pushed down by the child. Take a look at the demo:
/* header block */
header {
height: 536px;
background-color: #ddd;
background-position: center;
background-size: 100% 536px;
background-repeat: no-repeat;
overflow: hidden;
}
header .header-wrapper {
position: relative;
overflow: auto;
z-index: 2;
}
.header-slogan-1 {
font-size: 32px;
font-weight: 700;
font-style: italic;
margin-top: 88px;
}
.header-wrapper .header-info {
position: absolute;
top: 0;
z-index: 3;
background-color: #4caf50;
max-width: 600px;
padding: 25px 25px 25px 75px;
color: #fff;
}
.text-center {
text-align: center;
}
<header>
<div class="header-wrapper">
<div class="header-slogan-1 text-center">Base info</div>
<div class="header-info">Info</div>
</div>
</header>
If I'm understanding this correctly, you want the header to have no space around it. If this is the case, then just add
body {
margin: 0;
padding: 0;
}
to the top of your css and you should be all set.
I changed margin-top: 88px; into padding-top: 88px; of header-slogan-1 as it does not change my layout. I have an image in wrapper class and it is centered and may exceed the container size, so I need position:relative and overflow:hidden.
Finally I decided to pick my solution. Sorry Adam for not choosing your answer.

centering an image horizontall and vertically inside a DIV in ASP.NET

I want to display an ASP.NET image in the middle of a div (both horizontally and vertically), how should I arrange my div and image (image should be runat=server), also I should set max-width and max-height styles for my image, DIV acts as a placeholder, and my image should be inside the DIV, and it should be exactly centered both horizontally and vertically, can you show my the correct HTML and CSS? is there any sample?
Use:
CSS
.placeholder{min-width:200px; min-height:200px;}
.placeholder img{
margin: 0px auto; /*centers element horizontally*/
vertical-align:middle; /*centers element vertically */
}
Your html should like something like:
<div class="container">
<div class="placeholder">
<!-- load image here -->
</div>
</div>
I worked REALLY HARD on finding the solution, hopeful it's 100% helpful :-)
<html>
<style>
.placeholder {
width: 250px;
min-width: 250px;
height: 250px;
min-height: 250px;
display: block;
margin-left: auto;
margin-right: auto;
position: absolute;
left: 50%;
top: 40%;
margin: -100px 0 0 -125px;
}
</style>
<IMG class="placeholder" src="http://www.swoo.co.uk/content/images/icons/stackoverflow.png">
</html>
Replace any img you want in place of the URL that equals to the "src".
Note that it works also while zoom in and zoom out in the browser, it stays EXACTLY in the MIDDLE of the page, best code you could find :)

css three column divs - middle div fixed width - outer divs elastic

Could someone show me how to create three column divs with the middle div 1040px width and the left and right divs are elastic so they contract when the window is resized. this will center the middle div at all times.
There is a way, I just find out! :D
HTML:
<div id='siteHeadLeft'></div>
<div id='site'>
<div id='siteHeadRight'></div>
CSS:
#siteHeadRight{
height:95px;
float: right;
margin-right: -500px;
background-image:url(../IMG/menu_bg.png);
background-repeat:repeat-x;
width: 50%;
}
#site{
float: left;
width:1000px;
}
#siteHeadRight{
height:95px;
float: right;
margin-right: -500px;
background-image:url(../IMG/menu_bg.png);
background-repeat:repeat-x;
width: 50%;
}
You can center a fixed width div simply by adding this CSS:
div {
width: 1040px;
margin: 0px auto;
}
If you want to add a background, you would add it to the div's parent element, or the body of the page.
If your goal is simply to center the middle div, simply give it the following CSS properties:
width: 1040px;
margin: auto;
position: relative (or static. NOT absolute or fixed);
You can set a tiling background-image for the body element that will cover the area on either side of your div.
EDIT: Here's an example that comes pretty close to what you're looking for: http://jsfiddle.net/kqVFy/

Sizing embedded divs

I am trying to emulate a sort of pop-up help dialog for my web site.
When the user clicks help, a mask covers the whole page with a dark, partially transparent mask, and a helppage div with a higher z-order is made visible.
The helppage div is 80% wide and 90% high positioned absolute at left 10% and top 5%, all relative to body.
So far all is great.
The content of the helppage div is:
- a full-width header with a close anchor 20px high floating right.
- a iframe-div to occupy the rest of the helppage div containing:
- an iframe to display the html document in question
The problem:
I would expect the height of the iframe-div to be 20 px less than the helppage div, but for some odd reason it is 3px larger than the helppage div.
As a result the bottom of the iframe is invisible.
The html:
<div id="helpbox">
<div id="helppage" class="window" style="display: block; position: absolute;">
<div class="hd-header">
<a class="close" onclick="hidehelp()"></a>
</div>
<div class="iframe-div">
<iframe id="HelpPageFrame" src="/help-system.html"></iframe>
</div>
</div>
The css:
#helpbox .window {
position:absolute;
display:none;
z-index:9999;
}
#helpbox #helppage {
background: white;
width:80%;
left: 10%;
top: 5%;
height:90%;
padding: 0px;
overflow: hidden;
}
#helppage iframe {
border: none;
width: 100%;
height: 100%;
}
#helppage .iframe-div {
width: 100%;
position: relative;
display: inline-block;
}
#helpbox .hd-header {
height: 20px;
font-weight: bold;
overflow: hidden;
display: inline-block;
width: 100%;
}
#helpbox .close {
width:20px;
height:20px;
display:block;
float:right;
clear:right;
background:transparent url(images/close_icon_double.png) 0 0 no-repeat;
}
Any suggestions would be greatly appreciated
edit As mixel pointed out, an important detail slipped, when I trie to simplify the scenario, that has been corrected.
Please, be accurate, when you are asking questions.
There is whitespace in '#helppage .window' selector. It selects nothing. Because of that '#helppage' is not absolute positioned.
There is no '#helpbox' element.
edit
Though you are still a bit innaccurate (you forgot to close 'DIV'), there is answer. You need to fill the remainder of '#helppage' with '.iframe-div'. If you set '.iframe-div' height to 100%, it takes 100% height of parent element - '#helppage'. To solve this problem you need absolute positioning for '.iframe-div':
#helppage .iframe-div {
position: absolute;
top: 20px;
bottom: 0;
right: 0;
left: 0;
}
Or set height with javascript.
Check out this: Make DIV fill remainder of page vertically?
It's fairly common question.

How to center an image horizontally and align it to the bottom of the container?

How can I center an image horizontally and aligned to the bottom of the container at the same time?
I have been able to center the image horizontally by its self. I have also been able to align the bottom of the container by its self. But I have not been able to do both at the same time.
Here is what I have:
.image_block {
width: 175px;
height: 175px;
position: relative;
margin: 0 auto;
}
.image_block a img {
position: absolute;
bottom: 0;
}
<div class="image_block">
<img src="..." border="0">
</div>
That code aligns the image to the bottom of the div. What do I need to add/change to make it also center the image horizontally inside the div? The image size is not known before hand but it will be 175x175 or less.
.image_block {
width: 175px;
height: 175px;
position: relative;
}
.image_block a {
width: 100%;
text-align: center;
position: absolute;
bottom: 0px;
}
.image_block img {
/* nothing specific */
}
explanation: an element positioned absolutely will be relative to the closest parent which has a non-static positioning. i'm assuming you're happy with how your .image_block displays, so we can leave the relative positioning there.
as such, the <a> element will be positioned relative to the .image_block, which will give us the bottom alignment. then, we text-align: center the <a> element, and give it a 100% width so that it is the size of .image_block.
the <img> within <a> will then center appropriately.
This also works (taken a hint from this question)
.image_block {
height: 175px;
width:175px;
position:relative;
}
.image_block a img{
margin:auto; /* Required */
position:absolute; /* Required */
bottom:0; /* Aligns at the bottom */
left:0;right:0; /* Aligns horizontal center */
max-height:100%; /* images bigger than 175 px */
max-width:100%; /* will be shrinked to size */
}
wouldn't
margin-left:auto;
margin-right:auto;
added to the .image_block a img do the trick?
Note that that won't work in IE6 (maybe 7 not sure)
there you will have to do on .image_block the container Div
text-align:center;
position:relative; could be a problem too.
This is tricky; the reason it's failing is that you can't position via margin or text-align while absolutely positioned.
If the image is alone in the div, then I recommend something like this:
.image_block {
width: 175px;
height: 175px;
line-height: 175px;
text-align: center;
vertical-align: bottom;
}
You may need to stick the vertical-align call on the image instead; not really sure without testing it. Using vertical-align and line-height is going to treat you a lot better, though, than trying to mess around with absolute positioning.
Remove the position: relative; line. I'm not sure why exactly but it fixes it for me.
have you tried:
.image_block{
text-align: center;
vertical-align: bottom;
}
#header2
{
display: table-cell;
vertical-align: bottom;
background-color:Red;
}
<div style="text-align:center; height:300px; width:50%;" id="header2">
<div class="right" id="header-content2">
<p>this is a test</p>
</div>
</div>

Resources