Flex & space-around are intersecting the content - css

I have a container that's scrolling horizionally with checkboxes in it. When a box is checked, a text apears. I want to keep the functionality of the items growing from both sides. The thing is that when some checkboxes are checked, the first one is hidden and you cannot scroll to it:
.container {
white-space: nowrap;
text-align: center;
display: flex;
overflow-x: scroll;
justify-content: space-around;
align-items: stretch;
}
.item {
transition: 0.5s ease;
height: 40px;
justify-content: center;
text-align: center;
}
label {
font-size: 0;
transition: 0.5s ease;
}
input:checked~label {
font-size: 20px;
}
<div class="container">
<div class="item">
<input type="checkbox" id="1"></input>
<label for="1">1111111111111111111111111</label>
</div>
<div class="item">
<input type="checkbox" id="1"></input>
<label for="2">22222222222222222222222222222222222</label>
</div>
<div class="item">
<input type="checkbox" id="1"></input>
<label for="3">333333333333333333333333333333</label>
</div>
</div>
Is there a way to keep the transition coming from the center, but still let the user scrolling all the way to the start and the end?
Thanks!

Just done by 2 actions:
Remove the justify-content: space-around; from your .container
Add margin:auto; to your .item.
(Pay attention, className got replaced by class, as the CSS does not recognize the className)
DEMO
.container {
white-space: nowrap;
text-align: center;
display: flex;
overflow-x: scroll;
/*justify-content: space-around;*/
align-items: stretch;
}
.item {
transition: 0.5s ease;
height: 40px;
justify-content: center;
text-align: center;
margin:auto;
}
label {
font-size: 0;
transition: 0.5s ease;
}
input:checked~label {
font-size: 20px;
}
<div class="container">
<div class="item">
<input type="checkbox" id="1" />
<label for="1">1111111111111111111111111</label>
</div>
<div class="item">
<input type="checkbox" id="1" />
<label for="2">22222222222222222222222222222222222</label>
</div>
<div class="item">
<input type="checkbox" id="1" />
<label for="3">333333333333333333333333333333</label>
</div>
</div>

Ooh!!
That's a good one..
It took months from me to solve before.
It's very easy and tricky bro.
Just make your justify-content: flex-start; and then add this flex-grow: 1; for your children which are .item which will seams like a space-around effect.
That's the code:
.container {
white-space: nowrap;
text-align: center;
overflow-x: scroll;
display: flex; align-items: stretch;
justify-content: flex-start; /* ••• this one ••• */
}
.item {
transition: 0.5s ease;
height: 40px; text-align: center;
display: flex; justify-content: center;
flex-grow: 1; /* ••• and this one ••• */
}
label { font-size: 0; transition: 0.5s ease; }
input:checked~label { font-size: 20px; }
<div class="container">
<div class="item">
<input type="checkbox" id="1"></input>
<label for="1">1111111111111111111111111</label>
</div>
<div class="item">
<input type="checkbox" id="2"></input>
<label for="2">22222222222222222222222222222222222</label>
</div>
<div class="item">
<input type="checkbox" id="3"></input>
<label for="3">333333333333333333333333333333</label>
</div>
</div>

justify-content: space-around; from the container and use justify-content: space-between; instead.

Related

How to make adaptive centered div?

What I have now:
display: flex;
align-items: center;
flex-direction: column;
How it looks with error:
What's the target:
I can do it with
margin-left: *X*px, but it is not the way I want to solve it.
May I do it with some flex properties ?
It is important to do it without bootstrap and grid.
This could be a possible solution: Center the box with flexbox and display the errors with position: absolute;. You need to take care of responsive optimization if you want to use it on a wider range of devices.
* {
box-sizing: border-box;
}
.container {
display: flex;
align-items: center;
justify-content: center;
}
.box {
border: 1px solid black;
padding: 10px;
width: 400px;
}
.field {
position: relative;
}
.input {
margin-bottom: 10px;
width: 100%;
}
.error {
position: absolute;
left: 105%;
top: 0;
width: 200px;
}
<div class="container">
<div class="box">
<div class="field">
<input class="input" type="text">
<div class="error">error 1</div>
</div>
<div class="field">
<input class="input" type="text">
<div class="error">error 2</div>
</div>
<button>Send</button>
</div>
</div>

