Align text to the bottom of a div - css

I tried to align my text to the bottom of a div from other posts and answers in Stack Overflow I learned to handle this with different CSS properties. But I can't get it done. Basically my HTML code is like this:
<div style='height:200px; float:left; border:1px solid #ff0000; position:relative;'>
<span style='position:absolute; bottom:0px;'>A Text</span>
</div>
The effect is that in FF I just get vertical line (the div in a collapsed way) and the text is written next to it. How can I prevent the div collapsing but having the width fitting to the text?

Flex Solution
It is perfectly fine if you want to go with the display: table-cell solution. But instead of hacking it out, we have a better way to accomplish the same using display: flex;. flex is something which has a decent support.
.wrap {
height: 200px;
width: 200px;
border: 1px solid #aaa;
margin: 10px;
display: flex;
}
.wrap span {
align-self: flex-end;
}
<div class="wrap">
<span>Align me to the bottom</span>
</div>
In the above example, we first set the parent element to display: flex; and later, we use align-self to flex-end. This helps you push the item to the end of the flex parent.
Old Solution (Valid if you are not willing to use flex)
If you want to align the text to the bottom, you don't have to write so many properties for that, using display: table-cell; with vertical-align: bottom; is enough
div {
display: table-cell;
vertical-align: bottom;
border: 1px solid #f00;
height: 100px;
width: 100px;
}
<div>Hello</div>
(Or JSFiddle)

You now can do this with Flexbox justify-content: flex-end now:
div {
display: flex;
justify-content: flex-end;
align-items: flex-end;
width: 150px;
height: 150px;
border: solid 1px red;
}
<div>
Something to align
</div>
Consult your Caniuse to see if Flexbox is right for you.

Related

How do I position a flex item at the top of a columnar flex-container, when the container is set to center-justify its children?

