SoooâŚthey can be made to look like buttons, but I shouldnât do that? Even though thatâs what I need? Surely there must be a good viable option for making a button that takes you to another page or location on that page.
For instance, the HTML & CSS button at the top of this page that links me to that portion of the forumsâŚ
It all depends on the context and if this is part of a navigation that clearly suggests navigation then you can style anchors to look like buttons if you want.
If you have a single button on its own then the user wonât know if its a link or is going to submit a form or perform some other action. Once you make your users think then you are lost
If you want to ignore the semantics then just style the anchor to look like a button. CSS doesnât care what an element looks like,
So I made these into links instead of buttons and now Iâm trying to get them back to being stylized how I had them but Iâm running into a problem. I canât seem to change the height of the div. I adjust it and nothing happens. When I changed them to links they got way smaller and I need the div to be larger so there is some room between them and the surrounding items.
Here is a codepen. Itâs a little messed up, I was having some issues with the Javascript for some reason. But the important part is there. The pink div is the one iâm having issues with.
Anchors are inline elements by default and you canât apply dimensions to inline elements.
Buttons are basically inline-block elements so you need to set tho display of the anchor to inline-block (display:inline-block) and then vertical padding and margins will take effect properly along width dimensions.
I mentioned in your other post about your use of percentages with vertical margins and in most cases you will never have a use for them. Percentages for margins and padding refer to the width of the containing block even for top and bottom which means the space will change depending on how wide the page is. You wonât want that so use px for vertical margins when you want a consistent space (you can also use rem or em but as em is based on the parentâs font-size that means it will be different if elements have a different font-size).
Also avoid setting heights on main containers especially percentage heights as they can mostly not be resolved (a percentage height can only be based on another parent that has a fixed height also (not itâs content height) and so on up the chain until you get to root. vh units allow a viewport height or percentage to be used but again unless you know what you are doing they will cause you more trouble along the line.).
The percentage height cannot be resolved and it is highly unlikely that it would be workable there anyway. Remove the height and let its content dictate the height.
Ok so I got that part figured out, but now iâm trying to get them sized the same. All 3 are different sizes because of the amount of text in them. When I had them as buttons they all 3 seemed to go to the same size as the button with the most text and they looked great. But now they are all different sizes. Can I just set height and width on them directly? I thought that was usually frowned upon though.
No they would probably have had a default (browsers specific) âmin-widthâ and would not have sized in respect of each other (you can test that for yourself and easily see).
You can set a min-width on your buttons if you want them all to be the same width. A min-width will allow the buttons to grow if someone resizes the text from the browsers controls or you could set a fixed width/height in ems assuming you know what the longest item will be.
If you are using bootstrap (I see you have a stylesheet that seems to suggest you are using it) you could add the 'btn class to the anchors and use the standard bootstrap buttons (just check the bootstrap documentation for buttons as there is a whole section on them).
Ok so I tried thisâŚbut the text is completely off center of the button. The buttons are oval shaped and the text is up towards the top when I resize them.
You didnât show your code but I assume again you have used height to create the button instead of simply using an equal amount of top and bottom padding (in px) and then the text will automatically be vertically centred without the need for a height.
If you want to use the fixed height appproach then you also need to set the line-height to match the height and that will also vertically centre the text.
Well I canât use padding because that doesnât solve my problem. Setting padding sets padding around the text. If there is more text in one button than another, then the buttons will still be different sizes. However, using the line height did fix it, although not by matching the height. But I was able to adjust it to make it look nice. Thanks for the help.
Top and bottom padding will not increase the width of the element.
You donât need height as I have mentioned a few times but if you are using height and line-height then you may have to tweak the line-height smaller or larger depending on whether you have just upper or lowercase letters in your buttons. If you have a normal mixture of upper and lowercase then the line-height should match the height within a px or two.
Remember that the text you see is not the full story as a letter that has descender will not look the same as a Capital letter in regards to a vertical central position. If you have adjusted the space for ALL CAPS then obvioiulsy that will sit higher on the line (and vice versa).
Also note that although line-height is the space that the text sits the glyphs that make up the fonts may or may not use all that space and indeed have what is called a half leading space on each vertical edge of that font. It is likely that on odd pixel sizes there will be a 1px difference between top and bottom simply because there is no such thing as half a pixel.
Can you send a link to your page I love to look at ideas people make in the codepen
Or if it isnât your codepen can you show me how or where you found it as I always have trouble finding work related to HTML and CSS always SCCS and never really website layouts
Yes, I just added the code a few minutes ago for the OP.
Not sure what you will see but my homepage is here. You should be able to search or follow my pens or others from the search function on the site. A lot of my pens are just answers to forum questions and not that interesting. (Note that a number of the pages have images missing as codepen changed toi https a while ago and updated all my paths to https although I donât use https so the image paths need to be set back to http to view the original.)