Text selection background becoming invisible in Safari when selecting inside flex elements - html

When I select text across multiple flex element items in Safari, the selection background becomes invisible on some parts of the text.
Here are some screenshots of the difference between Firefox and Safari:
Safari
Firefox
And here's a simple code sandbox to reproduce:Link
Did anyone encounter this problem before?

This is a documented Webkit/Safari bug affecting display: flex and display: grid.
As of this writing, the text selection bug appears to affect the first block-level leaf descendent of certain direct children of the flex or grid container, depending on certain layout properties (see Layout Fiddles below).
It's worth noting that no valid values of the user-select property will have any effect on the above-described bug, nor will use of the ::selection pseudo-element. It's also worth noting that table / display: table, inline-block, float, and columns layouts do not appear to be affected by this bug, so those could be viable alternatives for certain use cases. However, since flex and grid are arguably the most powerful tools for building layouts nowadays, here are a couple of workarounds that will still allow you to use those implementations:
Approach 1: Empty Block Element
Since the above-described bug causes the first block-level leaf of the second flex item in the OP's example to be un-selectable, this workaround simply adds an empty block element (in this case a div, but any element with a display value of block will work) above <p>invisible</p> which fixes the problem:
<div style={{ display: "flex" }}>
<div style={{ marginRight: 12 }}>
<p>blue</p>
<p>blue</p>
</div>
<div>
<div />
<p>invisible</p>
<p>blue</p>
</div>
</div>
Updated code sandbox showing Approach 1 in action:
https://codesandbox.io/s/flamboyant-raman-6yq7m
Approach 2: ::before Pseudo-element
This fix uses the same concept as the first, except it uses a CSS pseudo-element instead of an empty div. For simplicity, it also applies this rule to all flex items, regardless of whether or not they might be affected.
.flexContainer {
display: flex;
}
.flexItem::before {
display: block;
content: "";
}
<div class="flexContainer">
<div class="flexItem">
<p>blue</p>
<p>blue</p>
</div>
<div class="flexItem">
<p>invisible</p>
<p>blue</p>
</div>
</div>
Updated code sandbox showing Approach 2 in action:
https://codesandbox.io/s/sharp-andras-jv6x4?file=/src/styles.css
This approach is arguably more maintainable since you don't have to manage special-case child elements like you would with the first approach. One caveat is you will use up your one and only ::before pseudo-element on the flex item in case you were planning to use it for something else.
Safari-specific Fix
A compatibility fix meant for Safari that is benign in other browsers could be the ideal solution. This appears to be the case with both approaches mentioned above (at least with the latest Chrome and Firefox as of this writing, though a solid round of browser testing is always a good idea).
However, if you want to "contain the hack" so to speak then you could try using a device/user-agent sniffing Javascript library (e.g. react-device-detect if you're using React) and conditionally render workarounds accordingly.
Alternatively, if you end up going with Approach 2 then you could use Safari-specific CSS targeting to render the pseudo-elements only in Safari.
Layout Fiddles
In the fiddles below, I have extrapolated on the OP's original example showing how different flex and grid layouts affect which first leaf descendants do and do not show highlighted text. I have added additional flex items and have nested the first element of each direct child of the flex container to show that tree depth appears to be irrelevant.
Layout
Fiddle
flex row
Fiddle
flex row (wrap)
Fiddle
flex column (wrap)
Fiddle
grid
Fiddle

Related

Centered Button, Not Centered?

So, I'm attempting to make my own website (Yeah, I finally sucked it up and started doing markup, sigh) - problem I'm having is I'm trying to center a button, and it's offset a little. Without the <center> it's all the way to the left.
Also tried :
style="align-items:center"
<div id="form-container" style="align-items:center;">
<div>
<fieldset>
<center><input class="button0" value="Install Redux" type="button" /></center>
</fieldset>
</div>
</div>
You just have to put <center> before your <div> and close it after </div>.
Like this:
<center>
<div id="form-container" style="align-items:center;">
<div>
<fieldset>
<input class="button0" value="Install Redux" type="button" />
</fieldset>
</div>
</div>
</center>
I've also made a CodePen and a JSFiddle for you.
Try text-align:center on the parent, or use left:0;top:0;position:relative;webkitTransform:translate3d(-50%,0%,0%); where parent doesn't have position:static (the default)
I would also recommend checking out Bootstrap because it has a nice grid layout that lets you define which 12ths of the page you want columns to lay in, simply by defining classes like .btn-default or .nav or in your case class="col-xs-12" inside that other column
They also have really nice styles for forms and input buttons etc. (see video on my example site below)
Try resizing your browser while looking at their examples. Pretty much, you define class="col-xs-12" if you want it to appear as 12/12 width of the row on extra small (mobile) and LARGER devices, and you can mix them class="col-xs-12 col-md-6" so it will split the row on larger (tablet) size devices. It's the number 1 repository on GitHub, and only takes about 30 minutes to read through the Grid Layout and search around for "Nav" and "Button" elements.
I recently made a quick site http://neaumusic.github.io, feel free to check it out, and good luck
Two ways:
1) Set margin-left: auto; AND margin-right: auto; to the containing div OR
2) Set display:flex; AND justify-content:center;to the parent container.
Google flex box for a little more information, its very useful for layout once you get the hang of it.
As stated in the comments, the center tag is no longer supported.
What about if you try #form-container { text-align: center; } ? It will center all children, including button.
I would definitely recommend using flexbox, the only issue being ie8/9 support.
It really makes layout so much easier and you don't have to create very specific, often arbitrary margins to get your stuff to align nicely, particularly vertical alignment.
Your alignment options are split between the container and the items. It does row and column layout too.
Here is a link I used to get me started.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Textarea (wrongly?) inheriting white-space css property from parent class

