Alternative to using label and input in css modal - html

I got this css modal script and wondering why I can't replace <label> with <a> or <div> tag.
Also, What is the point of this line below? Why can't I replace <input> with something else like <div>?
Here's the complete CSS Code:
.modal {
opacity: 0;
visibility: hidden;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
text-align: left;
background: rgba(0,0,0, .9);
transition: opacity .25s ease;
}
.modal__bg {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
cursor: pointer;
}
.modal-state {
display: none;
}
.modal-state:checked + .modal {
opacity: 1;
visibility: visible;
}
.modal-state:checked + .modal .modal__inner {
top: 0;
}
.modal__inner {
transition: top .25s ease;
position: absolute;
top: -20%;
right: 0;
bottom: 0;
left: 0;
width: 50%;
margin: auto;
overflow: auto;
background: #fff;
border-radius: 5px;
padding: 1em 2em;
height: 50%;
}
.modal__close {
position: absolute;
right: 1em;
top: 1em;
width: 1.1em;
height: 1.1em;
cursor: pointer;
}
.modal__close:after,
.modal__close:before {
content: '';
position: absolute;
width: 2px;
height: 1.5em;
background: #ccc;
display: block;
transform: rotate(45deg);
left: 50%;
margin: -3px 0 0 -1px;
top: 0;
}
.modal__close:hover:after,
.modal__close:hover:before {
background: #aaa;
}
.modal__close:before {
transform: rotate(-45deg);
}
#media screen and (max-width: 768px) {
.modal__inner {
width: 90%;
height: 90%;
box-sizing: border-box;
}
}
and HTML
<p>
<label class="btn" for="modal-1">Show me modal with a cat</label>
</p>
<input class="modal-state" id="modal-1" type="checkbox" />
<div class="modal">
<label class="modal__bg" for="modal-1"></label>
<div class="modal__inner">
<label class="modal__close" for="modal-1"></label>
<h2>The title!</h2>
<p>This is the body</p>
</div>
</div>
JSFIDDLE DEMO

Because clicking on a label will toggle a checkbox, but clicking on a link will go to another page and clicking on a div will do nothing.
The modal depends on the checked state of the input. A div can't be checked.
The whole thing is a nasty hack. It's clever, but nasty. Tracking state in order to display content based on user interaction is a job better handled by JavaScript.

You have to use a label as explained here
This attribute explicitly associates the label being defined with another control. When present, the value of this attribute must be the same as the value of the id attribute of some other control in the same document. When absent, the label being defined is associated with the element's contents.

The label will change the state of the input[type=checkbox] when clicked, and can be placed anywhere on the document.
The input[type=checkbox] must be placed right next to the modal so the modal can be targeted with CSS:
input + .modal { display:none; }
input:checked + .modal { display:block}

The <label> element has a for="" attribute, it looks for the same value of id="" where you set on a form element.
Example: <label for="myid1">...</label> <input id="myid1"> when you click on the label it will change the state of the matching form element. They are normally next to each other in the markup, but not necessarily.
For <input type="checkbox">, you can capture :checked state in the CSS and change some style to itself or next siblings by using + or ~ selector. Which means that modal div needs to be a sibling of the checkbox and next to it.
All of these put in together is to use the label to control the div, a CSS way of making a modal without using any Javascript.

Related

Tooltip is hiding behind the heading of table

