beginner question on flexbox with absolute content - html

i have a probably rather easy question. I gave my body a background gradient and set it as a flexbox. I can give my 2 text elements the position absolute and everything works fine. But as soon as i give my box (which should have the same relation to the body as the text elements) an absolute position, my background gradient disappears. With a relative positioning that doesnt happen. I know i can work with relative here as well, but I just want to know why this happens to my background gradient. Hope sb can help,
Thanks
body {
margin: 0;
height: 100%;
background-image: linear-gradient(
to right,
rgb(69, 174, 201),
rgb(169, 248, 253)
);
display: flex;
justify-content: center;
flex-wrap: wrap;
}
h2 {
text-align: center;
font-size: 40px;
width: 100%;
position: absolute;
}
.poben {
text-align: center;
font-size: 20px;
letter-spacing: 0.2em;
width: 100%;
position: absolute;
top: 60px;
}
.center-box {
/* if I change the position below to absolute, the page gets white */
position: relative;
height: 800px;
width: 200px;
background: linear-gradient(rgb(17, 17, 53), rgb(62, 55, 102));
border-radius: 2%;
box-shadow: 10px 10px 10px hsl(284, 100%, 9%);
}
<h2>Test survey form</h2>
<p class="poben">Thanks for talking part in this survey!</p>
<div class="center-box">
</div>

Position: Absolute; when you use this it basically removes the element from the flow of the document. It behaves like it's not present there. You can use left/right/top/bottom properties to move the element anywhere you want.
Position: Relative; when you use this, it basically keeps the element in the flow of the document. You can use left/right/top/bottom properties to move the element relative to its parent container.
In your code, the body is the parent container.
When you give the child elements of this container as position: absolute; it basically moves them out of that body container.
Now body thinks there is no child inside me and its height is now 0 i.e no content no value. You inspect that in the Chrome Inspector and check the box model to understand it.
body {
margin: 0;
padding: 1px; /* Filling the body with some content */
height: 100%;
background-image: linear-gradient(
to right,
rgb(69, 174, 201),
rgb(169, 248, 253)
);
display: flex;
justify-content: center;
flex-wrap: wrap;
}
h2 {
text-align: center;
font-size: 40px;
width: 100%;
position: absolute;
}
.poben {
text-align: center;
font-size: 20px;
letter-spacing: 0.2em;
width: 100%;
position: absolute;
top: 60px;
}
.center-box {
/* if I change the position below to absolute, the page gets white */
position: absolute;
height: 800px;
width: 200px;
background: linear-gradient(rgb(17, 17, 53), rgb(62, 55, 102));
border-radius: 2%;
box-shadow: 10px 10px 10px hsl(284, 100%, 9%);
}
<h2>Test survey form</h2>
<p class="poben">Thanks for talking part in this survey!</p>
<div class="center-box">
This might help - https://css-tricks.com/almanac/properties/p/position/

Related

Sticking/layering an image to top-left corner of div container

Been attempting to layer and image over the top left corner of a div container; iv achieved it once but it didn't stick to position if the page was adjusted!
^^ This is what im redesigning
^^ This is what ive managed to design myself
I have come across different posts and answers suggesting that I try to use:
display: block, relative
position: block, relative
margins, float: start
In the end I am stumped an have resorted to removing most displays besides for the p tags an its composed container:
This is the CSS I have been trying to use to make this happen
.roadmap__section__container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
min-height: 100%;
max-height: 100%;
background-color: #383636;
align-items: center;
}
.roadmap__header {
font: 3rem "Nunito";
margin: 3% 50% 3% 50%;
}
.roadmap__phase__one {
width: 100%;
height: 100%;
}
.phase__one__img {
width: 10%;
position: absolute;
margin-left:10%;
}
.phase__one__data {
display: flex;
flex-direction: column;
width: fit-content;
height: fit-content;
align-items: flex-start;
justify-content: center;
text-align: center;
padding: 25px;
margin: 0% 5% 5% 15%;
box-shadow: 1px 1px 40px #ff00e6;
outline-color: #ff00e6;
outline-offset: 0px;
outline-style: solid;
outline-width: 3px;
border-radius: 25px;
}
.phase__one__data p {
font-size: 1.75rem;
color: #fff;
font-family: "Nunito", serif;
padding: 5px 10px;
}
Here is the HTML Code used for my redesign:
<section class="roadmap__section__container">
<h1 class="roadmap__header">Roadmap</h1>
<div class="roadmap__phase__one">
<img class="phase__one__img" src="images/CasinoWRLD__dice1" alt="">
<div class="phase__one__data">
<p>- Working To Perfect The NFT Artwork.</p>
<p>- Finalise The Marketing Plan.</p>
<p>- Plan And Develop Casino WRLD.</p>
</div>
</div>
</section>
.phase__one__img {
grid-column: 3;
width: 11%;
position: absolute;
top: 157.5%;
right: 32%;
}
.phase__zero__img {
grid-column: 1;
width: 11%;
position: absolute;
margin-left: 5%;
top: 119.75%;
}
I've found this to work currently but it is not a 100% answer, if the browser height is adjust the settings regarding ''top:157.5%'' needs to be adjusted accordingly or it will not line up with the top-left corner of the div container
The difference in the two is pertaining to it switches back and forth going down the page

