如何使父元素扩展到其子元素的宽度?

时间:2019-02-11 19:06:39

标签: html css html5 css3 parent-child

我有一个带有嵌套<button></button>的{​​{1}}元素。按钮的样式使用<span>:before伪选择器:

:after

但是,当我扩展<div> <button type="submit"><span>Submit Form Too Long</span></button> </div> 的内容时,它只会换行到下一行,而不会:

  1. 扩大父按钮的高度,从而在样式溢出时丢失样式
  2. 它也不会扩展<span>父元素的宽度

具有能够利用这些功能之一的选项将是理想的。我在以下链接中创建了一个Codepen示例。内容太长的按钮位于顶部的<span>(蓝色)。

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

我该怎么做才能使其表现出我想要的方式?我怀疑伪选择器在这里引起了一些有趣的事情。

1 个答案:

答案 0 :(得分: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>