Question regarding styling default buttons with CSS

Hello Campers,
(Yes, another 5 mile long post by Aramini, one day soon after learning to code, I will take a business writing class to learn to be more concise… I promise! :laughing: )

I am working on a personal project and I wanted to style some buttons with CSS. I have the issue resolved. But I have a question about the precedence of CSS with regards to the styling.

So say you have some buttons in a web application (the context is HTML, CSS, PHP, MySQL, so that is why the HTML attribute values are being escaped).

Code like this…(sample code, not necessarily the actual code)

<button type = \"button\" name=\"back\" value = \"Back\" onclick=\"history.back()\">Back</button>
			
<button type = \"button\" name = \"lastquery\" value = \"Last Query\" onclick = \" window.open('$url' , '_self');\">Last Query</button>

<button type = \"button\" name = \"edit\" value = \"Edit\" onclick = \"window.open('edit.php, '_self');\">Edit</button>

For some reason, if I add a class to buttons like that, the styling doesn’t take. Nor if the type="reset" or type="submit" does using a class or id selector seem to work in regard to the styling being applied.

I did get the styling to apply to the button elements defined by <button></button> tags by using this:

button {
  display: inline-block;
  margin: 10px 5px 10px 2px;
  background-color: #12689B!important;
  color: white;
  border-radius: 5px;
  /* box-shadow: 3px 3px darkgray; */
  padding: 5px;
  width: 100px;
  font-weight: bold
}

but for the <input type="submit"... > and the <input type = "reset" ... >" buttons, I had to do this:

input[type=submit] {
  display: inline-block;
  margin: 10px 5px 10px 2px;
  background-color: #12689B!important;
  color: white;
  border-radius: 5px;
  padding: 5px;
  width: 100px;
  font-weight: bold
}

input[type=reset] {
  display: inline-block;
  margin: 10px 5px 10px 2px;
  background-color: #12689B!important;
  color: white;
  border-radius: 5px;
  padding: 5px;
  width: 100px;
  font-weight: bold
}

I “duckduckgoed/googled” for a while but could not find any explanation that satisfied me, just… “oh you have to do it like this” or very shallow tutorial style articles. I want to understand WHY the behavior. The MDN didn’t seem to explain it well either (unless I overlooked some web doc article).

Why is it that those type of buttons have to be done that way and styling doesn’t seem to work if you try to apply it via class or id? I have switched the order and played with internal <style></style> tags, but for whatever reason, those approaches did not work as expected.

I am aware of the CSS cascade priorities. But the above is the only CSS code regarding buttons in my linked stylesheet so I don’t get what the !@#% was overriding my styles.

INPUT and BUTTON are two completely different elements. One is a control, the other a simple semantic element to which we can assign behavior. A button has no built in default behavior. A control does. Submit and Reset are two distinct examples.

Aside

Since all the property declarations are the same, their selectors can be combined into a comma separated list:

input[type=submit],
input[type=reset] {

}

Thanks for your reply.

I do understand the difference between these element types with regard to what they are for… and I know you can make a <button> behave like an input control…

<button type ="reset" ... >Reset</button>
<button type = "submit"... >Submit</button>

But to make sure I’m understanding what you’re saying… since “buttons” as <input> elements are built in controls, you have to target them very specifically like that rather than simply giving them an id or making them a member of a CSS class? In order to specifically override the default styling as defined by the browser? (or OS or whatever and sometimes possibly even use a webkit?)

Am I interpreting what you are saying correctly?

Yes, I was experimenting with different properties/values in each selector to quickly see the different effects during testing and troubleshooting. I ultimately just did a copy/paste/edit during testing. I will combine into 1 selector now that I got it working.

Thanks again! :smiley:

Will have to read up on it but I don’t believe button has a type attribute for any reason other than to follow the same default behavior of the input control of the same type. That does not preclude it being a separate element by definition. Because it has no default behavior this vector has been opened to it by the browser.

One will do best not to draw any assumption from this. Treat BUTTON as not inheriting any behavior, period. In other words, don’t expect that behavior. If you want built in behavior, use the controls.

OK, that makes sense. Thanks

I was still a little fuzzy as to the CSS selector usages effect when trying to style by doing something like…

<button class="button1">

<input type = "submit" class="button2">

I was under the impression all this time that:

.someclass {
 someproperty: somevalue;
}

meant element class = someclass

but reading up on it , it seems that really means ~=

W3 - Selectors Level 3

The following CSS rules illustrate the differences between “=” and “~=”. The first selector would match, for example, an a element with the value “copyright copyleft copyeditor” on a rel attribute. The second selector would only match an a element with an href attribute having the exact value “http://www.w3.org/”.

a[rel~=“copyright”] { … } a[href=“http://www.w3.org/”] { … }

Working with HTML, authors may use the “period” notation (also known as “full stop”, U+002E, .) as an alternative to the ~= notation when representing the class attribute. Thus, for HTML, div.value and div[class~=value] have the same meaning. The attribute value must immediately follow the full stop (.).

So it seems one needs to be very careful how they name and use classes and class selectors, or there might be unintended or unexpected consequences. I think it just finally clicked in my brick of a brain lol :smiley:

Not sure why I didn’t realize this before, I guess I never tried to name classes that way before.

In addition to what you’ve stated in your reply, I finally found these docs, which also helped better clarify the button issue for me …

<button>: The Button element

The <button> HTML element is an interactive element activated by a user with a mouse, keyboard, finger, voice command, or other assistive technology. Once activated, it then performs a programmable action, such as submitting a form or opening a dialog.

By default, HTML buttons are presented in a style resembling the platform the user agent runs on, but you can change buttons’ appearance with CSS.

type

The default behavior of the button. Possible values are:

  • submit : The button submits the form data to the server. This is the default if the attribute is not specified for buttons associated with a <form> , or if the attribute is an empty or invalid value.
  • reset : The button resets all the controls to their initial values, like <input type=“reset”>. (This behavior tends to annoy users.)
  • button : The button has no default behavior, and does nothing when pressed by default. It can have client-side scripts listen to the element’s events, which are triggered when the events occur.

<input> types

MDN - Attribute Selectors

Thanks again for you help!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.