How can i get 4 divs next to each other [duplicate]

This question already has answers here:
How do you make div elements display inline?
(20 answers)
Closed 4 years ago.
Hello everyone i have a header with a div in my header with a pic in it but i want to display my pics next to eachother but instead of that its displays it under each other how can i fix this i tried to change my css but didnt work.
its now like this when i add anoter div
How it is now:
I want it like this:
this is my code
#import url('https://fonts.googleapis.com/css?family=Josefin+Sans:400,400i,600,600i');
html,
body {
margin: 0;
height: 120%;
font-family: 'Josefin Sans', sans-serif;
}
.header {
position: relative;
overflow: hidden;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: flex-start;
align-content: flex-start;
height: 50vw;
min-height: 400px;
max-height: 550px;
min-width: 300px;
color: #eee;
}
.header:before {
content: "";
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
-webkit-backface-visibility: hidden;
-webkit-transform: translateZ(0) scale(1.0, 1.0);
transform: translateZ(0);
background: #1B2030 url(https://unsplash.it/1999/999?image=1063) top center no-repeat;
background-size: cover;
background-attachment: fixed;
animation: grow 60s linear 10ms infinite;
transition: all 0.2s ease-in-out;
z-index: -2
}
.header a {
color: #eee
}
.info {
width: 100%;
padding: 25% 10% 0 10%;
text-align: center;
text-shadow: 0 2px 3px rgba(0, 0, 0, 0.2)
}
.author {
display: inline-block;
width: 50px;
height: 50px;
border-radius: 50%;
background: url(http://favim.com/media/uploads/images/610/140308/black-n-white-cute-funny-iron-man-Favim.com-1462744.jpg) center no-repeat;
background-size: cover;
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.3);
margin-bottom: 3px
}
.info h4,
.meta {
font-size: 0.7em
}
.meta {
font-style: italic;
}
.twtr {
margin-top: 100px
}
.btn.twtr:after {
content: "\1F426";
padding-left: 5px
}
<body>
<div class="header">
<div class="info">
<h1>TestHeader</h1>
<div class="meta">
test
</div>
<div class="meta">
<br> By James Nodws on May 30, 2017
</div>
</div>
</div>
</body>
You First Have clear the body from margin and padding just in case
body{padding:0;margin:0;}
after that give each div 25% width and display either inline or inline-block
div{width:25%;display:inline;}
Add :
.meta {
display: inline-block
}
div are int display mode block by default, so it wraps the entire ligne. Change to inline-block to allow multiple divs horizontally
You have two options here
You can use Bootsrap as described in here.
Use below CSS
.meta {
float:left;
width: 30%;
}
You can use any other class which is applied only for image divs.
I think you should try using .
.meta {
display : flex;
}
This also aligns all the the <divs> in a single row
you can use flex property for,
Display:flex apply flex layout to the flex items or children of the container only. So, the container itself stays a block level element and thus takes up the entire width of the screen. ... Display:inline-flex apply flex layout to the flex items or children as well as to the container itself.

Set caption to always begin in the photo's left corner

I have a photo caption. It works nicely when the browser is not
bigger than the photo size. When it is bigger, the photo is in the
center. I don't know how I can make the caption always begin in the photo's left corner.
HTML
<div>
<img id="pht" src="http://farm4.static.flickr.com/3065/2741653860_468fda7e7a.jpg">
<span id="cap">Photo Caption</span>
</div>
CSS
#pht{
position:relative;
display: block;
margin-left: auto;
margin-right: auto;
}
#cap{position:absolute;
background: rgba(0, 0, 0, 0.7) none repeat scroll 0 0;
color: yellow;
display: inline-block;
font: bold 13px/30px Helvetica,sans-serif;
letter-spacing: -1px;
padding-left: 5pt;
padding-right: 5pt;
}
fiddle: http://jsfiddle.net/qpxkypn8/
Try this..
HTML
<div>
<div id="dpht"><img id="pht" src="http://farm4.static.flickr.com/3065/2741653860_468fda7e7a.jpg"></div>
<span id="cap">Photo Caption</span>
</div>
CSS
div{
max-width: 500px;
margin: 0 auto;
}
#dpht{
display: flex;
flex-direction: row;
}
#pht{
display: block;
}
#cap{
flex: none;
background: rgba(0, 0, 0, 0.7) none repeat scroll 0 0;
color: yellow;
display: inline-block;
font: bold 13px/30px Helvetica,sans-serif;
letter-spacing: -1px;
padding-left: 5pt;
padding-right: 5pt;
}
Check out this Fiddle
Add the following CSS
div {
position: relative; /* positions absolutely-positioned
children relative to this element */
display: inline-block; /* makes the div only as large as its img child */
}
#cap {
top: 100%; /* these put #cap under the */
left: 0; /* bottom left corner of the img */
}
Updated JSFiddle
Are you able to alter the HTML? If so, you could use a container div to achieve the following:
http://jsfiddle.net/hkqgkee5/
To center a div using auto margins, you generally need to specify a width explicitly. Using display: table on the containing div will allow you to bypass that restriction.

