Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pad a div without extending the border?

I have this code:

<html>
<head>
<style type="text/css">
.container {
    border-bottom: 1px dotted #999999;
    width:500px;
    padding-left:200px
}
</style>
</head>
<body>
<div class="container">asdf</div>
</body>
</html>

And it works fine except for the fact that the bottom border is also applied to the 200px before the indent. I want the bottom border to start at 200px. Can this be done?

like image 399
John Smith Avatar asked Mar 13 '11 01:03

John Smith


People also ask

How do you change padding without increasing width?

To avoid the width or height getting increased or decreased when using CSS properties like margin , padding , etc, we can use the CSS property called box-sizing and set its value to border-box on the element in CSS.

Can you add padding to a border CSS?

The CSS padding properties are used to generate space around an element's content, inside of any defined borders. With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left).

Does width 100% include padding?

If you set an element's width to 100 pixels, that 100 pixels will include any border or padding you added, and the content box will shrink to absorb that extra width.


2 Answers

Use margin instead of padding or use an inner div.. (updated to match requirements disclosed in comments)

<html>
<head>
    <style type="text/css">
        .container {            
            width:500px;
            padding-left:200px
        }
        .inner{
            border-bottom: 1px dotted #999999;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="inner">
            asdf</div>
    </div>
</body>

This is what it should look like: http://jsfiddle.net/dKVTb/1/

like image 117
markt Avatar answered Oct 15 '22 13:10

markt


Or with calc

<html>
  <head>
    <style type="text/css">
    .container {
        position: relative;
        width:500px;
        padding-left:200px
    }

    .container:after {
        content: '';
        width: calc(100% - 200px);
        position: absolute;
        left: 200px;
        bottom: 0px;
        border-bottom: 1px dotted black;
    }
    </style>
  </head>
  <body>
    <div class="container">asdf</div>
  </body>
</html>

fiddle

like image 25
frontend-trends Avatar answered Oct 15 '22 14:10

frontend-trends