On hover event I show a div which has absolute positioning, at the bottom of that div I need a fixed div.
Have a look at the image what I want to achieve.
I have following code for this:
CSS
.showpro{position:absolute;z-index:999;width:500px;height:auto;display:none;max-height:500px;}
.bottom{width:500px;position:fixed;bottom:0;left:0;}
CODE
<div class='showpro'>
<div class='top'>
</div>
<div class='bottom'>
</div>
</div>
With this code, it comes at left-bottom corner of Container div. Can anyone say why? Any help?
An element with position: fixed is always positioned relative to the browser window. Try using position: absolute for that "fixed" element.
Here is an example which shows your expected result.
.container{
position:relative;
width:100vw;
height:100vh;
background-color:brown;
}
.showpro{
position:absolute;
z-index:999;
width:200px;
height:auto;
max-height:300px;
padding-bottom:80px;
overflow:hidden;
background-color:red;
right:0;
}
.top{
overflow:auto;
max-height:220px;
margin-right: 20px;
}
.bottom{
width:200px;
position:absolute;
bottom:0;
left:0;
height:80px;
background-color:green;}
<div class="container"><div class='showpro'>
<div class='top'>
Here can be a lot of text. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div class='bottom'>
This is the "fixed" element.
</div>
</div></div>
Change position to relative for both the main div and the div inside main div :
Example: use this css code
#four {
background-color: blue;
height: 200px;
position: relative;
top: 50px;
width: 100px;
}
Related
I mostly don't use html\css in my professional life, but for now I have a task that requires some work with markdown.
I have a text which should be no more than 80 character width. Here is a css:
.content {
width: 80ch;
margin-left:auto;
margin-right:auto;
}
I want to embed the image like this one:
<img src="https://ucarecdn.com/4922ea06-6240-4be5-bd43-06021daa81d4/accountcredentials.png">
If I apply it simply within the same div image would go beyond borders (it has bigger width)
How can I put image within width of text block?
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="centered">
<p class="content">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p class="img">
<img src="https://ucarecdn.com/4922ea06-6240-4be5-bd43-06021daa81d4/accountcredentials.png">
</p>
</div>
.centered {
}
.content {
width: 80ch;
margin-left:auto;
margin-right:auto;
}
Did you mean like that?
.content {
width: 80ch;
margin: 0 auto;
}
img {
width: 100%;
}
<div class="centered">
<p class="content">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<img src="https://ucarecdn.com/1d0fc207-9d61-4bf3-8e74-a273f1b4ce91/accountcredentials.png">
</p>
</div>
Image inside the p tag and width: 100%
fiddle
Set the image as a background.
.content {
width: 80ch;
margin-left:auto;
margin-right:auto;
background-image: url('https://ucarecdn.com/4922ea06-6240-4be5-bd43-06021daa81d4/accountcredentials.png');
background-size: cover;
}
You can then apply padding to the class to add spacing around the text, and the borders of the background.
Set a max-width on the image to prevent it overflowing the container.
.content img {
max-width: 100%;
}
So, to make a div (navbar) stay on top of the page I need to have a fixed position. This I know and works fine. But on the current web design I am working on this navbar ends up behind all of the other elements. The difference between now and the previous times is that I am using javascript to modify the placement of the content depending on how the user scrolls. This also works fine but the navbar ends up behind all of these elements. After googling I found that I could use z-index and an absolute or relative position. But I need a fixed one to have it on top..
What is a good way to do this?
What do I need to have in mind?
I am not sure what is actually relevant here but..
#nav {
position:fixed;
top:0;
width:100%;
height:50px;
z-index:999;
}
#header {
position:absolute;
top:0;
left:0;
transition: 0.6s;
}
#wrapper {
width:100%;
position:absolute;
top:45%;
left:-10px;
transition: 0.6s;
}
Use z-index for the placement of the navbar. You may try this setting by varying z-index value in the browser unless you get the desired result. Try to enforce z-index using css rather than script (in case you are trying it with the script).
Try use css in your navbar:
position: fixed;
top: 0px;
left: 0px;
z-index: 999;
Exapmle:
.navbar{
position: fixed;
top: 0px;
left: 0px;
z-index: 999;
background: #555;
width: 100%;
}
.item {
color: rgba(255,255,255,0.75);
font-weight: bold;
list-style-type: none;
margin-right: 5px;
float:left;
padding: 10px;
background: #555;
cursor: pointer;
} .item:hover {
color: rgba(255,255,255,1);
background: #999;
}
.page{
position: absolute;
top: 40px;
left 0px;
}
<div>
<div class='navbar'>
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
</div>
<div class='page'>
Some long text.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
When flex-basis:auto is set on a div, the div expands vertically to fit contents.
Example
Html:
<div class="parent">
<div class="image">
<img src="http://placebacon.net/400/103">
</div>
<div class="text">
-- Removed for readability --
</div>
</div>
scss:
.parent {
position:absolute;
top:0px;
left:0px;
right:0px;
bottom:0px;
display:flex;
flex-direction:column;
.image {
flex-basis:auto;
flex-grow:0;
flex-shrink:0;
img {
width:100%;
height:auto;
}
}
.text {
flex-basis:0px;
flex-grow:1;
flex-shrink:1;
overflow-y:scroll;
}
}
However this does not work horizontally :
Example 2
.parent {
position:absolute;
top:0px;
left:0px;
right:0px;
bottom:0px;
display:flex;
flex-direction:row;
.image {
flex-basis:auto;
flex-grow:0;
flex-shrink:0;
img {
height:100%;
width:auto;
}
}
.text {
flex-basis:0px;
flex-grow:1;
flex-shrink:1;
overflow-y:scroll;
}
}
If you resize the height of the parent, the image will change size to preserve aspect ratio. However the flex box retains the size of the image before the transformation.
Is there a CSS property I need to set to get the expected behavior?
Seems like you need to set an height in .parent, .image and finally to img to trigger the calculation for each elements : https://jsfiddle.net/knjesznx/4/
You should not mind the flex value unless you have really litlle content aside and a background to show , then just add flex:1 to the container that is suppose to fill all space left demo
.parent {
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
height: 100vh;/* here */
display: flex;
flex-direction: row;
}
.parent .image {
height: 100%;/* here */
}
.parent .image img {
height: 100%;/* here */
width: auto;
}
.parent .text {
overflow-y: scroll;
/* eventually use flex:1; to fill all space left*/
}
<div class="parent">
<div class="image">
<img src="http://placebacon.net/103/400">
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
I would remove the and set the image as the background of the image container. The css would look like:
.image {
flex-basis:auto;
flex-grow:0;
flex-shrink:0;
height: 100%;
width: 150px;
background-image: url('http://placebacon.net/103/400');
background-size: cover;
}
If a parent element has a height of auto, a child element with a height of 100% will size to the child content.
Example:
html, body{ height:100%; padding:0; margin:0;}
.grandParent{
background-color:red;
height:100%;
}
.parent{
background-color:blue;
height:auto;
}
.child{
background-color:green;
height:100%;
}
<div class="grandParent">
<div class="parent">
<div class="child">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
However, if you set the parent element to display:inline, a child element with a height of 100% it will size to 100% of the grandparent.
Example:
html, body{ height:100%; padding:0; margin:0;}
.grandParent{
background-color:red;
height:100%;
}
.parent{
background-color:blue;
display:inline;
}
.child{
background-color:green;
height:100%;
}
<div class="grandParent">
<div class="parent">
<div class="child">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
Why does height:auto size to the child content and display:inline size to the grandparent?
The height: auto gives the div the height that contents have. So if you have a line of code it will have 20px height for example but if you have two lines the height will be double of that.
In the height auto the browser calculates the height.
This is my code:
#footer {
font-size: 10px;
position:absolute;
bottom:0;
background:#ffffff;
}
I've no idea what is wrong with this - can anyone help?
EDIT: For some more clarity on what's wrong: The footer is displayed on the bottom as expected when the page loads. However, when the web page's height is > than the dimensions on the screen such that a scroll bar appears, the footer stays in that same location. That is to say, when the height of the page is <= 100%, the footer is at the bottom. However, when the page height is >100%, the footer is NOT at the bottom of that page, but at the bottom of the visible screen instead.
EDIT: Surprisingly, none of the solutions below worked. I ended up implementing a sidebar instead.
You're probably looking for this example:
<div class="wrapper">
Your content here
<div class="push"></div>
</div>
<div class="footer">
Your footer here
</div>
CSS:
For a 142-pixel footer
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -142px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
height: 142px; /* .push must be the same height as .footer */
}
/*
Sticky Footer by Ryan Fait
http://ryanfait.com/
*/
Try this:
position: fixed;
bottom: 0;
I had the same question, came here looking for an answer, didn't find it, then tried a few experiments on my own, and finally got the solution:
#body {
overflow-y: 0 auto;
}
#footer {
position: fixed;
top: 100vh; left: 0;
margin-top: -100px;
width: 100%; height: 100px;
padding: 10px;
color: #fff; background-color: rgba(0,0,0,0.6);
}
<div id="body">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
</div>
<div id="footer">
<span>Some dummy Text</span>
</div>
The wrapper is the rest of your page. The negative/positive margin/height values are where the magic happens.
.wrapper
{
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -142px;
}
.footer, .push
{
height: 142px; /* .push must be the same height as .footer */
}
#footer { clear:both; position:fixed; width:100%; height:50px; bottom:0; background:black;}
Do not use position: absolute; for any footer as the page will change in height. If it is absolute then your footer will not move with the page height.
You want to use ryan fait's method.
Although I would personally do it like this;
.wrap {margin: auto; width: 980px;}
#content {min-height: 600px;}
#footer {height: 300px;}
<div class="wrap">
<div id="content">
</div>
</div>
<div id="footer">
<div class="wrap">
</div>
</div>
This way you don't have to mess around with negative margins and padding. Also this can easily be a part of html5 changing #footer to
<footer>
</footer>
This is what I did and it caused my footer to stay at the bottom.
.footer2{
background-color:#606060 ;
color: #ffffff;
height: 30px;
bottom:0px;
position:fixed;
width:100%;
}
.footer-small, .push {
background-color: #2C3E50;
position: fixed;
padding-top: 5px;
clear:both;
width: 100%;
bottom:0px;
z-index: 0;
}
this is also working for me....
I struggled to find a solution, as none of the suggested achieved what I wanted:
If there is to less content, stay at the bottom of the page, not in the middle.
If there is enough content, do not be stick and overlap the content, just stay at the bottom.
Hide it from the first sight, so only if the user scrolls down the footer is seen.
This is what worked for me:
html:
<body>
<div class="page-wrapper">
<h1>
Page
</h1>
</div>
<footer>
Footer here
</footer>
</body>
css:
body {
height: 100%;
width: 100%;
}
.page-wrapper {
min-height:100vh; /*1vh = 1% of browser screen height*/
}
footer{
position: relative;
width: 100%;
bottom: 0px;
}
Here in action.
Why not with jquery?
Put a wrapper div between header and footer and assign min-height property for wrapper with jquery equal with the difference between document height and (header height + footer height).
<script type="text/javascript">
$(document).ready(function(){
var dh = $(document).height(); //document height here
var hh = $('header').height(); //header height
var fh = $('footer').height(); //footer height
var wh = Number(dh - hh - fh); //this is the height for the wrapper
$('#wrapper').css('min-height', wh); //set the height for the wrapper div
});
</script>