CSS - Add padding only if another div is visible - html

I have the following html..
.wrapper {
background:wheat;
}
.woocommerce-store-notice, p.demo_store {
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0;
width: 100%;
font-size: 1em;
padding: 1em 0;
text-align: center;
background-color: #a46497;
color: #fff;
z-index: 99998;
box-shadow: 0 1px 1em rgba(0,0,0,.2);
}
<div class="wrapper">
This is my content
</div>
<p class="woocommerce-store-notice demo_store" style="display: block;">DEMO NOTICE CLICK HERE Dismiss</p>
The notice is covering up my content, I know I can add some padding to fix this but how can I add it so that the padding only applies if the .woocommerce-store-notice is visible?

You may solve it using 1 line of jQuery.
In the code, $(".woocommerce-store-notice").outerHeight() gets the height of the notice and applies the value to margin-top of .wrapper
$(".wrapper").css({"margin-top":$(".woocommerce-store-notice").outerHeight()})
.wrapper {
background:wheat;
}
.woocommerce-store-notice, p.demo_store {
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0;
width: 100%;
font-size: 1em;
padding: 1em 0;
text-align: center;
background-color: #a46497;
color: #fff;
z-index: 99998;
box-shadow: 0 1px 1em rgba(0,0,0,.2);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="woocommerce-store-notice demo_store" style="display: block;">DEMO NOTICE CLICK HERE Dismiss</p>
<div class="wrapper">
This is my content
</div>

If you can't change the html markup, you can use flex to change the order of your selectors i.e. .woocommerce-store-notice, p.demo_store would be order: 0; and .wrapper would be order: 1; This way you don't have to depend on absolute positioning to make sure the notice appears at the top of your page.
Try the snippet below.
// dismiss the notice by clicking the "Dismiss" link"
document.querySelector('.woocommerce-store-notice__dismiss-link').addEventListener('click', function(e) {
e.preventDefault();
document.querySelector('.woocommerce-store-notice').style.display = 'none';
});
* {
margin: 0;
}
.flex {
display: flex;
flex-direction: column;
}
.wrapper {
background: wheat;
order: 1;
}
.woocommerce-store-notice,
p.demo_store {
order: 0;
font-size: 1em;
padding: 1em 0;
text-align: center;
background-color: #a46497;
color: #fff;
box-shadow: 0 1px 1em rgba(0, 0, 0, .2);
}
<div class="flex">
<div class="wrapper">
This is my content
</div>
<p class="woocommerce-store-notice demo_store">DEMO NOTICE CLICK HERE Dismiss</p>
</div>

Related

White space between body and div

I have been looking everywhere for a solution to my problem, but nothing seems to work. It always bugged me that the top of my website always had a gap on the top and all the answers to the problem did nothing. I inspected the website in firefox and found that there was a whitespace between the body and first div (I tried putting the code on the same row but that did nothing). Any Idea on how to remove the whitespace? (I can remove it in inspect on firefox and it goes away there)
Here is the HTML and CSS:
/* CSS Document */
html,
body {
height: 100%;
}
* {
overflow: auto;
margin: 0;
}
body {
margin: 0;
background-color: #111111;
padding: 0;
font-family: verdana;
box-sizing: border-box;
font-size: 1rem;
font-weight: 400;
color: white;
text-align: left;
}
video {
width: 100%;
z-index: 0;
}
.menubar {
left: 100;
}
.ulmenubar {
list-style-type: none;
margin: 0;
padding: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 1;
}
.ulmenubar a {
float: left;
}
.space {
margin: 0;
padding: 0;
}
.rightside {
float: right;
}
.ulmenubar a {
display: block;
color: whitesmoke;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.ulmenubar a:hover {
color: grey;
}
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
background: #181818;
}
::-webkit-scrollbar-thumb {
background: white;
border-radius: 10px;
}
.row {
display: flex;
}
.column {
flex: 33.33%;
padding: 5px;
}
#contact {
width: 200px;
margin-left: auto;
margin-right: auto;
}
#cover {
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.6);
z-index: 5;
width: 100%;
height: 100%;
display: none;
}
<div class="menubar">
<ul class="ulmenubar">
<div class="rightside">
Home
Portfolio
About
Contact
</div>
</ul>
</div>
<div class="space"><video id="background-video" autoplay="" loop="" muted=""><source src="vid.mp4" type="video/mp4"></video></div>
<div id="vids"></div>
<div class="row">
<div class="column">
<img src="https://media0.giphy.com/media/xT8qBoDhee6aWOhLqw/giphy.gif?cid=790b7611a86749120009311d9c18651c96962c8e7e27def2&rid=giphy.gif&ct=g" alt="Sea" style="width:100%">
</div>
<div class="column">
<a onclick="openWin()"><img src="https://media4.giphy.com/media/UxTZDNv0Zej4s/giphy.gif?cid=790b76118b066a194adc23988667623a5aea3c7b55f3c44b&rid=giphy.gif&ct=g" alt="Forest" style="width:100%"></a>
</div>
<div class="column">
<img src="https://media4.giphy.com/media/l41lQIclE3lItAlfq/giphy.gif?cid=ecf05e47mgmmt0kas0kp9zc0435gcanw878chwmk129z9uhf&rid=giphy.gif&ct=g" alt="Sky" style="width:100%">
</div>
</div>
* {
margin: 0px;
padding: 0px;
}
Add this to your CSS
After many hours of trying, I figured out what the problem was. This may not be the case but when I went into the HTML file on text edit, I saw two <body> on the top, I deleted one and also deleted the space between <head> and <body>. It worked.

