I want to align elements inside div
a - > to the top
b - > to the bottom
c - > above b element (but also to the bottom of the div container)
#container { position: relative}
#a { position: absolute; top: 0px;}
#b { position: absolute; bottom: 0px;}
#C {????}
I didn't find a way for C
You can use another container for below divs.
<div id="container">
<div id="a"></div>
<div id="container-below">
<div id="b"></div>
<div id="c"></div>
</div>
</div>
#container { position: relative}
#a { position: absolute; top: 0px;}
#container-below { position: absolute; bottom: 0px;}
You can put c inside b and position it absolutely with bottom: 100%, thus c will always be on top of b.
.d {
position: relative;
height: 200px;
width: 200px;
float: left;
margin: 0 5px;
border: 1px solid green;
}
.a, .b, .c{
position: absolute;
width: 100%;
min-height: 40px;
left: 0;
}
.a {
top: 0;
background: lightgreen;
}
.b {
bottom: 0;
background: tomato;
}
.c {
bottom: 100%;
background: orange;
}
<div class="d">
<div class="a">a
</div>
<div class="b">b
<div class="c">c
</div>
</div>
</div>
<div class="d">
<div class="a">a
</div>
<div class="b"> Block b Lorem ipsum dolor amet Lorem ipsum dolor amet Lorem ipsum dolor amet Lorem ipsum dolor amet Lorem ipsum dolor amet Lorem ipsu
<div class="c">c
</div>
</div>
</div>
please start using flexbox and soon css grids :P
.p > div {
width: 100px;
height: 100px;
border: 1px solid red
}
.p {
height: 500px;
display: flex;
background-color: beige;
width: 200px;
flex-direction: column;
}
.b {
margin-top: auto;
}
<div class="p">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</div>
Related
I have two div's.
For first div I have position:absolute and for second div I just have margin-top:50px.
Assuming the first div (which one have position absolute) has some data from backend
No matter how my text is long, how can I always have 50px gap from first div to second
my code
.box1 {
position: absolute;
top: 0;
border: 1px solid red;
width: 100px;
}
.box2 {
margin-top: 50px;
border: 1px solid green;
width: 100px;
}
<div class="box1">
Lorem Ipsum is simply
</div>
<div class="box2">box 2</div>
This should do the job
.wrap{
position: absolute;
top: 0;
}
.box1 {
border: 1px solid red;
width: 100px;
word-break: break-all;
}
.box2 {
position:absolute;
margin-top: 50px;
border: 1px solid green;
width: 100px;
}
<body>
<div class="wrap">
<div class="box1">
Lorem Ipsum is
</div>
<div class="box2">box 2</div>
</div>
</body>
Better solution don't fix your first div with position: absolute;
I propose you this solution
.main {
display: block;
}
.box1 {
top: 0;
border: 1px solid red;
width: 100px;
}
.box2 {
margin-top: 50px;
border: 1px solid green;
width: 100px;
}
<div class="main">
<div class="box1">
Lorem Ipsum is simply
Lorem Ipsum is simply
Lorem Ipsum is simply
Lorem Ipsum is simply
</div>
<div class="box2">box 2</div>
</div>
I am using only CSS and Flexbox to build a responsive page. I have a child element that should "overflow" outside the parent element as shown here:
<div class="container-hero">
<div class="hero-content">
<h1>Tech Challenge</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit </p>
</div>
<div class="hero-img">
<img src="assets/image-1.jpg">
</div>
</div>
CSS
.container-hero {
display: flex;
justify-content: flex-start;
align-items: center;
overflow: hidden;
position: relative;
margin: 40px 0;
}
.hero-img {
flex-shrink: 0;
min-width: 100%;
min-height: 100%;
}
.hero-img img {
width: 100%;
height: auto;
}
.hero-content {
background-color: #D64C31;
color: #FFFFFF;
align-self: flex-end;
width: 50%;
position: absolute;
bottom:0;
left:0;
padding: 40px 60px;
}
Any help would be appreciated!
Like that?
<html>
<head>
<style>
.container {
background: #ccc;
width: 50%;
margin: 0 auto;
position: relative;
height: 700px;
}
.overflowing-element {
background: red;
width: 200px;
height: 200px;
position: absolute;
right: -200px;
top: 0;
}
</style>
</head>
<body>
<div class="container">
test
<div class="overflowing-element">
bla
</div>
</div>
</body>
</html>
Just works with fixed width of that overflowing element, or with JavaScript.
EDIT: You just edited your images and now I don't know really what you mean :D
I figure it out, thank you for your help!
My parent element had an overflow: hidden I disabled it and adjusted the child element as follows:
bottom: -40px
If you have any feedback or this is considered a bad practice please let me know. I am just starting out here :)
.container-hero {
display: flex;
justify-content: flex-start;
align-items: center;
/* overflow-x: hidden; */
position: relative;
margin: 40px 0;
}
.hero-img {
flex-shrink: 0;
min-width: 100%;
min-height: 100%;
}
.hero-img img {
width: 100%;
height: auto;
}
.hero-content {
position:absolute;
background-color: #D64C31;
color: #FFFFFF;
width: 50%;
padding: 40px 60px;
bottom: -20px;
left:0;
}
</div>
<div class="container-hero">
<div class="hero-content">
<h1>Tech Challenge</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit </p>
</div>
<div class="hero-img">
<img src="https://source.unsplash.com/800x300">
</div>
</div>
The property you are looking for is CSS Position.
Reference: CSS Position
.parent{
width:250px;
height: 20px;
background: yellow;
position:relative;
}
.child{
width:80px;
height: 100px;
background: purple;
position:absolute;
bottom: 0;
right:0;
}
<div class="parent">
<div class="child"></div>
</div>
Use the CSS positioning properties.
.container-hero {
position: relative; /* creates the container for absolutely positioned children */
}
.hero-content {
position: absolute;
bottom: -20px; /* use this offset to align vertically */
left: 20px; /* use this offset to align horizontally */
background-color: #D64C31;
color: #FFFFFF;
width: 225px;
padding: 40px 60px;
}
<div class="container-hero">
<div class="hero-content">
<h1>Tech Challenge</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit </p>
</div>
<div class="hero-img">
<img src="https://via.placeholder.com/500x250.png?text=hero image">
</div>
</div>
I'm having a lot of trouble trying to make a layout. I'm trying to make a box overlap an image, I'm trying to use absolute/relative positions but I think I'm doing it wrong. here is the layout
here is the code i have so far:
.trabalhos{
position: relative;
margin-top: 100px;
width: 100%;
max-width: 960px;
}
.caixa{
position: absolute;
left: 0%;
top: auto;
right: auto;
bottom: 10%;
overflow: visible;
width: 30%;
height: auto;
padding: 25px;
border-radius: 10px;
background-color: #333;
color: white;
}
#trabalhos-img-right{
float: right;
}
#trabalhos-img-left{
float: left;
}
#esquerda{
text-align: right;
}
#direita{
text-align: left;
right: 0%;
left: auto;
}
<div class="section-trabalhos">
<div class="container">
<h1>blablablablabla</h1>
<div class="trabalhos">
<div><img id="trabalhos-img-right" src="img-01.png"></div>
<div class="caixa" id="esquerda">
<h2>Lorem ipsum</h2>
<p>blablabla</p>
</div>
</div>
<div class="trabalhos trabalhos_2">
<div><img id="trabalhos-img-left" src="img-01.png"></div>
<div class="caixa" id="direita">
<h2>Lorem ipsum</h2>
<p>blablabla</p>
</div>
</div>
I'll have to make 8 of those, what is the best way?
Thanks in advance
I hope that I have solved this problem
<html>
<head>
<style>
.trabalhos{
width:100%;
max-width: 960px;
height: 60%;
margin-top: 100px;
position: relative;
}
.img{
width:80%;
height: 100%; /*Do not change it*/
}
.caixa{
position: absolute;
bottom:10%;
overflow: visible;
width: 30%;
height: auto;
padding: 25px;
border-radius: 10px;
background-color: #333;
color: white;
}
#trabalhos-img-right{
float: right;
}
#trabalhos-img-left{
float: left;
}
#esquerda{
text-align: right;
left: 0px
}
#direita{
text-align: left;
right: 0px;
}
#trabalhos-img{
width: 100%; /*Do not change it*/
height: 100%; /*Do not change it*/
}
</style>
</head>
<body>
<div class="trabalhos">
<div id="trabalhos-img-right" class="img">
<img id="trabalhos-img" src="img-01.png" /></div>
<div class="caixa" id="esquerda">
<h2>Lorem ipsum</h2>
<p>blablabla</p>
</div>
</div>
<div class="trabalhos">
<div id="trabalhos-img-left" class="img">
<img id="trabalhos-img" src="img-01.png" /></div>
<div class="caixa" id="direita">
<h2>Lorem ipsum</h2>
<p>blablabla</p>
</div>
</div>
</body>
This CodePen could be the solution. Just replace the svg element in the html and css code with your image and you're all set.
Also, this website can help you understanding the position property.
HTML
<div class="container">
<svg width="400" height="110">
<rect width="500" height="500" style="fill:rgb(0,0,255);stroke-width:3;"/>
</svg>
<div class="description">
<h1>Lorem Ipsum</h1>
<h2>Lorem ipsum dolor sit amet</h2>
</div>
</div>
<div class="containersecond">
<svg width="400" height="110">
<rect width="500" height="500" style="fill:rgb(0,0,255);stroke-width:3;"/>
</svg>
<div class="description">
<h1>Lorem Ipsum</h1>
<h2>Lorem ipsum dolor sit amet</h2>
</div>
</div>
CSS
svg {
position: relative;
left: 30px;
}
.description {
background-color: red;
width: 300px;
position: absolute;
top: 70px;
}
.containersecond {
position: absolute;
top: 300px;
}
I imagine CSS looks out of place in my example but these styles are added by library, particularily styles in #top, #middle, #bottom. I need .header elements to stick to top.
I've tried all kinds of styles but no luck. Feel free to overwrite or add any styles. How to make this work with the HTML structure I have?
#top {
display: flex;
position: relative;
z-index: 0;
overflow: hidden !important;
max-height: 300px;
max-width: 300px;
border: 2px dashed #ec6161;
}
#middle {
padding-right: 19px;
margin-bottom: -34px;
overflow: scroll;
overflow-x: hidden !important;
min-width: 100%! important;
max-height: inherit !important;
box-sizing: content-box !important;
}
#bottom {
padding-bottom: 17px;
margin-right: -19px;
overflow: scroll;
overflow-y: hidden !important;
box-sizing: border-box !important;
min-height: 100% !important;
}
.header {
position: sticky;
top: 0;
z-index: 1;
background: #eee;
padding: 5px 7px;
font-weight: bold;
}
.content {
height: 100px;
padding: 5px 7px;
}
<div id="top">
<div id ="middle">
<div id="bottom">
<div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
</div>
</div>
</div>
</div>
Also: https://jsfiddle.net/bkn0e3gL/2/
Setting overflow: hidden on any parent divs seems to be causing the problem.
Check out this answer:
if you set overflow to hidden on any ancestor of your sticky element, then this ancestor element will be the scrolling container for your sticky element.
.header {
background: #B8C1C8;
border-bottom: 1px solid #989EA4;
border-top: 1px solid #717D85;
color: #FFF;
position: -webkit-sticky;
position: sticky;
top: -1px;
}
.content {
min-height: 200px;
}
#bottom {
padding-bottom: 17px;
margin-right: -19px; //overflow: scroll;
//overflow-y: hidden !important;
box-sizing: border-box !important;
min-height: 100% !important;
}
#middle {
padding-right: 19px;
margin-bottom: -34px; //overflow: scroll;
//overflow-x: hidden !important;
min-width: 100% ! important;
max-height: inherit !important;
box-sizing: content-box !important;
}
#top {
display: flex;
position: relative;
z-index: 0;
//overflow: hidden !important;
max-height: 300px;
max-width: 300px;
border: 2px dashed #ec6161;
}
<div id="top">
<div id="middle">
<div id="bottom">
<div>
<div>
<div class="header">Header</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque viverra.</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque viverra.</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque viverra.</div>
</div>
<div>
<div class="header">Header</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque viverra.</div>
</div>
</div>
</div>
</div>
</div>
This question already has answers here:
How to align 3 divs (left/center/right) inside another div?
(21 answers)
Closed 6 years ago.
How can i make 3 divs side by side. where div1 would be extreme left ,div3 would be extreme right and div2 in the middle.
I know this can be done by display:flex and justify-content:space-between ,but i am looking for an approach without flex.
Here is my approach ,but could not achieve it successfully.
I tried to make all div's display:inline-block and float:left and float:right to the two extreme divs and for the middle one i tried margin:auto,but looks like it is not respecting it
Please help
.container {
border: 1px solid;
}
.container div {
height: 50px;
width: 50px;
background: red;
display: inline-block;
}
#div1 {
float: left;
}
#div3 {
float: right;
}
#div2 {
margin: auto;
}
<div class="container">
<div id="div1"></div>
<div id="div2">he</div>
<div id="div3"></div>
</div>
try this one. position: absolute;
.container {
border: 1px solid;
position: relative;
overflow: hidden;
}
.container div {
height: 50px;
width: 50px;
background: red;
display: inline-block;
}
#div1 {
float: left;
}
#div3 {
float: right;
}
#div2 {
position: absolute;
left: 0;
right: 0;
margin: auto;
}
<div class="container">
<div id="div1"></div>
<div id="div2">he</div>
<div id="div3"></div>
</div>
this is what your code will be
.container {
border: 1px solid;
}
.container div {
height: 50px;
width: 50px;
background: red;
display: inline-block;
}
#div1 {
float: left;
background-color:red;
}
#div3 {
float: left;
background-color:green;
}
#div2 {
float: left;
background-color:yellow;
}
and
<div class="container">
<div id="div1">div 1</div>
<div id="div2">div 2</div>
<div id="div3">div 3</div>
</div>
Kindly check below CSS see if this is what you want:
.container {
float: left;
width: 300px;
border: 1px solid;
}
#div1 {
float: left;
width: 100px;
background-color: red;
}
#div2 {
float: left;
width: 100px;
background-color: green;
}
#div3 {
float: left;
width: 100px;
background-color: blue;
}
<div class="container">
<div id="div1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ullamcorper eleifend volutpat. Class aptent taciti sociosqu ad litora torquent per conubia nostra.</div>
<div id="div2">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
<div id="div3">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ullamcorper eleifend volutpat.</div>
</div>
.container {
border: 1px solid;
position: relative;
}
.container div {
height: 50px;
width: 50px;
background: red;
display: block;
}
#div1 {
float: left;
}
#div3 {
position: absolute;
right: 0;
top: 0;
}
#div2 {
margin: auto;
}
<div class="container">
<div id="div1"></div>
<div id="div2">he</div>
<div id="div3"></div>
</div>
Don't use float and inline-block together. This will work:
<div class="container">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</div>
.container {
width:100%;
}
#div1, #div2, #div3 {
height: 50px;
background: red;
display: inline-block;
width:33.33%;
margin-right:-4px;
}
If you want to style the divs individually then target them individually