Fiddle URL: https://jsfiddle.net/e8h0jb4a/1/.
<article><span>1</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>2</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>3</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>4</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>5</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
<article><span>6</span><img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg"></article>
html{
font-size: 18pt;
}
body{
background-color: black;
font-family: "PT Sans", "Arial", sans-serif;
font-size: 20pt;
color: white;
padding: 0.5rem;
margin: 0.8rem;
display: flex;
flex-flow: row wrap;
gap: 0.5rem;
justify-content: center;
}
article{
border-style: solid;
border-color: #202020;
border-width: 1.5pt;
padding: 0;
width: 9.5rem;
height: 7.5rem;
background-color: #101010;
transition: 202ms;
text-align: center;
box-sizing: border-box;
font-size: 50pt;
cursor: pointer;
}
img{
object-fit: cover;
height: 100%;
width: 100%;
display: block;
opacity: 0;
transition: 202ms;
}
article:hover{
background-color: #1f1f1f;
}
.open{
cursor: default;
}
.open span{
display: none;
}
.open img{
opacity: 1;
}
.open:hover{
transform: scale(1.8);
}
$('article').click(function() {
$(this).addClass('open');
});
You see that each card has some text on it. Once you click a card an image is shown and the text disappears. So far so good. Now I want to get the text vertically centered in each box. And I want the box which you can click for the image to be shown just the visibile box (at the moment you can click below too because of the text which takes up space).
I tried to solve this with absolute position of the text. The problem then is that the animation when you hover images brings the images below the other boxes.
How would you solve the problems? I'm looking for css only changes. Is there even a nice way with my html code or does the html code need to changed as well?
Try using Flexbox. You can use the align-items: center property to vertically align your text, and justify-content: center to horizontally align your text. You could also use text-align: center for this. Don't forget to set display: flex, otherwise, Flexbox will not be used.
This website is very useful for understanding Flexbox.
So for aligning text vertically center you can use CSS property.
article{
line-height:7.5rem; //value same as the height
}
And for hiding the image when user clicks on different element
$('article').click(function() {
for(var i=0;i<$("article").length;i++){
$("article").eq(i).children().eq(0).css("display","block");
$("article").eq(i).children().eq(1).css("opacity","0");
}
$(this).children().eq(0).css("display","none");
$(this).children().eq(1).css("opacity","1");
});
Instead of adding a class you can use this method.
Edit
You were able to click beneath the article because there was image with opacity:0; so to fix this problem you can use display:none;
Here is the code
img{
display: none;
}
.open img{
display:block;
}
And you can use the default JS code i.e
$('article').click(function() {
$(this).addClass('open');
});
Fiddle https://jsfiddle.net/vrf58L4s/5/
Related
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;
}
I have bee trying to do a close button using CSS in order to close a tag.
I have done this using a compination of a pattern described here: Patterns for Closebuttons, and a css only pattern for creating an X Close Button using CSS.
Now to my question:
I can't figure out why the X is not entierly centered.
If I change the placement to 50% of whatever width I'm using for the lines it seemes fine, thus I come to the conclusion it has to have something to do with the width of the lines.
I'm writing here to see if someone could explain this to me.
'HTML
<button type="button" aria-label="Close">
<span aria-hidden="true" class="close"></span>
</button>
'CSS
button {
display: flex;
justify-content: center;
align-items: center;
font-weight: 700;
padding: 0px;
background-color: rgb(192,192,192);
border: none;
color: white;
text-decoration: none;
height: 150px;
width: 150px;
border-radius: 50%;
}
button:hover {
background-color: rgb(146, 146, 146);
cursor: pointer;
}
.close {
position: relative;
/* right: 10px; */
width: 60%;
height: 60%;
}
.close:before, .close:after {
position: absolute;
content: ' ';
height: 100%;
width: 20px;
background-color: rgb(255, 255, 255);
}
.close:before {
transform: rotate(45deg);
}
.close:after {
transform: rotate(-45deg);
}
I created a codepen for simplicity: Codepen link
Just add flexbox to the .close class and you're ready to go
.close {
position: relative;
width: 100%;
height: 60%; // 100% means that both lines will take the entire space
display: flex;
align-items: center;
justify-content: center;
}
The width of the .close:before, .close:after CSS rule is actually letting both lines "expand" horizontally, starting from the center.
But, as they are being rotated, their starting position is not the same as if they were not rotated, so that's why their alignment becomes non-centered. You can inspect the item and you will see that the actual box has an empty space, which is the "rotated width" empty space.
Consider a x = 0 coordinate (0px from CSS box left margin) which is the horizontal starting point of the CSS box; rotating elements will make content to start at x = ? (usually half of the content width)
You could solve this issue also by setting a negative margin-left with its value being half the width, but using flexbox will require much less maintenance (just imagine working with UI/UX guys telling you to change its dimensions)
Add This CSS left:50%; margin-left:-10px; on .close:before, .close:after
.close:before, .close:after {
position: absolute;
content: ' ';
height: 100%;
width: 20px;
background-color: rgb(255, 255, 255);
left:50%;
margin-left:-10px;
}
Hello there try to add this to your span class .close:
display: flex;
justify-content: center;
align-items: center;
You should not use right or left when your attribute is relative.
I'm trying to display an image in an icon next to a piece of text. It sits slightly below the text and I'd like it to at least be the same height as the text aka vertically aligned to the middle.
.text {
font-family: 'Roboto', sans-serif;
font-size: 20px;
display: inline-block;
margin-top: 0;
margin-bottom: 0;
line-height: normal;
color: black;
}
.em-image{
background-image: url(https://i.imgur.com/r8hkG1o.png);
}
[class^="em-"], [class*=" em-"], .em-png {
height: 1em;
width: 1em;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
display: inline-block;
vertical-align: middle;
}
<p class="text"><i class="em-image"></i> Name</p>
It's currently looking like this
Whereas I'd prefer it to look like this
Is there a way to at least make the image match the line height? The image sits at the bottom now but I'd like it the full height and aligned vertically to the text, I'm just not sure how to go about this, could the same be done with a span or a DIV containing the image as a background instead?
I Suggest that use vertical-align:baseline and use 'rem' instead 'em' that should work.
.text {
font-family: 'Roboto', sans-serif;
font-size: 2.5rem;
display: inline-block;
margin-top: 0;
margin-bottom: 0;
line-height: normal;
color: black;
}
.em-image{
background-image: url(https://i.imgur.com/r8hkG1o.png);
}
[class^="em-"], [class*=" em-"], .em-png {
height: 2rem;
width: 2rem;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
display: inline-block;
vertical-align: baseline;
}
<p class="text"><i class="em-image"></i> Name</p>
I would use Flexbox and <span> instead of <p> to achieve this. With the Flexbox approach you can center all the items horizontally and vertically.
Why <span>? Because it is a generic inline container for phrasing content, which does not inherently represent anything.
.wrapper {
display: flex;
align-items: center;
font-family: 'Roboto', sans-serif;
font-size: 75px;
line-height: 1;
}
span {
margin: 0 0 0 .25em;
}
.em-image {
background-image: url(https://i.imgur.com/r8hkG1o.png);
height: 1em; /* Change this value to match the desired height */
width: 1em; /* Change this value to match the desired width */
background-position: center;
background-repeat: no-repeat;
background-size: contain;
display: inline-block;
}
<div class="wrapper">
<i class="em-image"></i>
<span>Name</span>
</div>
Simply, Try to find the height of the text and make max height to the icon equal to the height of the text
you may try to add this CSS code:
[class^="em-"], [class*=" em-"], .em-png {
max-height: 200px;
}
if the icon is larger smaller than the text height try to increase or decrease the icon height
i hop this answer helps
My suggestion is to use inline-flex to the class .text (instead of inline-block)
then you can just give the icon a align-self: center and it should be what you are looking for. You will probably need to adjust the spacing but should be good.
.text {
display: inline-flex;
}
[class^="em-"] {
align-self: center;
}
I'd suggest several things:
Use "em", as you were doing. "rem" will take the root font-size, and will break the component if that changes, while "em" will keep the image size proportional to the label text.
Use flexbox. It's shorter, more clear and modern.
No font actually occupies the whole vertical space. There is some extra space above and bellow. The typography you are using, Roboto, occupies approximately 80% of the vertical space, so that should be reflected in your css if want to be really precise.
Optional: the font is also not exactly centered, it has slightly more space bellow than above, so you'll need to correct this, by a factor of 2%.
The final code would be:
.text {
font-family: 'Roboto', sans-serif;
font-size: 20px;
display: flex;
align-items: center;
}
.em-image{
background-image: url(https://i.imgur.com/r8hkG1o.png);
}
[class^="em-"], [class*=" em-"], .em-png {
height: .8em;
width: .8em;
background-position: center;
background-size: contain;
margin-right: .25em;
margin-top: -.02em;
}
<p class="text"><i class="em-image"></i> Name</p>
I'm trying to vertically center a text which is inside a div (.site-title), itself inside a div (.site-title-wrapper), and all of this inside another div (.site-header), which is the menu of the website.
Here are some pictures:
In green: .site-title-wrapper
In red: .site-title
and in white: .site-header
And I have the following CSS for these divs:
.site-title-wrapper {
display: table;
height: 100%;
position: absolute;
padding: 23px;
background-color: green;
}
and
.site-title {
font-family: "Roboto", sans-serif;
display: table-cell;
vertical-align: middle;
}
I've seen that using table and table-cell to vertically center a div inside another one was a good solution. It works fine, but the only thing I need to do is to force .site-title-wrapper to take all available height, so the green box goes down to the end of the white one (the menu).
The idea is to simply center the title with the menu elements.
I can't really change the html part, so I'm trying to fix it only with CSS.
Do you know how I can fix it?
.site-header{
width: 100%;
height: 100px;
border: 2px solid black;
}
.site-title-wrapper {
display: table;
height: 100%;
padding: 23px;
background-color: green;
}
.site-title {
font-family: "Roboto", sans-serif;
display: table-cell;
vertical-align: middle;
background-color: red;
}
<div class='site-header'>
<div class='site-title-wrapper'>
<div class='site-title'>
Some Text
</div>
</div>
</div>
EDIT: Here is a fiddle, in which what I tried works (I just removed the absolute) : https://jsfiddle.net/0xhL76gk/2/
I am working on a button style for Azure Active Directory B2C. Azure automatically provides the following content
<div class="options">
<div>
<button class="accountButton firstButton" id="AmazonExchange" tabindex="1">Amazon</button>
</div>
<div>
<button class="accountButton" id="LinkedInExchange" tabindex="1">LinkedIn</button>
</div>
<div>
<button class="accountButton" id="FacebookExchange" tabindex="1">Facebook</button>
</div>
<div>
<button class="accountButton" id="GoogleExchange" tabindex="1">Google+</button>
</div>
<div>
<button class="accountButton" id="MicrosoftAccountExchange" tabindex="1">Microsoft</button>
</div>
</div>
Using FontAwesome and :before css magic, I am able to add an icon, and fix width the button
.accountButton {
border: 1px solid #FFF;
color: #FFF;
margin-left: 0;
margin-right: 2px;
padding-right: 15px;
transition: background-color 1s ease 0s;
-moz-border-radius: 0;
-webkit-border-radius: 0;
border-radius: 0;
text-align: center;
word-wrap: break-word;
width: 120px;
background-color: #5x05050;
opacity: 0.7;
}
#AmazonExchange:before {
font-family: fontAwesome;
content: "\f270";
//font-size: large;
float:left;
width:32px;
text-align: center;
}
This is nice, but I would like the icons to be just a bit bigger. However, increasing the size of the FontAwesome item to large causes the primary text to go up, as it is aligned to the top of the float:left.
I've tried a variety of valign combinations with no luck. I cannot change the html (as it is dynamically generated). How can I center the "Amazon" with the larger icon? (Notice that the Amazon text is now higher than the LinkedIn and other text.)
Attempted jsfiddle, doesn't look quite the same, but should show the issue.
https://jsfiddle.net/tofutim/0637yknj/9/
There isn't a dynamic way to adjust valign with floating elements. You can either use display: inline-block; + vertical-align: middle;
.accountButton {
vertical-align: middle;
line-height: 1.5;
}
.accountButton:before {
vertical-align: middle;
margin-right: 8px;
}
jsFiddle
Or, use display: flex; + align-items: center;
.accountButton {
display: flex;
align-items: center;
line-height: 1.5;
}
.accountButton:before {
margin-right: 8px;
}
jsFiddle
The line-height part is just to make the buttons a bit taller, you can also use padding-top + padding-bottom instead.
Giving a line-height to .accountButton might correct this issue for you. This will vertically centre text within the given vertical space, and in your example should be equal to the desired height of the button.
Try something like: line-height: 30px;.