How to put text inside border html

#border {
position: static;
z-index: 1;
width: 120px;
height: 120px;
margin-left: 92% ;
padding: 15px;
border-radius: 11px;
background: white;
opacity: 0.2;
}
#text {
margin-left: 93%;
z-index: 2;
color: white;
font-weight: bold;
}
<div id="border"></div>
<div id="text">Users online</div>
I can't post the image here, cuz I have less than 10 reputation, so try to imagine it please. I want to place it's "Users online" inside the border, how should I do this? Thanks.
I'm assuming you are trying to have an element with a semitransparent background.
Since you are using the opacity property on the element with an id of border.
The problem here is that z-index will not have any effect, if the position is set to static, which is the default value for div elements.
The other thing is, that you should be using a relative positioned parent to make your life easier and have more control over the elements since positioned elements will leave the normal document flow and result in new stacking order.
Here you can find good information on the the z-index property, stacking and the document flow.
This is one solution to your problem.
body {
background:black;
}
.holder {
position:relative;
}
#border {
position: absolute;
z-index:1;
right:0;
width: 120px;
height: 120px;
padding: 15px;
border-radius: 11px;
background: white;
opacity: 0.2;
}
#text {
position: absolute;
z-index:2;
right:0;
width: 120px;
height: 120px;
padding: 15px;
text-align: center;
color: white;
font-weight: bold;
}
<div class="holder">
<div id="border"></div>
<div id="text">Users online</div>
</div>
But i would actually try to solve this with a different approach, because i find the above solution a bit to complex and it involves to much positioning, so if all you need is a semitransparent background just make use of the background property with an rgba value. Here is an example.
.user-panel {
float:right;
height: 120px;
width: 120px;
padding: 15px;
border-radius: 11px;
/* fallback for browser that do not support rgba */
background: #ccc;
/* semitransparent background */
background: rgba(0, 0, 0, .2);
text-align: center;
color: white;
}
/* clear the float using the pseudo after element */
user-panel:after {
clear: both;
display: block;
visibility: hidden;
height: 0px;
}
<header>
<div class="user-panel">Users online</div>
</header>
Hope that helps.
Change
position: static;
to
position: absolute;
for #border. That way, border will be "removed from the flow" (i.e. other elements will ignore it). You may need to adjust the margin-left property for #text so it properly aligns.
Fiddle: https://jsfiddle.net/xzdmLt33/1/

Can't place my DIV 10px from bottom

would you have any idea why my red div (.testbox) is not placed 10px from bottom as I would like it to be and despite the margin-bottom: 10px? See http://jsfiddle.net/Hs977/
Thanks for your help
body {
background-color: rgb(255, 255, 255);
font-family: Helvetica;
font-size: 16px;
font-weight: 400;
line-height: 1.38;
color: rgb(21, 84, 244);
margin: 0;
margin: 0;
padding: 0;
border: 0;
}
header {
width: 100%;
height: 600px;
min-height: 300px;
margin-right: auto;
margin-left: auto;
background-image: url('http://lorempixel.com/output/nature-q-c-1020-711-1.jpg');
background-size: cover;
background-position: center center;
background-color: rgb(222, 222, 222);
}
h1 {
width: 100%;
margin-top: 39px;
margin-right: auto;
margin-left: auto;
font-size: 37px;
font-weight: 400;
line-height: 1.38;
text-align: center;
color: rgb(0,0,0);
}
.blablatext {
margin-top: 16px;
margin-right: auto;
margin-left: auto;
font-weight: 400;
line-height: 1.38;
text-align: center;
color: rgb(21, 84, 244);
}
.testbox {
float: right;
width: 200px;
height: 90px;
background: red;
margin-right: 20px;
margin-bottom: 10px;
}
Does not work that way. To place the div at a specified distance from the bottom of it's container, you need to use position:absolute on the div and set position:relative on the container. Then use bottom:10px; to place it 10px from it's parents bottom.
Updated your fiddle: http://jsfiddle.net/Hs977/1/
from the Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification
In the absolute positioning model, a box is explicitly offset with
respect to its containing block. It is removed from the normal flow
entirely (it has no impact on later siblings). An absolutely
positioned box establishes a new containing block for normal flow
children and absolutely (but not fixed) positioned descendants.
However, the contents of an absolutely positioned element do not flow
around any other boxes. They may obscure the contents of another box
(or be obscured themselves), depending on the stack levels of the
overlapping boxes.
since your header have height defined:
height: 600px;
min-height: 300px;
you can specify absolute position, then set bottom position
.testbox {
float: right;
width: 200px;
height: 90px;
background: yellow;
margin-right: 20px;
position:absolute;
bottom:0;
}
http://jsfiddle.net/InferOn/Hs977/8/
I don't know why would you define float for .textbox