Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text on left and right side of element

Tags:

html

text

css

Using CSS what is the best way to have text on both the right and the left side of an element and be in the same spot vertically?

Thus ending up with the following layout:
enter image description here

The container has a fixed width, so I don't want to use positioning, because I know I don't have to.

like image 639
Web_Designer Avatar asked Jul 07 '11 16:07

Web_Designer


People also ask

How do I put text left and right in HTML?

To set text alignment in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property text-align for the center, left and right alignment.

How do you write on both sides in HTML?

In order to suggest that some text be justified on both sides, you can use the align="justify" attribute in HTML, or the text-align:justify declaration in CSS, or both.


2 Answers

(1) Add two divs within the element that contain each text string

<div>
    <div class="div1">Left Text</div>
    <div class="div2">Right Text</div>
</div>

(2) Float the two divs next to each other

.div1 {
    float: left;
}

.div2 {
    float:right;
}

(3) Set the text-align properties for the right div (this will ensure that the text is pushed all the way to the right as in your example).

.div2 {
    float:right;
    text-align: right;
}
like image 85
rolling stone Avatar answered Sep 19 '22 15:09

rolling stone


You can place your two items inside the same container and float them into the right position.

So you might have something like:

<div class="container">
   <div class="item1">Item 1</div>
   <div class="item2">Item 2</div>
</div>

and CSS:

.container{width:500px;}

.item1, item2{width:200px;}
.item1{float:left;}
.item2{float:right;}

Example: http://jsfiddle.net/7Wvhj/1/

like image 27
Jamie Dixon Avatar answered Sep 21 '22 15:09

Jamie Dixon