Vertically center elements in CSS

前端 未结 5 1168
醉酒成梦
醉酒成梦 2020-12-03 23:05

I have two elements side-by-side. Element 2 is smaller than Element 1. Both elements do not have a fixed height. I need to vertically center Element 2. How do I achieve

相关标签:
5条回答
  • 2020-12-03 23:45

    Not easily. Some popular hacks include using display: table-cell and a parent using display: table (I don't remember if the valign="center" attribute is needed), or using absolute positioning with top: 45% or so (not precise, but OK for small elements).

    To determine the best method, we need to know more about your layout. What are they centered within? Will/can there be a large Y-distance between elements 1 and 2? Does their parent have a fixed height? Do they both have the same parents, or is one a sibling of the other? What method are you using to place them side by side?

    Keep in mind that many tricks require additional hacking to work in IE, and that using Javascript is just cheating and will make your site inaccessible/annoying to people with low vision (who may be using script-unaware screen readers), people with scripts disabled (esp. on mobile or command-line browsers that may not support them well if at all), search engines, etc. It's possible using only CSS (though you may have to add some container elements), but the exact method depends what exactly you're doing.

    0 讨论(0)
  • 2020-12-03 23:45

    Put them both inside another element, give it a min-width and set the left and right margins to auto.

    0 讨论(0)
  • 2020-12-03 23:47

    If you only need to support new browsers like Safari (e.g., building webapp for the iPhone), CSS3 offers an elegant approach with no floats or negative margins. All details here: http://www.html5rocks.com/en/tutorials/flexbox/quick/#toc-center

    0 讨论(0)
  • 2020-12-03 23:59

    The most straightforward and clean way to do it is to use display: table and vertical-align. However, IIRC (it's been a while since I was up on browser compatibility issues) support for display: table and friends is absent from ... some common-then version of IE, perhaps? Anyway, adding other rules to make an adequate fallback if the display: table is ignored might be good.

    <div id="container" style="display: table;">
        <div id="div1" style="display: table-cell; vertical-align: middle;">
            <img id="img1" src="image1.jpg" />
        </div>
        <div id="div2" style="display: table-cell; vertical-align: middle;">
            <img id="img2" src="image2.jpg" />
        </div>
    </div>
    

    (Of course the styles ought to be in a stylesheet; this is just matching your original example and not knowing the use case.)

    The display values table, table-row, and table-cell basically perform exactly like HTML table, tr, and td, but you are permitted to omit any of them (as I do here, using table-cells directly within tables) and the layout engine will do the sensible thing.

    0 讨论(0)
  • 2020-12-04 00:01

    I don't think you can do this reliably without a table. Kevin's solution would probably work, unless you need to support IE (which most of us do). And, in this case, the table markup might actually be smaller than the div-based markup.

    0 讨论(0)
提交回复
热议问题