Why is my container height not fitting to content? - html

I cannot figure out why my container (main-container) background is not stretching with the content inside. It looks like the container background is stuck on initial view height. When I scroll pass the initial view height, the rest of it is white.
Here is the css
.main-container {
margin: 0;
padding: 0;
font-family: 'montserrat';
height: fit-content;
}
.main {
position: absolute;
top: 65%;
left: 50%;
transform: translate(-50%, -50%);
width: 400px;
background: greenyellow;
border-radius: 10px;
margin-bottom: 40px;
padding-bottom: 20px;
}
<div class="main-container">
<div class="main">
<h1>Signup</h1>
<form method="POST">
<div class="txt-field">
<input type="text" required>
<span></span>
<label>First Name</label>
</div>
<h1>Upload Image</h1>
<button class="btn btn-primary">Upload<i class="fa fa-upload fa-1x"></i></button>
<input type="submit" value="Signup">
</form>
</div>
</div>
This is what it looks like initially.
This is what it looks like when I scroll down

The reason is that you have position: absolute on your .main class - any elements positioned absolutely will be taken out of the regular document flow and will have no effect on the layout of their parent(s).
It looks like you are using absolute position to try and center the .main element. Have you considered using a flexbox on .main-container instead? Using a flexbox with justify-content: center and align-items: center is an easy way to center an element inside its parent while keeping the regular document flow.

Related

Having trouble centering div in CSS

