1

HTML

<div>Test</div>

CSS

div {
text-align: center;
width:200px;
margin-left:20px;
}

I don't want to have the text at center but pushed slightly right/left. Is this possible?

P. Camilleri
  • 12,664
  • 7
  • 41
  • 76
123321123321
  • 81
  • 1
  • 3
  • 10
  • plain text within a div is not positionable. you have to enclose the text in something else, like a `` – fnostro Apr 18 '14 at 15:16
  • the css in your post is modifying and positioning the div, and that gives the effect of positioning the text. what is the bigger picture of what you're trying to do? – fnostro Apr 18 '14 at 15:25

3 Answers3

7

If it's a single line you should be able to do this:

 text-indent:1em;

ref: https://developer.mozilla.org/en-US/docs/Web/CSS/text-indent

Mister Epic
  • 16,295
  • 13
  • 76
  • 147
2

It is possible. Currently your margin-left property will affect the div and does not specifically target the inner text.

I would do something like the following. If you surround the text in a <p> tag you can offset it to the left or right by using padding.

div {
  text-align: center;
  width:200px;
  background:blue;
}
div p{
  padding-left:30px;
}

<div>
   <p>Test</p>
</div>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
katzkode
  • 1,911
  • 1
  • 13
  • 18
0

If you use padding on one side, it will offset the text visually by whatever you set it to. What you have listed would work, as would using padding instead of a margin, but it depends on what the site looks like visually.

Matthew Darnell
  • 4,538
  • 2
  • 19
  • 29