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
Related
I have to create a div over which there is a 3px border, and this boundary is positioned over the content in the div, how can I do this without knowing the size of the block?
An example is in the image below:
My code: https://codepen.io/pen/yLObXvv
HTML:
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-md-4">
<div class="case-study">
<div class="case-study-image">
<img src="https://images.unsplash.com/photo-1551434678-e076c223a692?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" class="img-fluid" alt="Intro image"/>
</div>
<div class="case-study-content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
</div>
</div>
</div>
</div>
CSS:
body {
background-color: #04142d;
}
.case-study {
color: #fff;
display: flex;
margin-top: 2rem;
flex-direction: row;
background-color: #0E53DD;
border-radius: 1rem;
overflow: hidden;
}
.case-study-image {
flex: 0 0 50%;
width: 50%;
}
.case-study-image img {
height: 100%;
max-height: 20rem;
object-fit: cover;
oject-position: 0 0;
}
.case-study-content {
flex: 0 0 50%;
padding: 1rem;
}
Please Use CSS ::after Selector with position: absolute
The coordinates of an absolute positioned element are relative to its parent. It is positioned automatically to the starting point (top-left corner) of its parent element.
body {
background-color: #04142d;
}
.case-study {
color: #fff;
display: flex;
margin-top: 2rem;
flex-direction: row;
background-color: #0E53DD;
border-radius: 1rem;
position:relative;
max-width:500px;
}
img {
max-width:100%;
}
.case-study-image {
flex: 0 0 50%;
width: 50%;
}
.case-study-image img {
height: 100%;
max-height: 20rem;
object-fit: cover;
oject-position: 0 0;
}
.case-study-content {
flex: 0 0 50%;
padding: 1rem;
}
.case-study:after {
content: '';
border: 3px solid yellow;
position: absolute;
width: calc(100% - 6px);
height: calc(100% - 6px);
border-radius: 5px;
right: -10px;
top: -10px;
}
<div class="case-study">
<div class="case-study-image"> <img src="https://images.unsplash.com/photo-1551434678-e076c223a692?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80" class="img-fluid" alt="Intro image"/> </div>
<div class="case-study-content">
<p>Lorem ipsum dolor sit amet</p>
</div>
</div>
<!DOCTYPE html>
<html>
<head>
<title>style</title>
<style>
div {
width: 200px;
height: 200px
background-color:white;
}
.move {
transform: (30px, 180px)
background: transparent;
border: 3px solid red;
}
</style>
</head>
<body>
<div class="move"></div>
<div></div>
</body>
</html>
you can use this and try to style it the way you want
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>
Essentially this is what I'm getting...
While this is what I want.
I have the image height and width set up like this...
#content img{
padding: 3%;
width: 60%;
height: 50%;
float: left;
}
With %s as I want it to be responsive and all. So the exact height of the image in px I can't really tell.
When I try to set up the same dimensions for the gray box, it only fills up with what is in it as you can see.
#text{
padding: 3%;
margin-right: 3%;
margin-top: 3%;
width: 37%;
height: 50%;
float: left;
background-color: #333333;
color: #FFFFFF;
}
Anyway on how to go about this? I'm also starting to think the problem may be I'm trying to make it responsive incorrectly.
Thanks in advance.
EDIT: Here is the HTML
<div id="content">
<img src="projectphotos/1.jpg">
<span class="arrows" style="float: right;"><i class="fa fa-angle-`double-right fa-3x"></i></span>`
<div id="text">
Test
</div>
</div>
You can use Flexbox
body, html {margin: 0; padding: 0}
.content {
display: flex;
}
.text {
background: #333333;
color: white;
flex: 1;
margin: 10px;
}
.image {
flex: 2;
margin: 10px;
}
img {
width: 100%;
vertical-align: top;
}
<div class="content">
<div class="image">
<img src="http://placehold.it/900x450">
</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil, id.</div>
</div>
just add display: flex to #content and remove the float: left from both.
I want to create the following layout:
where the blue block is an image and the red and green blocks contain vertically centered text. The container needs to have position:fixed, the image is sized dynamically so that its height is set to the height of the container and the red and green boxes are of equal height and fill the remainder of the container horizontally.
I initially tried using divs:
body {
padding: 0;
margin: 0;
border: 0;
}
.container {
height: 15vh;
width: 100vw;
position: fixed;
background-color: red;
}
.imgContainer {
height: 100%;
float: left;
}
.imgContainer img {
height: 100%;
}
.textContainer {
height: 100%;
background-color: yellow;
text-align: right;
display: table;
table-layout: fixed;
float: right;
}
.row1 {
height: 50%;
width: 100%;
display: table-row;
}
.row2 {
height: 50%;
background-color: blue;
display: table-row;
}
span {
display: table-cell;
vertical-align: middle;
}
<div class="container">
<div class="imgContainer">
<img src="http://ingridwu.dmmdmcfatter.com/wp-content/uploads/2015/01/placeholder.png" />
</div>
<div class="textContainer">
<div class="row1">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</div>
<div class="row2">
<span>More text.</span>
</div>
</div>
</div>
This worked fine for the image but I couldn't figure out how to get the red and green divs to fill the remaining width satisfactorily.
My second attempt was based around tables but, again, I don't seem to be able to get the widths correct:
body {
background-color: red;
padding: 0;
border: 0;
margin: 0;
}
div {
background-color: yellow;
height: 15vh;
width: 100vw;
position: fixed;
}
table {
height: 100%;
width: 100%;
background-color: blue;
border-collapse: collapse;
table-layout: fixed;
}
tbody {
height: 100%;
width: 100%;
background-color: purple;
}
tr {
height: 50%;
width: 100%;
background-color: green;
padding: 0;
}
tr:last-child {
background-color: yellow;
}
td {
height: 100%;
padding: 0;
white-space: nowrap;
}
td:last-child {
max-width: 100%;
}
img {
max-height: 100%;
display: block;
}
<div>
<table>
<tbody>
<tr>
<td rowspan="2">
<img src="http://ingridwu.dmmdmcfatter.com/wp-content/uploads/2015/01/placeholder.png" />
</td>
<td>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</td>
</tr>
<tr>
<td>
More text.
</td>
</tr>
</tbody>
</table>
</div>
I have also had problems ensuring that both red and green sections remain at 50% of the total height, regardless of content.
How can I get either of these to work? Or is there a completely different approach that can work?
You don't say what your target market is, but since in most my work I only have to worry about the latest browser versions, this answer makes use of the new CSS flexbox. If you need compatibility with older browsers, see the 2nd set of code below.
body {
padding: 0;
margin: 0;
border: 0;
}
.container {
height: 15vh;
width: 100vw;
position: fixed;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.imgContainer {
height: 100%;
}
.imgContainer img {
height: 100%;
}
.textContainer {
height: 100%;
width: 100%;
}
.row1 {
background-color: red;
}
.row2 {
background-color: green;
}
.row1,
.row2 {
height: 50%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: flex-end;
overflow: hidden;
}
<div class="container">
<div class="imgContainer">
<img src="http://ingridwu.dmmdmcfatter.com/wp-content/uploads/2015/01/placeholder.png" />
</div>
<div class="textContainer">
<div class="row1">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</div>
<div class="row2">
<span>More text.</span>
</div>
</div>
</div>
Below is a solution that works in older browsers, except IE9 and below where the text will not be properly centered vertically. If that's a concern, you might be able to find something that works on this page, but not knowing all your limitations, I was unable to select the right solution.
body {
padding: 0;
margin: 0;
border: 0;
}
.container {
height: 15vh;
width: 100vw;
position: fixed;
}
.imgContainer {
height: 100%;
float: left;
}
.imgContainer img {
height: 100%;
}
.textContainer {
height: 100%;
}
.row1 {
height: 50%;
background-color: red;
}
.row2 {
height: 50%;
background-color: green;
}
span {
right: 0; /* right-justify */
}
.row1 > span {
position: absolute;
top: 25%; /* put the top 25% down within .container - the first non-static ancestor element */
transform: translateY(-50%); /* nudge the line up half it's height */
}
.row2 > span {
position: absolute;
top: 75%;
transform: translateY(-50%);
}
<div class="container">
<div class="imgContainer">
<img src="http://ingridwu.dmmdmcfatter.com/wp-content/uploads/2015/01/placeholder.png" />
</div>
<div class="textContainer">
<div class="row1">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</div>
<div class="row2">
<span>More text.</span>
</div>
</div>
</div>
I have this situation:
I want the table on the right side to be at the same height as the form on the left side.
Here is my CSS:
.left {
height: auto;
width: 550px;
}
.right {
height: auto;
width: 550px;
padding-left: 40px;
}
.innerRight {
height: auto;
}
.middle {
height: auto;
position:absolute;
left:50%;
top:17%;
bottom:15%;
border-left:1px solid grey;
}
.left, .right {
display: inline-block;
}
How can I do this?
I used floating, but then the height doesn't adjust automatically, so I can't used it.
You could give them both position:absolute; then position them with top, left, right, ect..
Without your actual HTML code, here is an example that might help you by using the table-row, table-cell css property.
.wrap {
overflow: hidden;
width: 250px;
display: table;
border-collapse: collapse;
}
.row {
display: table-row;
}
.left {
width: 50%;
display: table-cell;
background-color: yellow;
}
.middle {
border-left: solid 1px red;
width: 1px;
display: table-cell;
}
.right {
width: 50%;
background-color: orange;
display: table-cell;
}
<div class="wrap">
<div class="row">
<div class="left">Lorem</div>
<div class="middle"></div>
<div class="right">Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<br/>
<br/>
<br/>
<br/>
<br/>
</div>
</div>
</div>