Html div align relative

How can I vertically align elements in a div?

I have a div with two images and an h1 . All of them need to be vertically aligned within the div, next to each other. One of the images needs to be absolute positioned within the div . What is the CSS needed for this to work on all common browsers?

I made a list of all ways to vertical align..I am going to leave it here: jsfiddle.net/techsin/FAwku/1

Here are two simple methods to center an element within a div, vertically, horizontally or both (pure CSS): stackoverflow.com/a/31977476/3597276

28 Answers 28

Wow, this problem is popular. It’s based on a misunderstanding in the vertical-align property. This excellent article explains it:

“How to center in CSS” is a great web tool which helps to find the necessary CSS centering attributes for different situations.

In a nutshell (and to prevent link rot) :

  • Inline elements (and only inline elements) can be vertically aligned in their context via vertical-align: middle . However, the “context” isn’t the whole parent container height, it’s the height of the text line they’re in. jsfiddle example
  • For block elements, vertical alignment is harder and strongly depends on the specific situation:
    • If the inner element can have a fixed height, you can make its position absolute and specify its height , margin-top and top position. jsfiddle example
    • If the centered element consists of a single lineandits parent height is fixed you can simply set the container’s line-height to fill its height. This method is quite versatile in my experience. jsfiddle example
    • … there are more such special cases.

    The problem with the «valid» solutions (so not the vertical-align: middle ) is that you have to specify a fixed height. Not very possible for Responsive Web Design.

    I had this structure:

    My Text
    Worked for me set the property lineHeight using the same value as used in div height property: ‘##px’ (## because this value is dynamic in my case) thank you

This also works with inline-block and table-cell elements. If you are having issues with this, try adjusting line-height of the container element (i.e. context) since it is used in the vertical-align line box calculations.

css-tricks.com/centering-css-complete-guide

This tool (howtocenterincsss.com) is pretty awesome because it worked right out of the box inside my existing CSS! That is super rare. Remember the old days where you had to copy entire stylesheets from some example, sometimes even the example HTML an then remove and rename elements one after each other until it’s what I needed. Not the case for this site!

Now that Flexbox support is increasing, this CSS applied to the containing element would vertically center all contained items (except for those items that specify the alignment themselves, e.g. align-self:start )

Use the prefixed version if you also need to target Internet Explorer 10, and older (< 4.4 (KitKat)) Android browsers:

A nice solution, thank you. Can you suggest how to make flexbox display a horizontal scrollbar, in case the inner element is bigger than the container? (I’m trying to make inner element zoomable/scrollable within the container, like canvas in powerpoint)? Is it possible at all?

@YazidErman this solution depends on flexbox so no, only IE 10 and above support or any other modern browser caniuse.com/#search=flexbox Flexbox is great where it’s supported but that’s not everywhere. The display table and table cell is probably the best solution really, container just needs to be table, element being centered is wrapped an element with display table cell then can just center as some other answers here point out.

Why was this so hard for me to find >.< thanks for the answer! Can confirm this is working for me in a scenario where other answer did not work.

I used this very simple code:

Obviously, whether you use a .class or an #id , the result won’t change.

Here’s a full example of all nine possibilities of alignments (top-middle-bottom and left-center-right): jsfiddle.net/webMac/0swk9hk5

I think this solution is the quickest and easiest to understand. Perhaps it’s because I wasn’t patient enough to grok the selected answer. Flexbox all the things!

Why do we need a combination of 2 styles to make vertical alignment working in all screen size? If I use only display:flex and resize my screen, the vertical alignment is lost however if both display and align-items are used it works.

Then if you want to horizontal align with margin : 0 auto , it won’t work because of the display: table-cell

Vertically and horizontally align element

Use either of these. The result would be the same:

Enter image description here

1. Bootstrap 4.3+

For vertical alignment: d-flex align-items-center

For horizontal alignment: d-flex justify-content-center

For vertical and horizontal alignment: d-flex align-items-center justify-content-center

2. CSS 3

.container < height: 180px; width:100%; background-color: blueviolet; >.container > div < background-color: white; padding: 1rem; >.center

