我有两个div
标签,只有一个标签有input
个标签;为什么输出是这样的?
div.logo, div.form {
display: inline-block;
height: 200px;
width: 200px;
border: 1px dotted;
}
<div class="logo">
<input type="text">
</div>
<div class="form">
</div>
任何人都能解释一下吗?这是小提琴:https://jsfiddle.net/ag487L5m/
答案 0 :(得分:5)
这是因为内联元素的垂直对齐默认为基线。将其更改为顶部:
import UIKit
import PlaygroundSupport
class TestViewController : UIViewController {
let labelOne: UILabel = {
let label = UILabel()
label.text = "Scroll Top"
label.backgroundColor = .red
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let labelTwo: UILabel = {
let label = UILabel()
label.text = "Scroll Bottom"
label.backgroundColor = .green
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let scrollView: UIScrollView = {
let v = UIScrollView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .cyan
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
// add the scroll view to self.view
self.view.addSubview(scrollView)
// constrain the scroll view to 8-pts on each side
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8.0).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 8.0).isActive = true
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -8.0).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8.0).isActive = true
// add labelOne to the scroll view
scrollView.addSubview(labelOne)
// constrain labelOne to left & top with 16-pts padding
// this also defines the left & top of the scroll content
labelOne.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 16.0).isActive = true
labelOne.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 16.0).isActive = true
// add labelTwo to the scroll view
scrollView.addSubview(labelTwo)
// constrain labelTwo at 400-pts from the left
labelTwo.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 400.0).isActive = true
// constrain labelTwo at 1000-pts from the top
labelTwo.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 1000).isActive = true
// constrain labelTwo to right & bottom with 16-pts padding
labelTwo.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: -16.0).isActive = true
labelTwo.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -16.0).isActive = true
}
}
let vc = TestViewController()
vc.view.backgroundColor = .yellow
PlaygroundPage.current.liveView = vc
div.logo, div.form {
display: inline-block;
height: 200px;
width: 200px;
border: 1px dotted;
vertical-align:top;
}
div.logo, div.form {
display: inline-block;
height: 200px;
width: 200px;
border: 1px dotted;
vertical-align:top;
}
答案 1 :(得分:4)
默认情况下,inline
和inline-block
元素设置为vertical-align: baseline
。
由于您的div.logo
有一个文字输入,div.form
现在是inline-block
元素,因此会在input
的基线上对齐。
将vertical-align: top
添加到CSS应修复它。如:
div.logo, div.form {
display: inline-block;
height: 200px;
width: 200px;
border: 1px dotted;
vertical-align: top;
}
div.logo, div.form {
display: inline-block;
height: 200px;
width: 200px;
border: 1px dotted;
vertical-align:top;
}
<div class="logo">
<input type="text">
</div>
<div class="form">
</div>