I have a textarea within a parent div. The parent div has 'white-space: nowrap'. Should the textarea be inheriting the 'white-space: nowrap'? In IE11 the textarea inherits the 'nowrap'. Chrome ignores the inherited 'nowrap' and instead uses 'white-space: pre-wrap' from the user agent stylesheet. I know I can fix this by explicitly specifying the textarea's white-space property. Should I have to set the whitespace of the textarea explicitly, or is this an IE11 bug?
The following JSFiddle demonstrates the issue. (Open it in IE11 and Chrome and note the difference).
http://jsfiddle.net/sTAB3/1/
Here is the code in the JSFiddle:
.a {
white-space: nowrap;
}
<div class="a">
<textarea id="log" rows="20" cols="50">If I may... Um, I'll tell you the problem with the scientific power that you're using here, it didn't require any discipline to attain it. You read what others had done and you took the next step. You didn't earn the knowledge for yourselves, so you don't take any responsibility for it. You stood on the shoulders of geniuses to accomplish something as fast as you could, and before you even knew what you had, you patented it, and packaged it, and slapped it on a plastic lunchbox, and now you're selling it</textarea>
</div>
This appears to be an IE 11 bug that allows the white-space property of a parent element to be inherited by a child textarea. This should be considered as unexpected behaviour.
I would apply white-space: pre-wrap to the textarea and treat it as an IE 11 bugfix.
I cannot find an official bug report (sources welcomed), but below are two examples of problems arising from this bug.
Example 1
Example source
From the link:
Ticket description textarea doesn't wrap on whitespace in IE11
Example 2
Example source
Internet Explorer 11 word wrap is not working
It seems that word wrapping no longer works for textarea elements in IE 11. In IE 10 and earlier, FF, Safari, and Chrome word wrapping works as expected.

Which elements can be safely made contenteditable?

