How can I make my flex divs overflow to the next line? - css

In my website, I have a search which outputs its results in card format, like this:
However, when multiple results are displayed, they don't overflow to the next line, but instead the cards stay on the same line and get skinnier, and the content inside the cards looks ugly. It becomes more like this:
.
The div-card code:
<div class="card">
<a href="#">
<div class="image">
<div class="title">
</div>
</div>
</a>
<div class="description">
<h5>Title</h5>
<p>Description</p>
</div>
</div>
and the CSS for the card div (I don't think the image and description div css matter here):
.card
{
height:18em;
width:14em;
margin: 10px;
display: flex;
flex-wrap: wrap;
flex-direction:column;
position:relative;
border-radius:16px;
overflow:hidden;
box-shadow: 15px 15px 27px #e1e1e3, -15px -15px 27px #ffffff;
}
As you can see, the divs are flex objects, and I've added the flex-wrap: wrap property, but it doesn't wrap. I've also tried other solutions, such as display: inline or display: inline-block or float: left, none of which work (I've obtained all these solutions from online, mostly Stack Overflow, but since they don't work I had to make another post about this issue).
Could someone help me fix the issue? Remember, the results are output by a search, so it's not possible to manually fix the wraps, by, for example, doing a line break.
Thanks!

the problem is that you are making a single card flex. this is not how flex works the container div in which all of your search result cards appears need to be flex so if we have a div like this
<div class="continer">
<item1 class="card">
<item2 class="card">
<item3 class="card">
<item4 class="card">
</div>
then you need to do this
.container{
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: <as you like it is optional>;
.... and other properties as you like
}

Related

Bulma level item height is too small

How could i make sure my <p class="title"></p> and
<p class="subtitle"></p> are displayed underneath of eachother because right now they are being displayed next to eachother
You can make use of flexbox, it'll definitely solve your problem.
Example
If you want to arrange two items underlying with each other:
.container {
display: flex;
flex-direction: column;
}
<div class="container">
<p class="title">Hello</p>
<p class="subtitle">World</p>
</div>
If you want to arrange two items next to each other.
.container {
display: flex; /*by default it will align items in one row*/
}
<div class="container">
<p class="title">Hello</p>
<p class="subtitle">World</p>
</div>

In CSS, display inline-block is acting like display block

What I want to have is two divs side-by-side and within one of them is an image and in the other is two divs, one above the other.
This happens to be a Wordpress theme, but I'm pretty sure this is basic CSS question.
The Wordpress stack exchange told me it was off-topic.
Call the left div #divL and the right div #divR.
I found an answer on SO mentioning that I should set display of #divL and #divR to
inline-block. I can get this to work on a test html file that I created in isolation but it doesn't work in the wordpress header. Specifically the divs in the wordpress header #divL and #divR act as if they had display: block rather than being positioned side-by-side.
Changing them to display: inline does put them side-by-side but then it
doesn't work to stack two divs within #divR.
I'll replicate here some of the code in the Wordpress header. Note that I'm going to simplify this by omitting the stacked divs inside #divR, because the symptom is obvious without that.
the following is what I'm using to try to get #divL and #divR to display side-by-side.
#divL { display: inline-block; }
#divR { display: inline-block; }
<header class="site-header">
<div class="wrap">
<div class="title-area">
<div id="divL">
<img id="logo-img" class="attachment-full size-full">
</div>
<div id="divR">Some text that should go on right</div>
</div>
<nav> .... </nav>
</div>
</header>
But they display one above the other.
Note that this actually does work to get them side-by-side, but then the
stacked divs inside #divR don't work as intended:
#divL { display: inline; }
#divR { display: inline; }
<header class="site-header">
<div class="wrap">
<div class="title-area">
<div id="divL">
<img id="logo-img" class="attachment-full size-full">
</div>
<div id="divR">Some text that should go on right</div>
</div>
<nav> .... </nav>
</div>
</header>
There is a lot of CSS on these other elements but I'm not sure which of it is important to this question so I'll wait for someone to comment and tell me what I should include.
As I wrote in my comment, you should set widths for those ìnline-blocks, that should basically do what you are after.
But as an alternative you can also use display: flex; on the container DIV. This can be done rather simple, but in the snippet below I added some additional settings to define a certain width for the two DIVs and to center the contents in these DIVs both horizontally and vertically (by also making the child elements flexboxes with flex-directon: column. For the rest of the settings see the code below.
.title-area {
display: flex;
justify-content: center;
align-items: stretch;
}
.title-area>* {
width: 40%;
border: 1px solid green;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
<header class="site-header">
<div class="wrap">
<div class="title-area">
<div id="divL">
<img id="logo-img" src="https://placehold.it/200x150/fa0" class="attachment-full size-full">
</div>
<div id="divR">Some text that should go on right</div>
</div>
<nav> .... </nav>
</div>
</header>
Here's an example of what would work:
<header class="site-header">
<div class="wrap">
<div class="title-area">
<div id="divL">
<img id="logo-img" class="attachment-full size-full" />
</div>
<div id="divR">
<div id="divTR">Some text that should go on top right</div>
<div id="divBR">Some text that should go on bottom right</div>
</div>
</div>
<nav>....</nav>
</div>
</header>
And the CSS:
#divL {
display: inline-block;
width: 49%;
}
#divR {
display: inline-block;
width: 49%;
}
But also Jon P is right; it might be worth your while to investigate one of the newer methods for dynamically spacing and sizing content.

Stick div at the bottom of equally height cards

I'm using Bulma have a column of cards which need to have the same height regardless of the content.
To achieve so I have created the following class
.equal-height
display: flex
flex-direction: column
height: 100%
My HTML looks like
<div class='columns is-multiline'>
<div class='column is-one-fifth'>
<div class='card equal-height'>
<div class='card-content'>
# CONTENT GOES HERE
</div>
<div class='card-footer'>
# FOOTER GOES HERE
</div>
</div>
</div>
<div class='column is-one-fifth'>
<div class='card equal-height'>
<div class='card-content'>
# CONTENT GOES HERE
</div>
<div class='card-footer'>
# FOOTER GOES HERE
</div>
</div>
</div>
</div>
Which produces something like
Now I'm trying to make the card-footer to stick at the bottom of the card like below.
I have tried a few things with flex but they don't really make sense.
Any ideas on how I may do it?
Add "flex: auto;" to '.card-contents' to make the card-footer to stick at the bottom of the card. Here is the working jsfiddle link.
.equal-height {
display: flex;
flex-direction: column;
height: 100%;
}
.equal-height .card-content {
flex: auto;
}
Add this CSS
.card-footer {
margin-top: auto;
}
working demo : https://jsfiddle.net/baLg7940/

Firefox does not scale images inside flexbox

Edit: Here is a codepen and a screenshot showcasing the issue
Codepen
I 'm trying to center align an image using flexbox, maintain its aspect ratio, and also have transparent background below it (check images below).
I approached the problem by creating a full-height-width parent container, containing 4 rows (first is title, second is logo, third are social links, fourth is a chevron-down-icon)
So parent container has flex-direction: row containing these 4.
Inside the logo container I have a parent column container with flex-direction: column, containing 2 empty divs with flex: 1 and my app logo.
Mixins
=full-height-width
width: 100%
height: 100%
=fill($background-color: null)
flex: 1
display: flex
height: 100%
=flex-center-contents
display: flex
justify-content: center
align-items: center
// grid mixins
=row($z-index: auto)
display: flex
flex-direction: column
z-index: $z-index
flex-wrap: nowrap
=column($z-index: auto)
display: flex
flex-direction: row
z-index: $z-index
flex-wrap: nowrap
Styles
// Grid implementation
.fill
+fill($logo-background-color)
z-index: $not-animated-background-z-index
.parent-row-container
+full-height-width()
+row($not-animated-background-z-index)
.parent-column-container
+full-height-width()
+column($not-animated-background-z-index)
#parent-container
+full-height-width()
#social-container, #name-container, #logo-container
flex: 1
#social-container, #name-container
background-color: $logo-background-color
+flex-center-contents()
z-index: $not-animated-background-z-index
#name-container
> #name
color: $home-primary-text-color
font-family: $font-stack-sci-fi
font-size: $name-font-size
text-align: center
user-select: none
#logo-container
display: flex
> #logo-img-wrapper
> #logo
max-height: 100%
max-width: 100%
object-fit: contain
#social-container
display: flex
flex-wrap: wrap
> .social-icon
font-size: $social-icon-font-size
min-width: 85px
+media("<=tablet")
min-width: 45px
text-align: center
#bottom-arrow-container
background-color: $logo-background-color
padding-bottom: 1rem
+flex-center-contents()
z-index: $not-animated-background-z-index
Html
<div class="parent-row-container">
<!-- Row -->
<div class id="name-container">
<p id="name">
Artist name
</p>
</div>
<!-- End Row -->
<!-- Row -->
<div id="logo-container" class="parent-column-container">
<!-- Column -->
<div class="fill"></div>
<!-- Column -->
<div id="logo-img-wrapper">
<img id="logo" src="#img/home/logo.png" />
</div>
<!-- Column -->
<div class="fill"></div>
</div>
<!-- End Row -->
<!-- Row -->
<div class="fill">
<!-- Column -->
<div id="social-container">
</div>
</div>
<div id="bottom-arrow-container">
<font-awesome-icon class="selectable" :icon="['fa', 'chevron-down']" :style="{ color: 'white'}" />
</div>
</div>
This results in the following when run on Chromium/opera (expected)
But firefox produces this
Has this something to do with how firefox treats percentage widths?
If i amend the img container and put flex: 1 on the image container firefox is fixed, but chromium/opera break, and they center the image (because of object-fit: contain) leaving spaces at the edges (also expected behavior) Also, if I remove completely the img from the html, the grid is the same as with chrome/opera
Have any of you experienced anything similar?
I think you use oddly complex approach (using extra empty columns, object-contain and hard way). Here I've just used align-items,text-align and justify-content in one or two cases, and problem is solved:
https://codepen.io/anon/pen/NOOZqb
Flexbox not working anymore after firefox 34.0.5 but you get it to fix by a CSS trick as:
This worked for me:
img {max-width:100%; width:100%;}

How do I right align div elements?

The body of my html document consists of 3 elements, a button, a form, and a canvas. I want the button and the form to be right aligned and the canvas to stay left aligned. The problem is when I try to align the first two elements, they no longer follow each other and instead are next to each other horizontally?, heres the code I have so far, I want the form to follow directly after the button on the right with no space in between.
#cTask {
background-color: lightgreen;
}
#button {
position: relative;
float: right;
}
#addEventForm {
position: relative;
float: right;
border: 2px solid #003B62;
font-family: verdana;
background-color: #B5CFE0;
padding-left: 10px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript" src="timeline.js"></script>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" />
</head>
<body bgcolor="000" TEXT="FFFFFF">
<div id="button">
<button onclick="showForm()" type="button" id="cTask">
Create Task
</button>
</div>
<div id="addEventForm">
<form>
<p><label>Customer name: <input></label></p>
<p><label>Telephone: <input type=tel></label></p>
<p><label>E-mail address: <input type=email></label></p>
</form>
</div>
<div>
<canvas id="myBoard" width="600" height="600" style="background:lightgray;">
<p>Your browser doesn't support canvas.</p>
</canvas>
</div>
</body>
</html>
Floats are okay, but problematic with IE 6 & 7.
I'd prefer using the following on the inner div:
margin-left: auto;
margin-right: 0;
See the IE Double Margin Bug for clarification on why.
You can make a div that contains both the form & the button, then make the div float to the right by setting float: right;.
Old answers. An update: use flexbox, pretty much works in all browsers now.
<div style="display: flex; justify-content: flex-end">
<div>I'm on the right</div>
</div>
And you can get even fancier, simply:
<div style="display: flex; justify-content: space-around">
<div>Left</div>
<div>Right</div>
</div>
And fancier:
<div style="display: flex; justify-content: space-around">
<div>Left</div>
<div>Middle</div>
<div>Right</div>
</div>
You can use flexbox with flex-grow to push the last element to the right.
<div style="display: flex;">
<div style="flex-grow: 1;">Left</div>
<div>Right</div>
</div>
Note that while this answer is not wrong, it is very outdated methodology written in 2015
Other answers for this question are not so good since float:right can go outside of a parent div (overflow: hidden for parent sometimes might help) and margin-left: auto, margin-right: 0 for me didn't work in complex nested divs (I didn't investigate why).
I've figured out that for certain elements text-align: right works, assuming this works when the element and parent are both inline or inline-block.
Note: the text-align CSS property describes how inline content like text is aligned in its parent block element. text-align does not control the alignment of block elements itself, only their inline content.
An example:
<div style="display: block; width: 80%; min-width: 400px; background-color: #caa;">
<div style="display: block; width: 100%">
I'm parent
</div>
<div style="display: inline-block; text-align: right; width: 100%">
Caption for parent
</div>
</div>
Here's a JS Fiddle.
If you have multiple divs that you want aligned side by side at the right end of the parent div, set text-align: right; on the parent div.
Do you mean like this? http://jsfiddle.net/6PyrK/1
You can add the attributes of float:right and clear:both; to the form and button
Maybe just:
margin: auto 0 auto auto;
Simple answer is here:
<div style="text-align: right;">
anything:
<select id="locality-dropdown" name="locality" class="cls" style="width: 200px; height: 28px; overflow:auto;">
</select>
</div>
Sometimes float: left leads to design problems, for that cases you can use display flex like this:
.right {
display: flex;
justify-content: flex-end;
margin-left: auto;
margin-right: 0;
}
<div>
<div class="right">Right</div>
</div>
If you are using bootstrap, then:
<div class="pull-right"></div>
One way could be setting a parent div for those elements that need to be pulled right and do the rest like the way shown in the the example below to have them right-aligned:
.parent-div {
display: flex;
float: right;
}
/*Below: child-div styling is not needed for this purpose! this is just for demonstration:*/
.child-div {
text-align: center;
background-color: powderblue;
margin: auto 10px;
height: 100px;
width: 50px;
}
<div class="">CANVAS div </div>
<div class="parent-div">
<div class="child-div">child 1</div>
<div class="child-div">child 2</div>
<div class="child-div">...</div>
<div class="child-div">child n</div>
</div>
If you don't have to support IE9 and below you can use flexbox to solve this: codepen
There's also a few bugs with IE10 and 11 (flexbox support), but they are not present in this example
You can vertically align the <button> and the <form> by wrapping them in a container with flex-direction: column. The source order of the elements will be the order in which they're displayed from top to bottom so I reordered them.
You can then horizontally align the form & button container with the canvas by wrapping them in a container with flex-direction: row. Again the source order of the elements will be the order in which they're displayed from left to right so I reordered them.
Also, this would require that you remove all position and float style rules from the code linked in the question.
Here's a trimmed down version of the HTML in the codepen linked above.
<div id="mainContainer">
<div>
<canvas></canvas>
</div>
<div id="formContainer">
<div id="addEventForm">
<form></form>
</div>
<div id="button">
<button></button>
</div>
</div>
</div>
And here is the relevant CSS
#mainContainer {
display: flex;
flex-direction: row;
}
#formContainer {
display: flex;
flex-direction: column;
}
display: flex;
justify-content: space-between;
hasnt been mentioned. if there are 2 elements (even if one is an empty div) it will place one on the left and one on the right.
<div style="display: flex; justify-content: space-between;">
<div id="emptyDiv"></div>
<div>I'm on the right</div>
</div>
You can simply use padding-left:60% (for ex) to align your content to right and simultaneously wrap the content in responsive container (I required navbar in my case)
to ensure it works in all examples.
You can do it easy by just add this css:
(Works in IE11)
<div>
<!-- Subtract with the amount of your element width -->
<span style="margin-left: calc(100vw - 50px)">Right</span>
</div>
I know this is an old post but couldn't you just use <div id=xyz align="right"> for right.
You can just replace right with left, center and justify.
Worked on my site:)

Resources