Bootstrap isn’t magic. It’s subject to the very same CSS limitations affecting everyone. It’s just hiding what it does from the avg. user.

A technique from a friend of mine:

To position block elements to the center (works in Internet Explorer 9 and above), it needs a wrapper div :

This method can cause elements to be blurry due to the element being placed on a “half pixel”. A solution for this is to set the perspective of the element. transform: perspective(1px) translateY(-50%);

Use this formula, and it will work always without cracks:

#outer #outer[id] #middle /* For explorer only*/ #middle[id] #inner /* For explorer only */ /* Optional: #inner[id] */
 
any text any height any content, for example generated from DB everything is vertically centered

I’ve never seen in css this kind of selector «#outer[id]». What should that do? What if I will have class instead of id?

Almost all methods needs to specify the height, but often we don’t have any heights.

So here is a CSS 3 three-line trick that doesn’t require to know the height.

but this seems work only when the height of element’s parent is fixed. the property ‘top’ with percentage value only works in the container with fixed height, correct?

Yes it works only for some cases, I had problems later with this methods. The thing is to have the most possibilities as possible and as soon as you need one you test all of them until you get the good one, because all methods got things that won’t works in a specific case.

All of them need to be vertically aligned within the div

Aligned how? Tops of the images aligned with the top of the text?

One of the images needs to be absolute positioned within the div.

Absolutely positioned relative to the DIV? Perhaps you could sketch out what you’re looking for.

fd has described the steps for absolute positioning, as well as adjusting the display of the H1 element such that images will appear inline with it. To that, i’ll add that you can align the images by use of the vertical-align style:

. this would put the header and images together, with top edges aligned. Other alignment options exist; see the documentation. You might also find it beneficial to drop the DIV and move the images inside the H1 element — this provides semantic value to the container, and removes the need to adjust the display of the H1 :

Источник

How to vertically center inside the parent element with CSS? [duplicate]

I’m trying to make a small username and password input box. I would like to ask, how do you vertically align a div? What I have is:

How can I make the div with id Username and Form to vertically align itself to the center? I’ve tried to put:

17 Answers 17

The best approach in modern browsers is to use flexbox:

Some browsers will need vendor prefixes. For older browsers without flexbox support (e.g. IE 9 and lower), you’ll need to implement a fallback solution using one of the older methods.

Recommended Reading

To align horizontally, I added justify-content: center as well. (Not quite the same scenario though.)

Flex is the worst method I’ve ever seen, so much pain with Bootstrap that I would more likely use Javascript over this .

You literally saved my life. My boss, CJ, said if I don’t get this done today I’m in big trouble. Also @meshy that worked like a charm ty

This can be done with 3 lines of CSS and is compatible back to (and including) IE9:

Can you provide an example of how and why it breaks «responsive design» as a whole? Whether or not it breaks responsive design is a matter of your project’s structure already in my opinion.

WOAH .. thanks man, i had to remove flexbox from a slider because of it inserting extra divs between flex parent & child, this was the perfect solution.

You can vertically align a div in another div. See this example on JSFiddle or consider the example below.

The .innerDiv ‘s margin must be in this format: margin: auto *px;

[Where, * is your desired value.]

display: inline-flex is supported in the latest (updated/current version) browsers with HTML5 support.

It may not work in Internet Explorer 😛 🙂

Always try to define a height for any vertically aligned div (i.e. innerDiv) to counter compatibility issues.

This solution only works if you add the parts noted as being responsible for vertical alignment, AND the height on the outerDiv. Without the height, it doesn’t work.

I’m pretty late to the party, but I came up with this myself and it’s one of my favorite quick hacks for vertical alignment. It’s crazy simple, and easy to understand what’s going on.

You use the :before css attribute to insert a div into the beginning of the parent div, give it display:inline-block and vertical-align:middle and then give those same properties to the child div. Since vertical-align is for alignment along a line, those inline divs will be considered a line.

Make the :before div height:100% , and the child div will automatically follow and align in the middle of a very tall «line.»

.parent:before, .child < display:inline-block; vertical-align:middle; >.parent:before < content:""; // so that it shows up height:100%; // so it takes up the full height >