I've been working with contenteditable recently within a HTML5 page and encountering bugs when using it with certain elements, and I'd like to know where and how I can actually safely use it.
I've discovered that making a span element contenteditable results in some buggy behaviour in both Firefox1 and Chrome2. However, making a div or section contenteditable appears completely safe3.
A guideline a couple of people have mentioned is that only block-level elements should be made contenteditable. However, the Mozilla Developer Network lists the heading elements h1 through to h6 as block-level elements, and making a heading element contenteditable is buggy in Firefox4 and can crash the page in Chrome5.
I'd like to be able to use more than just divs and sections, but I'm not clear on what elements I can actually safely make contenteditable. By safely, I mean that using the element under normal conditions, I should be able to perform normal editing tasks without it doing unexpected or buggy things. I should be able to write in it, delete content, cut, copy, paste, and move my text cursor about and highlight text without unexpected or strange behaviour.
So, which elements can I really make contenteditable safely? Is there a specific category? Are there certain criteria the safely-contenteditable element must match?
Bug notes:
Firefox 21 w/ span: Element loses focus if the text cursor is brought to the beginning or end of the element, but not if it got there by deleting content. Highlighting part of the element, cutting and then pasting will split the element in two at that point then insert a blank element between the two parts - without actually putting the text you were trying to paste anywhere.
Chrome 27 w/ span: If the span covers multiple lines e.g. by being wordwrapped, cutting and pasting content will often insert a linebreak after the pasted content.
Unless you make the div display:inline, in which case it can still lose focus as in 1, but apparently only if you bring the text cursor to the end. I don't consider this "normal" usage of the element though.
Firefox 21 w/ heading: Selecting part of the content then cutting and pasting will, similarly to 1, split the heading element in half at that point, and insert a third heading element between the two halves. It will, at least, have your pasted content inside it, but now you have three heading elements where there was originally one.
Chrome 27 w/ heading: Select some content and cut and paste. The page crashes. You get an "Aw snap!" message. That's it.
Demo code
Here's a demo for reproducing the above. It's pretty simple, though at the moment the only thing it isn't reproducing is the lose-focus bug.
[contenteditable=true] {
border: 1px dotted #999;
}
<article style="width: 100px">
<h1 contenteditable="true">Heading</h1>
<p>
<strong>Some adjacent content</strong>
<span contenteditable="true">Span! This is long enough it will spread over multiple lines.</span>
</p>
<div style="display: inline" contenteditable="true">An inline div also with multiple lines.</div>
</article>
In my opinion, I'd say div is the safest bet across the board. Any element you wish to truly edit (be it a span, header, etc), you can place inside the div and edit as if it were just that element. Also, to account for the display:inline issue you mentioned, you could always use float:left or float:right on your editable div to give it an "inline feel" without having it actually be inline.
Hope that helps!
Since this is an evolving feature with, apparent, low priority from the browser vendors support has been sketchy and regressions not uncommon. The current state of affairs is evolving, so check the Googles, CanIUse etc and make sure support is there for your sites visitors, everything else is moot ...
Support in Firefox seems to be solid, at least for some elements, now https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content
It works well in Chrome as well as far as my testing goes.
And CanIUse looks good: http://caniuse.com/#feat=contenteditable
There are a number of different bugs related to the feature in the different browsers though, but for simple use cases it should be ok now, as of August 2016.

Encapsulating a sequence of grids in a parent div to control height while using grid960

Browsers I'm using for testing: IE, Chrome, FF;
Ideal layout example pdf: http://designobvio.us/dov2/Homepage1.pdf
Heres the link to the direct page http://designobvio.us/dov2/index.html
While Grid systems are excellent for laying out by width, I always stumble when using height constants.
Inside my code I have 2 major problems:
Ideally, I want to be encapsulate sections of my code by using
parent div that controls the height, margin-top. I've tried to
create this div colored a pale green and padded it for visuals.
this class is shown here:
siteBody #businessSection{ padding-bottom:200px; background:#0F9;}
I've also demonstrated idea in this imageURL: http://www.designobvio.us/dov2/Homepage1Encapulate.pdf
While trying to do this I've come up completely empty handed.
2.So next, I tried breaking down each section individually by using a class .businessInfo
#siteBody .businessInfo{height:200px; background:#ccc;}
Unfortunately this also didnt work and I have no idea why because all the grids line up perfectly.
I hope what I've stated above is understandable. I prefer solution one; however, if not possible 2 will work.
If I'm missing anything I'll response ASAP.
Thanks!
this problem stems from the use of float. For example problem : http://jsfiddle.net/GAvcL/
you can use clear attribute for solve problem.
Solution:
http://jsfiddle.net/qeDs3/
This is a common problem in html. Before closing the div that encapsulates the divs using float, use a div has clear attribute.

