如何使一个父元素扩展到其子的宽度是多少?

问题描述 投票:0回答:1

我有一个嵌套<button></button>一个<span>元素。按钮的造型采用了:before:after伪选择:

<div>
  <button type="submit"><span>Submit Form Too Long</span></button>
</div>

然而,当我展开<span>的内容,它只是换到下一行,没有:

  1. 扩大母按钮的高度,从而失去了风格溢出
  2. 它也不会扩大<span>s父元素的宽度

具有能够利用任一选项将是理想的。我在下面的链接创建codepen例子。与内容按钮过长是在顶部(蓝色)的submit

https://codepen.io/barrychapman/pen/vbRbwR

我能做些什么,使这个行为我想要的方式?我怀疑伪选择在这里引起一些有趣的事情。

html css html5 css3 parent-child
1个回答
1
投票

这是你的CodePen example的更新版本。你有很多CSS的战斗与本身的文本样式为无文本元素等

我打扫它,并简化您的按钮。背景现在元素本身,不需要额外的跨度,等等。如果你增加一个最大宽度事情会正确地包装,一切都应该保持居中。

将无法正常工作完全正确的,但在这里是在SO的格式的代码。

@charset "UTF-8";
* {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  font-size: 100%;
  vertical-align: baseline;
  background: transparent;
}

body {
  font-size: 100%;
  vertical-align: baseline;
  padding: 20px;
  background: #fff;
  font-family: "Roboto";
}

button {
  display: block;
  margin: 20px auto;
  position: relative;
  cursor: pointer;
  min-width: 13em;
  min-height: 50px;
  border-top: 1px solid #0e0e0f;
  border-radius: 6px;
  padding: 8px 50px 8px 8px;
  font-size: 16px;
  line-height: 100%;
  vertical-align: middle;
  color: #333;
  transition: all 0.4s ease;
}
button:after {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%) rotate(0deg);
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  font-family: "Font Awesome 5 Free";
  transition: transform 400ms ease-in-out;
}
button:before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: 40px;
  width: 1px;
  background-color: #777777;
}
button:hover:after {
  transform: translateY(-50%) rotate(360deg);
}
button:active {
  transform: translate(2px, 2px);
}
button:active:after {
  color: #0c397c;
}
button[type="button"] {
  padding: 8px;
  background-image: linear-gradient(to bottom, white, #aaaaaa);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), inset 0 0 1px rgba(255, 255, 255, 0.1), 0 3px 6px rgba(0, 0, 0, 0.65), 0 1px 2px rgba(0, 0, 0, 0.9);
}
button[type="button"]:before, button[type="button"]:after {
  display: none;
}
button[type="button"]:hover {
  background-image: linear-gradient(to bottom, #f4f4f4, #a0a0a0);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), inset 0 0 1px rgba(255, 255, 255, 0.1), 0 3px 6px rgba(0, 0, 0, 0.65), 0 1px 5px rgba(0, 0, 0, 0.9);
}
button[type="button"]:active {
  border-color: #aaaaaa;
  background-image: linear-gradient(to bottom, #bcbcbc, #c4c4c4);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), inset 0 0 1px rgba(255, 255, 255, 0.1), 0 1px 1px rgba(0, 0, 0, 0.75), 0 1px 1px rgba(0, 0, 0, 0.95);
}
button[type="submit"] {
  color: #fafafa;
  background-image: linear-gradient(to bottom, #0780cc, #033351);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), inset 0 0 1px rgba(255, 255, 255, 0.1), 0 3px 6px rgba(0, 0, 0, 0.65), 0 1px 2px rgba(0, 0, 0, 0.9);
}
button[type="submit"]:after {
  content: "";
}
button[type="submit"]:hover {
  background-image: linear-gradient(to bottom, #0671b3, #044269);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), inset 0 0 1px rgba(255, 255, 255, 0.1), 0 3px 6px rgba(0, 0, 0, 0.65), 0 1px 2px rgba(0, 0, 0, 0.9);
}
button[type="submit"]:hover:after {
  color: #ededed;
}
button[type="submit"]:active {
  background-image: linear-gradient(to bottom, #03395b, #033f64);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), inset 0 0 1px rgba(255, 255, 255, 0.1), 0 1px 1px rgba(0, 0, 0, 0.75), 0 1px 1px rgba(0, 0, 0, 0.95);
}
button[type="submit"]:active:after {
  color: #d4d4d4;
}
button[type="reset"] {
  color: #efefef;
  border-color: #546e7a;
  background-image: linear-gradient(to bottom, #869ba5, #333f45);
}
button[type="reset"]:after {
  content: "";
  color: #eceff1;
}
button[type="reset"]:hover {
  background: linear-gradient(to bottom, #889da7 0%, #55707e 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), inset 0 0 1px rgba(255, 255, 255, 0.1), 0 3px 6px rgba(0, 0, 0, 0.75), 0 1px 2px rgba(0, 0, 0, 0.95);
}
button[type="reset"]:hover:after {
  color: #e2e2e2;
}
button[type="reset"]:active {
  border-color: #2f4753;
  background: linear-gradient(to bottom, #496878 0%, #3c5b6b 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), inset 0 0 1px rgba(255, 255, 255, 0.1), 0 1px 1px rgba(0, 0, 0, 0.75), 0 1px 1px rgba(0, 0, 0, 0.95);
}
button[type="reset"]:active:after {
  color: #c9c9c9;
}
<button type="submit">Submit Form Too Long</button>
<button type="button"><span>Default Action</span></button>
<button type="reset"><span>Reset Form</span></button>
© www.soinside.com 2019 - 2024. All rights reserved.