I have a simple < div > (without fixed hegiht) with a text in it:
<div id="section">
<div class="container">
<h1>text</h1>
<p>More text</p>
</div>
<!-- <div id="overlay"></div> -->
</div>
The CSS for this is something like:
#section {
background: red;
overflow: hidden;
}
It's possibile to add a div with a transparent image background?
The overlay sholud be hover the main red background, but under the text.
I think is something like this, but dont works:
#section #overlay {
position: relative;
top: 0px;
left: 0px;
width: 100%;
height: 100px; /* ??? */
background: green;
opacity: 0.1;
}
#section {
background: red;
display: block;
position: relative;
overflow: hidden;
}
#container {
position: relative;
width: 100%;
text-align: center;
z-index: 9999;
}
#overlay {
position: absolute;
left: 0;
width: 100%;
height: 100%;
top: 0;
background-color: rgba(0,0,0,0.3);
}
If I'm understanding you correctly, how about something like this:
HTML:
<div id="section">
<div id="container">
<h1>My background is transparent!</h1>
<p>More text</p>
</div>
</div>
CSS:
#section {
width: 300px;
background-color: red;
}
#container {
position: relative;
left: 50px;
width: 200px;
text-align: center;
background-color: rgba(0,0,0,0.3);
}
Results here: JSFiddle
If that's not what you wanted, can you be more specific about the positioning?
try below example using jquery ui will reduce the effort
<div id="dialog">Your non-modal dialog</div>
Open dialog
$('#open').click(function() {
$('#dialog').dialog('open');
});
jQuery(document).ready(function() {
jQuery("#dialog").dialog({
autoOpen: false,
modal: true,
open: function(){
jQuery('.ui-widget-overlay').bind('click',function(){
jQuery('#dialog').dialog('close');
})
}
});
});
Related
I have 2 div's, one after the other. When i move first div with postion: relative and top: -60px it creates gap between them.
Here is example: https://codepen.io/dusannis/pen/oNgBpoK
As you can see there is gap between red and yellow div. Is there some css property that I can add to parent div that can remove this gap, or something simillar?
This is HTML:
<body>
<div class="container">
<div class="div-1">
<p>something here</p>
</div>
<div class="div-2"></div>
</div>
</body>
This is CSS:
body {
background: blue;
padding: 60px
}
.div-1 {
padding: 60px;
position: relative;
top: -50px;
background: red;
}
.div-2 {
height: 50px;
background: yellow;
}
Use negative margin instead of relative positioning.
body {
background: blue;
padding: 60px
}
.div-1 {
padding: 60px;
/* position: relative; --> not required */
margin-top: -50px;
/* change this */
background: red;
}
.div-2 {
height: 50px;
background: yellow;
}
<div class="container">
<div class="div-1">
<p>something here</p>
</div>
<div class="div-2"></div>
</div>
Codepen Demo of the effects of various methods of "moving" elements:
"Relative Position vs Margin vs Transform".
You can try add same top/position to the second div:
.div-1 {
padding: 60px;
position: relative;
top: -60px;
background: red;
}
.div-2 {
position: relative;
top: -60px;
height: 50px;
background: yellow;
}
Alternatively you can add internal div and use padding for that one, then get rid of padding for the parent and the body (or adjust to the real value if you want it):
<body>
<div class="container">
<div class="div-1">
<div class="div-1-inside">
something here
</div>
</div>
<div class="div-2"></div>
</div>
</body>
body {
background: blue;
padding: 10px;
}
.div-1 {
position: relative;
background: red;
}
.div-1-inside {
padding: 60px;
background: red;
}
.div-2 {
height: 50px;
background: yellow;
}
I've got an absolutely positioned menu above a parent that has a hover state.
I'm looking for a way to make it so that hovering over the menu won't trigger the parent's hover state.
.row {
height: 52px;
background: #F4F3D9;
width: 100%;
position: relative;
}
.row:hover {
background: black;
}
.menu {
height: 100px;
width: 100px;
background-color: green;
position: absolute;
z-index: 500;
}
<div class="container">
<div class="row">
<div class="menu">
Hovering me should not make the row black
</div>
</div>
</div>
Thanks!
You can use a bit of Javascript to tell the mouseover event on the .menu not to propagate on its parent. Here is an example:
document
.querySelector('.menu')
.addEventListener('mouseover', e => { e.stopPropagation() })
.row {
height: 52px;
background: #F4F3D9;
width: 100%;
position: relative;
}
.row:hover {
background: black;
}
.menu {
height: 100px;
width: 100px;
background-color: green;
position: absolute;
z-index: 500;
}
<div class="container">
<div class="row">
<div class="menu">
Hovering me should not make the row black
</div>
</div>
</div>
I have a sidebar that has the position: sticky added to it, but when I scroll past a certain point it stops being sticky?!
Tested in Chrome version: Version 61.0.3163.100 and Safari Version: 11.0
HTML:
<div class="sticky">
this should stick!
</div>
<div class="content">
this is content
</div>
CSS:
<style media="screen">
.sticky {
background-color: #ccc;
position: -webkit-sticky;
position: -moz-sticky;
position: -ms-sticky;
position: -o-sticky;
position: sticky;
top: 15px;
width: 100px;
float: left;
}
.content{
background-color: #eee;
height: 3000px;
width: 700px;
float: right;
}
</style>
position: sticky; is not supported most of browsers http://caniuse.com/#feat=css-sticky
You can try something like this:
HTML:
<div class="sticky-block">
this should stick!
</div>
CSS:
.sticky {
position: fixed;
top: 15px;
}
JS:
var $stickyBlock = document.querySelector('.sticky-block');
var origOffsetY = $stickyBlock.offsetTop - 15; // 15 is your top margin
function onScroll() {
window.scrollY >= origOffsetY ? $stickyBlock.classList.add('sticky') :
$stickyBlock.classList.remove('sticky');
}
document.addEventListener('scroll', onScroll);
or jQuery:
var $stickyBlock = $('.sticky-block');
var origOffsetY = $stickyBlock.offset().top - 15; // 15 is your top margin
function onScroll() {
window.scrollY >= origOffsetY ? $stickyBlock.addClass('sticky') :
$stickyBlock.removeClass('sticky');
}
$(document).on('scroll', onScroll);
jsfiddle
Here, try this, I would say it's better for this rather than using "Sticky" and it doesn't use Jquery or anything just simple position fixed.
Html
<div class="sticky">
this should stick!
</div>
<div class="content">
this is content
</div>
Css
.sticky {
background-color: #ccc;
top: 15px;
width: 100px;
float: left;
position:fixed;
}
.content{
background-color: #eee;
height: 3000px;
width: 700px;
float: right;
}
https://jsfiddle.net/udxuh1qf/
I tested your code on jsfiddle and also here, it is probably your screen media query that causes your problem. Check out my version of sticky. I hope it will fix your issue.
.sticky {
background-color: #ccc;
color:red;
position: sticky;/* required */
position: -webkit-sticky;/* required */
top: 15px; /* required */
float:left;
}
.content{
background-color: #eee;
height: 3000px;
width: 700px;
float right;
}
<div class="sticky">
this should stick!
</div>
<div class="content">
this is content
</div>
Here's my working example:
http://jsfiddle.net/UGhKe/2/
CSS
#body {
height: 200px;
background: black;
width: 100%;
}
.header {
position: fixed;
top: 0;
background: #369;
z-index: 1;
width: 100%;
height: 5em;
}
.content {
position: absolute;
top: 5em;
overflow: hidden;
height: 1000px;
background: #936;
z-index: 0;
width: 100%;
}
.footer {
position: fixed;
bottom: 0;
background: #396;
width: 100%;
}
.large {
font-size: 120%;
padding: 2em;
}
HTML
<div id="body">
<div class="header">
<div class="large">Header</div>
</div>
<div class="content">
Content, you should be able to see this when you scroll to top.
</div>
<div class="footer">
<div class="large">Footer</div>
</div>
</div>
I want the content to be positioned below the header when you scroll the top (but hidden when you scroll down, under header) - this works fine...
However I need to remove top: 5em and use something like "inherit the current height of the header" - is it possible without JS?
If it's really not possible without JS, then I can just use JS but I'd rather try and find a solution in pure CSS.
EDIT:
I should note that the reason I can't use top: 5em is because the header will not have a fixed height - an image (for a logo) will be used inside of the text, and that would be set to max-width: 100% so that it shrinks to right width for an iPhone and doesn't expand too much on say an iPad.
See if thats work for you. http://jsfiddle.net/UGhKe/3/
I added another div with the same height but "non-fixed" to simulate your fixed header.
HTML
<div id="body">
<div id="blockHeader"></div>
<div class="header">
<div class="large">Header</div>
</div>
<div class="content">
Content, you should be able to see this when you scroll to top.
</div>
<div class="footer">
<div class="large">Footer</div>
</div>
</div>
CSS
html, body { margin:0; padding:0; }
#blockHeader
{
width:100%;
height: 5em;
}
.content {
position: absolute;
overflow: hidden;
height: 1000px;
background: #936;
z-index: 0;
width: 100%;
}
You can do it using variables(Use SASS or LESS for that). Take a look at the pen.
CODE:
$headerContentVariable: 5em;
#body {
height: 200px;
background: black;
width: 100%;
}
.header {
position: fixed;
top: 0;
background: #369;
z-index: 1;
width: 100%;
height: $headerContentVariable;
}
.content {
position: absolute;
top: $headerContentVariable;
overflow: hidden;
height: 1000px;
background: #936;
z-index: 0;
width: 100%;
}
.footer {
position: fixed;
bottom: 0;
background: #396;
width: 100%;
}
.large {
font-size: 120%;
padding: 2em;
}
as you can tell by the title I want to have a footer stick to the bottom. I know that there are a lot of topics on that. I already read through them. But I can not get it to work, because of my navigation, which is fixed to the top.
The layout looks like this:
<header class="navbar navbar-fixed">
</header>
<div class="content">
<div class="container">
</div>
<div class="clearfooter"></div>
</div>
<footer>
</footer>
And here is the CSS:
html, body {
height: 100%;
}
body {
padding-top: 40px; /* height of the navbar */
}
.navbar-fixed {
left: 0;
position: fixed;
right: 0;
top: 0;
z-index: 1030;
}
.content {
margin-bottom: -30px;
min-height: 100%;
position: relative;
}
.clearfooter {
clear: both;
height: 30px;
}
#footer {
height: 30px;
position: relative;
}
I tried this tutorial. But the footer is not pinned to the bottom of the window it is further down (not in the viewport anymore). I already tried to fix it with additional padding/margin but nothing worked :(
Instead of adding padding to the body to push your page just create a push div to add some space between your fixed header and your content, like so:
HTML
<div class="push"> </div>
CSS
.push { height:40px; }
.push:before, .push:after {
display: table;
content: "";
zoom: 1;
}
.push:after {
clear: both;
}
Here is a demo:
http://jsfiddle.net/andresilich/fVpp2/1/show/
Edit here http://jsfiddle.net/andresilich/fVpp2/1/
Note: Added a bunch of break lines to illustrate the positioning of the footer.
(edit: jsfiddle cut my CSS, added it back.)
I did an experiment and it worked, here is the html:
<body>
<div class="wrapper">
<div class="header">
</div>
<div class="contain">
</div>
<div class="footer">
</div>
</div>
</body>
and the css:
.header {
height: 100px;
background-color: red;
}
.contain {
height:1500px;
background-color: black;
}
.wrapper {
width: 960px;
background-color: yellow;
margin: 0 auto;
}
body {
margin: 0;
}
.footer {
height: 200px;
background-color: blue;
}
it has both header and footer fixed, I hope you get the clue out of it.