Text centered next to image, and if no space, aligned to top - html

Let's check this fiddle:
img {
float: left;
}
#inner {
height: 128px;
background-color: yellowgreen;
display: table-cell;
vertical-align: middle;
}
#content {
background-color: red;
}
<img src="http://cdn.memegenerator.net/instances/250x250/37934290.jpg" width="128" height="128" />
<div id="inner">
<div id="content">text text tertkl elknr tlken lsl kdmfsldkfmsldkfmslkd mfkndfln dflkfndg lkn</div>
</div>
this works so far as I expect - text is centered, and as you shrink the width, text goes underline: but then its "too far" from the image. The best would be if the vertical-align: middle; became vertical-align: top; when it needs to jump. How to do it without possibly jQuery?

A simple way to achieve this is to use a CSS Media Query.
Your markup would stay the same and your CSS would only need to have the following added:
#media screen and (max-width: 290px) {
#inner {
vertical-align: top;
}
}
in action: http://jsfiddle.net/uWMkH/1/
What that says is, "When the viewport's width is no more than 290px, do this stuff to #inner.
Take a look at these links for more information:
http://www.w3.org/TR/css3-mediaqueries/
http://cssmediaqueries.com/
http://css-tricks.com/css-media-queries/
The caveat with using media queries to do this is that they aren't supported in IE8 and below. I hope you don't have to deal with those headaches!
Look here for a complete list of browsers with support:
http://caniuse.com/#feat=css-mediaqueries

You can do this without media queries, but it requires a browser that supports the entire CSS Flexible box module (most browsers are missing support for wrapping). At this point in time, support is limited to Opera, Chrome, and IE10.
http://codepen.io/cimmanon/pen/rFdkt
figure {
display: -ms-flexbox;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
background-color: yellowgreen;
}
#supports (flex-wrap: wrap) {
figure {
display: flex;
}
}
figcaption {
-webkit-flex: 1 15em;
-ms-flex: 1 15em;
flex: 1 15em;
background-color: red;
}
What Flexbox offers over media queries is the ability to reflow the content based on the available space, not just the browser width.

Related

Buttons outside of his flexbox

Hello, i've lost half of my hair in 2 days, sorry for my english :)
If you resize the window the buttons in #header go outside of his container.
Here is the code:
#root {
background: grey;
display: flex;
flex-direction: column;
align-items: center;
}
#header {
background: red;
align-self: stretch; /* fill in main-axis */
flex: 0 0 65px; /* fixed height */
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
}
<div id="root">
<div id="header">
<button>PUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSH</button>
<button>BYE.</button>
</div>
<----- resize me ---->
</div>
I think the problem is #root.width is leaded by window/body.width ?
I have a solution with min-content:
#root { min-width: min-content; ... }, but i don't want use this value.
How configure #root correctly acting as a good and beautiful container for my layout ?
Here is a codepen for playing: https://codepen.io/papawa/pen/NLKbKR
Simply there is no problem, it's a logical result, unless you decide what do you expect to get in a smaller screens then that's what you will get and the reason is that you have a button with a long 'one word text' and the solution to fix this is just by wrapping the text itself and that's how you do it:
overflow-wrap: break-word;
or
word-wrap: break-word;
Here's the overall result:
#root {
display:flex;
flex-direction: column;
align-items:center;
background:grey;
overflow:hidden;
}
#header {
align-self:stretch;
flex: 0 0 65px;
display: flex;
justify-content: space-between;
align-items: center;
background: red;
padding: 10px;
}
.btn{
width: 50vw;
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
}
<div id="root">
<div id="header">
<button class="btn">PUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSH</button>
<button>BYE.</button>
</div>
<---- resize me ---->
</div>
FOR FURTHER READING: Handling Long Words and URLs (Forcing Breaks, Hyphenation, Ellipsis, etc)

Automatic margin doesn't center elements in div