Here’s a fiddle to demonstrate what I’m talking about. The child div can be any height, and you never have to modify its margins/paddings.
And here’s a more explanatory fiddle.

If you’re not fond of :before , you can always manually put in a div.

And then just reassign .parent:before to .parent .before

Источник

How to center a relative div?

I have been trying to get the following code working for hours, without success. Could you please help me to get the projects div centered (even when the page is zoomed in and out)? Here is my HTML & CSS:

#bottom < position: absolute; top: 100%; left: 0; right: 0; background-color: #FFF;>#secondsection < background-size: 100% auto; background-size: cover; color: #eaeaf0; margin-left: 7%; margin-right: 7%; padding-top: 35px; padding-bottom: 35px; position: relative;>#ss_top < width: 100%; height: 100%;>.ss_title < display: inline; float:left; color: #000000; font-family: 'Eurostile'; font-size: 35px; text-transform: uppercase;>.ss_title2 < color: #a5a5a5;>#gallerybutton < position: relative; display: inline; float: right; margin-right: 0%; margin-top: 50px; padding: 20px; background-color: #000; text-decoration: none; border: none; color: #FFF; text-transform: uppercase;>#projects < position: relative; margin-left: auto; margin-right: auto; max-width: 2000px; padding: 175px 0px 0px 0px;>#pr_one, #pr_two < display: block;>.pr_img < float: left; display: inline; margin-right: 1%; margin-bottom: 1%;>#viewprofilebutton

 

A selection of projects
I've worked on lately

Html div align relative
Html div align relative
Html div align relative
Html div align relative
Html div align relative
Html div align relative

How do you want your images to display, two rows of three images wide? If so, you page will be approximately 3×488 px wide. Please elaborate.

Well, if possible I would like to have two rows wide for small screens and three wide for larger screens. The point is that no matter if I scroll in or out, the projects div sticks to the left side of the screen.

5 Answers 5

Here is a start. Look at the following CSS:

#bottom < position: absolute; top: 100%; left: 0; right: 0; background-color: #FFF;>#secondsection < background-size: 100% auto; background-size: cover; color: #eaeaf0; margin-left: 7%; margin-right: 7%; padding-top: 35px; padding-bottom: 35px; position: relative; border: 1px dotted red; >#ss_top < width: 100%; height: 100%; border: 1px dotted blue; overflow: auto; >#ss_top p < margin: 0; >.ss_title < display: inline-block; color: #000000; font-family: 'Eurostile'; font-size: 35px; text-transform: uppercase;>.ss_title2 < color: #a5a5a5;>#gallerybutton < position: relative; display: inline; float: right; margin-right: 0%; margin-top: 50px; padding: 20px; background-color: #000; text-decoration: none; border: none; color: #FFF; text-transform: uppercase;>#projects < position: relative; margin-left: auto; margin-right: auto; max-width: 2000px; padding: 175px 0px 0px 0px; border: 1px dashed blue; >#pr_one, #pr_two < display: block; border: 2px dashed blue; overflow: auto; text-align: center; >.pr_img < display: inline-block; width: 30%; margin-right: 1%; margin-bottom: 1%; >.pr_img img < display: inline-block; width: 100%; height: auto; >#viewprofilebutton

I started by getting rid of the floats in the title, #ss_top , you don’t need it.

For the #projects panel with the images, floats are getting you into trouble with centering.

On #pr_one and #pr_two , add text-align: center and then use display: inline-block on .pr_img , this will center align your images (give/take some margins), and then apply a suitable width of say 30% so that the images auto scale to form a row of three.

The trick now is to apply display: inline-block to the images ( .pr_img img ) so you can now use margins to control top/bottom/left/right spacing.

Note: You still have some polishing up to do but at least this clarifies the issues related to centering and floated elements.

Responsive Design: If you want 2 or 3 images in a row depending on the screen size, you need to learn about media queries. However, because you wrapped 3 images in a div , you are locked into 3 per row, but that may be okay.

Источник

Читайте также:  Python str убрать символы
Оцените статью