Noupe Editorial Team June 15th, 2008

Using CSS to Fix Anything: 20+ Common Bugs and Fixes

Without a doubt, a logical and structured layout is the best way to go. Not only because your layout varies between browsers, but also because CSS has a lot of ways to position every element you have. Today we wanted to share with you some quick tips on how to avoid easy pitfalls when creating your CSS layout. This is the first part in this series as there are SO MANY good tricks out there and if you see an easier or better methods, then post a comment below or email me. I will do my best to include it in our next post in this series.
You might be interested to check our other related post for some inspiration:

IE Bug Fixes

1- Bug Fix: IE Double Margin Float Bug- It’s an Internet Explorer-exclusive bug wherein an element that is floated – and given a margin in the same direction as the float – ends up with twice the specified margin size. The fix is extremely simple. All you have to do is apply a display: inline rule to your floated element. So you simply go from something like this:
#content {
    float: left;
    width: 500px;
    padding: 10px 15px;
    margin-left: 20px; }
To something like this:
#content {
    float: left;
    width: 500px;
    padding: 10px 15px;
    margin-left: 20px; 
    display:inline;
}


2-Overcoming the Box Model Hack- If you want to specify a width to any div, do NOT specify padding or margins. Just create an inner div with no width set and specify its padding and margins instead. So instead of doing this:
#main-div{
width: 150px;
border: 5px;
padding: 20px;
}
Do something like this:
#main-div{
width: 150px;
}
#main-div div{
border: 5px;
padding: 20px;
}

3-Min-height attribute ignored in IE- "min-height" attribute works well in Firefox but IE ignores it. IE's height act as FF's min-height. Note: in IE 7 problem was fixed.
/* for understanding browsers */
.container {
width:20em;
padding:0.5em;
border:1px solid #000;
min-height:8em; 
height:auto;
}
/* for Internet Explorer */
/**/
* html .container {
height: 8em;
}
/**/
4- Min-Width for IE -A fix for the lack of min-width in Internet Explorer.

Centering a Block Element

5-Centering Block Element- There are multiple techniques for centering a block element; and the choice for the technique depends on whether you have the size set in percentages or in more absolute values. Centering an entire page's contents:
body{
text-align: center;
}
#container
{
text-align: left;
width: 960px;
margin: 0 auto;
}

6-Vertical Align with CSS- If you want to know how you can achieve vertical-align functionality the right way, simply specify the line-height for your text the same height as that of the container’s.
#wrapper {
	width:530px;
	height:25px;
	background:url(container.gif) no-repeat left top;
	padding:0px 10px;
}
#wrapper p {
	line-height:25px;
}

Column Issues

7- Top reasons your CSS columns are messed up- An easy-to understand article on how to fix common CSS Column issues with helpful diagrams and code snippets.


8- The Expanding Box Bug-When you try to create a two-column float layout, IE will create a "float drop", and it's caused by having over-sized content in a fixed-width floated div that must fit into a particular spot in the layout. Several possible workarounds are detailed in this post.


CSS Positioning

9- Understanding CSS Positioning part 1- An interesting series of articles that doesn’t only cover positioning, but also properties that define layout such as display and float, and a preview of the new CSS3 layout modules. The first part will introduce the positioning and display property. Part1, Part2, Part3 gives you a deep understanding of the possibilities you have in positioning.


10- What is the difference between relative and absolute positioning?- Whether to use relative or absolute positioning can be extremely frustrating to people just getting started with CSS. The answer to this question will add a bit of clarity to this confusion.
#redSquare
{
position: relative;
bottom: 40px;
right: 40px;
}

11-HangTab- Create a Sticky Tag from the Edge of the Browser Window (Even with Centered Content). Check out Panic’s website for their software Coda.
#hang_tab {
position: absolute;
top: 7px;
left: 0px;
width: 157px;
height: 93px;
}


CSS Float Concept

12- CSS Float Theory: Things You Should Know- SmashingMagazine browsed through dozens of related articles and selected the most important things you should keep in mind developing css-based layouts with floats.
<div> <!-- float container -->
<div style="float:left; width:30%;"><p>Some content</p></div>
<p>Text not inside the float</p>
<div style="clear:both;"></div>
</div>

13- Floatutorial: Simple tutorials on CSS Floats- Floatutorial takes you through the basics of floating elements such as images, drop caps, next and back buttons, image galleries, inline lists and multi-column layouts.


14- Clear Your Floats - The Right Way- Float clearing can be one of the most frustrating aspects of CSS development, one of the best ways is to use the EasyClearing on your site.
/* EasyClearing http://www.positioniseverything.net/easyclearing.html */
#container:after
{
	content: ".";
	display: block;
	height: 0;
	clear: both;
	visibility: hidden;
}

#container
{display: inline-block;}

/* Hides from IE-mac */
* html #container
{height: 1%;}

#container
{display: block;}
/* End hide from IE-mac */


Easier Rounded Corner Solutions

15- Mike asks the CSS Guy for recommendations on rounded corners- “Simplest way is to use a giant gif, then I'll markup my box”
<div class="roundBox">
  <p>beautifully-encapsulated paragraph</p>
  <div class="boxBottom"></div>
</div>
“And give it the background”
.roundBox {
  background:transparent url(roundBox.gif) no-repeat top left;
  width:340px;
  padding:20px;
}
.roundBox .boxBottom {
  background:white url(roundBox.gif) no-repeat bottom left;
  font-size:1px;
  line-height:1px;
  height:14px;
  margin:0 -20px -20px -20px;
}

Also Askthecssguy explains the technique used in Google Analytics, which works by leaving a 1px notch in every corner to get the rounded corner effect which is a new way of creating rounded corners without using static images.


16-3 Simple Steps in Coding a Rounded Corners Layout- Alen Grakalic's approach to coding a fixed width, rounded corners layout in 3 simple steps. He also created a demo here.


CSS Form Issues

17- Tips For Creating Great Web Forms- Cris Coyer shares with us some tips for floating labels, :focus Pseudo Class, using hints and more. He also created the Nice & Simple Contact Form, which he first posted about here.
label {
	float: left;
	text-align: right;
	margin-right: 15px;
	width: 100px;
}


18- Clean and pure CSS FORM design- For CSS lovers, this tutorial illustrates a proposal about how to design a pure CSS form without using html tables. You can grab the code here.


19-Autopopulating text input fields with JavaScript- Sometimes we need to explain to users what they are supposed to enter into text input fields. One common workaround when no label can be displayed is to put some placeholder text in the text field and let that act as the label. This tutorial introduces a nice solution, with JavaScript enabled, the label element is hidden and the value of the input element’s title attribute is copied to the value attribute. If JavaScript is disabled, the label is displayed above the text input, which is left empty.

Worth checking CSS Tricks

20- Cross browser Horizontal Rule with Background Image- You'd like to create a cross-browser horizontal rule that utilizes a custom image as the content separator.
div.hr {
background: #fff url(myhrimg.gif) no-repeat scroll center; 
height: 10px
}
div.hr hr {
display: none
}

Your tag should look like this:
<div class="hr"><hr /></div>

Noupe Editorial Team

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.

74 comments

Leave a Reply

Your email address will not be published. Required fields are marked *