Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you align a div vertically without using float?

Tags:

html

css

When doing something like this:

<div style="float: left;">Left Div</div> <div style="float: right;">Right Div</div> 

I have to use an empty div with

clear: both; 

which feels very dirty to me.

So, is there a way to align without the use of float?

Here is my code:

.action_buttons_header a.green_button{     }
<div class="change_requests_container" style="width:950px !important">      <div class="sidebar">                                          <a href="/view/preview_unpublished_revision/422?klass=Proposal" class="preview sidebar_button_large action_button" name="preview" onclick="window.open(this.href);return false;">Preview New Version</a>                                        </div>      <div class="content_container">          <div class="content">                                            <div class="action_buttons_header">                      <a href="/changes/merge_changes/422" class="re_publish publish green_button" style="      margin: 5px 0px 5px auto;  ">Apply Changes</a>                  </div>                                              <div id="change_list_container">        <div class="changes_table">                                            <style type="text/css">                  #original_492 .rl_inline_added {                      display: none;                  }                                    #492.change_container .actial_suggested_text_container{                      display: none;                  }              </style>              <div class="content_section_header_container">                  <div class="content_section_header">                      <a href="#" class="collapse" name="492"></a>                      The Zerg |                       Overview                      <div class="status" id="492_status">                          <div id="492_status_placeholder">                                                        </div>                      </div>                  </div>              </div>              <div class="change_container" id="492">                                </div>          </div>      </div>  </div>

I want the green button on the right of the horizontal bar that it's in but in the cleanest way possible.

Just trying to learn how to do CSS elegantly, cleanly, etc.

like image 900
NullVoxPopuli Avatar asked Jan 09 '12 20:01

NullVoxPopuli


People also ask

How do you vertically align a div?

How to Center a Div Vertically. To center a div vertically on a page, you can use the CSS position property, top property, and transform property. Start by setting the position of the div to absolute so that it's taken out of the normal document flow. Then set the top property to 50%.

How do you float a div vertically?

By creating a wrapper around the content you want floated, you can then use the ::after or ::before pseudo selectors to vertically align your content within the wrapper. You can adjust the size of that content all you want without it affecting the alignment.

How do I align content vertically centered?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.


2 Answers

Another way to do something similar is with flexbox on a wrapper element, i.e.,

 .row {          display: flex;          justify-content: space-between;      }
  <div class="row">          <div>Left</div>          <div>Right</div>      </div>       
like image 192
myrcutio Avatar answered Sep 27 '22 01:09

myrcutio


In you case here, if you want to right-align that green button, just change the one div to have everything right-aligned:

<div class="action_buttons_header" style="text-align: right;"> 

The div is already taking up the full width of that section, so just shift the green button the right by right-aligning the text.

like image 42
DCNYAM Avatar answered Sep 27 '22 01:09

DCNYAM