简体   繁体   English

内容可编辑跨度(内联块)Webkit浏览器中的垂直对齐插入符号

[英]Vertical align caret in contenteditable span (inline-block) webkit browsers

I have form made with contenteditable spans and my problem is that on focus my span is not vertical align better said the caret is not in the middle of span. 我有使用contenteditable span制作的表格,但我的问题是在聚焦时我的span不是垂直对齐的,所以说插入号不在span的中间更好。 I would like to have it in the middle and fully visible but also with scalable width so span should not be a dispaly: block neither inline. 我希望它在中间且完全可见,但宽度可缩放,因此跨度不应是分散的:既不能内联,也不能阻塞。

Picture : http://i62.tinypic.com/125kri8.jpg This is the issue i want it in the middle but also the caret must be fully visible 图片http : //i62.tinypic.com/125kri8.jpg这是我要在中间出现的问题,但插入符号必须完全可见

DEMO : http://jsfiddle.net/hqf8apwr/ 演示http : //jsfiddle.net/hqf8apwr/

HTML 的HTML

 <form action="">
  <ul class=form-input>
   <li>
    <span class="input-span" contenteditable="true" data-width=30 data-placeholder="Vaše meno"></span>
  </li>
  <li>
   <span class="input-span" contenteditable="true" data-width=30 data-placeholder="Spoločnosť"></span>
  </li>
  <li>
   <span class="input-span" contenteditable="true" data-width=30 data-placeholder="Pracovná pozícia"></span>
  </li>
  <li>
   <span class="input-span" contenteditable="true" data-width=30 data-placeholder="Telefónne číslo"></span>
  </li>
  <li>
   <span class="input-span" contenteditable="true" data-width=30 data-placeholder="Emailová adresa"></span>
  </li>
 </ul>
</form>

CSS 的CSS

 li {
   list-style: none;
 }
 form {
   text-align: center;
   position: relative;
   left: -100px;
 }
 form > ul {
   margin: 0 auto;
 }
 form > ul > li {
   height: 60px;
   line-height: 60px;
   display: inline-block;
   font-size: 1.2em;
 }
 form .form-label {
   text-align: right;
   padding-right: 25px;
   color: black;
}
form .form-input {
   text-align: left;
   width: 200px;
   position: absolute;
   top: 0;
}
form .form-input li {
   height: 60px;
} 
form .form-input span {
   font-size: 1em;
   border-bottom: 2px solid orange;
   padding: 15px 20px;
   margin-bottom: -20px;
   white-space: nowrap;
   display: inline-block;
   height: 20px;
   line-height: 20px;
   max-width: 260px;
   overflow-x: hidden;
   background: #fff;
   color: #000;
}
form .form-input span.input-span[contenteditable]:focus {
   display: inline-block;
   font-size: 1em;
   padding-top: 20px;
   padding-bottom: 40px;
   overflow: hidden;
   background: orange;
   color: #fff;
   margin-top: 2px;
   padding-bottom: 20px;
}
form .form-input span.input-span[contenteditable]:empty::before {
   content: attr(data-placeholder);
}
form .form-input span.input-span[contenteditable]:empty:focus::before {
   content: '';
}

Just add vertical align 只需添加垂直对齐

vertical-align: middle;

See your fiddle: 查看您的小提琴:

http://jsfiddle.net/u1ba8eve/2 http://jsfiddle.net/u1ba8eve/2

One easy way to fix the caret position would be to move the padding to the li element. 固定插入符位置的一种简单方法是将填充移至li元素。 The result isn't exactly the same - the bottom border for example takes the whole width instead of only width of placeholder text and the blue outline is only on the text - but with some tweaking you may be able to get wanted result. 结果并不完全相同-例如,底部边框占据整个宽度,而不是占位符文本的宽度,并且蓝色轮廓仅出现在文本上-但是通过一些调整,您可能可以得到想要的结果。

li
{
    padding: 15px 20px;
    border-bottom: 2px solid orange;
}
.form-input span {
  font-size: 1em;
  color: #000;


  white-space: nowrap;
  display: inline-block;
  height: 20px;
  line-height: 20px;
}

http://jsfiddle.net/564hjw8w/1/ http://jsfiddle.net/564hjw8w/1/

EDIT 编辑

If you want to keep an outline, you only need to give your span a width. 如果要保留轮廓,则只需要给跨度设置一个宽度即可。 You can also give it an offset so it has the same look as before. 您也可以给它一个偏移量,使其具有与以前相同的外观。 But note that outline-offset isn't supported by Internet Explorer 但是请注意,Internet Explorer不支持轮廓偏移

   .form-input span.input-span[contenteditable]:focus
{
    width: 100px;
    outline-offset: 15px;
}

http://jsfiddle.net/564hjw8w/3/ http://jsfiddle.net/564hjw8w/3/

You can achieve some decent look by removing your fixed height and padding from the span . 您可以通过消除span固定heightpadding来获得不错的外观。 And use padding and/or margin only on li to control your spacing ( as suggested by Julien ). 并仅在li上使用padding和/或margin来控制您的间距( 如Julien所建议 )。 But, continue using border-bottom on span to give it a variable width effect. 但是,请继续在span上使用border-bottom以使其具有可变宽度效果。

Here is a sample fiddle: http://jsfiddle.net/abhitalks/s3jmqh20/3/ 这是一个样本小提琴: http : //jsfiddle.net/abhitalks/s3jmqh20/3/

Tested in IE-11, GC-43, FF-38, and AS-5.1.7 (FF shows a vertical offset initially when content is absent, type in something and it corrects itself. No workaround for that) 在IE-11,GC-43,FF-38和AS-5.1.7中进行了测试(当内容不存在时,FF最初会显示垂直偏移,请输入内容并进行自我校正。没有解决方法)

This also allows you to change the font-size without worrying about breaking the spacing too much. 这也使您可以更改font-size而不必担心太大的间距。 Try changing the size in the fiddle/snippet. 尝试更改小提琴/片段中的大小。

Snippet : 片段

 * { box-sizing: border-box; margin: 0; padding: 0; } ul.form-input { text-align: left; width: 200px; list-style: none; margin-left: 12px; } ul.form-input li { margin: 6px 0px; padding: 12px 0px; } ul.form-input span { display: inline-block; color: #000; white-space: nowrap; padding: 0px 16px; border-bottom: 2px solid orange; font-size: 1em; line-height: 2em; } ul.form-input span.input-span[contenteditable]:empty::before { content: attr(data-placeholder); } ul.form-input span.input-span[contenteditable]:empty:focus::before { content:'\\00a0 '; } ul.form-input span.input-span[contenteditable]:focus { background-color: orange; color: #fff; } 
 <form action=""> <ul class=form-input> <li><span class="input-span" contenteditable="true" data-width=30 data-placeholder="Vaše meno"></span></li> <li><span class="input-span" contenteditable="true" data-width=30 data-placeholder="Spoločnosť"></span></li> <li><span class="input-span" contenteditable="true" data-width=30 data-placeholder="Pracovná pozícia"></span></li> <li><span class="input-span" contenteditable="true" data-width=30 data-placeholder="Telefónne číslo"></span></li> <li><span class="input-span" contenteditable="true" data-width=30 data-placeholder="Emailová adresa"></span></li> </ul> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM