What can make two elements with equal CSS properties look different? [duplicate] - html

This question already has answers here:
What makes the text on a <button> element vertically centered?
(7 answers)
Closed 2 years ago.
In the following HTML/CSS page, I'm trying to make a link and a button look the same. The reason is that the element used should follow its meaning (go somewhere vs. do something), while the look-and-feel depends on whatever turns out to look good for users.
*,
:after,
:before {
box-sizing: inherit
}
.button {
margin: 0px;
width: 100px;
inline-size: 100px;
padding: 20px;
color: black;
text-decoration: none;
font-family: sans-serif;
align-items: normal;
perspective-origin: 60.8906px 44.5625px;
transform-origin: 60.8984px 44.5625px;
cursor: pointer;
display: inline-block;
min-height: 4em;
outline: 0;
border: none;
vertical-align: baseline;
background: #e0e1e2 none;
font-weight: 700;
line-height: 1em;
text-align: center;
font-size: 1rem
}
.container {
height: 100px;
}
<div class="container">
<a class="button" href="#">A</a>
<button class="button">A</button>
</div>
The look pretty similar, but in both Chrome and Firefox, they are arranged in a different way vertically. The text seems to be at the same Y, but the background box around it isn't. I would expect such a behaviour if they somehow get different CSS properties, possibly from the user-agent stylesheet.
The interesting thing is that when I have the browser (Chrome) show me all computed CSS properties, all of them are equal. I verified this by copying the properties of each element to a file, then diff'ing the files.
I also checked whether the order of the elements is relevant, but it isn't -- in the sense that the link/button always look the same no matter where in the element order they appear.
Why do the elements look different even though all CSS properties are the same, and what changes are needed to make them look the same?
(The second question is meant to pinpoint the exact fix for the problem, not just some "shotgun approach".)

I have used flexbox for both tags
*,
:after,
:before {
box-sizing: inherit
}
.button {
margin: 0px;
width: 100px;
inline-size: 100px;
padding: 20px;
color: black;
text-decoration: none;
font-family: sans-serif;
align-items: normal;
perspective-origin: 60.8906px 44.5625px;
transform-origin: 60.8984px 44.5625px;
cursor: pointer;
display: inline-block;
min-height: 4em;
outline: 0;
border: none;
vertical-align: baseline;
background: #e0e1e2 none;
font-weight: 700;
line-height: 1em;
text-align: center;
font-size: 1rem;
display: inline-flex;
align-items: center;
justify-content: center;
}
.container {
height: 100px;
}
<div class="container">
<a class="button" href="#">A</a>
<button class="button">A</button>
</div>

You can just use the a or button tag for both elements, then handle their behavior using javascript

Related

I'm reordering some CSS elements, but certain properties seem to be going ignored

I started copying elements and reordering the style elements, I found out that some elements are simply not being computed into the HTML (even though I haven't changed any of the code). It should show as a button inside a flexbox, but it's not displaying properly now on my latest version. Do you mind looking at the code and helping me find out why it's not adequately displaying most of the text attributes I assigned to it?
#flexIV {
width: 404px;
height: 100px;
margin: 0;
padding: 0;
display: flex;
flex-direction: row-reverse;
justify-content: space-around;
}
#BTT_1 a {
font-family: 'Poppins', sans-serif;
font-weight: bold;
color: yellow;
text-decoration: none;
text-align: center;
border-style: solid;
border-style: solid;
background-color: crimson;
border-width: 2px;
padding: 5px;
}
#BTT_1 {
/*background-color: rgb(255, 255, 0);*/
width: 134.6px;
height: 100px;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
}
<div id="flexIV">
<div id="BTT_1">
<!-- <div id="check">-->
Check<br>webpage
<!--</div>-->
</div>
</div>
</div> <!-- if I delete this element the next property disappears -->
The text in the element is just showing as a clickable link.
Your link isn't working so I can't check your example, however, as your CSS is currently written, everything below this line won't compute:
*/
/*Enter-BTT*/
}
As this is an open/improperly written comment. Try deleting that line and see if it solves your issue.

the text is decentered in the circular button with the increase of the font size

