Specifying heights in responsive layouts - html

I've been reading online and have come across numerous blogs and 'Professionals' that say you should not set heights and you should allow your content to fill the element instead. First of all.. why?
And also how can i allow my background colour to show on my header if i don't specify a height without using absolute or fixed position with top:0.
Here is my code:
HTML:
<header>
<div class="row-std">
<div class="logo">
<img src="images/logo.png">
</div>
<nav>
<ul id="navbar">
<li class="nav-item">LINK 1</li>
<li class="nav-item">LINK 2</li>
<li class="nav-item">LINK 3</li>
<li class="nav-item">LINK 4</li>
<li class="nav-item">LINK 5</li>
</ul>
</nav>
</div>
</header>
CSS:
header {
width: 100%;
background: #2f3842;
position: absolute;
top: 0;
}
header div.logo img {
float: left;
width: 20em;
margin-top: 1.5em;
margin-left: 1em;
}
header nav {
float: right;
}
header nav ul#navbar a {
text-decoration: none;
list-style-type: none;
color: #ebebeb;
border: 0;
transition-duration: .25s;
-webkit-transition-duration: .25s;
}
header nav ul#navbar a:visited {
color: #ebebeb;
}
header nav ul#navbar a:hover, header nav ul#navbar a:active {
color: #16a085;
}
header nav ul#navbar .nav-item {
display: inline-block;
padding: 0.3125em 0.3125em;
margin: 2.5em 1em;
font-size: 0.84em;
font-weight: 600;
}
As you can see the header currently has the absolute position with a top property value of '0'. This works for allowing the header to show the background colour and allow the element to be filled in terms of its height by its content but if i was to display an another element beneath this i cannot really do so as it will be displayed under it unless i use a margin-top of 5.5em perhaps to push the element down into its correct position which works fine, but this feels tacky. Is there a better way of doing this.

Having unnecessary heights on elements in responsive designs can result in overflowing content on different screen resolutions and it's generally a good idea to not specify heights and let your content dictate the height of an element.
That being said, there are always circumstances where you need to manually specify heights of elements (usually these circumstances are where elements have no intrinsic height, like when specifying background images on elements with no other content, but other cases do exist).
Exceptions prove the rule. Bottom line when developing responsive sites, don't specify heights...unless you have to :).

Related

transparent navigation bar css

I am currently working on a website and at the top of the site i have a navigation bar that stays at the top of the screen as you scroll. Here is a sample image of it: https://i.imgur.com/R4QiDoP.png
The problem it, when I scroll down, some (but not all) text is visible through the navigation bar and makes it illegible: https://i.imgur.com/LDnZ3ZN.png
Here is the code for the:
HTML
<div class="nav">
<div class="container">
<ul class="pull-left">
<li>Home</li>
<li>Tools</li>
</ul>
<ul class="pull-right">
<li>About Us</li>
<li>Contact Us</li>
</ul>
</div>
</div>
CSS
.nav {
position: fixed;
background-color: #efefef;
border-bottom: 1px solid #dbdbdb;
width: 100%;
top: 0;
opacity: 1.0;
}
.nav a {
color: #5a5a5a;
font-size: 11px;
font-weight: bold;
padding: 10px 10px;
text-transform: uppercase;
}
.nav a:hover {
background: #e1e1e1;
color: black;
}
.nav li {
display: inline;
}
I have already tried changing the opacity but that made it illegible 100% of the time. I am willing to try any suggestions that you have. Thank you!
Check the z-indexes you use in your website and make sure to give the .nav the heigest z-index, that should solve the problem. Do it like this:
.nav {
z-index: ...; /* higher amount than used somewhere else */
}
Let me know if that works or not!
Your nav and the rest of the page are likely on the same 'layer' which is mish-mashing them together when they overlap. Try adding a z-index to .nav to place it 'above' the rest of the page. The z-index number will need to be 1 higher than the current highest z-index on your page (if no other element has a z-index, that would be 1).
.nav {
z-index: 1;
}

