Cannot get option from select if position is set to absolute? - html

It appears than when I attempt to set my phonebook's CSS position to absolute, I simply lock the select element. Relevant HTML/CSS below. When I remove the position: absolute; from the code, it runs perfectly fun; however, my HTML gets pushed down which is what I'm trying to avoid.
HTML
<div id="phonebook">
<select id="catalog">
<option value="u_none">None</option>
<option value="u_1">John Doe (555-555-1254)</option>
<option value="u_2">Jane Doe (555-555-2894)</option>
</select>
<div id="employee-info">
<div id="employee-content">
Role or Title<br>
Full Name<br>
Contact:<br>
Telephone: 555-555-5555<br>
Email: name.lastname#domain.com<br>
Room: A-5555<br>
Mail Stop: A-555
</div>
</div>
</div>
CSS
#phonebook {
position: absolute;
width: 320px;
z-index: -1;
}
#employee-info {
border: 1px solid #aaa;
border-top: 0 none;
}
#employee-content {
padding: 10px;
}
#catalog {
position: relative;
border: 1px solid #aaa;
width: 100%;
z-index: 1;
}
I've tried to play around with z-index -- at the suggestion of a somewhat unrelated post -- but it still seems to do me no good. I need this element in a specific portion and it relies heavily on using this dropdown select element.
Is there anyway to overcome this issue? I'm not exactly sure what is preventing my access to the select dropdown. An explanation would also be awesome.
Thanks ahead. Any help is appreciated.

There must be more to it, because when I put your exact code into a snippet, it works fine. I see no difference with and without the position: absolute;
Please edit your question to put the code in a snippet (or jsfiddle) that shows the problem.
#phonebook {
position: absolute;
width: 320px;
z-index: -1;
}
#employee-info {
border: 1px solid #aaa;
border-top: 0 none;
}
#employee-content {
padding: 10px;
}
#catalog {
position: relative;
border: 1px solid #aaa;
width: 100%;
z-index: 1;
}
<div id="phonebook">
<select id="catalog">
<option value="u_none">None</option>
<option value="u_1">John Doe (555-555-1254)</option>
<option value="u_2">Jane Doe (555-555-2894)</option>
</select>
<div id="employee-info">
<div id="employee-content">
Role or Title
<br>Full Name
<br>Contact:
<br>Telephone: 555-555-5555
<br>Email: name.lastname#domain.com
<br>Room: A-5555
<br>Mail Stop: A-555
</div>
</div>
</div>

Related

Programmatically added <br>-element to text inside a span is not breaking the line

