Flex wrap and flex basis not working using CSS? - html

I am making this application, and I am using React. Here, I have many items, and I am dynamically adding them using React. Here's my code to help you understand better -
CSS:
.flexContainer {
display: flex;
flex-wrap: wrap;
}
.flexContainer .item {
padding: 5px;
border: 1px solid aqua;
flex: 1 1 50%;
}
.flexContainer .item {
display: block;
width: 500px;
text-align: center;
}
* {
box-sizing: border-box;
}
React:
<div class="flexContainer">
<div key={p.id} class="item">
<div class="item">
<p className="itemTitle">{p.title}</p>
<button onClick={() => addItem(p)}>
{alreadyAdded ? "Add again" : "Add to Cart"}
</button>
</div>
</div>
</div>
Here is a snippet of my items:
{
id: 7,
price: 50,
button: (
<button
onClick={() => {
alert("This function is not ready yet!!!!");
}}
>
Click!
</button>
),
title: "Apple",
},
So, when I run my program, I get this-
My Pic
I had taken another similar code from here
As you can see, the code I had taken from the other stackoverflow is working, but mine is not working. I've used the same technique to do for mine as well but it's not working.
What I want is, 2 of the items to come in a row, with 50% of width, and so on.
Currently, it's not working.
Please help me out, it would help out a ton!
Thanks.

in terms of your question for that row removing some of the the code will allow this to work.
.flexContainer .item {
display: block;
width: 500px;
}
This code is not really needed unless you have other reasons for it to be in your code but doesnt really play part of your question
As you will see from this link your row works
https://jsfiddle.net/ct96dL2o/1/
so from the original code you supplied this answer will work if you need a more elaborate answer you would need to show more code.

change your item style width
.flexContainer .item {
width:50% !important
}

If you are using bootstrap then use row and inside of that and two col-6 class and also checkout link of bootstrap grid
https://getbootstrap.com/docs/5.2/layout/grid/
<div class="container">
<div class="row">
<div class="col-6">
first
</div>
<div class="col-6">
second
</div>
</div>
</div>

Related

Css : Handle two flexbox that can overflow

I must handle a case where I want to display on one line a value, a category and the number of concerned item in the following format.
[ Item name ] ([ Path start ], [ Item count ])
Example : My item value (My category, 5)
My issue is that the item name and the path start can be too long. I listed the different case in the following drawing
And I can't find a way to handle all the case using CSS only. My biggest issue is to fix the size of the two flex-box when they are both overflowing.
The closest version I have now is the following (you can un-comment the max-width to see an other result I have):
Code:
.search_list {
padding: 5px;
display: flex;
/* only for exemple */
width: 400px;
border: solid 1px black;
}
.search_info {
display: flex;
flex: 1 1 30%;
/*max-width:30%; Get rid of this */
}
.search_info .flex {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.search_info .no_flex {
flex-grow: 0;
flex-shrink: 0;
}
.search_value {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
flex: 1 1 70%;
max-width: fit-content;
}
<div class="search_list" >
<div class="search_value">Short values</div>
<div class="search_info">
<div class="flex">(Category</div>
<div class="no_flex">, 1)</div>
</div>
</div>
<div class="search_list" >
<div class="search_value">Item value with a long long text that overlow</div>
<div class="search_info">
<div class="flex">(Category</div>
<div class="no_flex">, 1)</div>
</div>
</div>
<div class="search_list" >
<div class="search_value">Long category</div>
<div class="search_info">
<div class="flex">(The category will overflow for sure in this case</div>
<div class="no_flex">, 1)</div>
</div>
</div>
<div class="search_list" >
<div class="search_value">Long category and text that will both overflow</div>
<div class="search_info">
<div class="flex">(The category is very</div>
<div class="no_flex">, 1)</div>
</div>
</div>
But, as you can see, this is not the intended results
I tried many things but I can't find a correct solution and I don't want to do thinks like counting the number of character to do a manual overflow and I also would like to avoid JavaScript since this is for a project in Web-assembly and it's a bit annoying to use JavaScript

No jQuery : div onclick toggle div functions

I am jQuery-phobic and new to css. So, please do not recommend to use jQuery.
I have two questions with my code.
[solved]When I click any one of .active divs, others go down. How to fix it on top?
How to change display from none to active as I click the div? (the code I wrote only show the div as long as I 'click' it)
.wrap{
text-align: center;
}
.hidden{
background: grey;
display : none;
}
.active{
background : lightcoral;
display: inline-block;
vertical-align: top;
}
.active:active > .hidden{
display: block;
margin : 0 0;
}
<div class = 'wrap'>
<div class = 'active'>
<h2>1</h2>
<div class ='hidden' onclick='layer_toggle()'>a</div>
</div>
<div class = 'active'>
<h2>2</h2>
<div class ='hidden'>b</div>
</div>
<div class = 'active'>
<h2>3</h2>
<div class ='hidden'>c</div>
</div>
</div>
If the usual javascript function suits you.
function switch_active(e) {
if (e.querySelector('.hidden').style.display == 'none') {
e.querySelector('.hidden').style.display = 'block';
} else {
e.querySelector('.hidden').style.display = 'none';
}
}
.wrap{
text-align: center;
}
.hidden{
background: grey;
display : none;
}
.active{
background : lightcoral;
display: inline-block;
vertical-align: top;
}
<div class = 'wrap'>
<div class = 'active' onclick='switch_active(this)'>
<h2>1</h2>
<div class ='hidden'>a</div>
</div>
<div class = 'active' onclick='switch_active(this)'>
<h2>2</h2>
<div class ='hidden'>b</div>
</div>
<div class = 'active' onclick='switch_active(this)'>
<h2>3</h2>
<div class ='hidden'>c</div>
</div>
</div>
You can easily fix the vertical alignment by adding
"vertical-align: top;" to your ".active" class in your CSS.
You say you don't want to use jQuery. I don't know if you are also plain javascript-phobic but if not, I would suggest use simple javascript since that is the easiest way. If you want to keep focussing on CSS, one way to do it is to make them checkboxes or radio buttons (depending on the effect you are looking for), making the checkbox/radio button invisible and styling them as your divs.
Also, there is no reason to be jQuery-phobic. It can look a bit overwhelming but the basics are really not that difficult! :)