CSS <div> strange top padding [duplicate]

This question already has answers here:
What is the default padding and/or margin for a p element (reset css)?
(5 answers)
Closed 1 year ago.
Im new to web development and currently developing a webapp for my mothers company.
I'm stuck on writing text block above the background image and I'm getting strange top padding in the div. I can't figure out where that comes from so I can remove it and work from there, adding additional padding and applying more css to the .text-block.
Talking about this one:
$(window).scroll(function() {
var sticky = $('.main-banner'),
scroll = $(window).scrollTop();
});
html,
body {
font-family: cursive, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
}
.main-banner {
width: 100%;
height: 60px;
background-color: #51ad89;
box-shadow: 2px 2px 5px grey;
position: fixed;
z-index: 1;
top: 0;
left: 0;
right: 0;
}
.main-nav {
display: flex;
justify-content: center;
align-items: center;
}
.main-nav a {
text-decoration: none;
color: black;
font-size: 18px;
text-shadow: 2px 2px 5px #ccdbd5;
margin: 0px 80px 0px 80px;
}
.main-nav a:hover {
/*text-decoration: underline;*/
color: #e6f0ec;
}
.main-nav img {
display: flex;
align-items: center;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
.main-div img {
width: 100%;
}
.main-div {
position: relative;
}
.text-block {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.678);
color: white;
display: flex;
flex-direction: column;
align-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header class="main-banner" id="stickyHeader">
<nav class="main-nav">
О студии
Услуги
<img src="/src/tree.png" height="60px" width="60px">
Работы
Контакты
</nav>
</header>
<section class="main-div">
<img src="/src/mainphoto.png">
<div class="text-block">
<p>СТУДИЯ «GREEN ERA»</p>
<p>АВТОРСКИЙ ЛАНДШАФТНЫЙ ДИЗАЙН</p>
<button>Консультация дизайнера</button>
</div>
</section>
You can set the margin on the top to 0 for your <p> elements;
p {
margin-top: 0;
}
It is because of <p> tag's default margin see the picture.
Use a <span> or <div> instead.

Absolutely positioned black div with opacity does not cover a certain element and I can't figure out why

I have a modal window which has an absolutely positioned black div that covers the entire viewport whenever the modal window is open. For some reason though this window doesn't cover a certain element even though it goes over it. Everything else is covered by the black screen just fine so I have no idea why this specific element doesn't.
.black-screen {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.8);
}
The element that is not being covered:
<div class="form-container">
<form class='send-message'>
<input class='message-input' placeholder="Type a message">
</form>
</div>
And the CSS of that element:
.form-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.send-message {
position: relative;
padding: 20px 0px 20px 20px;
flex: 1;
}
.message-input {
width: 100%;
padding: 15px;
outline: none;
border: none;
background-color: #40444b;
color: #dcddde;
font-size: 14px;
border-radius: 5px 0 0 5px;
}
With small chunk of css I can only guess (css is a cascade and is hereditary)
.form-container {
position: relative; /* remove this */
display: flex;
justify-content: center;
align-items: center;
}
.send-message {
position: relative; /* remove this */
padding: 20px 0px 20px 20px;
flex: 1;
}
.message-input {
width: 100%;
padding: 15px;
outline: none;
border: none;
background-color: #40444b;
color: #dcddde;
font-size: 14px;
border-radius: 5px 0 0 5px;
}
If for whatever reasons this is not possible or it doesn't work - replace div elements but I don't know if this will work in every browser ...
<div class="form-container">
<form class='send-message'> <input class='message-input' placeholder="Type a message"> </form>
</div>
<div class="black-screen">black</div>
If the problem persists, use Validator w3c to find out how bad it is
... in fact, use Validator no matter what