justify-content is not working when div is wrapped in 'flex' parent container

I have a container that has been set 'flex' but I am now trying to pass 'justify-content: space-evenly' to the button container but it doesn't seem to work. Any ideas?
.full-width {
width: 100%;
}
.banner__container {
display: flex;
justify-content: space-evenly;
background-color: #f6f6f6;
}
.button__container {
display: flex;
justify-content: space-evenly;
}
<div class="full-width">
<div class="banner__container">
<div>
<p>
Lorem
</p>
</div>
<div class="button__container">
Yes
No
<button>Close</button>
</div>
</div>
</div>
Everything is working fine. You just have to set a proper flex value to the child divs of .banner__container.
I have done that by setting flex: 1; to .banner__container div. This will give equal width for each divs inside .banner__container
.full-width {
width: 100%;
}
.banner__container {
display: flex;
justify-content: space-evenly;
background-color: #f6f6f6;
}
.banner__container div {
flex: 1;
}
.button__container {
display: flex;
justify-content: space-evenly;
}
<div class="full-width">
<div class="banner__container">
<div>
<p>
Lorem
</p>
</div>
<div class="button__container">
Yes
No
<button>Close</button>
</div>
</div>
</div>
You can set button__container to display:inline.
.full-width {
width: 100%;
}
.banner__container {
display: flex;
justify-content: space-evenly;
background-color: #f6f6f6;
}
.button__container {
display: inline;
justify-content: space-evenly;
}
.button__container a, .button__container button {
margin-right: 20px;
}
<div class="full-width">
<div class="banner__container">
<div>
<p>
Lorem
</p>
</div>
<div class="button__container">
Yes
No
<button>Close</button>
</div>
</div>
</div>

how to fix a problem with radio input costumized with css?