I am having a question regarding the tooltip, the tooltip is hiding behind the table heading or it is displaying in one box it is not showing out.
Please, refer to the image where the tip is hiding behind the another div
(marketing shown in image is in the tip):
.tooltipCustom {
position: relative;
display: inline-block;
}
.tooltipCustom .tooltipCustomtext {
visibility: hidden;
width: 120px;
background-color: #efeee6;
color: #868474;
text-align: center;
border-radius: 6px;
padding: 5px 0;
border-width: 1px;
border-style: solid;
border-color: black;
/* Position the tooltip */
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -60px;
}
.tooltipCustom:hover .tooltipCustomtext {
visibility: visible;
}
<div class='tooltipCustom'>
<sup style="vertical-align: top">
Something
</sup>
<span class='tooltipCustomtext'>
Underlying price: List price <br/>
Applied Discounts: Marketing
</span>
</div>
Fix it
/* Position the tooltip */
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -60px;
For example
/* Position the tooltip */
position: absolute;
top: 20px;
left: 50%;
margin-left: 0;
I think that the div above has a z-index defined, you need to put a higher z-index value on your tooltip to be on the front of previous div:
.mypreviousDiv {
z-index: 100;
}
.tooltipCustom .tooltipCustomtext {
z-index: 101; // ou higher, for tooltips we can put 9999
}

How to move a DIV to the center when clicked, using CSS and/or Angular?

I'm trying to create a CSS animation to move the clicked button (actually, the whole DIV that it's contained in) to the center. Each button is in a Div and I'm having trouble figuring out how, using one css class, to get any of the buttons to move to the center.
I've tried a variety of ways in the css, but haven't been able to do it where the other buttons don't move.
A codepen is here.
If you run the code and click the "DIV0" button, it moves to the center. (Click the button again to reset.) When you click any of the other buttons, I want them to go to the same spot as where the "DIV0" button went.
Update: I need to set up the DIVs/Buttons programmatically (passing the "divs" array to the HTML so that ng-repeat builds the layout). The size of the "divs" array may vary, so doing this method automates the building of the layout. Also, the div needs to take up 50% of the width and height so that it arrays out a 2-divs-across matrix on the screen. And each div will be fully filled with an image and text. I'm trying to move the full contents of the div to the center, not just the button.
so my layout may look like:
DIV0 DIV1
DIV2 DIV3
or just:
DIV0 DIV1
DIV2
Here is another codepen. http://codepen.io/furnaceX/pen/BKjyEp/
This one gets all the buttons to the right location, but the centering won't animate. Any ideas?
angular.module('myApp', [])
.controller("ctrl", ['$scope', function($scope) {
$scope.divs = [{id: 0, center: false}, {id: 1, center: false},
{id: 2, center: false}, {id: 3, center: false}];
$scope.fade = false;
$scope.go = function(div) {
if (!$scope.fade) {
div.center = true;
console.log("click again to reset");
$scope.fade = true;
} else {
div.center = false;
$scope.fade = false;
}
}
}])
html, body {
height: 100%;
width: 100%;
}
.block {
text-align: center;
width: 50%;
height: 50%;
float: left;
}
.center {
transition:0.5s linear all;
transform: translate(50%,50%);
top: 50%;
left: 50%;
//position: relative;
}
.fadeout {
transition:0.5s linear all;
opacity: 0 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div class="container" ng-controller="ctrl">
<div ng-repeat="div in divs" class="block">
<div ng-class="{center: div.center, fadeout: fade*!div.center}">
<button ng-click="go(div)">DIV{{ div.id }}</button>
</div>
</div>
</div>
</div>
Pure CSS solution using checkbox (seems to be a button, but that's a checkbox undercover):
updated with 4 buttons
body {
width: 100%;
height: 100vh;
margin: 0px;
background: honeydew;
}
label > input {
display: none;
}
#button0 {
position: absolute;
line-height: 45px;
font-weight: bold;
text-align: center;
display: inline-block;
height: 50px;
width: 50px;
border: 2px dashed black;
box-sizing: border-box;
left: 0;
right: 0;
margin: auto;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
opacity: 0.5;
}
label > input ~ .button {
cursor: pointer;
position: absolute;
line-height: 45px;
font-weight: bold;
text-align: center;
display: inline-block;
height: 50px;
width: 50px;
border: 2px solid crimson;
box-sizing: border-box;
-webkit-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
user-select: none;
-webkit-user-select: none;
}
label > input ~ #button1 {
left: 0;
top: 0;
background: gold;
}
label > input ~ #button2 {
left: calc(100% - 50px);
top: calc(100% - 50px);
background: yellowgreen;
}
label > input ~ #button3 {
left: calc(100% - 50px);
top: 0;
background: skyblue;
}
label > input ~ #button4 {
left: 0;
top: calc(100% - 50px);
background: tomato;
}
label > input:checked ~ #button1, label > input:checked ~ #button2, label > input:checked ~ #button3, label > input:checked ~ #button4 {
left: calc(50% - 25px);
top: calc(50% - 25px);
border: 2px solid black;
-webkit-transition: all 3s ease-in-out;
transition: all 3s ease-in-out;
}
<div id=button0 class=button>target</div>
<label>
<input type="checkbox" name="run" value="click" />
<div id=button1 class=button>click</div>
</label>
<label>
<input type="checkbox" name="run" value="click" />
<div id=button2 class=button>click</div>
</label>
<label>
<input type="checkbox" name="run" value="click" />
<div id=button3 class=button>click</div>
</label>
<label>
<input type="checkbox" name="run" value="click" />
<div id=button4 class=button>click</div>
</label>
Add z-index to center class because your other divs are covering your center button. When you click again you don't click the center button you click the sibling div.

