Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding padding for text

Tags:

html

jquery

css

In the following example the heading text is on several lines. Is it somehow possible to add the left and right padding on each line (without wrapping each word in a different element, of course)?

If not possible with CSS only, can it be done with jQuery?

HTML:

<div class="wrap">
<h1>Donec commodo sapien lectus, nec gravida magna</h1> 
</div>

CSS:

.wrap{
    width: 100px;
}

h1{
    display:inline;
    background:green;
    color:#fff;
}

Demo: http://jsfiddle.net/289oo48b/

enter image description here

like image 927
user1355300 Avatar asked May 06 '15 07:05

user1355300


People also ask

How do you add padding to text in HTML?

When you want to add space around an HTML element's content then you'll use the padding properties. The padding shorthand property allows us to set the padding on all four sides at once instead writing out padding-top , padding-right , padding-bottom , padding-left .

What is text padding?

Generally, padding is the space between the things for anything the same as in the HTML refers to the space between the HTML contents and its borders. The HTML padding is also a property for using the web pages more attractively and highlight the web sites.

What's padding in HTML?

An element's padding area is the space between its content and its border. Note: Padding creates extra space within an element. In contrast, margin creates extra space around an element.

Can I add padding to an A tag?

Anchor links ( a elements) are inline elements, they can't have paddings.


2 Answers

By adding one container (span for example) element for the content of the h1:

HTML:

<div class="wrap">
    <h1>
        <span>Donec commodo sapien lectus, nec gravida magna</span>
    </h1>
</div>

And using following CSS:

.wrap {
    width: 100px;
    line-height: 1.3;
    padding: 2px 0; 
    border-left: 20px solid green;
    margin: 20px 0;
}

h1 {
    display:inline;
    background:green;
    color:#fff;
    padding: 4px 0;
    margin: 0;
}

span {
    position: relative;
    left: -10px; 
}

JSFiddle: https://jsfiddle.net/anini/rwLenu6w/

like image 135
Mohammad Anini Avatar answered Sep 26 '22 01:09

Mohammad Anini


.wrap{
    width: 100px;
}

h1{
    display: inline-block;
    white-space: pre-wrap;
    color:#fff;
    padding: 0 10px;
    background: green;
    word-spacing: 100px;
}

fiddle: http://jsfiddle.net/a56kap9L/

like image 32
braks Avatar answered Sep 26 '22 01:09

braks