img width 100% in display: box (css flexbox parent) - css

I am trying to resize an image to be the width of its parent div; normally width: 100%; works fine, but when the parent has display: box; the img is not resized. Giving the child image box-flex: 1 has no effect.
<div style="display: -webkit-box; -webkit-box-pack: center; width: 100%;">
<img src="foo.jpg" style="-webkit-box-flex: 1;" />
</div>

Maybe you have solved this, but you can use (Providing example for mozilla)
IMAGE {
display: -moz-box;
-moz-box-flex: 1;
}
#cont {
-moz-box-orient: horizontal;
display: -moz-box;
}
Haven't tested the example and in worst case you need to make some tweek, but i'm pretty sure it's correct.

Granted this is an old question, however, it still shows up in searches so wanted to update with an answer:
Currently in Chrome 23 and Firefox Nightly 21.0a1 this layout works to keep the image the size of the parent div and resize (along with keeping it centered).
http://jsfiddle.net/qAErr/
HTML
<section id="holdMe">
<div>
<img src="http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png" alt="html5"/>
</div>
</section>
CSS
#holdMe {
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: -o-flex;
display: flex;
-webkit-justify-content: center;
-moz-justify-content: center;
-ms-justify-content: center;
-o-justify-content: center;
justify-content: center;
}
#holdMe img {
width: 100%;
}

Browser?
I'm not aware of any browser that supports the flexbox spec without vendor prefixes.
display: -moz-box; and display: -webkit-box; etc.
Actually, I just looked at your code again... if you're using flex on the images, you don't need the width declaration since flex defines width dynamically.
You should also define the width of the parent div.

Related

Scrollbar apperars in Chrome when there is nothing to scroll

I am using this template for a small portfolio webpage. The problem is that when the website is opened in Chrome, a scrollbar always appears on the main page as shown below:
This does not happen in Firefox, Edge and IE. Here is a link to the demo of the template and here a jsfiddle with the CSS. Both display the unnecessary scrollbar when Chrome is used. I tried adding the following line to the html file but it didn't help:
.content {
overflow: hidden;
}
I hope that somebody can help me out here. Thanks!
Edit: The problem is somewhere here, but I cannot figure out a way to fix it.
#wrapper {
display: -moz-flex;
display: -webkit-flex;
display: -ms-flex;
display: flex;
-moz-flex-direction: column;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-moz-align-items: center;
-webkit-align-items: center;
-ms-align-items: center;
align-items: center;
-moz-justify-content: space-between;
-webkit-justify-content: space-between;
-ms-justify-content: space-between;
justify-content: space-between;
position: relative;
min-height: 100vh;
width: 100%;
padding: 4rem 2rem;
z-index: 3;
}
#wrapper:before {
content: '';
display: block;
}
I'm on Chrome and I'm not seeing any scrollbar when I visit your demo. Or the fiddle for that matter
Not completely sure why this occurs (can indeed see the same issue) but it's super easy to workaround by simply applying overflow-y: hidden to the body
body{
overflow-y: hidden;
}
Is there something overwriting your call to hide the scroll bar elsewhere on the page? E.g:
overflow: auto;
Or is it perhaps another element on a page with a width/height (e.g image or div element) that is causing the issue?
Another option is to go to the inspector and start deleting items until you find out which one is causing the issue.

Scrollable items outside of scroll container; Invisible scroll container

I have container with buttons on top of a map control. This control needs to be scrollable as there might be more buttons than the screen height allows for.
What I am looking for is a way to have the buttons to be displayed outside of the container which means that I can put the container off the screen to be invisible.
Or to have the scrollbar on the left hand side so that it is not between the map and the controls.
Here is some html
<div class='ctrl__scroll'>
<button class="map__interface mdl-button mdl-js-button mdl-button--fab mdl-button--colored">
<i class="material-icons">add</i>
</button>
<button class="map__interface mdl-button mdl-js-button mdl-button--fab mdl-button--colored">
<i class="material-icons">remove</i>
</button>
<button>...</button>
</div>
Here is the css
.ctrl__scroll {
position: absolute;
top: 0;
left: 0;
width: 90px;
height: 100%;
overflow-y: auto;
overflow-x: hidden;
background-color: rgba(0, 0, 0, 0.5);
}
.ctrl__scroll > .mdl-button {
margin-top: 10px;
margin-left: 10px;
}
Here is the jsFiddle for the screenshot.
https://jsfiddle.net/goldrydigital/zez3gz21/
Edit: I have now worked this out and changed the jsFiddle. I am using the excellent jScrollPane plugin which allows me to do whatever I want with scrollpanes.
Even if you could display the children outside their scrollable parent (which is counter-intuitive at best) I don't think you'd be able to scroll them. However, you can't have overflow-x:visible; overflow-y:auto; on the same an element. It will automatically add a scrollbar for the X asis too.
Let's take into account that most mobile devices have nice-looking, self-hiding semi-transparent bars, making your solution look good even with the scrollbar visible (as it is now). We only need to fix the scrollbar on non-touch devices. On desktop devices, which are rendering it ugly and opaque. Here's a possible solution:
#media (min-width: 768px) {
.ctrl__scroll > .mdl-button {
margin: 10px 0 0 10px;
}
.ctrl__scroll {
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-moz-box-orient: vertical;
-moz-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-box-pack: start;
-webkit-justify-content: flex-start;
-moz-box-pack: start;
-ms-flex-pack: start;
justify-content: flex-start;
overflow: visible;
background-color: transparent;
}
}
Add it at the end of your current CSS. Your updated jsFiddle.
Just add:
.map {
padding-left: 90px;
...
}
which is the size of your side menu

