Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align two elements on the same line without changing HTML

I have two elements on the same line floated left and floated right.

<style type="text/css">     #element1 {float:left;}     #element2 {float:right;} </style>  <div id="element1">     element 1 markup </div> <div id="element2">     element 2 markup </div> 

I need for element2 to line up next to element1 with about 10 pixels of padding between the two. The problem is that element2's width can change depending on content and browser (font size, etc.) so it's not always lined up perfectly with element1 (I can't just apply a margin-right and move it over).

I also cannot change the markup.

Is there a uniform way to line them up? I tried margin-right with a percentage, I tried a negative margin on element1 to bring element2 closer (but couldn't get it to work).

like image 913
user1074378 Avatar asked Jan 30 '12 17:01

user1074378


People also ask

How do I keep my div on the same line?

Set size and make inline Because they're block elements, when reducing the size of Div one to make room for the other div, you're left with space next to Div one and Div two below Div one. To move the div up to the next line both div's need to have the inline-block display setting as shown below.

How do I align two lines on the same line in CSS?

If you have multiple buttons that should sit side-by-side on the same line, add the data-inline="true" attribute to each button. This will style the buttons to be the width of their content and float the buttons so they sit on the same line.

How do you stop a line break in HTML?

There are several ways to prevent line breaks in content. Using &nbsp; is one way, and works fine between words, but using it between an empty element and some text does not have a well-defined effect. The same would apply to the more logical and more accessible approach where you use an image for an icon.


1 Answers

Using display:inline-block

#element1 {display:inline-block;margin-right:10px;}  #element2 {display:inline-block;}  

Example

like image 78
Sameera Thilakasiri Avatar answered Sep 21 '22 06:09

Sameera Thilakasiri