I need to build a circular button... and I've already seen many answers... and yes... they all work... but the button I need has inside an angular material icon that occupies the entire space of the circle... so when I use these solutions and increase the size of the icon then it becomes unfit.
For example... using the same solution shown in centering-text adapted to what I need this is the code I use:
.btn-circle {
border: 10px solid white;
background-color: #444;
/*background: #97c83e;*/
width: 72px;
height: 72px;
border-radius: 100%;
display: inline-flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: white;
font-weight: bold;
text-decoration: none;
/*
text-orientation:sideways;
writing-mode: vertical-lr;
*/
text-align: center;
font-size: 55px;
/*
font-size: 35px;
*/
}
<a href="" class="btn-circle">
‹‹
</a>
In this case with the font size set to 35px everything looks fine.
The button I need to build would be exactly one like this:
I have also used the method of the div with display table and another div inside with display table-cell as in align-text-vertically and the same thing happens to me.
What you need to be aware of is that the vector object, in this case ‹ has been saved with additional alpha space above and below it. This is done because it is part of a font set and needs to align correctly with other font characters.
See this example of how the vectors have included space. Only the S will be centralised.
EXAMPLE
div {
font-size: 55px;
background:#ccc;
}
<div>Ss‹y</div>
MANUAL OFFSET
Use `line-height`, `margin`, `padding` or absolute positioning to manually offset font position after centering.
Also note align-items controls children. align-content controls self.
.btn-circle {
background-color: #444;
width: 72px;
height: 72px;
border-radius: 100%;
text-decoration: none;
display: flex;
justify-content: center;
align-content: center;
color: white;
font-weight: bold;
font-size: 55px;
line-height:60px;
}
<a href="" class="btn-circle">
‹‹
</a>
UNICODE ALTERNATIVE
You will get the best control by setting the flex content to a control that can be targeted like a span tag. this way you can directly manipulate the object. In this case setting it to fill its container.
This unicode example is not ideal as it has some alpha space.
You can find others here - https://unicode-table.com/
.btn-circle {
background-color: #444;
width: 72px;
height: 72px;
border-radius: 100%;
text-decoration: none;
display: flex;
justify-content: center;
align-items: center;
}
.btn-circle span {
color: white;
font-size: 55px;
height: 100%;
}
<a href="" class="btn-circle">
<span>«</span>
</a>
SVG
Personally I use svg icons that are already perfectly centered vectors and easier to work with.
I don't recommend linking svg's to independant files. I would use them
inline. This is just an example.
.btn-circle {
width: 72px;
height: 72px;
border-radius: 100%;
display: flex;
fill:#fff;
background: #ddd url("https://www.svgrepo.com/show/361731/chevron-double-left.svg") no-repeat center center;
background-size: 40px;
}

How to get a box like this on a webpage?

