Vertically Aligning and Positioning Parent DIVs in CSS - css

I'm fairly new to coding. I was making a JS project when I encountered a problem. I need to vertically align 2 parent divs. Those 2 parent divs have several divs underneath them. So, in order to vertically align the whole page, I need to style those parent divs. Please help me in doing so. If you know the solution to my problem, please teach me how to move my divs along the way.
Here's the CSS Code. (If you want to see the JS and HTML code, here's the js fiddle link: https://jsfiddle.net/y924rv7g/)
body {
background-image: url(Background.jpeg)
}
/* AGE IN DAYS */
.container-1, .container-2 {
border: 1px solid;
margin: 0 auto;
text-align: center;
width: 75%;
}
.flex-box-container-1, .flex-box-container-2 {
display: flex;
padding: 10px;
border: 1px solid black;
flex-wrap: wrap;
flex-direction: row;
justify-content: space-around;
}
.flex-box-container-1 div {
padding: 10px;
align-items: center;
display: flex;
}
#flex-box-result {
font-size: 32px;
font-weight: bold;
}
#yikes {
font-size: 32px;
font-weight: bold;
Animation-name: example;
Animation-duration: 0.5s;
animation-iteration-count: infinite;;
}
#keyframes example {
10% {color: red;}
15% {color: yellow;}
20% {color: blue;}
30% {color: green;}
40% {color: lightsalmon;}
50% {color: lightsteelblue}
60% {color: steelblue}
70% {color: ivory}
80% {color: purple}
90% {color: pink}
100% {color: magenta;}
}
.flex-box-container-2 img {
box-shadow: -12px 11px 28px 6px rgba(0,0,0,0.69);
margin: 10px;
}

When posting a question to Stack Overflow, please try to keep the sample code as minimal as possible, so we can concentrate only on the necessary parts :) Not sure what exactly are you trying to do - what and how do you expect to have aligned? For one dimensional styling have a look at flexbox, for 2D grid. I'd recommend you to go through one of those links (probably flexbox in your case) and read them thoroughly, your answer is most likely there :)

Related

Accordion in my FAQ Page, Chevron moves horizontally

I am building a small online shop using Shopify, and I am getting back into programming/coding after dropping out of my "IT school" in which I had to do more electrical engineering and metalworking than actual programming.
I want to learn HTML, CSS and JavaScript to further improve my shop. I built (copied) an accordion style FAQ page. (This one right here: https://www.youtube.com/watch?v=qJoxQMKV-ew)
It looks really nice and I am really happy with it. However, there is an issue. If the question is multiple lines long (especially on mobile since the screen width is much smaller than Desktop, and the majority of the customers use mobile), the chevron which rotates when the question is opened, moves horizontally, and that doesn't look good at all. I tried searching for solutions but I couldn't find anything. This is my last resort.
Here's a video of my problem: https://gfycat.com/shortripeblackwidowspider
And heres the CSS:
.faq {
border-radius: 15px;
overflow: hidden;
}
.faq .tab-toggle {
display: none;
}
.faq .tab-label {
background-color: black;
display:flex;
justify-content: space-between;
padding:1em;
font-weight: normal;
color:white;
}
.faq .tab-label:hover {
background-color: rgb(88, 88, 88);
}
.faq .tab-toggle:checked ~ .tab-label {
background-color: rgb(88, 88, 88);
}
.faq .tab-label::after {
content:'\276F';
transition: all .4s;
}
.faq .tab-toggle:checked ~ .tab-label::after {
transform: rotate(90deg);
}
.faq .tab-content {
background-color: white;
max-height: 0px;
overflow: hidden;
transition: all .4s;
padding: 0 1em;
}
.faq .tab-toggle:checked ~ .tab-content
{
max-height: 100vh;
padding: 1em;
}
You need to use transform-origin to ensure your element pivots around it's center, add:
transform-origin: 0 50%;
to your .faq .tab-label::after styles.

How to center content and keep the indicated size in CSS Grid? [duplicate]

This question already has answers here:
Centering in CSS Grid
(9 answers)
Closed 4 years ago.
I would like to create a grid of squares with their content centered:
#all {
display: grid;
grid-template-rows: 200px;
grid-template-columns: 200px 20px 200px;
}
.disp {
background-color: black;
color: white;
border-color: gray;
border-width: 10px;
border-style: solid;
#import url('https://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800');
font-family: 'Open Sans', sans-serif;
font-size: 120px;
font-weight: bolder;
padding: 0 20px 0 20px;
align-self: center;
justify-self: stretch;
}
<div id="all">
<span class="disp hour">9</span><span class="sep"> </span><span class="disp min">27</span>
</div>
I cannot make the 9 centered and preserve the 200px width at the same time. Replacing the last CSS line with justify-self: center; does center the 9, but also reduces the width of the cell to make it fit. The width of the grid is preserved.
How to say "center and expand to the width of the cell" in CSS Grid?
All you need is text-align:center
Add this in your CSS code:
If you want only 9 centered:
.hour {
text-align:center;
}
If you want both centered:
.disp {
text-align:center;
}
Tested in Chrome Inspect Element.
Same trick works with the .min class too.
Hope this helps.
This will work:
.hour {
text-align:center;
}