I use this solution to add a tooltip to my webpage. The application is an MVC ASP.NET application with Razor pages. I want the text of the tooltip to have line breaks (<br/>). This can be done by adding the HTML <br /> in the text. This works well, however, I have a function (see below) at the top of my page that renders a string that is added to my span-element with the <br>-tags.
private static string GetEmployeeList(ProjectSchedule ps)
{
if (ps.Employees.Count > 0)
{
string empList = string.Empty;
foreach (var employee in ps.Employees)
{
empList += employee.Name + "<br/>";
}
return empList;
}
return string.Empty;
}
The result of the function is something like: "Name 1<br/>Name 2<br/>Name 3<br/>".
The piece of code rendering the tooltip looks like:
<div class="data-tooltip">
Developers
<span class="data-tooltiptext">#GetEmployeeList(item)</span>
</div>
The problem is that the <br/> is not working, when the tooltips shows I see the hardcoded <br/> as part of the string while I expected the result looks something like:
Name 1
Name 2
Name 3
The styling used:
.data-tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.data-tooltip .data-tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-left: -60px;
/* Fade in tooltip - takes 1 second to go from 0% to 100% opac: */
opacity: 0;
transition: opacity 1s;
}
.data-tooltip .data-tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: black transparent transparent transparent;
}
.data-tooltip:hover .data-tooltiptext {
visibility: visible;
opacity: 1;
}
Result:
Does someone have an idea how to solve this? TIA.
You have recognized the right problem in your answer, but that is not the correct solution. Your solution has the danger that if the names contain any HTML, that will be rendered as well, which opens you up to HTML and script injection attacks.
Generally you should avoid using #Html.Raw and shouldn't generate HTML outside the template. All HTML generation should instead happen directly in the template:
<div class="data-tooltip">
Developers
<span class="data-tooltiptext">
#foreach (var employee in item.Employees)
{#employee.Name<br/>}
</span>
</div>
After all, the solution was simple (as always).
#Shashank Gb got me thinking. First I used the developer tools, but there the outputted string showed ad "Name 1 <br/> Name 2<br/>"
So nothing wrong there I would say. However, when I used "View page-source" I saw that the <br/> was rendered to <br;&gt
The solution for me was to use #Html.Raw, this prevented the conversion of the HTML <br/>-tag:
<div class="data-tooltip">
Developers
<span class="data-tooltiptext">#Html.Raw(#GetEmployeeList(item))</span>
</div>
Thx for the help.

HTML Select Menu issue in IE11

I have this html for an select control
<select class="form-control">
<option value="0"></option>
<option value="1: 1" >Jr.</option>
<option value="2: 2">Sr.</option>
<option value="3: 3">I</option>
<option value="4: 4">II</option>
<option value="5: 5">III</option>
</select>
It is getting rendered as expected in chrome
chrome image 1
chrome image 2
but in IE, the select option is hiding the control when it is clicked or in other words the the select option is not getting opened from the bottom of the select control as seen in this following screen shot
IE image 1
IE image 2
is this a default behaviour or can I change it? I tried giving using this css but did not work
select.form-control {
width: 100%;
max-width: 325px;
border-width: 0 0 1px 0;
box-shadow: none;
border-radius: 0;
-moz-appearance: none;
text-indent: .01px;
text-overflow: '';
position: relative;
}
option, select.form-control option {
color: blue !important;
top: 0px !important;
position: absolute !important;
}
any suggestion?
This is standard behavior. Every browser renders elements slightly different and has their own styles for it. Some styles can be changed, others are hidden in the shadow root of the elements and cannot be changed. option sadly has only a few styles like color that can be set...
One solution for this would be to hide the select element and control it via another element that can be styled (e.g. span) and JavaScript. That is not really pretty but many css frameworks already do so and if you absolutely have to make it look good (most of the times that is the case) that is your only option.
Here's a quick example of a custom built select box. As you can see, even putting images in the options is possible now. Hope this helps you.
Fontawesome is used for the caret. Documentation in the JS source code.
// Create a reference to the select box
const selectBox = document.getElementById("selected");
// Add an event listener to detect the click and make the options (in)visible
selectBox.addEventListener("click", function() {
// Add or remove class 'open'
document.getElementById("options").classList.toggle("open");
});
// Put all options in an array
const options = [...document.getElementsByClassName("option")];
// Add event listener for each option
options.map( option => option.addEventListener("click", function() {
// Create a reference to the input field
const myInput = document.getElementById("sel");
// Retrieve the text from the clicked option
const optionText = this.getElementsByTagName("span")[0].innerHTML;
// Put the text in the input field value
myInput.value = optionText;
// Put the text in the select box
selectBox.innerHTML = optionText;
// Close the select box
document.getElementById("options").classList.toggle("open")
}));
.container {
display: flex;
flex-wrap: wrap;
width: 25%;
}
#selected {
border: thin solid darkgray;
border-radius: 5px;
background: lightgray;
display: flex;
align-items: center;
cursor: pointer;
height: 1.5em;
margin-bottom: .2em;
padding-left: .5em;
min-width: 150px;
position: relative;
}
#selected:after {
font-family: FontAwesome;
content: "\f0d7";
margin-left: 1em;
position: absolute;
right: .5em;
}
#options {
display: none;
margin: 0;
padding: 0;
}
#options.open {
display: inline-block;
}
li {
display: flex;
justify-content: flex-start;
align-items: center;
cursor: pointer;
}
li>img {
margin-right: 1em;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<form>
<input type="hidden" id="sel">
<div class="container">
<div id="selected">Select an option</div>
<ul id="options">
<li class="option"><img src="http://placehold.it/50/00ff00"><span>Option 1</span></li>
<li class="option"><img src="http://placehold.it/50/ff0000"><span>Option 2</span></li>
<li class="option"><img src="http://placehold.it/50/0000ff"><span>Option 3</span></li>
</ul>
</div>
</form>
You can correct the behavior with CSS
select {
float: left;
display: inline-block;
}
<select class="form-control">
<option value="0"></option>
<option value="1: 1" >Jr.</option>
<option value="2: 2">Sr.</option>
<option value="3: 3">I</option>
<option value="4: 4">II</option>
<option value="5: 5">III</option>
</select>

html css sticky, etc

For the code below, I have two problems:
The background image cereal.jpeg was working, now for some reason, it's disappeared and I can't figure why. Because it used to work, I know the file name and location are correct.
My controls refuse to remain sticky!
div.container {
border-style: solid;
border-width: thin;
border-color: grey;
box-shadow: 10px 15px 8px black;
height: 1500px;
width: 700px;
}
div.container::before {
background-image: url("cereal.jpeg");
background-repeat: repeat;
opacity: 0.05;
}
div.controls {
float: left;
width: 140px;
border: 1px solid lightgrey;
position: -webkit-sticky; // Safari
position: sticky;
top: 0;
}
div.thePlots {
float: left;
border: 1px solid lightgrey;
width: 400px;
height: 1400px;
}
<div id=container class=container>
<div class=controls id=controls>
<label id="brand">Brand:</label>
<br>
<select size=7 id="brandOptions" multiple>
<option value=0 selected>All</option>
<option value=1>Kellog</option>
<option value=2>Post</option>
<option value=3>Quaker</option>
<option value=4>General Mills</option>
<option value=5>Tree House</option>
<option value=6>CPW</option>
</select>
<br><br><br>
<label id="barsLabel">Error Bars:</label>
<input id=barsPresent type=checkbox value="Error Bars" checked>
<br><br><br>
<label id="label">Elasticity<br>Means:</label>
<br>
<select id="plotY">
<option value="efm" selected="selected">efm</option>
<option value="enm">enm</option>
</select>
<br><br><br>
</div>
<div class=thePlots id=thePlots>
The Plots<br>
<div class="tooltip" id="tooltip"></div>
<div id="plotshare" class="plotshare"></div>
<div id="plotprice" class="plotprice"></div>
<div id="plotsugar" class="plotsugar"></div>
<div id="plotmushy" class="plotmushy"></div>
</div>
</div>
opacity is too low.
Remove float:left;
Your div.controls element is sticking just fine, but it's sticking to the top of your container element. If you put it outside out your container element you will see it stick to the top of the page as desired.
Also, you will need to remove the // safari from your css. The correct syntax for commenting in css is /* comment */
For the background issue, there is no content for your background to display behind. You will need to add something to the before in order for the background to show.
Check this working fiddle which solves both problems
: https://jsfiddle.net/c5vrhf7t/1/

<select> tags are not staying within specified borders in Firefox

I am creating an app the requires me to use 3 <select> tags that are stacked on top of each other. I need said tags to be flush with each other and also not leap over the background div's boundaries. I am making this work out fine in Google Chrome, but when I check it out Firefox, everything goes haywire. The select tags are nearly over the boundaries of the background div in unacceptable ways.
A visual example of my problem (Firefox on left Google Chrome on right)...
Here is my HTML...
<div id='wr-main-wrap'>
<div id='wr-wrapper'>
<div id='wr-opaq-background'></div>
<div id='input-wrapper'>
<div>
<h3 class='label-text'>work day length:</h3>
<select ng-model='ratioControl.workday'>
<option value='1'>1 Hour</option>
</select>
</div>
<div>
<h3 class='label-text'>break frequency:</h3>
<select ng-model='ratioControl.breakspan'>
<option value='.5'>30 Minutes</option>
</select>
</div>
<div>
<h3 class='label-text'>break lengths:</h3>
<select ng-model='ratioControl.breaklength'>
<option value='5'>5 Minutes</option>
</select>
</div>
</div>
</div>
</div>
Here is my CSS...
#wr-main-wrap{
width: 19em;
margin: 0 auto;
}
#wr-wrapper{
width: 17em;
margin: 3.5em auto;
}
#wr-opaq-background{
position: absolute;
width: 17em;
height: 7em;
background-color: white;
z-index: -1;
opacity: .7;
border-radius: .25em;
}
#input-wrapper{
margin: 5em 0;
padding: .85em 0 0 .6em;
}
.label-text{
font-family: 'Bebas Neue';
margin-bottom: .8em;
word-spacing: .1em;
display: inline-block;vertical-align: top;
width: 9em;
}
select{
width: 6em;
margin: 0 0 1.1em 0;
background-color: white;
border: black solid .1em;
}
I have check on stack overflow and the internets quite a bit, and have not found anything to answer my question. It is worth nothing that I am using Angular in case that may have something to do with it.
Try the following CSS:
#wr-wrapper{
position: relative;
}
The <input> tags themselves needed a height and width, otherwise, they would just adjust to the browser's default settings. This is completely unpredictable. I used % as a unit of measurement.