Styling span:hover to make span element remain visible

I've set up span elements to be visible only when buttons are hovered over. I'd like the span to continue to be visible if the cursor is then moved onto the span element. (This already happens in Chrome by default.) I thought I could do that just with span:hover {visibility: visible}, but it doesn't work. Do I need to add something else as well, or is something else wrong?
reduced CSS:
.tooltipT {
position: absolute;
}
.tooltipT span {
position: absolute;
width: 0px;
visibility: hidden;
}
.tooltipT:hover span, .tooltipT span:hover {
visibility: visible;
width: 400px;
}
.tooltipT:hover span, .tooltipT span:hover {
bottom: 16px;
left: -200px;
}
reduced HTML:
<div class="pix">
<button class="tooltipT" style="top: 50%; left: 50%;">1
<span>blah
</span>
</button>
<img src="img/MIPs.jpg" alt="Melt-in-Place Station details">
</div>
It's live here, the image with the span element involved is half-way down the page, it is used on the pink button in the middle.
I put it on Codepen and am playing with suggested solutions there.
You'll need JavaScript here because the minute you move off the button (to move towards the now visible span), the button is no longer being hovered and so the CSS class no longer applies. This should do it:
button.addEventListener("mouseover", function(){
span.setAttribute("class", "tooltip");
});
Obviously, replace button with the id of your button and tooltip with the name of the class that should be applied when the button is moused over.
It does work if you let the span outside of the button. In this case you'll need the element ~ element or element + element selector to show up the span when hovering the button.
body {
background: honeydew;
}
.tooltipT {
position: absolute;
background: tomato;
width: 50px;
height: 50px;
}
span {
position: absolute;
width: 50px;
height: 50px;
visibility: hidden;
background: gold;
border: 3px solid black;
top: calc(40% - 40px);
left: calc(50% + 40px);
}
span:hover, .tooltipT:hover ~ span {
visibility: visible;
}
button {
border: 3px solid black;
top: 40%;
left: 50%;
}
button:hover {
background: red;
}
<button class="tooltipT">0</button>
<span>_blah</span>

why is `position:relative` functioning like `z-index`?