I am doing an input radio customized with everything I learned about it step by step.
When I click on the older/standard radio inputs (without hiding them yet) everything is fine, I click on the old radio buttons and the new customized respective is checked.
But when I click on the newly customized radio buttons, they don't work as supposed to be.
The problem is that when I click the 2nd and 3rd customized radio it checks the 1rst automatically.
I really need your help because I don't know where I am failing. I already look from examples codes on the internet and it was exactly what I did so I don't really understand where is the problem that I should correct.
.content {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.items {
display: flex;
}
label {
display: flex;
align-items: center;
margin-left: 1rem;
}
.radio__span {
width: 1.2rem;
height: 1.2rem;
border-radius: 50%;
border: 3px solid #049372;
margin-right: .5rem;
display: flex;
align-items: center;
justify-content: center;
}
.radio:checked+.radio__span::before {
content: "";
display: block;
width: .8rem;
height: .8rem;
border-radius: 50%;
background-color: #049372;
}
/* .radio{
width:0;
height:0;
opacity:0;
visibility:hidden;
} */
<div class="content">
<div class="items">
<label for="chk">
<input type="radio" name="dev" class="radio" id="chk">
<span class="radio__span"></span>
<p>Frontend</p>
</label>
<label for="chk">
<input type="radio" name="dev" class="radio" id="chk">
<span class="radio__span"></span>
<p> Backend</p>
</label>
<label for="chk">
<input type="radio" name="dev" class="radio" id="chk">
<span class="radio__span"></span>
<p>Fullstack</p>
</label>
</div>
</div>
I expect to click on the new radio buttons and be checked.
IDs must be unique. Once fixed, you need to then update your label elements to point to the new IDs. Fix that and it works fine.
.content {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.items {
display: flex;
}
label {
display: flex;
align-items: center;
margin-left: 1rem;
}
.radio__span {
width: 1.2rem;
height: 1.2rem;
border-radius: 50%;
border: 3px solid #049372;
margin-right: .5rem;
display: flex;
align-items: center;
justify-content: center;
}
.radio:checked+.radio__span::before {
content: "";
display: block;
width: .8rem;
height: .8rem;
border-radius: 50%;
background-color: #049372;
}
/* .radio{
width:0;
height:0;
opacity:0;
visibility:hidden;
} */
<div class="content">
<div class="items">
<label for="chk1">
<input type="radio" name="dev" class="radio" id="chk1">
<span class="radio__span"></span>
<p>Frontend</p>
</label>
<label for="chk2">
<input type="radio" name="dev" class="radio" id="chk2">
<span class="radio__span"></span>
<p> Backend</p>
</label>
<label for="chk3">
<input type="radio" name="dev" class="radio" id="chk3">
<span class="radio__span"></span>
<p>Fullstack</p>
</label>
</div>
</div>
That is happening because of your for attribute which is looking for an input with an Id named chk, as you have got all three checkboxes with the same ID, it is checking the first...
The following would work:
.content {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.items {
display: flex;
}
label {
display: flex;
align-items: center;
margin-left: 1rem;
}
.radio__span {
width: 1.2rem;
height: 1.2rem;
border-radius: 50%;
border: 3px solid #049372;
margin-right: .5rem;
display: flex;
align-items: center;
justify-content: center;
}
.radio:checked+.radio__span::before {
content: "";
display: block;
width: .8rem;
height: .8rem;
border-radius: 50%;
background-color: #049372;
}
<div class="content">
<div class="items">
<label for="chk">
<input type="radio" name="dev" class="radio" id="chk">
<span class="radio__span"></span>
<p>Frontend</p>
</label>
<label for="chk1">
<input type="radio" name="dev" class="radio" id="chk1">
<span class="radio__span"></span>
<p> Backend</p>
</label>
<label for="chk2">
<input type="radio" name="dev" class="radio" id="chk2">
<span class="radio__span"></span>
<p>Fullstack</p>
</label>
</div>
</div>

CSS how to break flow in just one direction

Motivation
CSS does not allow styling the options of an html select component, so I'm building a custom "component" to do so.
The challenge is this
Along the x-direction it needs to keep the inline flow.
Along the y-direction, it needs to be able to overlap content below it.
Here's an example (does not break flow)
http://jsfiddle.net/4tyvLmqn/1
html
<div class="dropdown">
<div class="item selected">asdflkhj asdf adf</div>
<div class="item">b</div>
<div class="item">c</div>
<div class="item">ad fds adfs dfsa fsd fasd</div>
<div class="item">e</div>
</div>
<hr />
This should not move down
<hr />
JS (click handler - there may be a pure CSS way to do this)
document.querySelector('.dropdown .selected').addEventListener('click', e => {
e.target.parentElement.classList.toggle('isOpen')
})
SCSS
.dropdown {
display: inline-flex;
flex-direction: column;
justify-content: center;
align-items: stretch;
user-select: none;
border: 1px solid gray;
}
.item {
visibility: hidden;
height: 0;
&.selected {
visibility: visible;
cursor: pointer;
height: auto;
}
.isOpen & {
visibility: visible;
height: auto;
}
}
Simply keep a fixed height then adjust overflow (visible/hidden). You can also consider a max-height in case you will have a lot of elements.
const dropdown = document.querySelector('.dropdown');
dropdown.addEventListener('click', e => {
dropdown.classList.toggle('isOpen')
});
.dropdown {
display: inline-flex;
flex-direction: column;
justify-content: flex-start;
align-items: stretch;
user-select: none;
border: 1px solid gray;
height: calc(1em + 2 * 0.5em);
overflow: hidden;
}
.isOpen {
overflow: visible;
}
.background {
background-color: #fff;
z-index: 1;
border: inherit;
margin: -1px;
max-height: calc(5*(1em + 2 * 0.5em));
}
.isOpen .background {
overflow:auto;
flex-shrink:0;
}
.item {
padding: 0.5em;
cursor: pointer;
}
<div class="dropdown">
<div class="background">
<div class="item">Select an Item</div>
<div class="item">b</div>
<div class="item">c</div>
<div class="item">ad fds adfs dfsa fsd fasd</div>
<div class="item">e</div>
<div class="item">ad fds adfs dfsa fsd fasd</div>
<div class="item">e</div>
<div class="item">ad fds adfs dfsa fsd fasd</div>
<div class="item">e</div>
</div>
</div>
<hr /> This should not move down
<hr />
Snippet here seems not to be working with CSS3. I have created a JSFiddle for this: http://jsfiddle.net/42mpa6wn/
Javascript:
document.querySelector('.dropdown .selected').addEventListener('click', e => {
e.target.parentElement.parentElement.classList.toggle('isOpen')
})
CSS:
.dropdown {
display: inline-flex;
flex-direction: column;
justify-content: center;
align-items: stretch;
user-select: none;
border: 1px solid gray;
background: white;
height:25px;
width:200px;
position:relative;
}
.isOpen .itemlist{
position: absolute;
background: white;
width:inherit;
top:calc(100% - 20px);
}
.item {
visibility: hidden;
height: 0;
&.selected {
visibility: visible;
cursor: pointer;
height: auto;
}
.isOpen & {
visibility: visible;
height: auto;
}
}
HTML:
<hr />
This should not move
<hr />
This should not move
<hr />
<div class="dropdown">
<div class="itemlist">
<div class="item selected">asdflkhj asdf adf</div>
<div class="item">b</div>
<div class="item">c</div>
<div class="item">ad fds adfs dfsa fsd fasd</div>
<div class="item">e</div>
</div>
</div>
<hr />
This should not move down
<hr />
Basically, you want .dropdown to be relative to the page, where we put it. also, you want .itemlist to be in absolute position when the dropdown action is triggered. You still need to beautify it

get image and caption into one flex item?

AI want to put an image and its caption into one flex-item and have several such flex-items in one flex-container. I want the image to be above its caption. But what happens is that each caption is beside its image. How do I get them one above the other?
I tried putting the captions into another flex-container below the images. But when the screen size is less wide, the images stack, then the captions stack instead of being image, then its caption, image, then its caption.
<div class="flex-item-popup" id="popup">
<div class="close"><i class="fa fa-2x fa-times-circle"></i></div>
<h2></h2>
<div class='text'></div>
<div class="videos"></div>
<div class="flex-container images"></div>
<div class="flex-container captions"></div>
</div>
css:
#popup {
width: 90%;
height: auto;
padding: 20px;
border-radius: 10px;
background-color: black;
color: white;
}
#popup .close {
/*position: absolute;
right: 10px;
top: 10px;*/
float: right;
color: white;
opacity: 1;
}
#popup .close:hover {
opacity: .7 !important;
}
.images, .captions {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
align-content: flex-start;
align-items: flex-start;
}
.images {
margin-top: 20px;
}
.images .flex-item, .captions .flex-item {
display: flex;
flex-grow: 0;
flex-shrink: 0;
flex-basis: 300px;
}
Look into <figure> and <figcaption> HTML5 tags. I am pretty certain that is going to help you and it is what you are looking for.
Here is updated fiddle link.
And the SO code snippet here...
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: flex-start;
align-items: center;
}
.flex-item {
/*width: 350px;
height: null;*/
flex-grow: 1;
flex-shrink: 0;
flex-basis: 200px;
justify-content: flex-start;
display: flex;
padding-left: 20px;
}
.flex-item img {
cursor: pointer;
}
<div class="flex-container images">
<div class="flex-item">
<figure>
<img src="http://newsinteractive.post-gazette.com/hbcu/morehouse/img/grads.jpg" />
<!--<div style="clear: both;"></div>-->
<figcaption>Our grads.</figcaption>
</figure>
</div>
<div class="flex-item">
<figure>
<img src="http://newsinteractive.post-gazette.com/hbcu/morehouse/img/building.jpg" />
<figcaption>A building</figcaption>
</figure>
</div>
<div class="flex-item">
<figure>
<img src="http://newsinteractive.post-gazette.com/hbcu/morehouse/img/campus.jpg" />
<figcaption>The campus.</figcaption>
</figure>
</div>
<div class="flex-item">
<figure>
<img src="http://newsinteractive.post-gazette.com/hbcu/morehouse/img/trio.jpg" />
<figcaption>Three people</figcaption>
</figure>
</div>
</div>
Hope this helps

Resources