2

I have the following css class:

#header h1 {
 float:left;
 font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
 font-size: 2.5em;
 margin-top: 8px;
 margin-left: 0.8em;
 padding-left: 40px;
 background: url("h1bkgrnd.png") no-repeat left center;
 height: 100px;
}

And this html:

 <div id="header">
 <h1><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Our Services</h1>
</div>

it works fine, but as you can see, I have a bunch of &nbsp; to push the text to the right. Otherwise, it sits on top of the picture. Is there a better way to do this?

Thanks.

user229044
241k41 gold badges346 silver badges350 bronze badges
asked May 22, 2013 at 19:27
5
  • 2
    If you want the picture to be part of the flow of the document use an <img> tag instead of a background image with CSS Commented May 22, 2013 at 19:28
  • you really shouldn't have the <br> components in your <h1>. if you want it a distance from the top, use margins or padding appropriately. Commented May 22, 2013 at 19:29
  • 1
    Can't you just give it more padding? Commented May 22, 2013 at 19:30
  • From the responses you're giving, its clear that what you're looking for is more than just indenting text (see: the XY problem). cssdeck.com/labs/ybwmvl1d Commented May 22, 2013 at 20:00
  • ok thanks cimmanon. i'll keep that in mind for my next post. Commented May 22, 2013 at 20:09

5 Answers 5

8

Unsurprisingly, to indent text you should use the text-indent CSS property.

#header h1 {
 text-indent: 1em;
 /* ... */
}
answered May 22, 2013 at 19:28
Sign up to request clarification or add additional context in comments.

7 Comments

Are you sure it doesn't work? Because it looks like it works to me: cssdeck.com/labs/vn8ewsh7
meagar... i lied. your way does work too. but i realize why my brain defaulted to "that doesn't work" is because I wasn't able to find a way to push the text down without affecting the image. the <P> tag gives me the ability to control both. but i've marked your answer as useful! thanks!!
@dot Don't do that. Putting a <p> inside your <h1> is invalid HTML.
meager if i don't use the <p> tag, what's the best way to push the text down (like a margin-top) without affecting the img? i am limited in what I can do because I cannot actually change the html. i have to accomplish everything via css. otherwise, i'd just add an <img> element
the html is a part of a template library which i'm not allowed to change. I'm just creating a "skin" if you will...
|
1

Add:

#header h1 {
 padding-left: 20px; /*Or according with the picture size you have*/
}

Must work.

answered May 22, 2013 at 19:30

Comments

0

You have a few choices, but text-indent seems the most appropriate.

answered May 22, 2013 at 19:28

Comments

0

It depends, what are you trying to indent, are you indenting from another element or from another text.

p {
 margin-left: 20px;
}
p {
 text-indent: 20px;
}
answered May 22, 2013 at 19:30

9 Comments

this worked. i added a p tag inside h1... and then used the text indent. and its happy
@dot Why would you do that? The other answers do not require additional markup.
You can control the top margin on the h1 as well... This is the least elegant solution.
This is semantically broken. You shouldn't be putting a <p> inside your <h1>, that's not even valid HTML.
@Cam Except for the part where margin isn't going to help here. The OP wants to shift the text so that it isn't overlapping the background image. Christian's answer (to use padding) is the next correct solution after meagar's.
|
0

Separating out the image in an image tag looks like the best option if you do not really want the image to be in the background of the text.

HTML:

<img src="http://placekitten.com/g/50/100"/>
<h1>Our Services</h1>

CSS:

img{
 float:left;
 width:50px;
 height: 50px;
}
h1{
 padding-left: 60px;
}

http://cssdeck.com/labs/xixebfxw. In this case, I had to re-size the image by giving appropriate width and height as the text! Also margin-left also can be used alternatively for padding-left.

And if the spacing is not a matter of concern and you just need to place the text to the right and image in the left without removing the image as the the background, you can do: http://cssdeck.com/labs/wml9occf But this may not be the best option.

answered May 22, 2013 at 20:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.