How to align both vertically and horizontally in CSS?

I have a box like this:
<section class="notes-list-box">
<div class="nn">
<div class="heading">Notes</div>
<div class="boxdescription">With our complete study notes, your homework is much easier.</div>
</div>
<div class="ttn">
<div class="heading">Things To Know</div>
<div class="boxdescription">Things to know provides additional information on every topic which enhance the knowledge of the students.</div>
</div>
<div class="vdos">
<div class="heading">Videos</div>
<div class="boxdescription">Reference videos on each topic provides experimental ideas and develops interactive learning technique.</div>
</div>
<div class="sqaa">
<div class="heading">Solved Question and Answer</div>
<div class="boxdescription">With 100's of solved questions solved, now you can easily prepare your exam for free.</div>
</div>
</section>
Adding little bit of styling make it looks like this:
I have tried using vertical-align like this:
.notes-list-box > div {
vertical-align: top;
}
and it works. But I don't know how to align vertical at bottom so that all the white space also comes to bottom.
So the white space below notes and solved question and answer white background comes till bottom.
I want to fill those gaps with white space:
Use flexbox.
I used this CSS:
.notes-list-box {
display: flex;
font-family: sans-serif;
}
.notes-list-box > div {
margin: 0 5px;
background-color: white;
}
.heading {
color: white;
font-weight: bold;
padding: 10px 2px;
text-align: center;
}
.boxdescription {
padding: 5px;
}
.nn .heading {
background-color: #61B5DF;
}
.ttn .heading {
background-color: #41AF43;
}
.vdos .heading {
background-color: #FF8A00;
}
.sqaa .heading {
background-color: #FF1F2D;
}
See the result on JSfiddle.
The easiest way to do what you are trying to do is by using the display: table CSS properties.
JS Fiddle Here
HTML
<div class="table">
<div class="table-row">
<div class="heading table-cell">Notes</div>
<div class="heading table-cell">Things To Know</div>
<div class="heading table-cell">Videos</div>
<div class="heading table-cell">Solved Question and Answer</div>
</div>
<div class="table-row">
<div class="table-cell">With our complete study notes, your homework is much easier.</div>
<div class="table-cell">Things to know provides additional information on every topic which enhance the knowledge of the students.</div>
<div class="table-cell">Reference videos on each topic provides experimental ideas and develops interactive learning technique.</div>
<div class="table-cell">With 100's of solved questions solved, now you can easily prepare your exam for free.</div>
</div>
</div>
CSS
.table {
display: table;
}
.table-row {
display: table-row;
}
.table-cell {
display: table-cell;
border: 1px solid;
}
.heading {
vertical-align: middle;
text-align: center;
}
Here is an update with styling similar to yours.
Another alternative using jquery. Here is the fiddle.
JQUERY
$('.parentheight').each(function(){
var maxdivheight = 0;
$('.childheight',this).each(function(){
var divheight = $(this).height();
// compare height
if ( divheight > maxdivheight ) {
maxdivheight = divheight;
} else { }
});
// set all divs to max height
$('.childheight',this).height(maxdivheight);
});
HTML
<section class="notes-list-box parentheight">
<div class="alignbox nn childheight">
<div class="heading">Notes</div>
<div class="boxdescription">With our complete study notes, your homework is much easier.</div>
</div>
<div class="alignbox ttn childheight">
<div class="heading">Things To Know</div>
<div class="boxdescription">Things to know provides additional information on every topic which enhance the knowledge of the students.</div>
</div>
<div class="alignbox vdos childheight">
<div class="heading">Videos</div>
<div class="boxdescription">Reference videos on each topic provides experimental ideas and develops interactive learning technique.</div>
</div>
<div class="alignbox sqaa childheight">
<div class="heading">Solved Question and Answer</div>
<div class="boxdescription">With 100's of solved questions solved, now you can easily prepare your exam for free.</div>
</div>
</section>
CSS
.alignbox {
float: left;
width: 24%;
border: 1px solid red;
}
I got it working by setting everything to 100% height like so: http://jsfiddle.net/sur38w6e/
Your html was untouched.
.notes-list-box>div{
float:left;
width:120px;
background-color:yellow;
margin: 5px;
height:100%;
overflow:auto;
}
.heading{
background-color:red;
}
.notes-list-box{
background-color:green;
overflow:auto;
height:100%;
}
body,html{
height:100%;
}