Button-position in DIV

I came back to html&css coding after 10 years or something and quite a lot changed. I feel like Fred Flintstone in Black Mirror universe.
So I have maybe a little stupid question - how to position button in the middle of the div. I mean I text-aligned center in css. But horizontally its still in the top border. Ss below, I just made border of the div in PS to show how its built. Its also responsive so the row with the button go above row with the text when using phone.
Picture in here
.ghost-button {
display: inline-block;
padding: 15px 30px;
color: #fffff;
font-family: Inconsolata, monospace;
letter-spacing: 3px;
margin: 15px;
border-radius: 15px;
border: 2px solid #fff;
text-align: center;
outline: none;
text-decoration: none;
transition: background-color 0.2s ease-out,
color 0.2s ease-out;
}
Please try with below code snippet
.ghost-button {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: auto;
}
Thanks!

Unexpected behavior of flex-positioned elements when manipulating text appearance on hover

I've been playing around with the flexbox properties to see what they can do:
display: flex;
flex-grow: n;
align-items: center;
justify-content: center;
However I've run into some unexpected behavior, and I'm hoping someone here can clarify for me. Manipulate one of the following properties of a flex-positioned element on the hover pseudo-selector:
text-size
font-weight
text-shadow
The elements' relative weights appear to be remeasured, so the elements' positions jump around. If you repeatedly hover back and forth between the top two or bottom two elements, you can reduce their weights to essentially zero. A jsfiddle to illustrate:
https://jsfiddle.net/b8bjv70f/8/
(And one with twice as many items: https://jsfiddle.net/b8bjv70f/9/)
And the corresponding code:
[HTML]
<div class="container">
<div class="item one">
Item 1
</div>
<div class="item two">
Item 2
</div>
<div class="item three">
Item 3
</div>
</div>
[CSS]
div.container {
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
/* */
display: flex;
flex-direction: column;
justify-content: center;
}
div.item {
background-color: rgbA(0, 0, 0, 0.05); outline: 1px solid white;
/* */
display: flex;
flex-grow: 1;
align-items: center;
justify-content: center;
}
div.item a {
padding: 1em 2em;
font-family: 'sans-serif'; font-size: 1em;
text-transform: uppercase; text-decoration: none;
color: rgbA(150, 150, 150, 1.00); background-color: rgbA(255, 255, 255, 1.00); outline: 1px solid white;
}
div.one a:hover { text-shadow: 0 0 1em black; }
div.two a:hover { font-weight: bold; }
div.three a:hover { font-size: 1.5em; }
I didn't have the chance to test multiple environments before posting this question, but it turns out I can only to reproduce the odd behavior on Chrome OS, specifically build 48.0.2564.48 beta 64-bit. Standard Chrome 47 on Windows has no issues.
View the following recording to see what I'm seeing:
https://drive.google.com/file/d/0B6dJsjhml2eYb0thREc5S2J3Tnc/view
At this point I believe the issue is specific to Chrome OS or the Chrome beta channel. If I have the time, which is unlikely, I will investigate until I feel confident in submitting a Chromium issue to Google Code... otherwise, hopefully someone else will see this and pick up where I've left off.
I'll mark this as the accepted answer tomorrow, barring anyone else contributing a more comprehensive resolution in the meantime.

solve this style problem for IE6 and IE7

First i will show you the problem, wich only happens on IE6/IE7
As you can see, when the length of the innerHtml it's not long, no problem; but when it's 'longer' the sprite set as bg image gets repeated and the text jumps to the next line...
now, the CSS
.contButton {
list-style: none;
float: right;
}
.contButton p {
float: left;
display: inline; /*For ignore double margin in IE6*/
margin: 0 0 0 10px !important;
}
.contButton a {
text-decoration: none !important;
float: left;
color: #FFF;
cursor: pointer;
font-size: 14px !important;
line-height: 21px;
font-weight: bold !important;
}
.contButton span {
margin: 0px 10px 0 -10px;
padding: 3px 8px 5px 18px;
position: relative; /*To fix IE6 problem (not displaying)*/
float:left;
}
/*ESTADO NORMAL AZUL*/
.contButton p a {
background: url(../nImg/spriteBotones.png) no-repeat right -214px;
_background: url(../nImg/spriteBotones.gif) no-repeat right -214px;
color: #FFF;
}
.contButton p a span {
background: url(../nImg/spriteBotones.png) no-repeat left -214px;
_background: url(../nImg/spriteBotones.gif) no-repeat left -214px;
}
And the Html:
<div class="">
....
<div class="contButton mt10">
<p><a tabindex="" title="acceder" href="#"><span>ver disponibilidad</span></a></p>
</div>
...
</div>
This is the bg Image.
![the sprite][2]
Tried with:
<!--[if IE lte 7]>
<style type="text/css">
/*
.contNombrePrecioHtl .contButton p a{ height:20px; }
.contNombrePrecioHtl .contButton p a span{ height:20px; width:auto; } */
</style>
<![endif]-->
But that didn't solve the problem...
PS: class="mt10" it's only a margin-top:10px;
Any idea how to solve this for the glorious IE6/7?
Try adding white-space: nowrap to .contButton.
change this:
.contButton span {
margin: 0px 10px 0 -10px;
padding: 3px 8px 5px 18px;
position: relative; /*To fix IE6 problem (not displaying)*/
float:left;
white-space: nowrap;
}
I don't think it is a problem with either IE versions, it's probably just the newer browsers being less strict about this particular thing. I haven't tested anything, but "display:inline-block" has helped me sometimes. Still it doesn't seem like the most effective solution. The width seems to be limiting here, you shouldn't give the thing a fixed width if you don't want the text to "jump" into a second line...
can you try to add overflow: hidden to each parent of element with float: left? in this case you will have to add it to each div, p and a. I am not sure whether your actual code is optimal.
Moreover, float: left; and display: inline together make no sense. This might be the reason of the strange behaviour. Delete display: inline (remember about adding overflow: hidden to its parent) and it should work.
Haven't tested though.
UPDATE:
apparently as the author of the question mentions float:left + display: inline fixes IE6 double margin bug for floating elements.
defining dimensions for elements like p oder span is always somewhere between tricky and impossible, because they are inline elements. I'd recommend modifying the surrounding block element div.contButton.
In addition to height you should set overflow to hidden:
.contButton {
height:20px;
width:219px;
overflow: hidden;
}

Resources