Inline Block Elements Overflowing Parent Container

I have a list of 4 menu items sitting side by side using display:inline-block;. Each item is 120px, therefore I should be able to set the parent container to be 480px wide, however this sends the last item into the next row, why is this ??
Here is a jsfiddle :http://jsfiddle.net/htdgdhxn/
My html:
<section id="nav">
<div id="nav-wrapper">
<ul id="nav-list">
<li id="nav-home">Home
</li>
<li id="nav-clothes"><a class="category All">Clothes</a>
</li>
<li id="nav-about">About Us
</li>
<li id="nav-contact">Contact Us
</li>
</ul>
</div>
</section>
CSS:
* { margin: 0px; padding: 0px; }
#nav { background-color: #fff; }
#nav-wrapper { text-align: center; height: 74px; }
#nav-list { height: 100%; width: 480px; }
#nav-list li { display: inline-block; vertical-align: top; width: 120px; height: 100%; }
#nav-list li a { text-decoration: none; color: #000; font-size: 1.6em; display: block; height: 100%; line-height: 74px; }
#nav-list li a:hover { background-color: #F0ECE1; cursor: pointer; }
I have tested and this happens in Chrome, IE and Firefox.
Remove the whitespace between each <li>
<li></li> <...space here...> <li></li>
Inline block elements create a gap between li elements.
<ul id="nav-list">
<li id="nav-home">Home
</li><li id="nav-clothes"><a class="category All">Clothes</a>
</li><li id="nav-about">About Us
</li><li id="nav-contact">Contact Us
</li>
</ul>
See fiddle
The inline-block value is incredibly useful when wanting to control margin and padding on
"inline" elements without the need to block and float them.One problem that arrises
when you use inline-block is that whitespace in HTML becomes visual space on screen.
Gross.There are a few ways to remove that space; some of them are just as gross, one is
reasonably nicer.
Solution 0: No Space Between Elements:
The only 100% solution to this issue is to not put whitespace between those elements in the HTML source code:
<ul><li>Item content</li><li>Item content</li><li>Item content</li></ul>
Solution 1: font-size: 0 on Parent
The best white-space solution is to set a font-size of 0 on the parent to the inline block
elements.
.inline-block-list { /* ul or ol with this class */
font-size: 0;
}
.inline-block-list li {
font-size: 14px; /* put the font-size back */
}
Solution 2: HTML Comments
This solution is a bit gangsta but also works. Using HTML comments as spacers between the elements works just as placing no space between elements would:
<ul>
<li>Item content</li><!--
--><li>Item content</li><!--
--><li>Item content</li>
</ul>
It might help you.
Just increase the width of your container to 500px
#nav-list { height: 100%; width: 500px; }
or remove the white spaces between consecutive li tags
or
apply display:initial in #nav-list { height: 100%; width: 480px;}
i.e #nav-list { height: 100%; width: 480px; display: initial;}
Reason:-
1. The font size of the text in the li element might be causing the problem.
You can modify it by reducing the font-size.
#nav-list li a { text-decoration: none; color: #000; font-size: 1.2em; display: block; height: 100%;line-height: 74px; }
Instead of using this
#nav-list li { display: inline-block; }
You can do like this:-
#nav-list li { display: inline; font-weight:bold;}
Please let me know if this helps.

Ordering lists in Nav bar spacing