am looking for a way to get a box like this on a webpage Image here
This is actually reddit's theme and I would like to understand how to create a box like this with a different color and then another box sticked with it in white. I couldn't come up with a term to google for this, nonetheless, I still googled things like ''box with css'', ''box table in css'' etc. but no luck.
What this is called and how to exactly do this?
The height/width are probably going to be off for what your ultimate code will need, but with this snippet you can get the idea of adding border-radius to an outer card element, and then border-top-right-radius and border-top-left-radius to the inner card-header element to get the desired effect.
The display: flex on the card-header element allows you to use justify-content: space-between to push the title and the ellipsis away from each other with minimal code.
I added some other styling to just give the same sense of colors, font style, etc.
.body {
display: block;
background: #FAFAFA;
width: 1000px:
height: 1000px;
padding: 25px;
}
.card {
background: #fff;
border-radius: 5px;
width: 300px;
height: 400px;
}
.card .card-header {
display: flex;
justify-content: space-between;
color: #fff;
font-family: sans-serif;
font-size: 12px;
font-weight: bold;
letter-spacing: 0.5px;
background: #4BA2AF;
padding: 10px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
<div class="body">
<div class="card">
<div class="card-header">
<span class="title">COMMUNITY DETAILS</span>
<span class="ellipsis">…</span>
</div>
</div>
</div>

Centering Images and Text [duplicate]

This question already has answers here:
Vertically align text next to an image?
(26 answers)
Closed 7 years ago.
I want both the image and text centered horizontally and vertically. What is the best way to go about this? I have tried float but it doesn't seem to be working. See the above image for ideal result
HTML:
<div class="clearfix" id="one">
<img class="imac" src="imac.png">
<p1>
I want to work in Computer Design, changing the way people
interact with thoughtfully considered software and hardware
experiences.
</p1>
</div>
CSS:
#one{
background-color: #4E5B71;
width: 100%;
height: auto;
padding: 15px;
}
.clearfix{
overflow: auto;
}
p1{
font-family: AvenirNext-Regular;
font-size: 24px;
color: #FFFFFF;
line-height: 50px;
vertical-align: middle;
display: inline-block;
}
imac{
width: 100% auto;
height: auto;
float:left;
vertical-align: middle;
}
SOLUTION 1
Demo: https://jsfiddle.net/2Lzo9vfc/78/
HTML
<div class="clearfix" id="one"> <img class="imac" src="http://placehold.it/70x50"> <p> I want to work in Computer Design, changing the way people interact with thoughtfully considered software and hardware experiences. </p>
</div>
CSS
#one{
background-color: #4E5B71;
width: 100%;
height: auto;
padding: 15px;
display: table;
}
.clearfix{
overflow: auto;
}
p{
font-family: AvenirNext-Regular;
font-size: 16px;
color: #FFFFFF;
vertical-align: middle;
display: table-cell;
}
.imac {
vertical-align: middle;
display: table-cell;
}
SOLUTION 2 using flexbox
Demo: https://jsfiddle.net/2Lzo9vfc/81/
CSS
#one{
background-color: #4E5B71;
width: 100%;
height: auto;
padding: 15px;
display: -webkit-flex;
display: flex;
align-items:center;
}
.clearfix{
overflow: auto;
}
p{
font-family: AvenirNext-Regular;
font-size: 16px;
color: #FFFFFF;
}
The vertical-align property does not work in that context. It is a bit counter-intuitive but vertical-align will only effectively work in a table layout.
You have a few ways to solve your predicament, but on the topic of tables, it might not be a bad idea to use a table to assist with your alignment. For example, you could put create a table with one row <tr> and four table cells <td> and apply your vertical-align to the table cells. The image would be in cell two, and the paragraph in cell three. You would then apply the desired width to the cells to ensure correct horizontal alignment.
PS: There is no <p1> tag. You should be using just <p>.
To center the text, you should use text-align: center.
p1{
font-family: AvenirNext-Regular;
font-size: 24px;
color: #FFFFFF;
line-height: 50px;
display: inline-block;
text-align: center;
}
You can also use a paragraph to wrap the image and control it. You can use the same paragraph formatting as the text below it or give it its own class. Just make sure it also has text-align: center programmed in it.
<p1><img class="imac" src="imac.png" /></p1>

Button Link Formatting - Center and Match Width

I've created a couple of simple buttons using a link and some CSS to give it a background and I'm trying to center it on my page. However, because the text in one of the buttons is longer than the other, the buttons are of different sizes and for consistency, I'd like them to be the same width.
How can I keep these buttons the same size? Trying to float them and use percentage widths results in them not being centered. The relevant markup is below.
<section class="buttonsSection">
<a class="button" href="#">Very Long Sentence</a>
<a class="button" href="#">Short Phrase</a>
</section>
.button {
padding: 10px 15px;
background-color: deepskyblue;
color: white;
}
.buttonsSection{
text-align: center;
margin-top: 30px;
margin-bottom: 30px;
}
.buttonsSection a {
margin: 3px;
}
JSFiddle: http://jsfiddle.net/Dragonseer/eTvCp/11/
Answer
While both of the answer below are valid, I'm updating my answer to using Flexbox. Most modern browsers have excellent support for it, including IE11 which will be released in the very near future. Flexbox appears to provide a much better solution to doing complex layouts which requires less effort than it's alternatives, such as floating items.
use a fixed width with inline-block on the buttons.
Working Fiddle
.button {
padding: 10px 15px;
background-color:deepskyblue;
color: white;
display: inline-block;
width: 20%; /*or any other width that suites you best*/
}
.callToAction {
text-align: center;
margin-top: 30px;
margin-bottom: 30px;
}
using inline-block provides a little-bit of margin between the elements (caused by a white-space in the HTML) so I removed the marin from the CSS, but you can put it back.
Easily done with flexbox:
.button {
padding: 10px 15px;
width: 150px; /* Fixed width links */
background-color:deepskyblue;
color: white;
margin: 3px;
}
.callToAction {
margin: 30px 0;
display: flex; /* Magic! */
justify-content: center; /* Centering magic! */
}
Working Example
.button
{
width: 150px; /* Your custome size */
background-color:deepskyblue;
color: white;
margin: 3px;
padding: 10px 15px;
}
Section a
{
width: 150px; /* for your all buttons */
}