Barry Chapman
Barry Chapman

Reputation: 6780

How to make a parent element expand to the width of its child?

I have a <button></button> element with a nested <span>. The styling of the button uses the :before and :after pseudo selectors:

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

However, when I expand the content of the <span>, it just wraps to the next line, without:

  1. Expanding the height of the parent button, thus losing the style as it overflows
  2. It also does not expand the width of the <span>s parent element

Having the option of being able to utilize either of these would be ideal. I have created a codepen example at the following link. The button with content too long is the submit at the top (blue).

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

What can I do to make this behave the way I want? I suspect the pseudo selectors are causing something funny here.

Upvotes: 0

Views: 49

Answers (1)

Bryce Howitson
Bryce Howitson

Reputation: 7690

Here's an updated version of your CodePen example. You had a lot of CSS fighting with itself, text styling for elements without text, etc.

I cleaned it up and simplified your buttons. Backgrounds are now on the element itself, no additional spans, etc. If you add a max-width things will wrap correctly and everything should remain centered.

Won't work exactly right, but here's the code in SO's format.

@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>

Upvotes: 1

Related Questions