Problem
I have a header with the basic HTML structure
<div id="header">
<div id="logo"></div>
<div id="navigation"></div>
<div id="userInfo"></div>
<div class="headRight"></div>
<div id="callCenter" class="headRight"></div>
</div>
I cannot change the HTML. Currently it is laid out with floats, and navigation was aligned to the bottom of the header using padding-top. However, it only works when userInfo is 2 lines, and it can be 3 or 4.
What I need to do
Using only CSS, align navigation to the bottom for all nav heights while maintaining the original layout.
What I've tried
Half a dozen stack overflow solutions including the classics position:absolute and vertical-align:bottom on parent. (The former breaks the document flow, and the latter seems not to work because other elements get in the way.)
The fiddle
Cleaned fiddle best I could, but inspect will probably still be easiest.
https://jsfiddle.net/ohrhe4u5/1/
Notes:
The tabs should just touch the bottom of the header.
callCenter is misaligned in this example as well, but you can ignore. It's much lower priority.
New fiddle
I changed header, logo, and navigation to display:inline-block, allowed userInfo to float right, gave the nave extra height to make sure there's always room, and absolute positioned the headRight items.
That leaves me with this. A little janky due to the absolute positioning and forcing the nav height larger. Any better ideas?
https://jsfiddle.net/ohrhe4u5/2/
I generally dislike float for positioning where i can help it (this is a personal preference because i find it sometimes painfully unpredictable), as such, using a combination of position:absolute, min-height and margin i believe i have recreated what you're after.
Basically this solution works by position:absolute'ing the elements that we have some idea of consistent sizes of (the logo and the navigation), then have the header element take its height from the user data and links on the right. We add a min-height to the header element though so that should the user data be reduced to 2 lines, the height is still enough to accommodate the absolutely positioned elements (given they no longer affect the height of the header element by being absolute).
JSFIDDLE
CSS
/* new parts of the css */
#header {
min-height:112px; /* in case user data is made smaller */
padding:10px 10px 0 20px;
position:relative;
}
#logo {
width: 210px;
position:absolute;
top:50%;
width:210px;
height:62px;
left:20px;
margin-top:-32px;
z-index:1; /* bring logo above the user data */
}
#navigation {
position:absolute;
bottom:0;
left:210px;
font-size: 20px;
height: 40px;
z-index: 1; /* bring navigation above the user data*/
}
#userInfo table{
margin:0 0 0 auto;
}
.headRight{
text-align: right;
padding-bottom: 0.2em;
}
Related
I am trying to mark some div.ad containers with a label on the top so that visitors to a website knows that the container contains ads.
However, sometimes ads might not be served, and the container will have an height of 0px. So that it does not affect the view of the page.
I tried this:
.ad:before {
content: "This is an ad";
display:block;
font-size:10px;
}
However, this ads about 10-12 px to the container no matter if it has content or not. So when the .ad container is 0px, its still renders the :before.
I also tried:
.ad:before {
position: absolute;
top:0;
left:0;
content: "This is an ad";
}
That works to a point. It does not show when the .ad container is 0px high, but when it actually contains and ad, the ad is rendered over the text. Is there any way to implement that last solution, in such a fassion that it is only shown when the .ad container is larger than 0px of height, and so that it pushes the content of .ad down the same amount of px as the content I add to the :before?
you can use z-index to insure that the pseudo element always appear on top of anything inside .ad container. this will require setting the content to z-index that is lower than the pseudo element like that:
.ad:before {
position: absolute;
top:-14px;
left:0;
font-size:14px;
content: "This is an ad";
z-index:2;
}
.ad,.ad * {
position:relative;
z-index:1;
}
working fiddle : https://jsfiddle.net/35gh307r/
You can just add some padding-top to the .ad container, this way even if there is no content the container will still have the same padding.
I've edited the fiddle of #Heidar
https://jsfiddle.net/35gh307r/2/
I have searched and searched. Fiddled and tweaked. Spent hours trying different suggestions and code ideas...
How can I have the content on my webpage, be centered horizontally with regards to the browsers view port. No matter what I do, I always end up with uneven margins and it is quite noticeable.
The closest so far is:
#MyStyleName
{
margin: 0 auto;
width: 90%;
}
However, because I am using a percentage, no matter what I do with margins/alignment it just defaults to being on the left edge of the page.
extra info:
My issue relates to the top-level, most outer page element. The very first or that holds the content of everything else.
I am not sure what you are asking, but here is a example of af centered <div> with a percentage with.
I also added border-sizing: border-box which enables you to use padding.
#MyStyleName {
margin: 0 auto;
width: 90%;
display:block;
background:red; /* Only for testing */
box-sizing:border-box;
padding: 10px;
}
<div id="MyStyleName">
Hi!
</div>
If your #MyStyleName is a block element, then it should work. If not, then it's not gonna work. In such case you can add display:block; to the selector style.
Have a look at, http://thomaspalumbo.com
I have this CSS for my website's container:
.graybox {
padding: 0 30px 30px 30px;
background: #ededed;
position:absolute;
left:0;
right:0;
}
Then I have a container on top of that to center that info.
The .graybox container spreads the width of the page like I want but now my footer div is hidden, according to firebug is it actually behind? And up on the page?
Is there a fix for this?
While I'm here can anyone explain the white space on the right side of the page. It comes into effect once the page is resized smaller.
You can use the CSS z-index property to make sure your footer is in front of the content. Z-index only works when the element is positioned though. So make sure you add position:relative to your footer
#footer{
position:relative;
z-index:999;
}
Read more: http://www.w3schools.com/cssref/pr_pos_z-index.asp
EDIT
Just checked out the code of your website, and I don't understand why your graybox is positioned absolutely, this will only make things more complex. The same goes for your menu, why position it absolute, why not just add it in the right order in the HTML in the first place?
EDIT
If you want to center your content but with a background that has a 100% width then you can simply add a container div like so:
HTML
<div class="container">
<div>lorem ipsum....</div>
</div>
CSS
.container{
background:red;
}
.container div{
width:400px;
margin:0 auto;
background:yellow;
}
See JSFiddle here: http://jsfiddle.net/HxBnF/
Currently you cannot do this because you have a container which you set at 980px, don't ever do that unless you are sure you don't want anything to wrap over it, like in this case the background of a div in that container.
in the div style, just assign a z-index value greater than any other z-index such as
.divClass{
position: absolute;
z-index: 1 //if other elements are still visible chose a higher value such as 20 or even higher.
}
I need the menu (home, portfolio, services, about) is aligned in the middle and left.
The div#header-login should be flush right
How can I accomplish these tasks?
To solve the first problem, I put the divs with display: inline; but for some reason the ul#header-menu is leaving a space at the top
To solve the second problem, I tried to put the div#header-login with 100% width and thus align the text to the right but failed.
Here is the complete code:
The easiest way to achieve this is to make sure you float everything in the header. With the current mix of some float and some non-float (plus some elements with display:inline), this will be tricky to manage and potentially problematic if you need this to work in older versions of Internet Explorer.
I've made a few small modifications to your jsFiddle. This now floats the 3 elements in the header and applies float clearing to the header div itself so that the content after the header clears properly (there are also commented examples of how you'd need to do this for the IEs with conditional stylesheets).
http://jsfiddle.net/y4Qyw/1/
I've not tweaked the spacing specifically, but it should be a formality now to position everything where you want with some padding and/or margin. Automatic vertical positioning in this situation isn't possible unless you're working with display:table-cell (which isn't entirely cross browser), so you'll just need to vertically offset your menu downward to get it centre-aligned.
Here is the deal:
div#header
{
clear:both;
overflow:hidden;
}
div#header-login
{
text-align: right;
overflow:hidden;
float:right;
margin-top:-30px;
}
img#header-logo
{
display: block;
float:left;
}
ul#header-menu
{
margin: 0 auto;
padding: 15px;
display: block;
list-style-type: none;
overflow:hidden;
}
This might be a very simple question, but I can't get it working.
All I want is to have 2 boxes (left and right), both should take 50% of the space and they should show up next to each other.
My current css looks like this:
#left {
text-align: right;
width: 50%;
padding-right: 10%;
float: left;
}
#right {
width: 50%;
text-align: left;
padding-left: 10%;
}
#footer {
clear: both;
}
The HTML looks like this:
<div id='left'>
<h1>Left</h1>
<ul>
<li>Some Listing</li>
</ul>
</div>
<div id='right'>
<h1>Stuff</h1>
<p>
Stuff right
</p>
</div>
<div id='footer'>
</div>
As I said, it isn't working. But I think it should be clear what it should do.
You have to take the padding and margins into account. Putting 50% on each <div> while specifying any padding other than 0, will cause the <div> to wrap. Try removing the padding on the <div>, or reducing the width from 50% to, say, 45% and see what it looks like.
There are 2 things I needed to do to make it work:
1) The width + padding of each div must only add up to 50%. Otherwise, in your original code, they add up to 60%, and both add up to 120%, and they can't fit in the 100% width of the body.
2) I have to also float the second div to the left, or make both div overflow: hidden
(i am still looking into why step 2 is needed)
A full style reset will make sure you avoid falling foul of anything that XSaint mentioned. Margins, Borders and padding will affect this.
So you should make sure that these elements have:
div {
margin: 0;
padding: 0;
border: 0;
}
If you wish to have padding and borders, be sure to reduce the width of the elements accordingly.
One document worth referencing is the box model, that picture is worth 1000 words:
http://www.w3.org/TR/CSS2/box.html
In the note below that diagram, it states that the width affects the width of the content box, not the padded, bordered or margined box. That is the box inside all the others.
you may either do what XSaint32 has suggested or remove the padding from the #left div and put another div #context with the padding inside the #left div. i.e)
Xsaint and Danny Staple gave the best answers so far.
Just complementing their answers, you can also use a property named "box-sizing" in order to ensure correct calculations.
I even recommend adding this property to your (and everybody else) CSS reset, hence Webkit, IE, Opera and Mozilla tends to use different box models.