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.
Related
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>
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;
}
What am I doing wrong here? I want the stuff that's here to be on the middle of the screen, but however I'm trying, I can't get it working..
* {
box-sizing: border-box;
}
body {
max-width: 100%;
line-height: 1.5;
}
#wrapper {
max-width: 100%;
}
.bild {
padding: 2em 0 0 2em;
}
img {
max-width: 60%;
padding-top: 1em;
border-radius: 100px;
float: left;
display: block
}
h1 {
font-weight: 900;
font-size: 2em;
float: right;
margin-right: 25em;
margin-top: 2.2em;
}
.floatright {
float: left;
margin-left: 10em;
}
<body>
<div id="wrapper">
<div class="bild">
<img src="images/jag.jpg" alt="En bild på mig">
<h1>Välkommen!</h1>
<span class="floatright">Jag heter Patrik Qvarnström</span>
</div>
</div>
</body>
Remove float: left from img and add margin: auto like this:
* {
box-sizing: border-box;
}
body {
max-width: 100%;
line-height: 1.5;
}
#wrapper {
max-width: 100%;
}
.bild {
padding: 2em 0 0 2em;
}
img {
max-width: 60%;
padding-top: 1em;
border-radius: 100px;
display: block;
margin: auto;
}
h1 {
font-weight: 900;
font-size: 2em;
float: right;
margin-right: 25em;
margin-top: 2.2em;
}
.floatright {
float: left;
margin-left: 10em;
}
<body>
<div id="wrapper">
<div class="bild">
<img src="temp.PNG" alt="En bild på mig">
<h1>Välkommen!</h1>
<span class="floatright">Jag heter Patrik Qvarnström</span>
</div>
</div>
</body>
If Flexbox solution is OK for you - here it is (it's pretty cool for centering and in total):
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
line-height: 1.5;
}
#wrapper {
display: flex;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
}
.bild {
display: flex;
width: 100%;
height: 100%;
flex-direction: column;
align-items: center;
justify-content: center;
}
img {
max-width: 15%;
height: auto;
padding-top: 1em;
border-radius: 100px;
display: block
}
h1 {
font-weight: 900;
font-size: 2em;
margin-top: .5em;
}
<div id="wrapper">
<div class="bild">
<img src="https://www.adrln.com/wp-content/uploads/2017/05/img.png" alt="En bild på mig">
<h1>Välkommen!</h1>
<span class="floatright">Jag heter Patrik Qvarnström</span>
</div>
</div>
* {
box-sizing: border-box;
}
#wrapper{
display: table;
margin: 0 auto;
}
img {
max-width: 60%;
padding-top: 1em;
border-radius: 100px;
position: relative;
float: left;
}
.container {
position: relative;
float: left;
margin-left: 25px;
}
<body>
<div id="wrapper">
<div class="bild">
<img src="https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/68dd54ca-60cf-4ef7-898b-26d7cbe48ec7/10-dithering-opt.jpg" alt="En bild på mig" width="150">
<div class="container">
<h1>Välkommen!</h1>
<p class="floatright">Jag heter Patrik Qvarnström</p>
</div>
</div>
</div>
</body>
Set width on containing element: Declare a width or max-width property on the containing (parent)
element .bild, e.g: max-width: 55%; - adjust this value
accordingly to suit requirements.
Align horizontally center: Declare the shorthand margin property rule with a value of auto
on the containing (parent) element .bild, e.g: margin: auto; -
this will allow the containing element to horizontally center.
Code Snippet Demonstration:
* {
box-sizing: border-box;
}
body {
max-width: 100%;
line-height: 1.5;
}
#wrapper {
max-width: 100%;
}
.bild {
margin: auto;
max-width: 55%; /* adjust this value accordingly to suit requirements */
min-width: 425px; /* for the sake of demonstration */
overflow: auto;
border: 1px dashed; /* for the sake of demonstration */
padding: 10px;
}
img {
max-width: 60%;
padding-top: 1em;
border-radius: 100%;
float: left;
}
h1 {
font-weight: 900;
font-size: 2em;
margin-top: 2.2em;
}
.floatright {
float: right;
margin-left: 10px;
}
<body>
<div id="wrapper">
<div class="bild">
<img src="https://placehold.it/200x200" alt="En bild på mig">
<h1 class="floatright">Välkommen!</h1>
<span class="floatright">Jag heter Patrik Qvarnström</span>
</div>
</div>
</body>
In Summary:
.bild is a div element
a div element is a block-level element (occupies full horizontal
width, unlike inline elements)
to horizontally center a block-level element: 1. declare a width
property and 2. declare margin property with a value of auto
any element can be made a block-level element by declaring
display: block
Note: the aforementioned techniques will not behave as explained if any float properties have been declared, float will negate any attempt at horizontal alignment, unless the element in question has been declared as flex-box display, e.g: display: flex
Practical Demonstrations:
For block-level and inline elements
Horizontal Alignment (Arbitrary Elements) (CodePen)
Horizontal Alignment (Text Elements) (CodePen)
I want my inline images to automatically resize with browser size and at the same time it should be inline with the header too. I have already tried solution given here but it is not working for me.
Here is my code:
#header_title {
font-weight: bolder;
text-align: center;
}
#header_photo {
display: none;
}
.floatingimage {
float: right;
max-width: 100%;
height: auto;
}
#media all and (max-width: 1024px) {
#header_title {
margin-left: 10%;
min-width: 67%;
max-width: 77%;
display: inline-block;
vertical-align: top;
position: relative;
}
#header_photo {
display: inline-block;
vertical-align: top;
max-width: 20%;
height: auto;
}
}
<header>
<div id="header_title">
<h1>Title h1</h1>
<h3>Title h3</h3>
</div>
<aside id="header_photo">
<img class="floatingimage" src="http://www.planwallpaper.com/static/images/Awesome-Nature-Background.jpg" width="100px" height="100px" />
</aside>
</header>
You find this code on jsfiddle here.
NOTE: I am using Firefox 53.0.3(32-bit)
I slightly modified your HTML and CSS. I add flex display and image height related to width.
HTML code:
<header>
<div id="header_title">
<h1>Title h1</h1>
<h3>Title h3</h3>
</div>
<aside id="header_photo">
<img class="floatingimage" src="http://www.planwallpaper.com/static/images/Awesome-Nature-Background.jpg"/>
</aside>
CSS code:
header {
display: flex;
}
#header_title
{
font-weight: bolder;
text-align: center;
}
#header_photo
{
display: none;
max-width: 100%;
height: auto;
align-self: center;
}
#header_photo img {
width: 100%;
height: auto;
max-height: 120px;
}
.floatingimage
{
float: right;
}
#media all and (max-width: 1024px)
{
#header_title
{
margin-left: 10%;
min-width: 67%;
max-width: 77%;
display: inline-block;
vertical-align: top;
position: relative;
}
#header_photo
{
display: inline-block;
vertical-align: top;
max-width: 20%;
height: auto;
}
}
You check it on fiddle here
You have sized the image using inline-styling; that is the main problem. You can do two 2 things:
add the width and height to the floatingimage class
OR
create a new class e.g. .wh100, with width:100px; and height:100px; and use a second class with the .floatingimage in your html e.g img class="floatingimage wh100"
In either option, remember to REMOVE the inline styling from the html!!
Then your images should size appropriately.
Give that a whirl
RE-EDIT: (back on computer - sigh..).. here you go.. the fiddle i posted a while ago with the header narrowed to 75%.
header {
width: 75%;
height: auto;
margin: 0 auto;
padding: 0;
}
#header_title {
font-weight: bolder;
text-align: center;
max-width: 80%;
}
#header_photo {
/*display: none;*/
margin: 0;
padding: 0;
max-width: 20%;
height: auto;
float: right;
}
.floatingimage {
position: relative;
display: none;
}
.wh100 {
width: 100px;
height: 100px;
}
#media all and (max-width:480px){
#header_photo {
margin-top: 8%!important;
}
}
#media all and (max-width:1024px) {
#header_title,
#header_photo img {
margin: 0;
padding: 0;
top: 2%;
display: inline!important;
vertical-align: middle!important;
}
#header_title {
max-width: 80%;
position: relative;
float: left;
}
#header_photo {
margin-top: 4%;
max-width: 20%;
position: relative;
float: right;
}
#header_photo img {
position: relative;
max-width: 100%;
height: auto;
}
}
<header>
<div id="header_title">
<h1>Title h1</h1>
<h3>Title h3</h3>
</div>
<aside id="header_photo">
<img class="floatingimage wh100" src="http://www.planwallpaper.com/static/images/Awesome-Nature-Background.jpg" />
</aside>
</header>
New fiddle
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>