1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to have DIVs in % and borders in pixels?

Discussion in 'HTML & Website Design' started by Mr.Dog, Nov 14, 2013.

  1. #1
    Hi,

    I am having trouble setting my DIVs in percentage terms, while their borders must be in pixels.

    I want to make it as simple as possible. Yes, I could use an enternal wrapper DIV whose background could act as a border... but that's too complicated.

    Any simple way for combining pixels with percentages and make it all look well?

    Useful tips?
     
    Mr.Dog, Nov 14, 2013 IP
  2. vjproduction

    vjproduction Greenhorn

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #2
    Check this fiddle:

    http://jsfiddle.net/aL5zB/
     
    vjproduction, Nov 14, 2013 IP
  3. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #3
    You didn't understand,... my problem is when using percentage together with pixels (for borders and margins).

    My CSS is:

    
    wrapper{margin: auto; padding-top: 4px; overflow: hidden; width: 800px; border: none; background-color: #FFFFFF;}
    .left{float: left; width: 60%; border: 1px solid #B9B9B9; background-color: #F5F5F5; color: #555555;}
    .right{float: left; width: 40%; margin-left: 5px; border: 1px dotted #6A83BC; background-color: #FFFFFF; color: #686868;}
    HTML:
    Can't make it look nice if pixels are used in combination with percentages.
     
    Mr.Dog, Nov 14, 2013 IP
  4. HDaddy

    HDaddy Active Member

    Messages:
    287
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #4
    How about this?
    CSS:

    body {background: #ccc;}
    
    #div{
        width:50%;
        height:500px;
        border:5px solid white;
        margin: 10px auto;
        background: black;
        overflow:auto;
    }
    Code (markup):
    It gives you this:

    sample.PNG
     
    HDaddy, Nov 14, 2013 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    The problem is you're trying to set BOTH elements as % and float -- you only need one float, let the other one do what DIV do, auto-expand to the remaining space. To prevent the de-indent, set overflow:hidden on the second one and throw in a haslayout trigger for good measure.

    ... and please for the love of ghu stop cramming everything onto single lines. Pain in the ass to read and generally prone to making silly errors.

    .wrapper{
    	margin:0 auto;
    	padding-top:4px;
    	overflow:hidden;
    	width:800px;
    	/*
    		fixed width BAD -- and if you're gonna fix the width WTF
    		are you wasting time using % columns?!?
    	*/
    	border:none;
    	background:#FFFFFF;
    }
    
    .left { 
    	/*
    		you should probably say what they are,
    		NOT what they look like as your class
    		
    		I'm assuming this is the first of the two elements in flow
    	*/
    	float:left;
    	width:60%;
    	border:1px solid #B9B9B9;
    	background:#F5F5F5;
    	color:#555555;
    }
    
    .right{
    	overflow:hidden; /* prevent de-indent */
    	zoom:1; /* trip haslayout, prevent de-indent legacy IE */
    	margin-left:5px;
    	border:1px dotted #6A83BC;
    	background;
    	color:#686868;
    }
    Code (markup):
    Should do it, assuming your div.left is before your div.right in the code order... though as the comment says, you should REALLY have better names than 'left' and 'right' and maybe say what those content areas ARE.

    If the second one is set to auto-adjust to the available space, you're automatically golden and it saves you the math headaches.

    This method also makes it easier to open up the layout fluid or semi-fluid, and I'd suggest making the sidebar a fixed/elastic width instead of % since usually percentage layouts are flimsy broken crap.
     
    deathshadow, Nov 14, 2013 IP
    ryan_uk likes this.