How to move flex content to top of div? - html

Stubborn text will not move to the top of the div.. no matter what i do? Please help
<div class="totals" style="display:flex; align-content: flex-start; justify-content: space-between; width:100%; background-color:green;">
<p style="color:#0e487f; background-color:red;">£1,000</p>
<p style="color:#0e487f;">£50,000</p>
</div>
Thanks

The margin from the p elements pushing the text down. You can easily see this if you right-click on the element and select "Inspect element".
Remove the margin, and add a padding to .totals if you still want the spacing (I haven't added a padding in the code).
Never ever use styles; use classes.
.totals {
display: flex;
align-content: flex-start;
justify-content: space-between;
width: 100%;
background-color: green;
}
.value {
color: #0e487f;
margin:0px; /* ADDED*/
}
.red.value {
background-color: red;
}
<div class="totals">
<p class="red value">£1,000</p>
<p class="value">£50,000</p>
</div>

First , you have margin and maybe padding that you need to remove , i advise you to always start clean from margins and padding
* {
padding : 0 ;
margin : 0;
}
Second , if the parent element do sent have height then the flex items wont flex simply because there is no room
just add height ,remove padding your code is working and use classes

It's on top but you have a margin top and a margin bottom. You can see it with the inspector. It's a default CSS property of the p tag. Use padding instead on your div
#p1{
background-color:red;
}
p{
#0e487f;
margin:0;
}
.totals{
display:flex;
align-content: flex-start;
justify-content: space-between;
width:100%; background-color:green;
}
<div class="totals">
<p id="p1">£1,000</p>
<p>£50,000</p>
</div>
An advice: don't use CSS inside html tag, create a separate file and call it in your page / template. In the worst case, use style tag and insert your CSS inside it.

<style>
.totals{
width: 100%;
height: 50px;
position: relative;
background-color: green;
}
.first{
color:#0e487f;
background-color:red;
position: absolute;
left:0;
top:-15px;
}
.second{
color:#0e487f;
background-color:red;
position: absolute;
right: 0;
top:-15px;
}
</style>
<div class="totals">
<p class="first">£1,000</p>
<p class="second">£50,000</p>
</div>

.totals {
display: flex;
align-items: flex-start; /* use align-items: flex-start */
justify-content: space-between;
width: 100%;
height: 50px;
background-color: green;
}
.m-0{
margin:0px; /* ADDED*/
}
.red {
background-color: red;
}
<div class="totals">
<p class="red m-0">£1,000</p>
<p class="m-0">£50,000</p>
</div>

Related

How do I center two child divs where each of those divs each have their own width? For example, one is a content area and the other a sidebar?

I am new to css - thanks in advance for the help.
I have a parent div with two child divs in it. I want it so that the parent div spans 100% of the window and then to have the combined width of the two child divs be a max-width and centered within the parent div.
Here is what I have so far:
.learnings-main-content > * {
display: flex;
justify-content: center;
flex-basis: 100%;
padding: 20px;
background-color: #ef972d;
}
#learnings-main-content-area1 {
/*left side where content will live*/
}
#learnings-main-content-area2 {
/*right side where content will live*/
}
Thanks for the help, this has been a real pain and I am sure it's simple enough.
*{
margin:0;
box-sizing:border-box;
}
.learnings-main-content {
height:100vh;
width:100vw;
display: flex;
}
#learnings-main-content-area1 {
background-color:red;
flex:1;
}
#learnings-main-content-area2 {
background-color:blue;
flex:1;
}
<div class="learnings-main-content">
<div id="learnings-main-content-area1">
sa
</div>
<div id ="learnings-main-content-area2">
sa
</div>
</div>
Just change your CSS class .learnings-main-content to the example below:
.learnings-main-content {
display: flex;
justify-content: center;
flex-basis: 100%;
padding: 20px;
background-color: #ef972d;
}

Align first content center and second content extreme right in div

I have to align two contents in div as per below screenshot. I do not want to use flex as I am completely new to it. I have bootstrap3 library in my project
first content exactly at center
second content should be at extreme right. very minimum space at right corner will be ok.
I checked, float:right but it is not aligning second content at extreme right. how to fix?
Use display: flex and add invisible element. Then use justify-content: space-between; to align the items one on the left, one center and one on the right.
If you need vertical center use align-items: center;
Learn more on flex here. The bootstrap documentation does great job visualizing more configurations.
.parent {
display: flex;
justify-content: space-between;
align-items: center;
border: 1px solid black;
width: 200px;
height: 40px;
}
.left-child {
visibility: hidden;
}
.center-child {
background: red;
width: 10px;
height: 20px;
}
.right-child {
background: red;
width: 10px;
height: 20px;
}
<div class="parent">
<div class="left-child"></div>
<div class="center-child"></div>
<div class="right-child"></div>
</div>
In my answer, you have to change the margin-top of the right content as the font-size or height of the center content
<style type="text/css">
.center{
text-align:center;
margin:auto;
display:block;
}
.right{
float:right;
margin-top:-18px;
}
</style>
<span class="center">Center</span>
<span class="right">Right</span>

How do I align this row with flexbox?

