简体   繁体   English

如何使垂直输入完全适合整个侧面导航栏?

[英]How to make vertical input fit entirely into the whole side navigation bar?

I want the html text input field to complete overlap/imbricate its parent div. 我希望html文本输入字段完成重叠/嵌入其父div。

I tried position absolute, top:0, bottom:0, left:0, right:0 on the input element to stretch it to the four corners of its parent div. 我尝试在输入元素上定位绝对,顶部:0,底部:0,左侧:0,右侧:0,以将其拉伸到其父div的四个角。 I tried height:100%,width:100%, but the rotation makes it useless. 我尝试了height:100%,width:100%,但是旋转使其无效。

 *{ padding:0; margin:0; } html,body{ height:100%; width:100%; } div{ height:100%; width:20%; background:#111; } .field{ transform: rotate(-90deg); background: none; border: none; outline: none; color:blue; position:absolute; top:0;bottom:0;left:0;right:0; } 
 <div> <input class='field' type='text' placeholder='Enter zipcode'> </div> 

I made an attempt to meet your scenario. 我试图满足您的情况。 It does not work in code snippet, however. 但是,它不适用于代码段。

HTML 的HTML

<div id="container">
    <input id="textbox-element" class='field' type='text' placeholder='Enter zipcode'>
</div> 

CSS 的CSS

*{
    padding:0;
    margin:0;
}

html,body{
    height:100%;
    width:100%;
}

div{
    height:100%;
    width:20%;
    background:#111;
}

.field{
    transform: rotate(-90deg);
    transform-origin: top left;
    background: none;
    border: none;
    outline: none;
    color:blue;
    position:absolute;
}

Javascript Java脚本

document.addEventListener("DOMContentLoaded", function(event) { 
    var dimensions = document.querySelector("#container").getBoundingClientRect();
    var element = document.querySelector("#textbox-element");
    element.style.bottom = -dimensions.width;
    element.style.width = dimensions.height;
    element.style.height = dimensions.width;
});

What I'm doing here is to set the input's position and size after the DOM is loaded, meaning after the rotation is applied. 我在这里要做的是在加载DOM后(即应用旋转后)设置输入的位置和大小。

Basically this is quite hard (if not impossible) with just CSS as the width & height cannot be determined. 基本上, 仅凭 CSS很难做到这一点(如果不是不可能的话),因为无法确定宽度和高度。

The rotation is purely visual and does not affect the layout of elements around it. 旋转纯粹是视觉上的,不会影响其周围元素的布局。

You'd need JS, I'd suggest, to set the height and width of the rotated element to fit the parent div. 我建议您需要JS来设置旋转元素的高度和宽度以适合父div。

Something like this (using JQuery) 像这样(使用JQuery)

 $(window).on("resize", function() { var width = $("div").height(); var height = $("div").width(); $(".field").css({ width: width, height: height }); }).resize(); 
 * { padding: 0; margin: 0; } html, body { height: 100%; width: 100%; } div { height: 100%; width: 20%; background: green; } .field { transform-origin: bottom left; transform: translateY(-100%) rotate(90deg); display: block; background: pink; border: none; outline: none; color: blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input class='field' type='text' placeholder='Enter zipcode' /> </div> 

Codepen Demo Codepen演示

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

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