I have a flex-container with the 'flex-direction' set to column and 'justify-content' set to 'center'.
By default child items will be vertically aligned to the center of the flex-container.
However, I want the first item to be flush against the top of the column.
So for the child I've added the property 'justify-self: flex-start'.
This has no effect. The 'justify-self' property is being ignored.
[Codepen](https://codepen.io/bobdobbs_/pen/XWBNZwx)
Even when I set 'justify-self' to !important the property gets ignored.
The easiest thing to do here seems to be to use absolute positioning. But if that's the case there doesn't seem to be much of a point to using flexbox for anything much at all.
Is there a flexbox solution for this problem?
Or should I just stick to using absolute positioning?
I would separate the header content from the other content (for readability and semantic structuring of the column) and use flex to create two containing divs inside the parent (one for the header and one for the content) - and apply the vertical centering to the content area.
I have added outlines to show the different divs and aligned the text to the center of the column too.
.wrapper {
display:flex;
flex-direction: column;
outline: solid 1px red;
outline-offset: -1px;
width: 300px;
height: 100vh;
}
h2, p {margin: 0}
.header {
padding: 8px;
text-align: center;
flex-shrink:0;
outline: solid 1px blue;
outline-offset: -4px;
}
.content {
padding: 8px;
text-align: center;
flex-grow:1;
outline: solid 1px green;
outline-offset: -4px;
display:flex;
flex-direction: column;
justify-content: center;
}
<div class="wrapper">
<div class="header">
<h2>heading</h2>
</div>
<div class="content">
<p>content</p>
<p>content</p>
<p>content</p>
</div>
</div>
Please press "Run code snippet" and see the code. Stop useing position absolute for normal positioning things.
Use 1-2 hour here -> https://flexboxfroggy.com/ and you will be good at flexbox.
.flex {
display: flex;
}
.jcc {
justify-content: center;
align-items: center;
}
.column {
flex-direction: column;
}
<div class="flex jcc column">
<h2>I want this header to be at the top</h2>
<h3>let this be centered</h3>
</div>

How do I arrange the elements of my flex box into this pattern using css

How I want my elements arranged
this just made all the elements go side by side
Using display: flex alone will not do what you want. You should refer to a flexbox tutorial.
You could wrap the three vertically stacked articles in a div and use flexbox to align them vertically and the section could be used to align the articles and the aside like this:
section {
width: 400px;
height: 250px;
border: 1px solid black;
display: flex;
}
.articles {
display: flex;
flex-direction: column;
flex-grow: 1;
}
article {
border: 1px solid black;
flex-grow: 1;
background-color: blue;
}
aside {
height: 100%
border: 1px solid black;
width: 30%;
background-color: red;
}
<section>
<div class="articles">
<article id="1"></article>
<article id="2"></article>
<article id="3"></article>
</div>
<aside></aside>
</section>
Note the borders and background-colors are just to visualise the sections.
The flex-grow allows the articles to expand to fill in the space according to a ratio. In this case, by setting it to 1 they expand to 100% of the remaining space, making there only a need to specify the width of the aside.

Flexbox grid: How to display 3 items side-by-side but varying vertical positions

The red boxes are the items, the grey background is the container:
I have 3 items that I want to display in a container. Please see the included image.
What is the best way to go about this using flexbox? It should be the same on mobile view.
Thanks
Use flexbox, with nth-child to change the specific heights of the flex objects.
EX:
//HTML
<div class="container">
<div class="flex"></div>
<div class="flex"></div>
<div class="flex"></div>
</div>
//CSS
.container{
background: #AAA;
border: 1px solid black;
display: flex;
height: 15vw;
width: 20vw;
}
.flex{
background: #F00;
height: 7vw;
margin-left: 1.25vw;
width: 5vw;
}
.flex:nth-child(1){
margin-top: 6vw;
}
.flex:nth-child(2){
margin-top: 1vw;
}
.flex:nth-child(3){
margin-top: 5vw;
}
See an example here:
https://jsfiddle.net/mn8ukbae/7/
with flex you have the align-self property, which can be used to align an element on the cross-axis differently from the others, within the same flex parent.
.container { display: flex; }
.item--1 { align-self: flex-end; }
.item--2 { align-self: flex-start; }
.item--3 { align-self: center; }
I could almost bet my life that this is a 'homework' exercise, and you would do well to read up on flex and align-self to make the most of it.

Issue in aligning text vertically inside a tag

I've researched this and tried all the solutions and yet the text is not vertically aligned.
jsfiddle: http://jsfiddle.net/9a6pdfbt/
html:
<a>משטרה</a>
css:
a {
font-size: 2rem;
border: 2px solid black;
padding: 10px;
display: flex;
align-items: center;
vertical-align: middle;
}
If you use ctrl+shift+c to examine the a tag, you can see that the text is aligned to the bottom of the a tag and not in the exact middle
If you're trying to align your text in the center, vertically, there are multiple ways you can do it.
One way is to use absolute positioning like so (won't work for Bootstrap Columns):
.parent-class {
position: relative;
}
.parent-class a {
position: absolute;
top: 50%;
width: 100%;
transform: translateY(-50%);
}
And in your HTML:
<div class="parent-class">
<a>משטרה</a>
</div>
If you're using Flexbox, all you need is this in your CSS:
a.flexbox {
display: flex;
align-items: center;
justify-content: center;
}
And in your HTML:
<a class="flexbox">משטרה</a>
Unless you specify a height, the text should always be vertically aligned.
a {
font-size: 2rem;
border: 2px solid black;
padding: 20px;
display: flex;
align-items: center;
vertical-align: middle;
background-color: red;
}
<a>משטרה</a>
If you do specify a height, the flexbox should do it all for you. What would break your code there would be the display: inline-block;. I fixed the Fiddle for you: http://jsfiddle.net/3L5d7awj/

Prevent block stretching in flexbox in IE

I will provide code right away to explain my problem:
HTML:
<div class="outer">
<div class="inner">abc</div>
</div>
CSS:
.outer {
display: flex;
flex-wrap: wrap;
align-content: flex-start;
background-color: blue;
height: 500px;
}
.inner {
background-color: yellow;
min-height: 40px;
display: inline-flex;
width: 60%;
background-color: yellow;
border: 1px solid black;
}
As you can see the inner block is vertically streched, however, if you add another inner block, it behaves properly.
It seems to be an issue just in IE. Is there a way around to make the inner block not strech (when there is just one inside of the outer block)?

Resources