Im trying to get an image to be in the same row as my text in my ABOUT section.
I've tried to wrap my <div class="portrait"> and <p> into a <div class="wrapper"> with css flex-direction:row
Heres a pen: https://codepen.io/anon/pen/MqRdjb
I think some CSS is being overwritten, or I'm not using my CSS properly.
Thanks for any and all help!!
flex-direction: row has no effect without including display: flex.
Adjust the wrapper css to:
.wrapper {
flex-direction: row;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto;
}
You can also refactor the margins for the elements inside the wrapper if you want to center them. Any vertical margins can be removed and applied to the wrapper itself.
.portrait{
width:100px;
height:150px;
margin-left: auto;
margin-right: 10px; /* Align your image right */
z-index: 1;
border-radius: 10%;
background-color:#555;
}
.ABOUT p{
font-size:1.2em;
text-align: center;
margin-right: auto; /* align text left */
}

CSS Flex stretching out links

So I am learning a bit more about using CSS flex instead of using static positioning of content. However, I have defined my link styles as well as bold styles. My guess is that it's adapting to the container that is in (which is using flex feature) and that is why it is stretching across the size of the container it is inside. My question now is, how do I fix this? I've seen that I can do "display:inline-block" on the link, but that has not fixed it.
Here is my code:
.container{
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
width: 80%;
margin: auto;
background-color:#fff;
overflow: hidden;
font-size: 15px;
line-height: 20px;
padding:1em;
}
.container > * {
padding: 15px;
-webkit-flex: 1 100%;
flex: 1 100%;
}
a{
text-decoration:none;
border-bottom-style:double;
border-bottom-width:2px;
color:#99d3df;
display:inline-block;
padding:0px;
overflow: hidden;
}
i{
display:inline-block;
color:#88bbd6;
text-decoration:italic;
}
And what I have:
This is a Google Link<BR>
Google is <i>extremely helpful</i>!
This is what it looks like for reference.
Problem image
It seems you missed the .container wrapper div in the markup you provided.
Let's look at this code:
<!-- HTML -->
<div class="container">
<span>This is a </span><a href="http://google.com">Google Link</a
</div>
<div class="container">
<span>Google is </span><i>extremely helpful</i>!
</div>
<!-- /HTML -->
/* CSS */
.container > * {
padding: 15px;
-webkit-flex: 1 100%;
flex: 1 100%;
}
Property flex with value of 1 100% means we tell the browser to style any elements (the asterisk *) nested in .container to have 100% width of their parent's width.
I would suggest you to just remove that part to fix the problem.
Here's my approach to your markup.
.container {
display: flex;
width: 80%; /* flexible value */
flex-direction: row; /* this is to make sure that we'll have side-to-side layout within .container */
}
a {
text-decoration: none;
border-bottom-style: double;
border-bottom-width: 2px;
color: #99d3df;
display: inline-block;
padding: 0px;
}
a, i{
margin-left: 5px; /* this is to give a 10px spacing */
}
<div class="container"><span>This is a </span>random link<span></span></div>
<div class="container"><span>Google </span><i>is extremely helpful! </i></div>
It is working fine when I tried your code in js fiddle
see in this image
May be some other css is affecting your links to stretch it out.

Align Div at bottom on main Div

I have two divs, one inside the other and I would like the small div to be aligned at the buttom of the main div.
Here's my code so far. Currently, the button is at the top of main div, instead I want it positioned at the bottom. Any help would be appreciated.
.vertical_banner {
border: 1px solid #E9E3DD;
float: left;
height: 210px;
margin: 2px;
padding: 4px 2px 10px 10px;
text-align: left;
width: 117px;
}
#bottom_link{
vertical-align: bottom;
}
Here's how I've been trying to use it, but as you can see I'm still new to CSS :)
<div class="vertical_banner">
<div id="bottom_link">
<input type="submit" value="Continue">
</div>
</div>
Modify your CSS like this:
.vertical_banner {
border: 1px solid #E9E3DD;
float: left;
height: 210px;
margin: 2px;
padding: 4px 2px 10px 10px;
text-align: left;
width: 117px;
position:relative;
}
#bottom_link{
position:absolute; /* added */
bottom:0; /* added */
left:0; /* added */
}
<div class="vertical_banner">
<div id="bottom_link">
<input type="submit" value="Continue">
</div>
</div>
Give your parent div position: relative, then give your child div position: absolute, this will absolute position the div inside of its parent, then you can give the child bottom: 0px;
See example here:
http://jsfiddle.net/PGMqs/1/
This isn't really possible in HTML unless you use absolute positioning or javascript. So one solution would be to give this CSS to #bottom_link:
#bottom_link {
position:absolute;
bottom:0;
}
Otherwise you'd have to use some javascript. Here's a jQuery block that should do the trick, depending on the simplicity of the page.
$('#bottom_link').css({
position: 'relative',
top: $(this).parent().height() - $(this).height()
});
I guess you'll need absolute position
.vertical_banner {position:relative;}
#bottom_link{position:absolute; bottom:0;}
Please try this:
#b {
display: -webkit-inline-flex;
display: -moz-inline-flex;
display: inline-flex;
-webkit-flex-flow: row nowrap;
-moz-flex-flow: row nowrap;
flex-flow: row nowrap;
-webkit-align-items: flex-end;
-moz-align-items: flex-end;
align-items: flex-end;}
Here's a JSFiddle demo: http://jsfiddle.net/rudiedirkx/7FGKN/.
Another way to do it is to add a margin-top: auto and margin-bottom: 0. This tells the margin top to fill in the missing space.