I am currently trying to make a nav bar for my website. I am using lists to do so and am having trouble ordering it correctly.
im trying to have the name of my site hanging to the left, 3 lists in the middle and then one hanging to the right.
I am able to get my three in the center correctly but cant get the outside two lists.
Is there a way to space out the lists differently such as adding blank space?
Here is an example of what I have so far. http://jsfiddle.net/DnvLt/2/
Heres my code:
#navbar ul {
margin: 0;
padding: 5px;
list-style-type: none;
text-align: center;
background-color: #000;
}
#navbar ul li {
display: inline;
}
#navbar ul li a {
text-decoration: none;
padding: .2em 1em;
color: #fff;
background-color: #000;
}
#navbar ul li a:hover {
color: #000;
background-color: #fff;
}
You can do it like this
<ul style="overflow:hidden; list-style-type: none;">
<li style="float:left;">Left</li>
<li style="float:right;">Right</li>
<li style="overflow:hidden; display:block; width:300px; margin:0 auto;">
<ul style="list-style-type: none;">
<li style="float:left;">element 1</li>
<li style="float:left;">element 2</li>
<li style="float:left;">element 3</li>
</ul>
</li>
</ul>
At first you wrap all elements in one block (the div), then you place the right and left elements by float and lastly you place the center element statically in the center on top of the floated elements.
to place an element in the center you must make it a block, with a static width and margin:0 auto;
The overflow is there so that the elements that float don't overflow :)

how to make navigation bar stretch across the page (HTML)