:before selector not adding content

I'm trying to add an image before other images. It should be before every image except the first one. So thought of adding it like:
.class3:not(:first-child):before {
background-image: url('image.jpg');
width: 2rem;
height: 2rem;
}
The html gets added dynamically through Backbone and uses Handlebars. It looks something like this:
<div class="class1">
<div class="class2">
{{#each item}}
<div class="class3" style="background-image: url('{{image1}}')">
<p>{{text}}</p>
<img class="dialog-popup-rewards" src={{image2}} />
</div>
{{/each}}
</div>
</div>
But nothing gets added. I tested the before statement with less complexity. Adding a before to all of the divs:
.class3:before {
background-color: black;
width: 2rem;
height: 2rem;
}
And it never gets added. What am I missing? I rewrote the code for the sake of clarity, so I might have slipped in some syntax errors.
Would it be better to try this with a Handlebars helper?
Thank you in advance.
You need to include a content property in your :before css block. It can be just an empty string. Try this for your simplified example:
.class3:before {
content: '';
background-color: black;
width: 2rem;
height: 2rem;
}

Problem with position in CSS and html

I'm having a problem with a CSS. I have a little image with the problem:
The first part show what happend when in the block "Contenido" it's empty, and the second part when it has something. The problem? The second botton use a break and appear under of the other bootom. I have tried a pair of styls, but i can't get with one that work. What i have in CSS and html?
<div class="nav-pad">
<button id="previous_left" class="button_previous">
<img src="/images/left-arrow.png"/>
</button>
<div id=info_left>
Here we could put some info but it give a problem
</div>
<button id="next_left" class="button_next">
<img src="/images/right-arrow.png"/>
</button>
</div>
And in the CSS i have this one:
.nav-pad{
padding: 0.2em;
postion: relative;
}
.button_next{
float:right;
right:0
}
.button_previous{
left:0;
}
Any clue what i'm doing bad?
Thanks a lot!
Roberto
I edited your HTML and CSS a little bit but I made it work,
I am still not sure what you are trying to get here.
The code may need to change in order to get better results
click on the link bellow to see an example of the answer
http://jsfiddle.net/TkNDS/2/
Did u gave any width for the nav-pad div, if yes then remove it.
Else try adding overflow:auto to the nav-pad class..
Can you please give us a sample page with this style and content, so that we can figure out the exact error..
.nav-pad {}
.button-next { display:inline-block }
.button-previous { display:inline-block }
#info-left { display:inline-block }
To accomplish the same layout, What I will do is:
.nav-pad{
padding: 0.2em;
clear:both;
overflow: hidden;
}
.button_next{
float:right;
}
.button_previous{
float: left;
}
#info_left{
float: left;
}
Well i have solved. The help that you give me, give me a clues to solve it:
With this html:
<div class="nav-pad">
<button id="previous_left" class="button_previous">
<img src="http://www.baltimark.com/_/rsrc/1234983635623/public/tech/stackoverflow-logo-250.png"/>
</button>
<div id="info_left">
Here we cdfgdfould put some info but it give a problem
</div>
<button id="next_left" class="button_next">
<img src="http://www.baltimark.com/_/rsrc/1234983635623/public/tech/stackoverflow-logo-250.png"/>
</button>
</div>
and this css:
.nav-pad{
padding: 0.2em;
border: solid 1px green;
overflow: auto;
}
.button_previous{
float: left;
width : 15%;
}
.button_next{
float:right;
width : 15%;
}
#info_left {
float: left;
width : 70%;
}
Thanks a lot!