Align divs with position relative horizontally - html

I have 3 divs inside a div and I want these 3 divs to align horizontally. I was able to do this by giving absolute position but I want them to have relative position. Why I want it to have relative position is, if I zoom out or zoom in, the div size won't change but the elements inside these divs change. I want the div to zoom out/in as well. That is why I want them to have relative position.
.body_clr {
background-color: #eceff1;
position: fixed;
overflow-y: scroll;
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
.test_div {
width: 20em;
height: 20em;
margin-left: 2em;
margin-top: 20%;
position: relative;
background-color: #f5f5f5;
display: inline-block;
float: left;
z-index: 1;
}
.ff {
width: 40em;
height: 100%;
padding-top: 10%;
position: relative;
background-color: #2aabd2;
float: left;
margin-left: 20%;
margin-right: 20%;
display: inline-block;
}
.overview {
width: 20em;
height: 35%;
background-color: black;
margin-top: 20%;
float: left;
margin-right: 5%;
position: relative;
display: inline-block;
z-index: 1;
}
<div className="body_clr">
<div className="test_div"></div>
<div className="ff"></div>
<div className="overview"></div>
</div>
Right now my divs are not aligned horizontally.

I would use flexbox for this. Take a look at this example I made: https://jsfiddle.net/cfLfLnLx/.
Also use class and not className to specify the classes of HTML elements.
A more extensive guide to using flexbox:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

You have some mistakes in your css:
You can't use display: inline-block with float: left ( you have to use float: left or display: inline-block )
If you use float: left you have to put a clear after the 2 floated divs ( always ) .
My solve with table-cell
* { box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; }
.body_clr { width: 100%; height: 100%; display: table; }
.col1 { height: 100%; display: table-cell; padding: 0 10px; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; }
.ff { width: 100%; height: 40px; display: block; background: #000;}
.test_div { width: 100%; height: 40px; display: block; background: red;}
.overview { width: 100%; height: 40px; display: block; background: blue;}
<div class="body_clr">
<div class=" col1">
<div class="test_div"></div>
</div>
<div class="col1">
<div class="ff"></div>
</div>
<div class="col1">
<div class="overview"></div>
</div>
</div>
With float:
.body_clr { width: 100%; height: 100%; display: block; }
.clear { width: 0; height: 0; padding: 0; margin: 0; display: block; visibility: hidden; overflow: hidden; font-size: 0; line-height: 0; clear: both; }
.col1 { width: 33.33%; height: 100%; display: block; float: left; padding: 0 10px; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; }
.ff { width: 100%; height: 40px; display: block; background: #000;}
.test_div { width: 100%; height: 40px; display: block; background: red;}
.overview { width: 100%; height: 40px; display: block; background: blue;}
<div class="body_clr">
<div class=" col1">
<div class="test_div"></div>
</div>
<div class="col1">
<div class="ff"></div>
</div>
<div class="col1">
<div class="overview"></div>
</div>
<div class="clear"></div>
</div>

Related

Fitting padding within parent for vertical centering

I think I'm basically there. I've got a DIV at 200px in height and an inner at 150px. This leaves me 50px for the image caption. I want to then vertically center the text within the remaining 50px.
.captioned {
width: 300px;
height: 200px;
display: flex;
margin: 10px;
background-color: #FFFFFF;
border: solid 4px #000000;
border-radius: 0;
box-sizing: border-box;
overflow: hidden;
}
.captioned-inner {
width: 100%;
margin: 0;
}
.preview {
height: 150px;
width: 100%;
}
.preview-image {
height: 100px;
max-width: 100%;
margin-top: 25px;
margin-bottom: 25px;
}
.info {
padding: 5px;
box-sizing: border-box;
height: 50px;
}
.info-inner {
box-sizing: border-box;
}
.name {
font-family: 'Roboto', sans-serif;
font-size: 25px;
display: block;
margin-top: 0;
margin-bottom: 0;
line-height: 1;
overflow-wrap: anywhere;
}
<div class="captioned">
<div class="captioned-inner">
<div class="preview" style="background-color: #DE16C7 !important">
<img class="preview-image" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
</div>
<div class="info">
<div class="info-inner">
<p class="name">Google</p>
</div>
</div>
</div>
</div>
I hope the code makes sense for what I'm trying to acheive. I'm wondering how I can go about incorporating the padding in the 50px height unless I should vertically center and pad the text but that seems a worse way to do it.
Just use display:flex and align-items:center to vertically align the text
Also as you have given box-sizing:border-box in .captioned class so it would include the border as well in the 200px height that means
height of the container + top border + bottom border=200px
so the height of .preview container should be 150px - top border(i.e 4px)=146px and for .inner container it will be 50-4 =46px;
This will work for you.
.captioned {
width: 300px;
height: 200px;
display: flex;
margin: 10px;
background-color: #FFFFFF;
border: solid 4px #000000;
border-radius: 0;
box-sizing: border-box;
overflow: hidden;
}
.captioned-inner {
width: 100%;
margin: 0;
}
.preview {
display: flex;
justify-content: center;
align-items: center;
height: 146px;
width: 100%;
}
.preview-image {
height: 100px;
max-width: 100%;
}
.info {
display: flex;
align-items: center;
box-sizing: border-box;
height: 46px;
}
.info-inner {
box-sizing: border-box;
}
.name {
font-family: 'Roboto', sans-serif;
font-size: 25px;
display: block;
margin-top: 0;
margin-bottom: 0;
line-height: 1;
overflow-wrap: anywhere;
}
<div class="captioned">
<div class="captioned-inner">
<div class="preview" style="background-color: #DE16C7 !important">
<img class="preview-image" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
</div>
<div class="info">
<div class="info-inner">
<p class="name">Google</p>
</div>
</div>
</div>
</div>

How can I have a div like this?

I want to have a welcome page like this:
But instead I get this:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html,
body {
background-color: #000000;
margin: 0;
height: 90%;
width: 100%;
align-items: center;
}
#container1 {
height: 100%;
vertical-align: middle;
display: table-cell;
background-color: yellow;
display: flex;
align-items: center;
}
#left {
height: 500px;
color: white;
background-color: blue;
font-size: 20px;
float: left;
width: 100%;
}
#right {
height: 500px;
color: white;
background-color: red;
font-size: 20px;
float: left;
width: 100%;
}
<main id="container1" class="container my-6">
<div class="">
<div id="left" class="col-lg-6 my-3">
</div>
</div>
<div class="">
<div id="right" class="col-lg-6 my-3">
</div>
</div>
</main>
I don't know why my container doesn't fully fit the body of the page, and my left and right don't go in the middle and stretch width to each other's end.
You have a bunch of errors in your code. I commented out the CSS you don't need:
No need for float, that's what flex is for.
display: table-cell is being overwritten by display: flex
Use flex to set the properties of your left and right divs.
Remove the containing elements around those.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html,
body {
background-color: #000000;
margin: 0;
height: 90%;
width: 100%;
/* NOT NEEDED: align-items: center;*/
}
#container1 {
width: 100%;
height: 100%;
vertical-align: middle;
/* NOT NEEDED: display: table-cell; */
background-color: yellow;
display: flex;
/* This is probably unneeded. align-items, aligns elements on the cross access - which in this case would be vertically aligned in the center since flex-direction by default, is row */
align-items: center;
}
#left {
height: 500px;
color: white;
background-color: blue;
font-size: 20px;
/* NOT NEEDED float: left; */
/* NOT NEEDED width: 100%; */
flex: 1 1 50%;
}
#right {
height: 500px;
color: white;
background-color: red;
font-size: 20px;
flex: 1 1 50%;
/* NOT NEEDED float: left; */
/* NOT NEEDED width: 100%; */
}
<main id="container1" class="container my-6">
<div id="left" class="col-lg-6 my-3">
</div>
<div id="right" class="col-lg-6 my-3">
</div>
</main>
The problem comes mostly from the divs without classes, that shouldn't be there.
But you're also mixing floats, with flex and tables. Just stick with flex like in this example:
html, body{
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
.container{
width: 100%;
height: 100%;
display: flex;
}
.left,
.right {
width: 50%;
height: 100%;
}
.left {
background: #215f40;
}
.right {
background: #092414;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>

Center text under image in flex container

I'm aware of This Question and many others like it. I have reviewed several edge cases similar to mine, and none of the fixes I've tried have worked.
I have an image and text. I want the text centered below the image. What I'm getting is the paragraph always aligned to the left edge of the image and growing to the right, rather than being centered on the image such as the image below. The image itself has even-width transparent borders on each edge, the size of which you can determine by knowing the left edge of the paragraph is aligned with the left edge of the image (it's very small).
body {
background: gray;
}
#myLinks {
margin: 0;
display: flex;
flex-direction: row;
width: 100%;
width: 100vw;
height: 10vh;
background: black;
justify-content: space-around;
}
.menu-card {
height: 15vh;
width: 5vw;
margin: 0;
margin-left: 16%;
border-radius: 45px;
border: none;
padding: 0;
}
.menu-icon-container {
width: 100%;
vertical-align: top;
display: inline-block;
}
.menu-icon {
max-height: 10vh;
max-width: 5vw;
}
.card-text {
position: relative;
margin: 0;
margin-top: 100%;
font-weight: bold;
font-size: 1.2vw;
text-align: center;
border-radius: 45px;
color: white;
display: block;
}
<div id="myLinks">
<div class="menu-card">
<div class="menu-icon-container">
<a href="#">
<img class="menu-icon" src="http://placehold.it/100x300" id="portfolio-icon">
<p class="card-text">Portfolio</p>
</a>
</div>
</div>
</div>
You can use margin:auto to get this fixed.
Add a class .center-items to the parent a tag of the image with the following properties :
.center-items > img,p {
display : block;
margin : auto ;
}
body {
background: gray;
}
#myLinks {
margin: 0;
display: flex;
flex-direction: row;
width: 100%;
width: 100vw;
height: 10vh;
background: black;
justify-content: space-around;
}
.menu-card {
height: 15vh;
width: 50px;
margin: 0;
margin-left: 16%;
border-radius: 45px;
border: none;
padding: 0;
}
.menu-icon-container {
width: 100%;
vertical-align: top;
display: inline-block;
}
.menu-icon {
max-height: 10vh;
max-width: 5vw;
}
.card-text {
position: relative;
margin: 0;
margin-top: 100%;
font-weight: bold;
font-size: 1.2vw;
text-align: center;
border-radius: 45px;
color: white;
display: block;
}
.center-items > img,p {
display : block;
margin : auto ;
}
<div id="myLinks">
<div class="menu-card">
<div class="menu-icon-container">
<a href="#" class="center-items">
<img class="menu-icon" src="http://placehold.it/100x300" id="portfolio-icon">
<p class="card-text">Portfolio</p>
</a>
</div>
</div>
</div>
it may work.. plz modify the css code..
css
*,
*:after,
*:before {
margin: 0;
padding: 0;
/* Removes padding behaviour on widths */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.menu-card {
margin: 0px;
text-align: center;
}
Thanks to #TheVigilant for putting me on the right path:
.menu-icon-container a {
width: auto;
vertical-align: top;
display: inline-flex;
justify-content : center;
}
.menu-icon-container > img, p {
margin: auto;
display: block;
}

(margin/padding)-right does not create space for overflow container

For some reason, in an overflown container, the padding on the right side is not shown.
.parent {
background-color: orange;
width: 150px;
height: 50px;
overflow: auto;
padding-right: 15px;
}
.child {
background-color: blue;
width: 250px;
height: 100%;
margin: 0 10px;
}
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
<div class="parent">
<div class="child">
</div>
</div>
I expected the orange color to show up when I scrolled to the very end (right)
Let's start without applying any overflow property. We clearly have the element outside of it's parent container (add padding of the container will remain inside):
.parent {
background-color: orange;
width: 150px;
height: 100px;
padding:15px;
}
.child {
background-color: blue;
width: 250px;
height: 100%;
}
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
<div class="parent">
<div class="child">
</div>
</div>
Now by adding overflow:scroll or overflow:auto you will simply add the scroll to see the overflowing part and you won't have the padding as excepted:
.parent {
background-color: orange;
width: 150px;
height: 100px;
overflow:auto;
padding:15px;
}
.child {
background-color: blue;
width: 250px;
height: 100%;
}
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
<div class="parent">
<div class="child">
</div>
</div>
Same logic with the margin right. When the element is overflowing there is no room to add margin between the inner element and the parent element.
I have checked in also Mozilla Firefox. and it's working fine.
.parent {
background-color: orange;
width: 150px;
height: 50px;
overflow-y: hidden;
overflow-x: auto;
padding-right: 15px;
}
.child {
background-color: blue;
width: 250px;
height: 100%;
margin: 0 10px;
display: inline-block;
}
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
<div class="parent">
<div class="child">
</div>
</div>
You should use below CSS. It's working for me.
.parent {
background-color: orange;
width: 150px;
height: 50px;
padding-right: 15px;
overflow-y: hidden;
overflow-x: auto;
}
.child {
background-color: blue;
width: 250px;
height: 100%;
margin: 0 10px;
display: inline-block;
}

<ins> element shoving content in separate div further down page

Why is the text in my <p> element being pushed down because of the existence of the <ins> tag? If you delete the <ins> tag from DOM via developer tools, you will see my text gets put in the position I expect.
main {
max-width: 1000px;
margin: 0 auto;
width: 100%;
box-sizing: border-box;
}
#portal-wrapper {
width: 100%;
padding: 40px 0;
background: #fff;
}
aside {
width: 100%;
padding: 10px 20px 20px;
box-sizing: border-box;
position: relative;
}
#media only screen and (min-width: 795px) {
main {
display: table;
}
#portal-wrapper {
display: table-cell;
min-width: 400px;
max-width: 680px;
width: auto;
}
aside {
width: 300px;
display: table-cell;
padding-right: 0;
padding-top: 41px;
}
}
<main>
<div id="portal-wrapper">
<div id="portal">
<p>
Here's my text. Why am I pushed down so far.
</p>
</div>
</div>
<aside>
<ins style="display: block; height: 600px;"> </ins>
</aside>
</main>
Fiddle here: https://jsfiddle.net/wwvq7net/1/
You could use vertical-align: top property:
main {
max-width: 1000px;
margin: 0 auto;
width: 100%;
box-sizing: border-box;
}
#portal-wrapper {
width: 100%;
padding: 40px 0;
background: #fff;
}
aside {
width: 100%;
padding: 10px 20px 20px;
box-sizing: border-box;
position: relative;
}
#media only screen and (min-width: 795px) {
main {
display: table;
}
#portal-wrapper {
display: table-cell;
vertical-align: top;
min-width: 400px;
max-width: 680px;
width: auto;
}
aside {
width: 300px;
display: table-cell;
vertical-align: top;
padding-right: 0;
padding-top: 41px;
}
}
<main>
<div id="portal-wrapper">
<div id="portal">
<p>
Here's my text. Why am I pushed down so far.
</p>
</div>
</div>
<aside>
<ins style="display: block; height: 600px;"> </ins>
</aside>
</main>
JSFiddle
When you set display:table-cell those elements are automatically starting with vertical-align:baseline which is why your #portal is being pushed to the bottom of #portal-wrapper.
Change to vertical-align:top or another value to fix this. Read more about vertical-align here.