Hey guys I am relatively very new to HTML and CSS and have the following difficulty I made a small input box and I am trying to add a few CSS transforms and create a small animation on the input box. Code below:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.input {
position: relative;
display: inline-block;
max-width: 350px;
width: 100%;
}
.akira-input {
position: absolute;
top: 0;
left: 0;
display: block;
height: 100%;
width: 100%;
background: transparent;
z-index: 10;
}
.akira-label {
display: block;
height: 100%;
padding: 0;
width: 100%;
background: #696a6e;
color: #cc6055;
cursor: text;
}
.akira-label:before {
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: red;
-webkit-transform: scale3d(0.97, 0.50, 1);
transform: scale3d(0.97, 0.50, 1);
-webkit-transition: .3s;
-o-transition: .3s;
transition: .3s;
}
.label-content {
color: #000;
font-size: 1.3em;
text-transform: uppercase;
position: relative;
display: block;
text-align: center;
padding: 1.6em 0;
width: 100%;
-webkit-transition: .3s;
-o-transition: .3s;
transition: .3s;
}
<span class="input">
<input type="text" id="akira" class="akira-input">
<label for="akira" class="akira-label">
<span class="label-content">Akira</span>
</label>
</span>
My difficulty is, if I apply position:relative to <span class="label-content">Akira</span>, it shows, if I remove position:relative , that element disappears from view.
My question is why is position:relative functioning like z-index?
Can somebody elaborate ??
EDIT :: refering to Justinas answer , i have the folloing question ,
Does applying position:relative places an element
higher in the stack , even without applying z-index ??
z-index is only working for non-static elements, so when you remove position: relative than element becomes statically positioned and moves below higher index elements (disappears from view). When you add position: relative to element, than z-index will take effect and so element appears in your view.
Also position and z-index is two different properties
position - how element is positioned according to other elements on page. Default to static
z-index - how high element is in z-axis (z-index: 2 - is behind element with z-index: 10). Default to 5
.wrapper {
position: relative;
}
#static {
position: static;
z-index: 999;
width: 400px;
height: 150px;
background-color: #ddd;
padding: 3px;
}
#top-1 {
position: absolute;
z-index: 10;
left: 8px;
top: 45px;
width: 330px;
height: 80px;
background-color: #888;
padding: 3px;
}
#relative {
position: relative;
z-index: 11;
background-color: #88a;
width: 330px;
height: 80px;
padding: 3px;
top: 30px;
left: 8px;
}
#top-2 {
position: absolute;
z-index: 10;
left: 0;
top: 0;
width: 400px;
height: 150px;
background-color: #dda;
padding: 3px;
}
<div class='wrapper'>
<div id="static">
I'm static, so behind #top-1, but have z-index higher than #top-1... Means z-index has no effect.
<br/>Text that is not visible, because behind #top-1 element
</div>
<div id='top-1'>
I'm above #static, because i have non-static position, so my z-index has effect.
</div>
</div>
<hr/>
<div class='wrapper'>
<div id="relative">
I'm relative and above #top-2, because my z-index higher than #top-2... Means z-index has taken effect.
</div>
<div id='top-2'>
I'm below #relative, because i have lover z-index.
<br/>Text that is not visible, because behind #top-1 element
</div>
</div>
z-index only works on positioned elements so position:absolute, position:relative or position:fixed
It does not behave like a z-index, because z-index specifies an ordering rule, but not the way how the element is displayed.
position: relative; says to go to the relative mode where it can compete the absolutely positioned elements.
Your problem here is that :before pseudo-element is a hierarchical sibling of span, and it takes the whole available parent width. So it fully covers a static span element.
When you make it relative, it becomes shown because when z-index is not specified for both non-static elements they are shown in the same order like they are placed in HTML (so element which is defined in HTML later is always on top).
Your structure is:
label
:before
span
so the span becomes visible.

Customized <input type=“file”>?

I need to add in my form a few customized <input type=“file”>
I tried to use this code
<div id="upload-file-container">
<input type="file" name="pic[]" class="photo" value="Add photo" />
</div>
CSS
#upload-file-container {
position: relative;
width: 50px;
height: 20px;
overflow: hidden;
}
#upload-file-container input {
position: absolute;
left: 0;
top: 0;
font-size: 20px;
opacity: 0;
margin: 0;
padding: 0;
border: none;
width: 50px;
height: 20px;
}
but my inputs just became invisible.
http://jsfiddle.net/FXTCg/4/
How to make visible input title "Add photo"?
take a look at this: http://jsfiddle.net/gabrieleromanato/mxq9R/ . Your input need to be invisible opacity: 0 becuase you cannot apply any style or custom text in it. You need to put your visible content underneath it