2 row element layout within horizontal div [duplicate]

This question already has answers here:
How to create a multi-column list?
(8 answers)
Closed 4 months ago.
I'm trying to put together a small thumbnail gallery and have run into a slight snag. Structure is very basic and is as follows:
[parent container]
[x number of child elements]
[/parent container]
I want my child elements to load into the parent container like so:
example 1
[0][2][4][6][8]
[1][3][5][7][9]
What I want to know is if there is a pure CSS solution to this or if I'll have to position my elements with javascript.
Knowing that the browser wants to load the items in like this
example2
[0][1][2][3][4]
[5][6][7][8][9]
I'm pretty sure that there isn't a non-javascript way of achieving this but I wanted to ask if anyone had any ideas or experience with this kind of layout.
The ultimate goal is to accomplish 2 things:
1. Have the parent container grow horizontally as new elements are added to it.
2. Keep the 2 row layout as described in example 1.
See update below
I think I came to a pure CSS3 solution, involving 3d transformation: you can look a webkit only demo here: http://jsfiddle.net/7fUxz/
basically, the idea behind this demo is starting from a basic element displacement, floating both wrapper and children elements - I used <ul><li> for the sake of simplicity and made a clear:left starting form li:nth-child(2n+1) - in this way:
[0][1]
[2][3]
[4][5]
[6]...
then I rotateZ the ul so that the whole list is rotated by -90deg and then repositioned with translateX/Y for the right alignment.
But also list items will be rotated: so an inverse Z-rotation is applied to every <li>. Another rotation of 180deg along X-axis is also necessary to give list-items the correct order. Even in this case some adjustments with X|Y translation is needed
The result is
[0][2][4][6][8]
[1][3][5][7]...
In 3rd revision of the fiddle http://jsfiddle.net/7fUxz/3/ you can see how to adjust some properties on the list so that the elements before and after are correctly positioned.
Note: this demo is working only on webkit. For a list of browser supporting 3D-transforms look at http://caniuse.com/transforms3d
Update
I've done further experiment: if you apply a float:right (instead of float:left) to each <li> 3D Transformations are no longer needed (because elements are already in the right order by row when <ul> is rotated) and the css rules are greatly simplified
[1][0]
[3][2]
[5][4]
...[6]
so this fork
http://jsfiddle.net/fcalderan/2BDxE/
has an increased support (surprisingly even more respect CSS3 *-columns usage), since it works even on Firefox 3.5, Opera 10.5 and probably MSIE 9 (I haven't tested this) : http://caniuse.com/transforms2d .
For older IE consider to serve an alternative style (via conditional comments) or some kind of js/activeX effect using Matrix Filter
Without knowing too much of how the information is being pulled in, there is a pure-CSS way using CSS3 multi-columns. BUt, it's CSS3 and the multi-column module only works with the latest version of Firefox and Webkit browsers; as far as I know IE9 still does not support it.
You can read more about CSS3 multi-columns via the following links and how to use them properly.
http://www.csscripting.com/css-multi-column/
http://kmsm.ca/2010/an-almost-complete-guide-to-css3-multi-column-layouts/
There is also a column script that I've used once before, though a bit old, may help you.
http://www.csscripting.com/css-multi-column/
Hope this helps!