Lately I was creating a searchbox for my website, but I wanted it to be constantly centered in every y and x dimension.
I have div container searchbox:
.searchbox {
position: absolute;
text-align: center;
width: 100%;
left: 0%;
top: 55px;
height: 115px;
background-color: black;
}
Inside searchbox container, I made special mover container:
.mover {
margin: 0 auto;
width: 50%;
}
As you see width is 50% because I thought it would center it, but it didn't, and margin is automatic, which I don't think even works without 50% width.
Full code and Result.
I think my style is kinda messed up and there are useless things which may affect automatic margin.
What may the problem be? does margin: auto; doesn't work with current position of div? What do I need to change? If not, what's the problem?
I will be very thankful if you upload solution on my current fiddle.
UPDATED ANSWER
Here is correct code: https://jsfiddle.net/uda77168/7/
First...
1. Removed all absolute, top, left, right, bottom CSS properties.
Reason: Absolute positioning is generally a bad thing to do, because it gives sites an unresponsive layout.
2. I've also removed float CSS properties.
Reason: float is not bad, but it's unnecessary if you're using flexbox.
3. Set .search {width: 100%}
Reason: make the search bar bigger.
4. Removed width properties for #condition and #stattrack.
5. Made the margins more consistent.
6. Placed <label> before <select>.
Center Vertically
1. <body> is the flexbox that will center things vertically. In order for that to work, the width and height for <html> and <body> have to be defined.
html, body {
width:100%;
height:100%;
}
body {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
2. Next, we need to define <body> as a flexbox and give it some flexbox properties:
body {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
You can just copy-paste flexbox code like the one above from here.
Center Horizontally
1. Create a div around .statbox, .conbox, and .rarbox, and give it a width and make it a flexbox (again, the flexbox code is copied):
<div class="horizontal-flexbox"></div>
.horizontal-flexbox {
width: 100%;
display: flex;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
2. I've also set .statbox, .conbox, and .rarbox each to be 33.3% width long. Added together, that's 99.9% – just under 100%.
.horizontal-flexbox > div {
width: 33.3%;
margin-top: 10px;
}
3. I've also included some other stuff, but that's not important. Make sure you learn flexbox, it's real useful!
Your input.search class has a specified width in px which is larger than the container.
.search {
width: 100%;/*changed this line*/
height: 35px;
margin-top: 20px;
margin-left: 0 auto;
margin-right: 0 auto;
border: solid 1px black;
border-radius: 7px;
}
However using percentages can lead to unpredictable layouts when viewed on different screen resolutions.
Use this:
.searchbox {
display:flex;
display:-webkit-flex;
justify-content:center;
-webkit-justify-content:center;
align-items:center;
-webkit-align-items:center;
}
And
.mover{width:50%;}

How to fix Flexbox styles in IE 11

I'm using flexbox here - http://marcinzalewski.net/exo/ but I can't fix this for IE 11. Even Microsoft Edge is working fine and IE 11 is only version I need to fix.
My flex-container look like this:
.flex-container {
max-width: 1240px;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
-webkit-flex-wrap: row wrap;
justify-content: center;
padding: 0;
margin: 0 auto;
list-style: none;
}
And this is my inside elements code:
.offer-element {
width: 250px;
height: 250px;
border-radius: 50%;
margin-top: 60px;
margin-left: 25px;
margin-right: 25px;
}
Now those elements should be centered and fit in few lines, but they all are in the same line and have ~150px instead of 250px each. I tried to add display: -ms-flexbox; but it doesn't work anyway. Normally they are all centered and take 3 lines.
It looks like you're specifying flex-wrap only on webkit. By default flex will fit all items one line. You need to specify flex-wrap: wrap on your container.
IE 11 requires a unit to be added to the third argument, the flex-basis property
https://msdn.microsoft.com/en-us/library/dn254946%28v=vs.85%29.aspx

CSS Flexbox incorrect on Firefox and IE

I have recently been developing a website using flexbox, and have been doing so on Chrome. The site looks perfect on Chrome (and Safari, according to users) however it has some serious issues when viewed on Firefox and IE. I have tried to look online for documentation on which prefixes to include in my CSS and how to make it appear normal on those browsers, but I truly cannot find a summation or tutorial anywhere. Here is a sample of my CSS code, containing flexboxs that do not display correctly on Firefox and IE -
.header {
padding: 12px;
padding-top: 10px;
padding-bottom: 0px;
margin: 0px;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
justify-content: space-around;
height: 70px;
background-color: #000000;
}
.header-box {
padding-left: 15px;
padding-right: 15px;
margin: 0;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
align-content: flex-start;
align-items: flex-start;
height: 70px;
width: 1170px;
background-color: #000000;
}
This code is for a header bar along the top of the site. I thought by including the display: -moz-box; and such, that would allow it to be seen on Firefox, but the formatting is messed up in the sense that the box is not centered but instead along the left side of the screen, and the boxes within the header are all along the top of the parent container rather than on the bottom. Thank you for any insight you may have on this problem!
In only works on webkit browsers because you only use
-webkit-flex-flow: row wrap;
You should use the standard
flex-flow: row wrap;
Otherwise, the initial value row nowrap will be used instead.

Element with max-height based on the height of the viewport. Image within should scale and keep ratio

Looking for a way to make sure that the height of an article element is never greater than 65% of the viewport height. Also there's an image nested inside the element that must be contained to the height of it's parent and be able to scaledown to the max-height and keep the it's ratio (yes, the image should be fully visible, no cropping).
It's also important that img and the overlaying div .actions have the same width at all times.
Is this possible with css only?
This is just a test case, there's other elements like this, the markup is the same, but the ratio of the element within each element is unique.
Demo
http://jsfiddle.net/SpPDp/show/
Code
http://jsfiddle.net/SpPDp/
Source below
<article>
<div class="inner">
<div class="overlay">
<div class="actions">
<div class="text">Text</div>
<div class="yep">Yep</div>
<div class="heretoo">Here too</div>
</div>
</div>
<div class="content">
<img src="http://lorempixel.com/output/abstract-h-g-600-900-4.jpg">
</div>
</div>
</article>
article {
float: left;
width: 40%;
}
article .inner {
position: relative;
}
article .overlay {
position: absolute;
background: #000;
opacity: 0.7;
top: 0; right: 0; bottom: 0; left: 0;
height: 100%;
overflow: hidden;
}
article .actions {
text-align: center;
position: absolute;
bottom: 1%;
width: 100%;
color: #fff;
background: red;
}
.text {
float: left;
}
.yep {
display: inline-block;
}
.heretoo {
float: right;
}
article img {
max-width: 100%;
}
First, let me say that the ability to change even the tiniest part of your html would make it so much easy to accomplish the result.
For example, setting the image as background, and accepting the fact that it should be cropped, would make the task a no-brainer.
But you make it clear that:
Articles must be max 65% of viewport h;
The meta bar over it should always have the same width as the image;
Images must not be cropped nor distorted.
If your question is formulated correctly, which I really hope it is, since we are all trying to solve it, you imply that articles widths won't always be equal.
The solution is to let the image figure out what is best to do. First, we set it's max-size in vw and vh, then we clear the way of unwanted positionings and sizes, to allow the size of the image to go up to the container, then back down to the meta bar in the overlay. Also, notice the use of flex boxes.
Here's the CSS (I didn't touch the HTML)
article {
float: left;
overflow: hidden;
vertical-align: top;
}
.inner {
/* older browsers. you should add the other prefixes too. there are polyfills to have broader support, check link later in the answer */
display: -webkit-box;
display: -moz-box;
display: -ms-box;
display: box;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
-ms-box-orient: vertical;
box-orient: vertical;
-webkit-box-direction: reverse;
-moz-box-direction: reverse;
-ms-box-direction: reverse;
box-direction: reverse;
/* newer */
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex;
-moz-flex-direction: column-reverse;
-ms-flex-direction: column-reverse;
-webkit-flex-direction: column-reverse;
flex-direction: column-reverse; /* the meta box is added
after the image container, and the items are arranged in column */
}
.overlay {
z-index: 2;
margin: 0 0 -25px; /* magic: this cuts the container by 25px (height of meta bar) */
}
.actions {
color: #fff;
background: rgba(200,0,0,0.8);
/* older */
display: -webkit-box;
display: -moz-box;
display: -ms-box;
display: box;
-webkit-box-pack: justify; /* distribute the labels */
-moz-box-pack: justify;
-ms-box-pack: justify;
box-pack: justify;
-webkit-box-direction: normal; /* reset order (it used to be inherited) */
-moz-box-direction: normal;
-ms-box-direction: normal;
box-direction: normal;
/* newer */
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex;
-webkit-justify-content: space-between; /* distribute the labels */
-moz-justify-content: space-between;
-ms-justify-content: space-between;
justify-content: space-between;
/* move up */
-webkit-transform: translate(0,-25px);
-moz-transform: translate(0,-25px);
-ms-transform: translate(0,-25px);
transform: translate(0,-25px);
box-shadow: 0 -1000px 0 1030px rgba(0,0,0,.6); /* since it
can't actually know the size, the overlay is accomplished with
a shadow. as long as it is not blurred, it won't impact
performances much */
font-size: 1.4vw; /* the only hard limit now is the width
of the text */
}
.content {
z-index: -1;
position: relative;
/* commenting those 2 last properties has two effects in webkit. 1) it avoids the images to stretch. 2) when you resize your window, the images won't adapt. only on load/refresh. this should be ok though, as window resizing is not really what responsive is most useful for
--- old code ---
display: -webkit-box;
display: -ms-box;
display: -moz-box;
display: box;
--- new code ---
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex; */
}
.content img {
max-width: 40vw; /* and here you finally set the sizes.
note that you can set a min-width too, if you want, but
if you do there will be some image ratio that will force
them to stretch */
max-height: 65vh;
}
The code: http://jsfiddle.net/frapporti/NssKa/
The result, with many images of different ratios: http://jsfiddle.net/frapporti/NssKa/embedded/result/
Off course, this is just theory and is a valid answer to your question.
If you want to use this in production you should add some good polyfill for the older browsers, perhaps you could add this polyfill. As you can see I have already added the older box model properties as fallback.
Should be, there's no reason why not. Percentage heights are very useful.
You need to add this to the image tag:
<img src="whatever.jpg" width="95%">
It can be any value, depending on how wide you want it. As long as you make sure not to specify a height, it will keep scale.
Your CSS should change to be more like this:
article {
height:65%;
max-height:65%;
/* plus whatever else you want*/
}
As long as the parent is 100% window height, the article will be 65% window beight. Clearly if it's more, the article will be more.
The other solution is JavaScript. However, since you ask for CSS I'll omit that.
CSS:
article{
height:65%;
max-height:65%;
overflow:scroll; /* or hidden */
}
article .content > img{
max-height:100%;
width:auto;
}
JS Control (Because relative elements may not have a percentage height)
$(function(){
var $article = $('article');
var $height = $(window).height() * 0.65;
$article.css({'height':$height+'px', 'max-height':$height+'px'});
});
You should first set the 'inner' to 65% and then 'content' and image to 100% height. Like this,
.inner{
height:65%;
}
.content{
height:100%;
}
.content img{
height:100%;
}
I wrote a solution using vanilla Javascript. Why have jQuery as a dependency unless you need to? (rhetorical).
var viewport = document.documentElement.clientHeight;
var el = document.getElementsByTagName("article");
Array.prototype.forEach.call(el, function(el) {
el.style.maxHeight = Math.floor((viewport/100)*65)+"px";
});
http://jsfiddle.net/PgtAY/