Creating a "disabled" look by using a mask

I'm trying to create the following:
Using two images: one as mask (the diagonal lines) and the other the image and text themselves (the mask and image+text are the same size):
..and I just can't get it done!
I've tried all combinations with divs and z-indeces, opacity and background-image.. (should mention I'm noob to html).
Here's one shot I got at it (with only the mask and an image):
div {
position: absolute;
top: 775px;
left: 0px;
height: 188px;
width: 272px;
background-image: url('grey-out.png');
}
img {
z-index: 1000;
}
<div></div>
<img src="41_large.png" />
Which just gives the diagonal lines themselves..
Can someone please help me out?
How do I make that "disabled" look combining the (semi-transparent) mask and the div?
Thanks!
This approach works:
<div id="pspThing" class="disabled">
<img class="disabled" src="http://i.stack.imgur.com/lCTVr.png" />
</div>
#pspThing {
background: transparent url(http://i.stack.imgur.com/WpgNy.jpg) 0 0 no-repeat;
height: 93px;
width: 273px;
border: 1px solid #000;
margin: 0 auto;
overflow: hidden;
}
#pspThing img {
display: none;
opacity: 0.5;
}
#pspThing img.disabled {
display: block;
}
JS Fiddle demo
Bearing in mind that there's no transparency in your striped png (so far as the imgur hosted image is concerned, anyway, so I'm using opacity instead). Also the JS Fiddle demo's a little more complicated than necessary, so's I could show the disabled/enabled states.
Pleass consider this simple snippet. Very universal solution. Acts and feels very much like the 'disable' attribute of input elements. See the snippet
function disable(elementId, enabling) {
el = document.getElementById(elementId);
if (enabling) {
el.classList.remove("masked");
} else
{
el.classList.add("masked");
}
}
.masked {
position: relative;
pointer-events: none;
display: inline-block;
//visibility:hidden; /* Uncomment this for complete disabling */
}
.masked::before {
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
visibility: visible;
opacity: 0.5;
background-color: black;
//background: url('http://i.imgur.com/lCTVr.png'); /* Uncomment this to use the image */
content: "";
}
<button onclick="alert('Now, click \'OK\' then \'Tab\' key to focus next button.\nThen click \'Enter\' to activate it.');">Test</button>
<div id="div1" style="display:inline-block" class="masked">
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
<button onclick="alert('Sample button was clicked.')">Maskakable</button><br/>
<br/>
<button onclick="alert('Sample button was clicked.')">Maskakable</button>
<button onclick="alert('Sample button was clicked.')">Maskakable</button><br/>
<img src="http://i.stack.imgur.com/WpgNy.jpg">
</div>
<button>Dummy</button>
<br/>
<button id="enableBtn" onclick="disable('div1',true);disable('enableBtn',false);disable('disableBtn',true);">Enable</button>
<button id="disableBtn" onclick="disable('div1',false);disable('enableBtn',true);disable('disableBtn',false);" class="masked">Disable</button>
I built an example here.
I doubt that the position:absolute approach is the best way to handle this since you need to know the size of the image.
For doing it by z-index your both images should be in the container with img tag.