I'm having problems with my navigation bar, its not stretching across the page.
Here's the code:
#nav {
list-style: none;
font-weight: bold;
margin-bottom: 10px;
width: 100%;
text-align: center;
}
#nav ul {
list-style-type: none;
margin: 0px;
padding: 0;
}
#nav li {
margin: 0px;
display:
}
#nav li a {
padding: 10px;
text-decoration: none;
font-weight: bold;
color: #FFFFFF;
background-color: #000000;
float: left
}
#nav li a:hover {
color: #FFFFFF;
background-color: #35af3b;
}
<div id="nav">
<ul>
<li>Home
</li>
<li>Music
</li>
<li>Education
</li>
<li>Fun
</li>
<li>Entertainment
</li>
<li>Utilities
</li>
</ul>
</div>
It isn't exactly clear what you want here. If you're wanting the nav bar to continue across the page you need to add the background color to the parent div and make this div the same height as the ul list elements:
#nav {
list-style: none;
font-weight: bold;
margin-bottom: 10px;
width: 100%;
text-align: center;
background-color: #000000;
height:40px;
}
I did a fiddle - http://jsfiddle.net/F6nMg/
Put the background color on the container of the navigation bar (the div). Then, apply a clearfix to the div because the contents are floated. You could probably also use display: inline-block, but you don't have to.
#nav {
background-color: #000000;
}
#nav:after {
content: "";
clear: both;
display: table;
}
http://jsfiddle.net/ExplosionPIlls/DY6Nb/
I understand your problem. this can be achieved by putting display:table on parent div and display:table-cell on all lis in navbar.Then all will behave like teable-cells and take width according to provided space. Read my article at: http://www.aurigait.com/blog/how-to-make-navigation-bar-stretch-across-the-page/
Or Look at the below structure for example:
<nav class="main-menu">
<ul>
<li>Small Link</li>
<li>Another Link</li>
<li>One Another Link</li>
<li class="sp-width">A long link with 40% of total width</li>
</ul>
</nav>
And CSS
ul, li{ list-style:none; margin:0; padding:0;}/*1.1*/
.main-menu ul{background-color:black;} /*1.2*/
.main-menu a{color:white; display:block; padding:5px; text-decoration:none;} /*1.2, 1.3*/
.main-menu a:hover{background -color:#333333; text-decoration:none; color:white;}/*1.2*/
.main-menu > ul{ display:table; width:100%;} /*2.1, 2.2*/
.main-menu > ul > li{ display:table-cell; border-right:1px solid #d4d4d4;} /*3.1, 3.2 */
.main-menu > ul > li:last-child{ border-right:none;}/*3.2*/
.main-menu > ul > li > a{ text-align:center;}/*2*/
.sp-width{ width:40%;}
Now lets add 3 more links in it, so HTML Structure will now:
<nav class="main-menu">
<ul>
<li>Small Link</li>
<li>Another Link</li>
<li>One Another Link</li>
<li>Another Link</li>
<li>Another Link</li>
<li>Another Link</li>
<li class="sp-width">A long link with 40% of total width</li>
</ul>
</nav>
So CSS changes:
.main-menu > ul > li{ display:table-cell; border-right:1px solid #d4d4d4; width:10%;} /*4*/
.sp-width{ width:40%!important;} /*5*/
Points to be noted:
1.1. Global Definition
1.2. Global Definition for Main menu all uls and links. (In case of Sub-menu it will be applied on that sub-menu also)
1.3. Using display:block, so it will cover entire area of li and whole li will be click-able.
2.
2.1. I am using ‘>’(Direct Child) here so if we define any sub-menu inside, this CSS will not work on that.
2.2. ‘Width’ property is necessary with ‘display:table’. Because default width of display:table is ‘Auto’ means as per the inside content.
3.
3.1.Display:table-cell, divides the total width / remaining width(the un-divided width. In our case it is 100%-40%=60%) equally. It always need display:table on its parent container.
3.2. I am using border-right for showing links separately and removing extra border on last-child in the next line.
4. How width is distributed, if we define it explicitly:
4.1. If width is more than the average width(100% / No. of links) then it will give that width to first link and then from remaining if possible then to second link and then rest to other link and if no width left then to rest of the links as per content (with text wrapping as default) and remaining width in proportion as we provided. Example: we have 4 links and we define 50% width for each. So it will assign 3rd and 4th link as per the content and to 2nd and 1st link remaining width’s 50 %.
4.2. If width is less than the average width, it will distribute the width equally in all links.
4.3. If one link is having some specific width and we want all other links with a particular width (Our Case), It will provide the given width to that link(s) and then remaining width will be divided equally to all links including the specific width link.
5. We provide ‘!important’ here because of ‘order of precedence’. The hierarchical definitions have more weight than the class definitions. And ‘!important’ provides supreme power to class definition so it will be applied. I will discuss on Order of Precedence in my later blog.
Make sure in your HTML code, the list elements are under a separate container element, Assign background color to this new container.
For e.g.
.container-nav {
background: #ff3300;
}
<header class="container">
<h1> Monthly Resolutions </h1>
<h2 class=header-h> Dreaming out loud. Take 30 days at a time</h2>
</header>
<div class="container-nav">
<nav class="container">
<ul>
<li>Home
</li>
<li>Archives
</li>
<li>About Me
</li>
</ul>
<div class="clear"></div>
</nav>
<!--nav-->
</div>
<!--container-nav-->
Use this if you want the nav bar to always appear on the top of the screen (Just like stackoverflow's navbar ;)
#nav {
overflow: hidden;
display: flex;
flex-direction: row;
position: fixed !important;
left: 0 !important;
top: 0 !important;
width: 100%;
}
you should use
#nav {
width:100%;
}

IE PNG fix problem

I have applied ie PNG from here: http://www.twinhelix.com/css/iepngfix/
So I can use transparent PNG background images in my CSS. It works on divs but the problem is when I give a transparent background to unordered list (ul) it doesn't work.
Here is the markup:
<div id="footer">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
<p>© 2009 Your Name</p>
</div>
And here are relevant parts of the stylesheet:
/* IE PNG fix */
img, div, ul { behavior: url('/css/iepngfix/iepngfix.htc') }
#footer {
width: 876px;
margin: 0 auto;
background: none;
text-align: center;
line-height: 1.5em;
font-size: .8em;
}
#footer ul {
padding: 40px 0 13px;
background: url('wrapper-bottom.png') center top no-repeat;
}
#footer p {
padding-bottom: 15px;
}
I also tried adding background: transparent; to the #footer div but with no success. Other PNG images applied to divs work but under the wrapper-bottom.png there is a grey background (#333) which is a background of most website content areas but I specifically declared background: none; for the #footer so there should be none :(
EDIT: Actually when I don't specify height for the #footer div, the whole footer has grey background...
EDIT: I actuallly managed to solve this myself few minutes after I posted this. I used a very ugly hack though:
#footer {
height: 0;
}
#footer ul {
height: 30px;
}
This seems to work in all IE versions.
Try using Unitpng Fix.
Its easy to implement and works with background png too...
Check out this link