CSS Flex: flex items stretch vertically and text centered

I have a question related to CSS flex, i'm a web dev but not CSS expert and i need your help on this CSS flex issue.
Here is the plnkr code snippet, you can see what i wanted here: Plunker
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row;
justify-content: space-around;
align-items: center;
}
.flex-item {
background: #e6e6e6;
padding: 10px;
align-self: stretch;
}
I want that:
All flex items should be stretch.
Flex item on the left should be both vertically and horizontally centered of the box.
Flex item on the right should be vertically: center, horizontally: left.
Thanks in advance, appreciate all your helps :)
I have updated your Plunker[1] Plunker. Please review and let me know is this what you need.
I have added flex-container to the orange div, so that the height will be height of the container

CSS flexbox image centering

I have a variable set of images I'm displaying in a flexbox, and am trying to make sure the images vertically and horizontally align to their parent div. I've tried the usual vertical-align, text-align, max-width: 100%;, and margin: 0 auto;. Any good cross browser solutions to my dilemma that won't lead to warped/left-aligned images?
Here's the CSS (in it's current borky form, I've also tried the other aforementioned centering CSS):
.collage{
max-width: 100%;
max-height: 100%;
height: inherit;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: inline-flex;
flex-direction: column;
}
.collage div{
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: inline-flex;
overflow: hidden;
width: 100%;
}
.collage img{
max-height: 100%;
}
jsfiddle: http://jsfiddle.net/hSb93/3/
if you add margin: auto; to .collage img it solves it.
But flexbox is not supported by all browsers.
If you want it to be fully cross-browser you can put a span before the image and put this CSS in it:
CSS
.collage div{
text-align: center;
}
span{
display: inline-block;
height: 100%;
vertical-align: middle;
}
HTML
<div>
<span></span>
<img>
</div>

Switch Objects in Side Panel in CSS

I have two boxes within each of my member's profile page on my forum that I would like to switch.
Screenshot:
http://s22.postimg.org/69aqe1i29/panel.jpg
I want the "About" section below the "My Badges" section
is this possible within just CSS?
direct link to page in question: http://idenitties.com/vanilla/profile/3/test1
Reordering the HTML is your best option. If this is generated content and you're unable to modify it in any way, you can use Flexbox to do reorder them.
http://tinker.io/9d3f8/1
.container {
display: -webkit-box;
display: -moz-box;
display: -webkit-flexbox;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
-webkit-box-direction: normal;
-moz-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
}
.about {
-webkit-box-ordinal-group: 2;
-moz-box-ordinal-group: 2;
-webkit-flex-order: 1;
-ms-flex-order: 1;
-webkit-order: 1;
order: 1;
}
<div class="container">
<div class="about">
About (first in source)
</div>
<div class="badges">
Badges (second in source)
</div>
</div>
The up side is that you don't need to know the sizes of either of the elements. The down side is that browser support is limited: IE10, Chrome, Opera, Firefox, Safari
http://caniuse.com/#feat=flexbox
The best way would be to swap the <div> in the HTML but you can use a little absolute positioning to move the divs around.
For example this would work:
.about{
position: absolute;
width: 230px;
bottom: -30px;
}
But I would say the best option would be to just change the ordering in the HTML, simple copy paste.
If box height is constant you may switch them using relative positioning. I mean you may shift top box to down for height of bottom box and otherwise.
.Box.About {
position: relative;
top: 114px;
}
.Box.PeregrineBadgesBox {
position: relative;
top: -178px;
}

Resources