How to Float Vertically in CSS?

  • Thread starter davee123
  • Start date
  • Tags
    Css Float
In summary, to center a <div> vertically within a <td>, you can use the CSS "transform" property with the value of "translateY(-50%)" to move the div up 50% of its height. This avoids the need for complex javascript calculations.
  • #1
davee123
672
4
So, I've got a <table> full of <td>'s. And I want to have a <div> magically pop in sometimes, centered above a particular <td>. That is, I want it to partially COVER the content within the <td>. As is, I'm creating the <td>'s and <div>'s, but I just set the visibility to "hidden" until it's ready to appear. Plus, the <div>'s aren't fully opaque, so you can still see what's underneath.

Anyway, the problem: I can get the <div> to center horizontally-- but not vertically. And unfortunately, the <td>'s can be very different size-wise. Sometimes 50 pixels tall, sometimes 200 pixels tall (depends on the image that's inside it).

So, right now, I've got the div set to:

Code:
.addedtag {
    position: relative;
    top: 20px;
    background-color: #DDDDFF;
    font-color: #008000;
    border: 2px solid #904040;
    width: 50%;
    padding-left: 5px;
    padding-right: 5px;
    padding-top: 5px;
    padding-bottom: 5px;
    margin-left: auto;
    margin-right: auto;
    filter:alpha(opacity=50);
    opacity:0.5;
}

That puts it about 20 pixels down from the top of the <TD>, but sometimes that looks woefully pathetic. And setting top to a percentage doesn't seem to do ... anything. I was hoping that setting "top: 50%" would set it to 50% of the height of the containing element (a <td>), but apparently not!

Now, I know there's phenomenally complex ways to do this by getting the height of the <td> in javascript, and then referencing that in order to set the "top" to a certain fixed number of pixels. ... But, I was hoping (please?) that there's a better way to do this?

DaveE
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Yes, there is a better way to do this. You can use the CSS "transform" property to position the div relative to its containing element. For example, to center the div vertically, you can set the following:.addedtag { position: relative; transform: translateY(-50%); background-color: #DDDDFF; font-color: #008000; border: 2px solid #904040; width: 50%; padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; margin-left: auto; margin-right: auto; filter:alpha(opacity=50); opacity:0.5;}The "transform: translateY(-50%)" will move the div up 50% of its height, effectively centering it vertically.Hope that helps!
 

Related to How to Float Vertically in CSS?

1. What is the CSS property used to float elements vertically?

The CSS property used to float elements vertically is float. This property allows an element to be positioned to the left or right of its container, and other content will wrap around it.

2. How do I float an element to the top or bottom of its container?

To float an element to the top or bottom of its container, you can use the clear property. Set this property to both to clear any previous floats and make the element float at the top or bottom of its container.

3. Can I float multiple elements vertically?

Yes, you can float multiple elements vertically by setting the float property to left or right for each element. Keep in mind that elements will be positioned in the order they appear in the HTML document, so make sure to place them accordingly.

4. How do I center a floated element vertically?

To center a floated element vertically, you can use the vertical-align property. Set this property to middle for the element you want to center, and it will be aligned with the middle of the container.

5. Can I use flexbox or grid to float elements vertically instead of the float property?

Yes, you can use flexbox or grid to float elements vertically. With flexbox, you can use the align-items property to align items vertically, and with grid, you can use the align-content property. Both of these methods are more modern and flexible than using the float property.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
370
  • Programming and Computer Science
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Sci-Fi Writing and World Building
Replies
2
Views
2K
Back
Top