Creating a title overlaying a line (CSS)

For my web app's landing page, I'm trying to create a title that appears overlaid on a dotted line (similar to this effect). This is what I currently have:
How do I create this such that the dotted line does not run through the title? I prefer to use the simplest CSS/HTML I possibly can and support the max number of browsers.
My code is pretty rudimentary. So far it is:
<h2>New Account:</h2><br>
<h2 style="margin-top:-0.5em;border:2px dashed #ffffff;border-radius:4px;color:white;display: inline-block;padding:10px 5px 5px 5px;">Choose Nickname:<br>Password:<br></h2>
With the example below you don't need to know the background color, is perfectly scalable, the dots extend to the remaining space of the title.
Actually, the title can wrap on multiple lines.
Feel free to tweak it to your needs and don't forget to prefix.
dotted-container {
border: 2px dotted red;
border-top-width: 0;
margin: 2rem 1rem;
display: block;
}
dotted-container>.content {
padding: 1rem;
}
dotted-title {
display: flex;
align-items: center;
height: 2px;
margin: 0 2px;
}
dotted-title > span {
padding: 0 1rem;
}
dotted-title:after,
dotted-title:before {
border-top: 2px dotted red;
content: '';
display: inline-block;
height: 0;
flex:1;
}
<dotted-container>
<dotted-title>
<span>title</span>
</dotted-title>
<div class="content">
Actual content
</div>
</dotted-container>
<dotted-container>
<dotted-title>
<span>some other title</span>
</dotted-title>
<div class="content">
Some other actual content
</div>
</dotted-container>
<dotted-container>
<dotted-title>
<span>and here's a title<br /> on two lines</span>
</dotted-title>
<div class="content">
Some content for a title on two lines.
</div>
</dotted-container>
Of course, you might want to adjust the margin/padding to your own liking and to accommodate any title wrapping on more than one line.
If you want to replace the "crappy" dotted line with a true dotted one, here's an example. Read the blog post to understand it.
Another good write-up on border-image property here.
Also note you don't have to use custom tags, as I did. It's an example. You may use classes or any other selectors that work for your specific case.
And here's an SCSS script I made you can use to pass in your selectors and desired margin/padding values. Far from perfect, but seems to do the trick:
$border-width: 2px;
$border-style: dotted;
$border-color: red;
$container: 'dotted-container';
$title: 'dotted-title';
$content:'.content';
$padding: 2rem;
$margin: 1rem;
$title-padding-value: 3;
$title-padding-unit:rem;
#{$container} {
border: $border-width $border-style $border-color;
border-top-width: 0;
margin: #{$title-padding-value/2}#{$title-padding-unit} $margin $margin $margin;
display: block;
> #{$content} {
padding: #{$title-padding-value/2}#{$title-padding-unit} $padding $padding $padding;
}
#{$title} {
display: flex;
align-items: center;
height: $border-width;
margin: 0 $border-width;
> span {
padding: 0 $padding;
}
&:after,
&:before {
border-top: $border-width $border-style $border-color;
content: '';
display: inline-block;
height: 0;
flex: 1;
}
}
}
Here's a solution with a combination of pseudo elements, flexbox, and absolute positioning.
* {
margin:0;padding:0;
box-sizing:border-box;
}
body {
background: url('https://s-media-cache-ak0.pinimg.com/originals/33/3b/4f/333b4f22ae39d1aaf8c23d77e759d8e1.jpg') 0 0 no-repeat / cover;
}
h2:before,h2:after {
content: '';
bottom: 50%;
border-top: 3px dotted black;
flex: 1 0 0;
}
h2:before {
margin-right: 1em;
}
h2:after {
margin-left: 1em;
}
h2 {
display: flex;
align-items: center;
font-size: 3em;
position: absolute;
top: 0; left: 0; right: 0;
transform: translateY(calc(-50% + 1px));
text-shadow: 0 3px 0 #fff;
}
section {
border: dotted black;
border-width: 0 3px 3px;
position: relative;
width: 80%;
margin: 3em auto;
padding-top: 3em;
background: rgba(255,255,255,0.5);
}
<section>
<h2>New Account:</h2>
<p>foo</p>
<p>foo</p>
<p>foo</p>
</section>
You can use a combination of z-index and background-color, as shown in the snippet below:
z-index pulls the New Account: title in front, then the background-color hides the border behind the it
body {
background: green;
}
#one {
position: absolute;
left: 35px;
z-index: 1;
background: green;
display: inline-block;
color: white;
}
#two {
position: absolute;
z-index: -1;
top: 55px;
margin-top: -0.5em;
border: 2px dashed white;
border-radius: 4px;
color: white;
display: inline-block;
padding: 10px 5px 5px 5px;
}
<h2 id="one">New Account:</h2><br>
<h2 id="two">Choose Nickname:<br>Password:<br></h2>
Perhaps you can do something like this:
* {
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.box {
background: green;
padding: 15px;
width: 250px;
height: 250px;
}
.inner-box {
border: 1px dotted #ffffff;
height: 100%;
position: relative;
-webkit-border-radius: 7px;
border-radius: 7px;
}
.inner-box p {
position: absolute;
width:70%;
text-align: center;
top: -25px;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
color: #ffffff;
display: block;
background: green; /* Make it the same as background color */
}
<div class="box">
<div class="inner-box">
<p>My Awesome Title</p>
</div>
</div>
I would suggest having a background colour on the "New Account" if the background is only one colour, that way the dotted line will not be seen as it is covered by the background colour.
The code snippet shows how this can be adjusted to show more or less of the dotted border either side of the title.
.parent{
background-color: green;
position: relative;
font-size: 14px;
}
.parent h2:first-child{
color: #fff;
text-align: center;
z-index: 1;
background-color: green;
position: absolute;
left: 20px;
padding: 0 5px;
}
.parent h2:last-child{
margin-top: 15px;
z-index: 0;
}
.parent_two h2:first-child{
left: 12px;
padding: 0 17px;
}
<div class='parent'>
<h2>New Account:</h2><br>
<h2 style="border:2px dashed #ffffff;border-radius:4px;color:white;display: inline-block;padding:10px 5px 5px 5px;">Choose Nickname:<br>Password:<br></h2>
</div>
<div class='parent_two parent'>
<h2>New Account:</h2><br>
<h2 style="border:2px dashed #ffffff;border-radius:4px;color:white;display: inline-block;padding:10px 5px 5px 5px;">Choose Nickname:<br>Password:<br></h2>
</div>
I would move the title up with absolute positioning (just make sure the parent is relative positioned), wrap the title text in a <span> and then add padding and matching background color to that <span>.
body {
margin: 0;
background-color: green;
}
.box {
position: relative;
margin: 1rem;
padding: 1rem;
color: white;
box-sizing: border-box;
border: 1px dashed white;
}
.box-title {
position: absolute;
top: -1rem;
left: 0;
margin: 0;
width: 100%;
text-align: center;
font-size: 1.5rem;
}
.box-title>span {
padding: 0 1rem;
background-color: green;
}
<div class="box">
<h2 class="box-title"><span>New Account:</span></h2>
<p>Username:</p>
<p>Password:</p>
</div>
FWIW, I don't typically care for extra markup but if I have to work extra hard to make it work some other way then I find it acceptable. Especially when it's super simple.
body{
background: url('https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=750&h=350') center top 0 no-repeat / cover;
}
.wrapper {
max-width: 400px;
margin: 30px auto 0;
border: 2px dotted red;
height: 200px;
border-top: 0;
text-align: center;
}
.heading {
display: table;
width: 100%;
position: relative;
}
.heading:before {
content: '';
display: table-cell;
border-top: 2px dotted red;
}
.heading:after {
content: '';
display: table-cell;
border-top: 2px dotted red;
}
.txt-wrapper {
display: table-cell;
width: 1%;
white-space: nowrap;
line-height: 0;
padding: 0 10px;
}
<div class="wrapper">
<div class="heading">
<h2 class="txt-wrapper">
This is heading
</h2>
</div>
<P>
This is paragraph.
</P>
<P>
This is another paragraph.
</P>
</div>

Use before & after Pseudo-element to make a line

I'm using Pseudo-element :before and :after to draw a line before and after a title. It's working with an image:
.mydiv::before {
content: url(img/line.png);}
.mydiv::after {
content: url(img/line.png);}
Here is the result :
But, I would like the line to expand and fill in the whole div before and after the title, like this :
Is there a way to specify a percentage for the image for it to stretch? I try this, but it's not working :
.mydiv img {
width: 100%;
height: auto;
}
You don't need both :before and :after, either of the 2 will be enough and as you've been told, you don't need an image. See the approach below.
#header {
width: 100%;
height: 50px;
margin: 50px 0;
text-align: center;
font-size: 28px;
position: relative;
background-color: #57585C;
}
#header:after {
content: '';
width: 100%;
border-bottom: solid 1px #fff;
position: absolute;
left: 0;
top: 50%;
z-index: 1;
}
h3 {
background-color: #57585C; /* Same as the parents Background */
width: auto;
display: inline-block;
z-index: 3;
padding: 0 20px 0 20px;
color: white;
position: relative;
font-family: calibri;
font-weight: lighter;
margin: 0;
}
<div id="header">
<h3>Last Projects</h3>
</div>
In case you need <h3> title to have transparent background - you can use both :before and :after and display: flex
More about flex-grow you can read here https://developer.mozilla.org/en-US/docs/Web/CSS/flex.
body {
background: linear-gradient(0.25turn, #3f87a6, #000000, #f69d3c); /* example of custom background */
}
#header {
display: flex;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: center; /* making vertical centerign of all children */
}
#header::before, #header::after {
content: '';
flex: 1 1 auto; /* the first digint is 'flex-grow: 1', helps elemet to occupy all free space */
border-bottom: solid 1px #fff;
}
h3 {
flex: 0 1 auto; /* the first digint is flex-grow: 0 */
padding: 0 15px 0 15px;
color: #fff;
}
<div id="header">
<h3>Last Projects</h3>
</div>
<style>
.mydiv::before{
content: '';
position: absolute;
left: 0;
width: 100%;
height: 2px;
bottom: 1px;
background-color: black;
}
</style>
<div class="mydiv">About us</div>