How to center a div vertically from a unknown dynamic height? [duplicate] - css

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 5 years ago.
In my code there could be hundreds of elements depending on how much images is added to the pages, meaning the height of the page could be between 0 to 1000s or even more.
How would I center a overlay div in the middle of the page. The webpage is more or less trying to mimic Instagram, you can have hundreds of images but once you click on one of them the overlay is still set in the middle of the page.
This is the main div for the overlay of the images. this div works fine as long as the page is no longer than the view height of the page, if the height is too large that you have to scroll down, then it will mess with the position of the vertical. I know this problem is because im setting the bottom to 15% so it takes in consideration the entire height value. But I dont know how I can fix that.
.overlayImgContainer{
position: absolute;
height: 70%;
width: 120%;// (page is set to 960px this add a bit more room)
z-index: 1;
bottom: 15%;
left: -10%;
background-color: rgba(0,0,0,0.5);
overflow-x: hidden;
border-radius: 5px;
text-align: center;
}

You can use flexboxes for that.
CSS
.wrapper {
display: flex;
align-items: center;
justify-content: center;
}
HTML
<div class="wrapper">
<div>...</div>
</div>

Related

Center a position:fixed element with auto width [duplicate]

This question already has answers here:
Center aligning a fixed position div
(16 answers)
Closed 1 year ago.
I would like to make a position: fixed; header centered to the screen with a dynamic width and height. Right now, on desktop it is centered fine with this solution:
const Container = styled.div`
position: fixed;
left: 50%;
transform: translateX(-50%);
width: auto;
`;
I found that solution here. The problem I have is that if you enter my website, and make the window narrower, the header also gets smaller, and the menu items run out of space. I want to have width: auto; because I want the header to be just as wide as the menu and logo, even if I add or remove menu items, and I don't want to manually change the width every time I do that.
Can anyone think of a way to solve this?
Try to add this:
align-items: center;
justify-content: center;
I don't really know the reason for the width auto because that will just make the width the width of the child elements.

How to vertically center a div inside another div that does not have pre determined height? (has height auto) [duplicate]

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 3 years ago.
So I already have done this once in this page: link to the website's page
In this page I was able to vertically center the text displayed on the image:
That column has 2 codes that make the text centered:
This one is added to the mother div
.academy-pc .one-third.mcb-wrap {
float: none;
display: table-cell;
position: relative;
}
and this one is on the child div that has the text:
.academy-pc .vertic-center {
position: absolute;
top: 50%;
transform: translate(0, -50%);
}
I've been trying to apply the same logic on this page but without success: page link
I wanted to vertically center the text in this image bellow but I just can't find a way.
Any help? thanks in advance
Into the .section_wrapper .mcb-section-inner add
display: flex;
justify-content: center;
align-items: center;
then everything inside will be vertically centered.
Of course you don't need to add padding: 200px 80px 0px 80px;

How to force the padding-right to be respected in a CSS grid item when it overflows? [duplicate]

This question already has answers here:
Last margin / padding collapsing in flexbox / grid layout
(4 answers)
Closed 5 years ago.
I'm trying to get a centered dialog-like content with minimal markup and CSS and I almost got it working with the following code (you can try it in CodePen here: https://codepen.io/rosenfeld/pen/Rxgwrq):
#container {
display: grid;
padding: 1em;
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
background-color: white;
align-content: center;
justify-content: center;
}
#item {
text-align: justify;
font-size: 20px;
text-shadow: 1px 1px lightblue;
max-width: 400px;
background-color: lightyellow;
padding: 1em;
overflow: auto;
}
h1 { text-align: center; color: darkred; }
a { color: darkred}
<html>
<head>
<title>404 - Not Found</title>
</head>
<body>
<div id=container >
<div id=item>
<h1>404</h1>
Sorry, the page you are looking for does not exist. You may want to navigate to our main page. If you just clicked on a broken link, please get in touch so we can fix it.
</div>
</div>
</body>
</html>
The only issue I found was that the right padding of the item is not respected when the content overflows. Try shrinking the window width as much to force the horizontal scroll bar to appear and you'll notice that the item padding-right is completely ignored.
Is it possible to fix this without changing the markup or using :before and :after pseudo-selectors in the CSS?
P.S.: My first attempt to implement this was using Flexbox but I found several issues that don't seem to happen with grid layouts. For example, with Flexbox I was forced to use "margin: auto" for the item instead of the alignment properties, as they would make the top content disappear. Another difference is that with grid layout the bottom padding is respected, the only issue being the right padding (Update: in Chrome, but not in Firefox, for example, so this is not consistent among browsers). Using CSS grids allowed me to get closer to what I want with less and cleaner code when compared to using Flexbox. Also, the problem with using "margin: auto" is that you're unable to actually set up some margin to the item.
This is because the word can not be broken by a line break. And if it does not fit, part of it is on the "territory" of padding.
For exam give to .item property word-break: break-all;.

Sidebars 100% height as content and content 100% height as sidebars [duplicate]

This question already has answers here:
How do I achieve equal height divs (positioned side by side) with HTML / CSS ?
(11 answers)
Closed 7 years ago.
I'm building my website on Wordpress but stuck on one thing.
I want my sidebars, the left and right sidebar vertical in the same height as the content even the content inside the sidebar is shorter than the content in the midsection.
I also want the content in the midsection vertical in the same height as the content inside the sidebar if there is more content in the sidebar.
See website
See what I mean here
Add overflow: hidden; to the .containerdubbel div.
.containerdubbel {
position: relative;
padding: 0;
margin-left: auto;
margin-right: auto;
background: url(../Meerwoonruimte/images/achtergrond.png) repeat-y top left;
width: 990px;
min-height: 600px;
height: auto !important;
overflow: hidden;
}

writing div for layout and positiong

I am trying to edit a page to use the full width of the page. I am having problems setting out the div layout. There is a div there already
<div id="rightPanal" style="margin-top: 425px;">
but it has things too far to the right and it is a narrow column going down the middle of the page.
I deleting/editing/ a lot of div's already there and I end up with nothing and its all over the place on different broswers.
Can I go to the .css file and just write some css that would give me a box in the middle (where inside i can put in two columns) of the page that doesn't interfere with other div and layout on the screen - say start X and Y on the screen - 4 pictures of equal size and text underneath the four boxes - all square ?
Position: absolute
#rightPanal {
position: absolute;
top: 50%;
}
It sounds like you want absolute positioning:
https://developer.mozilla.org/en-US/docs/Web/CSS/position
This code should get you close:
#rightPanal {
position: absolute;
top: 50%;
margin-left: auto;
margin-right: auto;
text-align: center;
box-sizing: border-box;
}

Resources