I have 2 blocks that are wrapped in flex container. I specified so that flex-row will wrap, but is there any way to clearly indicate when should elements wrap? I want to resize content inside flex-items until some breakpoint and only then wrap them.
Code looks like that:
.flex-row{
display:flex;
flex-wrap:wrap;
}
<div class='flex-row'>
<div class="block1">
<h2>Some title</h2>
<p>Some text</p>
</div>
<div class="block2">
<img src="img"/>
</div>
</div>
Appreciate your help. Thank you in advance.
Without the flex-wrap property it wont wrap so you can use media querys to exactly define when to wrap the elements. But you also have to set the width for the flex-items.
.flex-row {
display: flex;
}
/* Wrap */
#media only screen and (max-width: 1023px) {
.flex-row {
flex-wrap: wrap;
}
.block1,
.block2 {
width: 100%;
}
}
<div class='flex-row'>
<div class="block1">
<h2>Some title</h2>
<p>Some text</p>
</div>
<div class="block2">
<img src="http://via.placeholder.com/350x350"/>
</div>
</div>
You could use min-width on the blocks. This means they will fully stretch, but when the screen size limits them to being 200px in this example, that breakpoint will lead them to wrap, and their width will never go below 200px.
Another option is to just apply flex-wrap: wrap; on that specific breakpoint you want with a media-query.
For further control, you could also look into flex-basis: https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis
EDIT: Responsive image
Genereally it's good to include this line of code on most images: max-width: 100%; height: auto; as this will make images auto-responsive. max-width: 100%; forces the image to never overflow from its container, and height: auto; adjusts the images height so its aspect ratio is correct. Try dragging the screen size and you will see its effect :)
.flex-row {
display: flex;
flex-wrap: wrap;
}
.block1,
.block2 {
min-width: 200px;
}
.block2 img {
max-width: 100%;
height: auto;
}
<div class='flex-row'>
<div class="block1">
<h2>Some title</h2>
<p>Some text</p>
</div>
<div class="block2">
<img src="http://via.placeholder.com/350x350" />
</div>
</div>
Related
I have two container. Outer container is wrapping inner container.And in inner container is wrapping two dives. For inner container CSS is display flex. For small screen size I mention display block using media quire which is not working.What I am doing wrong.For small screen two div should stacking up.Why media quire not working? When I should give max-width to image?
.row2{
display: flex;
padding:2em 3em;
}
.outercontainer{
background-color: darkgray;
}
#media (min-device-width: 426px) and (max-device-width: 764px) and (-webkit-min-device-pixel-ratio: 2) {
.row2{
display: block;
padding: 0.1em;
}
.image2{
width: 100%;
}
}
<div class="outercontainer">
<div class="innercontainer row2">
<div class="col text">
<h3>title text</h3>
<p>This is simple test text This is simple test text This is simple test text This is simple test text This is simple test text This is simple test text</p>
</div>
<div class="col image">
<img class="image2" src="https://images.unsplash.com/photo-1441974231531-c6227db76b6e?ixid=MXwxMjA3fDB8MHxzZWFyY2h8Mnx8bmF0dXJlfGVufDB8fDB8&ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60" />
</div>
</div>
</div>
Adding flex-direction: column to the Flexbox container .row2 inside your media query creates a stacked "column" layout at smaller viewports so the image appears underneath the text. The media query max-width value might need to bumped up a bit to around 1000px so the flex-direction column layout happens sooner and the text next to the image doesn't appear squished. Try this out.
Also, the device-width media feature is deprecated and no longer recommended. Use min-width and max-width for length values in your media queries instead.
.row2 {
display: flex;
padding:2em 3em;
}
.outercontainer {
background-color: darkgray;
}
/* 764px max-width makes the image squish the text, try around 1000px */
#media (min-width: 426px) and (max-width: 1000px) and (-webkit-min-device-pixel-ratio: 2) {
.row2 {
display: flex;
flex-direction: column;
padding: 0.1em;
}
.image2 {
width: 100%;
}
}
<div class="outercontainer">
<div class="innercontainer row2">
<div class="col text">
<h3>title text</h3>
<p>This is simple test text This is simple test text This is simple test text This is simple test text This is simple test text This is simple test text</p>
</div>
<div class="col image">
<img class="image2" src="https://images.unsplash.com/photo-1441974231531-c6227db76b6e?ixid=MXwxMjA3fDB8MHxzZWFyY2h8Mnx8bmF0dXJlfGVufDB8fDB8&ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60" />
</div>
</div>
</div>
I have a div with two contents: an image (on top) and text (on bottom).
Height of image plus height of the text is bigger than the height of the parent.
I want an image to shrink, so the whole text will be visible.
So - now it looks like this:
And I want it to look like this:
How to achieve this?
I tried it with display: flex and flex-shrink or flex-grow, but it's not working.
Solution with flex will be much appreciated :)
Here's a codepen with an example:
https://codepen.io/anon/pen/zQXyLb
And here's code used:
<html>
<head></head>
<body style="background: yellow;">
<div style="
width: 150px;
height: 150px;
background: #ddd;
overflow: hidden;
display: flex;
flex-direction: column;
">
<div>
<img src="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/198/skull_1f480.png">
</div>
<div>
<div>Here i have some text</div>
<div>which is multiline</div>
<div>and it should make</div>
<div>the skull smaller</div>
</div>
</div>
</body>
</html>
Instead of img tag try div with background-image.
flex: is short form of: flex-grow, flex-shrink, flex-basis.
0 0 auto means that element will take just as much space as needed.
1 1 auto means that element will take all available space — so image takes box size minus text size. And text is always visible.
.box {
display: flex;
flex-direction: column;
margin-bottom: 20px;
width: 150px;
height: 150px;
background: #ddd;
}
.image {
flex: 1 1 auto;
background: url(https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/198/skull_1f480.png) 0 0 no-repeat;
background-size: contain;
}
.text {
flex: 0 0 auto;
}
<div class="box">
<div class="image"></div>
<div class="text">
<div>Here I have some text</div>
<div>which is multiline</div>
<div>and it should make</div>
<div>the skull smaller</div>
</div>
</div>
<div class="box">
<div class="image"></div>
<div class="text">
<div>Here I have small text</div>
</div>
</div>
You can also keep image in the HTMl if you inbricate flex boxes to allow img container to shrink and img understand max-height:100%;
body {
background-color: #a3d5d3;
}
[class],
[class]>div {
display: flex;
flex-direction: column;
overflow: hidden;
}
img {
max-height: 100%;
margin-right: auto;
}
[class]>div[id] {
flex-grow: 1;
flex-shrink: 0;
}
<div class style="
width: 150px;
height: 150px;
background: #ddd;
">
<div>
<img src="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/198/skull_1f480.png">
</div>
<div id>
<div>Here i have some text</div>
<div>which is multiline</div>
<div>and it should make</div>
<div>the skull smaller</div>
</div>
</div>
Resize the image using css.
img{
max-width:50px;
}
The above code should make the image flexible and allow it to be smaller.
Also you can put variable height to the parent div, i.e.
<div style="
width: 150px;
max-height: 250px; //added this part
background: #ddd;
overflow: hidden;
display: flex;
flex-direction: column;
">
But this way the image will stay large but the parent's height will increase. Allowing the text to appear in the enlarged div.
So I am attempting to have 3 flex items with equal height and width inside a flex container. I am successful in making the 3 flex items have equal width. However, the first flex-item has a div (.images) that is also a flex container which contains a few more children than the other 2 flex items' (.images) div. This results in the height of the 1st flex-item to be larger than the other 2. How do I make the height of the other 2 flex items have the same height as the first, even though they do not have the same amount of children? I researched this issue, but I only found answers when the flex-direction property is set to column. In my case the flex-direction property is set to row within the flex container.
body {margin: 0}
.flex-container {
display: flex;
flex-direction: row;
width: 100%;
}
.flex-items {
width: 33.333%;
height: 100%;
}
.images {
display: flex;
flex-direction: row;
flex-wrap: wrap;
height: 100%;
justify-content: center;
}
<div class="flex-container">
<div class="flex-items">
<h1>Some Title</h1>
<div class="images">
<img src="image1.jpg" alt="">
<img src="image2.jpg" alt="">
<img src="image3.jpg" alt="">
<img src="image4.jpg" alt="">
<img src="image5.jpg" alt="">
</div>
</div>
<div class="flex-items">
<h1>Some Title</h1>
<div class="images">
<img src="image6.jpg" alt="">
<img src="image7.jpg" alt="">
</div>
</div>
<div class="flex-items">
<h1>Some Title</h1>
<div class="images">
<img src="image8.jpg" alt="">
<img src="image9.jpg" alt="">
</div>
</div>
</div>
You need to give the images inside .images a height of 100% and a width of auto - if you want them to scale proportionately, like so:
.images {
display: flex;
flex-direction: row;
flex-wrap: wrap;
height: 100%; (images is getting 100% height, but nothing is telling its children to go up in height)
justify-content: center;
}
.images img { (address the children)
height:100%;
width:auto;
}
If you don't set images width and height they will use auto, and scale it from that for the flex box depending on what browser your on of course.
Images can be manipulated inline or using css by using the height or width property, an example of both is below.
Inline Example 1:
<img src="#.png" width="auto" height="100%" />
Css Example 2:
div.images > img {
height: 100%;
width: auto;
}
It is also good idea to optimize images for your web page and not to rely on auto scaling for the best results. After all you are the one that knows what the end result should be.
:)
Having trouble conceptualizing how to go about making this set of boxes. They need to be horizontal and centered, but stack on a smaller screen size. Also need to center the image and two types of text. Appreciate your thoughts!
What I have so far:
Need to start with a main container and hold all 4 boxes inside of it. Not sure how to get the horizontal display or the stacking feature on smaller screens. Also unsure how to center the content inside of each box - not sure if I should use static or dynamic formatting inside the boxes.
I'd recommend using flex and media queries if you don't want to go down the bootstrap (or other grid layout) route.
HTML:
<div class="wrapper">
<div class="box">
<div class="image">Image</div>
<h1>Title Text</h1>
<div>detailed text that explains what the title means</div>
</div>
<div class="box">
<div class="image">Image</div>
<h1>Title Text</h1>
<div>detailed text that explains what the title means</div>
</div>
<div class="box">
<div class="image">Image</div>
<h1>Title Text</h1>
<div>detailed text that explains what the title means</div>
</div>
<div class="box">
<div class="image">Image</div>
<h1>Title Text</h1>
<div>detailed text that explains what the title means</div>
</div>
</div>
CSS:
.wrapper {
display: flex;
flex-direction: column;
}
.box {
width: 100%;
padding: 10px;
text-align: center;
border: 1px solid black;
}
.image {
display: inline-block;
background: black;
color: white;
padding: 10px;
}
#media (min-width: 600px) {
.wrapper {
flex-direction: row;
}
.box {
width: auto;
flex-grow: 1;
}
}
https://codepen.io/anon/pen/GvMPQe
Check out bootstrap. It is a responsive plugin used in many websites including Twitter. Highly recommend and great for designing multiplatform websites.
I'm currently working on responsiveness on my website and I hit the wall. On smaller resolutions I changed the flex-direction to column from row, changed the ordering of flex elements, changed the main textbox width to 100% and sideboxes to 50%.
Code looks like this:
<section class="about" id="ABOUT">
<div class="about-sidebox l">
</div>
<div class="about-mainbox">
</div>
<div class="about-sidebox r">
</div>
</section>
As you can see the sideboxes are not within the container. Putting them in container helps but completely ruins my previous layout.
CSS for wanted resolution is:
.about {
flex-direction: column;
}
.about-mainbox {
order: 1;
}
.about-sidebox {
order: 2;
width: 50%;
flex-wrap: wrap;
}
Effect is like this:
As you can see I want the sideboxes to be next to each other. Any solution I can't think of without adding additional container?
I hope I've understood you correctly. You could remove the flex-direction property from .about
Add flex-wrap. Then just give .about-mainbox a width of 100%
.about {
display: flex;
flex-wrap: wrap;
}
.about-mainbox {
order: 1;
width: 100%;
}
.about-sidebox {
order: 2;
width: 50%;
}
<section class="about" id="ABOUT">
<div class="about-sidebox l">demo left
</div>
<div class="about-mainbox">demo
</div>
<div class="about-sidebox r">demo right
</div>
</section>
The simplest possible is to do this;
Set the about to flex-wrap: wrap so its flex items can wrap, skip flex-direction so it uses its default row
Set the mainbox to order: -1 (default is 0), which will position it before the sidebox's, and flex-basis: 100%, which will make it take full width and by doing that, it pushes the sidebox's to a new line
Give the sidebox's flex-basis: 50% to be equally wide, and min-width: 0; so they stay side-by-side (if you wan't them to wrap and stack vertical when their content force's them to, drop this, or control it with a set width, i.e. min-width: 150px;)
I also recommend you use Flexbox's own properties, i.e. in this case flex-basis instead of width
.about {
display: flex;
flex-wrap: wrap;
}
.about-mainbox {
flex-basis: 100%;
order: -1;
}
.about-sidebox {
flex-basis: 50%;
min-width: 0;
}
<section class="about" id="ABOUT">
<div class="about-sidebox l">Left
</div>
<div class="about-mainbox">Main
</div>
<div class="about-sidebox r">Right
</div>
</section>