I'm working with one of my first pages. It contains a pop-up that needs to be centered in the exact middle of the page. I'm aware that there are many ways to do this, but I'm having trouble with each one of them;
The first one I tried is this:
<div class="mod" id="modal">
<div class="mod-header">
<div class="mod-title">15% off for new customers</div>
<button data-close-button class="close-button">×</button>
</div>
<div class="mod-body">
<h2>subscribe to our newsletter and get 15% off your first purchase</h2>
<form method="POST">
{{ form|crispy }}
{% csrf_token %}
<button class="submit-btn" type="submit">go</button>
</form>
</div>
</div>
Styles:
.mod{
position: fixed;
transition: 200ms ease-in-out;
transform: translate(-50%, -50%) scale(0);
border: 1px solid black;
z-index: 999;
padding: 1.2em;
background-color: #f6f1eb;
}
.mod.active{
top:50%;
left:50%;
transform: translate(-50%, -50%) scale(1);
-webkit-font-smoothing: antialiased;
}
The problem with this is that I'm getting a blurry effect in the div. I tried everything (webkit antialiasing, setting translate to 51%, etc) and it won't change.
Some other ways of centering implicate selecting the parent container, but that's impossible in my case since this is a large website and I need this to be in the center of the entire page, not the container.
I also considered using calc() instead of translate() and subtract the width and height of the container, but then I'd need to know the dimentions all the time, and it would lose the responsiveness.
Is there any better way to do this? I'm sure it's very simple, but I'm a beginner and don't know much. Let me know if I'm lacking code or I'm not being clear enough. Thanks in advance!
Just give your modal another container and it's good to go.
.mod {
/* Remove background if backdrop is not needed */
background: rgba(0, 0, 0, 0.1);
position: fixed;
/* 0, 0, 0, 0 gives 100% full height and width on absolute/fixed elements */
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.mod .wrapper {
width: 300px;
height: 300px;
/* since there's no feature to vertically center you need margin top,
while the auto will center horizontally automatically whatever the width is.
Do note that left, top 50% is quite unreliable since it doesn't take into account the size
of the container
*/
margin: 10% auto 0;
background: white;
display: block;
border-radius: 4px;
text-align: center;
}
<!-- Will be the one that is position fixed -->
<div class="mod" id="modal">
<!-- While this one will make the modal look, with this you can use margin left/right: auto to automatically center horizontally regardless of the width -->
<div class="wrapper">
<div class="mod-header">
<div class="mod-title">15% off for new customers</div>
<button data-close-button class="close-button">×</button>
</div>
<div class="mod-body">
<h2>subscribe to our newsletter and get 15% off your first purchase</h2>
<form method="POST">
</form>
</div>
</div>
</div>
Change the position property of the modal to absolute and add top and left property to it. Remove other properties of the active class and add only display property to it.
let btn = document.querySelector("#btn");
let closeBtn = document.querySelector(".close-button");
let modal = document.querySelector("#modal");
btn.addEventListener("click", () => {
modal.classList.add("active");
});
closeBtn.addEventListener("click", () => {
modal.classList.remove("active");
});
.mod {
position: absolute;
top: 50%;
left: 50%;
display: none;
width: 400px;
transition: 200ms ease-in-out;
transform: translate(-50%, -50%);
border: 1px solid black;
z-index: 999;
padding: 1.2em;
background-color: #f6f1eb;
}
.active {
display: block;
}
<button id="btn">Show Modal</button>
<div id="modal-container">
<div class="mod" id="modal">
<div class="mod-header">
<div class="mod-title">15% off for new customers</div>
<button data-close-button class="close-button">×</button>
</div>
<div class="mod-body">
<h2>subscribe to our newsletter and get 15% off your first purchase</h2>
<form method="POST">
Your content here!
<button class="submit-btn" type="submit">go</button>
</form>
</div>
</div></div>

HTML Section should be centered in the middle of an image on resizing

I've tried all the examples I found. I have a div which is positioned in the middle in front of an image. But when the screen resizes the div doesn't stay as it should. Can somebody please tell me what am I doing wrong? My code looks like this:
My HTML:
<section class="SliderOuter">
<img src="images/cover.png" alt="">
<div class="container" style="text-align:center;">
<div class="tags1">
<img src="images/ESCO_logo_big.png" alt="">
<h3 style="color:#ffff">Leading company in the fiel of energy efficiency</h3>
<br>
<h4 style="color:#ffff">Be the first to know about ESCO updates and announcements:</h4>
<form action="" method="post" role="form">
<input class="form-control" type="text" id="" name="" placeholder="your#email.com">
<br>
<input type="submit" value="Subscribe" class="btn btn-large btn btn-danger" />
</form>
</div>
</div>
</section>
My CSS:
.tags1 {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Please help!
Thank you in advance!
If I've understood this correctly your problem is not with the .tags div but with its parents .container and .SliderOuter
.container needs some positioning applied to it so that it sits within .SliderOuter and yet over the first image.
.Slider in turn needs positioning set on it so that .container can have its height relative to that.
Your CSS eventually should look something like this...
.SliderOuter {
position: relative;
}
.container {
text-align: center;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.tags1 {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 100%;
}
I've thrown together a Fiddle here http://jsfiddle.net/2owysubj/2/
For the fiddle I took the liberty of tidying up the markup a little (you should try to avoid using inline styles and <br> for styling purposes and I've used some dummy images to demonstrate, but the general principal is all there.
Another thing to think about might be to substitute the first image with a background image so you can remove the need to have .container absolutely positioned.
You could also look into using Flexbox for centring elements, but for what you need right now positioning works perfectly well.

Scrollbar is moving entire div

I'm writing an HTML program that will create a chat window.
And I have 3 divs and in the bottom div I'll enter text and my HTML is as below.
<div class="chatbox" id="chatbox" style="margin: 0px;">
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.41.0.min.js"></script>
<div class="chatHeader">
<span class="chat-text" style="text-align: center;">Chat with Care!!</span>
<div id="close-chat" onclick="closeChatbox()">×</div>
<div id="minim-chat" onclick="minimChatbox()" style="display: block;">
<span class="minim-button">−</span>
</div>
<div id="maxi-chat" onclick="loadChatbox()" style="display: none;">
<span class="maxi-button">+</span>
</div>
</div>
<div class="chatBody" style="margin-top:13%">
<ul id="ulid">
<li class="lexResponse">Hi I am CARE..Your Personal Assistant</li>
<li class="me">Hi THere</li><li class="lexResponse"><div>Sorry, can you please repeat that?</div></li><li class="me">H there</li><li class="lexResponse"><div>Sorry, can you please repeat that?</div></li><li class="me">start the fl;ow</li><li class="lexResponse"><div>what is your budget? is it very high, high, low or medium?</div></li><li class="me">Start the flow</li><li class="lexResponse"><div>what is your budget? is it very high, high, low or medium?</div></li></ul>
</div>
<form class="chat-form" onsubmit="return pushChat()">
<div class="backgroundColor">
<input type="text" id="textinput">
</div>
</form>
</div>
Here when I added my CSS, I need a scrollbar for the center div(I'm getting this), but when I scroll the bar, the bottom div is also moved.
please let me know on how can I fix the bottom div to bottom and have scroll bar only to the center div.
Here is a working fiddle.
https://jsfiddle.net/g87mfLxa/1/
Thanks
Add the following styles to the chatBody class.
`.chatBody {
overflow: auto;
height: 70%;
margin-bottom: 50px;
}`
remove
overflow: auto
from chatBox.
Hope this is what you meant.
The updated fiddle
You are almost close. You just need to change the position to position:sticky in your CSS in class chat-form. To give it better look you may wish to remove horizontal scroll bar. Your CSS got two changes in the CSS classes below . See this updated jsfiddle.
.chatbox {
position: fixed;
width: 80%;
height: 85%;
bottom: 0.5%;
right: 1%;
margin: 0 0 -1500px;
background: white;
overflow-y:auto;
overflow-x:hidden;
}
.chat-form {
position: sticky;
/* bottom: 0px; */
bottom: 0;
/* margin-top: 4%; */
display: flex;
align-items: flex-start;
width: 100%;
}
Let me know If It was helpful.

Centering a div in Skeleton

For a project of mine, I'm using Skeleton Boilerplate for the first time. And I'm looking for the best practice of centring a div in Skeleton without bashing into the rules of Skeleton.
At the moment, I've the following structure for a login page.
HTML:
<div class="container">
<div class="sixteen columns vertical-offset-by-one">
<div id="loginBox">
<img src="images/yeditepeLogo.png" alt="Yeditepe Logo" class="yeditepeLogo" />
<form action="" id="loginForm">
<input type="text" name="username" required placeholder="username" class="loginTextField">
<input type="password" name="password" required placeholder="password" class="loginTextField">
<input type="submit" value="Log In" class="loginButton" />
</form>
</div><!-- loginBox -->
</div><!-- sixteen columns -->
<div class="sixteen columns">
<p align="center">Click here to register</p>
</div>
</div><!-- container -->
CSS:
#loginBox, #registrationBox {
width: 470px;
height: 450px;
background-color: white;
left: 245px; */
top: 20px; */
position: relative;
margin: 0px auto; }
#registrationBox {
height: 500px; }
.yeditepeLogo {
position: relative;
left: 40px;
top: 33px; }
#loginForm, #registrationForm {
position: relative;
top: 45px; }
.loginTextField, .registrationTextField {
position: relative;
height: 40px;
width: 388px;
left: 40px;
border-color: #dedede;
border-style: solid;
border-width: 1px;
margin-bottom: 10px;
text-align: left;
font-size: 18px;
text-indent: 10px;
-webkit-appearance: none; }
.loginTextField:focus, .registrationTextField:focus {
outline-color: #ff9800;
outline-style: solid;
outline-width: 1px;
border-color: white; }
.loginTextField:nth-child(2), .registrationTextField:nth-child(3) {
margin-bottom: 40px; }
.loginButton, .registrationButton {
background-color: #77a942;
position: relative;
border: none;
width: 390px;
height: 60px;
left: 40px;
color: white;
font-size: 24px;
text-align: center;
opacity: 0.8; }
.loginButton:hover, .registrationButton:hover {
opacity: 1; }
As you can see, that #loginBox has a fixed width/height and it should always be on the centre of the page. margin: 0px auto code gives it the horizontal centring. But is it the best practice in Skeleton? Does Skeleton provide a better way?
Also how can I provide it's vertical centring?
There's actually a built in way of centering divs in Skeleton.
<div class="sixteen columns">
<div class="four columns offset-by-six">
<p>Lorem ipsum dolor sit amet.</p>
</div>
</div>
The offset-by-six in this case can be altered from one to fifteen, and offsets the column at hand by as many columns as entered. As a neat feature, the offsetting is not affecting alignment when smaller screens are used.
To clarify: This doesn't center the actual content in the div, but centers the div itself.
I know it has been a while since this question was asked, but maybe somebody else can use the answer.
I was able to accomplish centering with Skeleton by filling one-third column class with a space, then the next one-third column class with content, then another one-third column class with a space again.
<div class="one-third column"> </div>
<div class="one-third column"><p>Center of the screen.</p></div>
<div class="one-third column"> </div>
You can set the container to
.container {
position: absolute;
top: 50%;
left: 50%;
margin-left: -43 //replace with half of the width of the container
margin-top: -52 //replace with half of the height of the container
}
set the parent container or element to position: relative;
Here's a good article about How to Center Anything With CSS
Asus3000's answer is good as that is what I do and it works well. I would only add that on mobile, it adds quite a bit of unwanted vertical space. To avoid mobile vertical space, I use a class .filler and hide it on mobile.
HTML
<div class="one-third column filler"> </div>
<div class="one-third column"><p>Center of the screen.</p></div>
<div class="one-third column filler"> </div>
CSS
/* or whatever mobile viewport */
#media only screen and (max-width: 960px) {
.filler { display: none}
}
A way I believe works pretty good is:
<div class="container">
<div class="row">
<div class="two-half column">
centered div content
</div>
</div>
</div>
This makes the div centered and responsive. You can change margin-top to make it all the way in the middle, however changing width will (of course) not make it centered anymore.
Correct me if I'm wrong but this works for me! :)

Vertically centering a div in body?

I have tried to center this vertically in body (not horizontally). Also, I do not want to specify heights or anything like that. I tried adding a wrapper with a 100% height and other things, but got nowhere. Can you please help me fix this?
jsFiddle Here
<form name="signup" class="signup" action="signup.php" style="border: 1px solid #000; ">
<input type="text" placeholder="Email"/><br>
<input type="text" placeholder="Username"/><br>
<input type="password" placeholder="Password"/><br>
<button type="submit">Next</button>
</form>​
See this edited version of your jsFiddle.
Basically, just wrap your content in <div class = "main"><div class = "wrapper"></div></div>, and add the following CSS:
html, body {
height: 100%;
}
.main {
height: 100%;
width: 100%;
display: table;
}
.wrapper {
display: table-cell;
height: 100%;
vertical-align: middle;
}
If you have flexbox available, you can do it without using display: table;
Code example:
html,
body {
height: 100%;
width: 100%;
}
.container {
align-items: center;
display: flex;
justify-content: center;
height: 100%;
width: 100%;
}
<html>
<body>
<div class="container">
<div class="content">
This div will be centered
</div>
</div>
</body>
</html>
Ta-da! Vertically and horizontally centered content div. JSFiddle: https://jsfiddle.net/z0g0htm5/.
Taken mostly from https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/ and https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Update: codesandbox.io
form {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%); /* IE 9 */
-webkit-transform: translate(-50%, -50%); /* Chrome, Safari, Opera */
}
<form name="signup" class="signup" action="signup.php" style="border: 1px solid #000; ">
<input type="text" placeholder="Email"/><br>
<input type="text" placeholder="Username"/><br>
<input type="password" placeholder="Password"/><br>
<button type="submit">Next</button>
</form>​
Related: Center a div in body
this worked for me:
Style.css
div {
position: relative;
top: 50%;
transform: translateY(-50%);
}
I found this code snippet here.
You can indeed center vertically and horizontally the single container without a wrapper. The key is to understand what 100% height and width mean. It means the percentage of the parent element. You must chain percentages upward the hierarchy until you hit the actual viewport height and width.
HTML Element <---> BODY Element <----> DIV Element
100% of height in the DIV looks up the height of BODY, which in turn looks up the height of HTML, which in turn looks up the size of the viewport. In order for it to work properly, you cannot combine HTML and BODY in CSS, but rather address them separately.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 100%;
width: 100%;
}
body {
height: 100%;
width: 100%;
}
div {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
<html>
<body>
<div>
Center me!
</div>
</body>
</html>
A JSFiddle example with table warp
In the above solution, Provide css for html & body with "height: 100%" and then wrap the form that to be centered around a table.
Code sample
<html>
<body>
<table height="100%" width="100%" border="1">
<tr>
<td>
<form name="signup" class="signup" action="signup.php" style="border: 1px solid #000; ">
<input type="text" placeholder="Email"/><br>
<input type="text" placeholder="Username"/><br>
<input type="password" placeholder="Password"/><br>
<button type="submit">Next</button>
</form>
</td>
</tr>
</table>
</body>
</html>
​
css:
width : 200px;
margin 0 auto;