I have a div which has a height of 100vh so that it's always the height of the browser screen. Inside of this div I want to place an image and center it vertical to its parent.
The height is variable so I can't use fixed margins. My current Markup is as follows:
HTML
<div class="textportfolio">
<h1>This is a title</h1>
<p class="textbio-small">
The Roosevelt dime is the current ten-cent piece of the United States.
</p>
<img class="portfolio-slides-img" src="https://i.imgur.com/iheO43X.png">
</div>
CSS:
.textportfolio {
font-family: "Lora", serif;
margin: 5%;
background: #e9cca3;
height: 100vh;
}
img.portfolio-slides-img {
max-width: 40%;
max-height: 40%;
margin: 0 auto;
display: block;
}
Does anybody know how to center the image vertically according to the browser height?
Here is the code snippet
.textportfolio {
font-family: "Lora", serif;
margin: 5%;
background: #e9cca3;
height: 100vh
}
img.portfolio-slides-img {
margin-top: 15%;
max-width: 40%;
max-height: 40%;
margin: 0 auto;
display: block;
}
<div class="textportfolio">
<h1>This is a title</h1>
<p class="textbio-small">
The Roosevelt dime is the current ten-cent piece of the United States.
</p>
<img class="portfolio-slides-img" src="https://i.imgur.com/iheO43X.png">
</div>
I use this css snippet:
.selector {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
Applied to your sample: https://jsfiddle.net/nhdh8ycr/4/
Centering things in CSS has been a long debated topic where people weigh all the factors and argue what the least convoluted way is.
Then in 2014, something called Flexbox came out and basically obsoleted all that.
When a container has display: flex, there's properties to align its children. And you can anchor it in the middle on either/both axis.
<div id="container">
<img src="https://i.imgur.com/i9xpVnQ.jpg" />
</div>
html, body {
height: 100%; /* required to make body occupy the full viewport by default */
}
#container {
height: 100%;
display: flex;
align-items: center; /* horizontal */
justify-content: center; /* vertical */
}
img {
height: 200px;
}
https://jsfiddle.net/5goboeey/1/
It's so ubiquitously convenient I think it continues to fly under the radar because people assume it can't be so straightforward.
maybe this stackoverflow question could help you
jsfiddle
code is
HTML
<div class=frame>
<img src="http://jsfiddle.net/img/logo.png" height=3 />
</div>
CSS
.frame {
height: 25px; /* equals max image height */
line-height: 25px;
width: 160px;
border: 1px solid red;
text-align: center; margin: 1em 0;
}
img {
background: #3A6F9A;
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
Try this:
.textportfolio {
font-family: "Lora", serif;
margin: 5%;
background: #e9cca3;
height: 100vh;
position: relative;
}
img.portfolio-slides-img {
max-width: 40%;
max-height: 40%;
margin: 0 auto;
display: block;
position: absolute;
right: 0;
left: 0;
top: 35%
}
<div class="textportfolio">
<h1>This is a title</h1>
<p class="textbio-small">
The Roosevelt dime is the current ten-cent piece of the United States.
</p>
<img class="portfolio-slides-img" src="https://i.imgur.com/iheO43X.png">
</div>
Related
Im trying to position this image and tittle with text on my website.
How can I make it responsive so that the scale doesnt change on different devices and everything stays at the same position.
Im new at this, so im probably making a lot of mistakes.
div.content {
width: 100vw;
display: flex;
}
div.column1 {
width: 15%;
background-color: #F7F7F7;
overflow: hidden;
height: 100vh;
}
div.column2 {
width: 70%;
overflow: hidden;
}
.lobby {
width: 45%;
height: 45%;
padding-top: 5.6rem;
padding-left: 1rem;
}
.title {
padding-top: 4.7rem;
display: inline-block;
float: right;
width: 50%;
font-size: 1.8rem;
}
.descr {
display: inline-block;
width: 30vw;
float: right;
font-size: 1rem;
padding-top: 0.4rem;
}
<div class="content">
<div class="column1">
</div>
<div class="column2">
<div class="title">Installations
<p class="descr"> We are a gym based in Carballo, A Coruña, counting with an installation </p>
</div>
<img class="lobby" src="img/lobby.jpg" alt="photo of the lobby of the gym" />
</div>
</div>
<div class="column1">
</div>
As you can see im trying to position the image and the text on the middle row of my website, since it is divided into 3 columns.
Anything I can do to improve the code and to make it more responsive.
Thanks!
You don't need 3 columns. My approach starts defining the wrapper layout (the container), then working on the content layout (2 columns nested on the wrapper).
Here is my way of doing it:
HTML
<div class="main-container container">
<div class="inner-container content">
<div class="column-left column column-1">
<img class="lobby" src="https://upload.wikimedia.org/wikipedia/en/c/cb/Placeholder_logo.png" alt="photo of the lobby of the gym">
</div>
<div class="column-right column column-2">
<h1 class="title">Installations</h1>
<p class="descr">We are a gym based in Carballo, A Coruña, counting with an installation </p>
</div>
</div>
</div>
CSS
<style>
html,
body {
padding: 0px;
margin: 0px;
}
/* set the layout */
.main-container {
width: 100%;
max-width: 100vw;
min-height: 100vh;
box-sizing: border-box;
padding-left: 15%;
padding-right: 15%;
position: relative;
}
/* this is a pseudo element, it renders the grey background on the left */
.main-container:before {
content: "";
display: block;
height: 100%;
background-color: #F7F7F7;
/* same width of padding-left as it covers only the left side */
width: 15%;
position: absolute;
z-index: 1000;
top: 0;
left: 0;
}
.inner-container {
width: 100%;
display: flex;
flex-wrap: wrap;
padding: 2rem 1rem 0;
box-sizing: border-box;
}
.column {
padding: 3.6rem 0 1rem;
box-sizing: border-box;
/* Column width - Change this with media Queryes */
width: 50%;
flex-basis: 50%;
}
/* page elements*/
.title {
font-size: 1.8rem;
margin-top: 0px;
}
.descr {
font-size: 1rem;
padding-left: 3rem;
}
.lobby {
width: 45%;
height: auto;
}
/* responsive media query */
/* decide the breakpoint to start having 1 column */
#media screen and (max-width: 767px) {
.column {
width: 100% !important;
flex-basis: 100%;
}
}
</style>
Here is a working Codepen: https://codepen.io/Davevvave/pen/BaPZRbb
Consider the #Sfili_81 advice, first study and learn about #media_queries (https://www.w3schools.com/css/css3_mediaqueries_ex.asp), avoid float, and
(I suggest) try to understand the natural behavior of HTML rendering flux, the semantic meaning of HTML tags and empower all with CSS.
I don't have much knowledge about html and css and I couldn't find the answer on the internet so I am here.
Problem:
I am trying to make an image fill top part of the screen but one thing stops me from it and it's the default margin of the <body>. I've managed it by using margin: -10px; But now the image can't fill the screen by 20px, probably because there is no margin, image still thinks screen is that big.
html,body {
width: 100%;
height: 100%;
margin: -10px;
padding: 0;
overflow: hidden;
}
img {
width: 1600px;
height: 300px;
opacity: 70%;
object-fit: cover;
object-position: top 10px;
}
.cont {
position: relative;
text-align: center;
padding: 10px;
}
.main-text {
font-size: 100px;
color: white;
position: absolute;
top: 100px;
left: 70px;
}
<body>
<div class="cont">
<img src="https://i.stack.imgur.com/DWZAk.jpg">
<div class="main-text">Big Ass Title</div>
</div>
</body>
NOTE: If you have any questions or didn't understand anything about the question, please ask because I am ready for any answer. :) Thanks.
If your image is meant to be a decoration(design), then background is fine to use.
.cont can be a flex or grid element, to avoid position absolute and possible unwanted sides effects.
here an example with a background and grid:
body {
margin: 0;
min-height: 100vh; /* optionnal if it does not have a purpose */
}
.cont {
height: 300px; /* guessed from your code */
display: grid; /* default make a single column*/
background: url(https://picsum.photos/id/1015/600/300) 0 0 / cover; /* background covering */
}
.main-text {
margin-block: auto; /* vertical-centering in its row from here */
margin-inline-start:70px;
font-size: 100px; /* from your code */
color: white; /* from your code */
font-weight: normal; /* you looked for this? */
text-shadow: 0 0 1px #000; /*Optionnal increase readability*/
}
<div class="cont">
<h1 class="main-text">Big Ass Title</h1><!-- if that's a title, then make it a title ;) -->
</div>
Generally to eliminate all the margins and paddings you can add:
* {
margin: 0;
padding: 0;
}
By the way I attached a snippet where it's working as you requested. Is better to eliminate the margins than adding a negative margin, if you want to do it that way you must to compensate it in the width to achieve the 100% width intended.
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
img {
width: 100%;
margin: 0;
height: 300px;
opacity: 70%;
object-fit: cover;
}
.cont {
position: relative;
text-align: center;
}
.main-text {
font-size: 100px;
color: white;
position: absolute;
top: 100px;
left: 70px;
}
<html>
<body>
<div class="cont">
<img src="https://images2.alphacoders.com/941/thumb-1920-941898.jpg">
<div class="main-text">Big Ass Title</div>
</div>
</body>
</html>
So I have looked at other questions like this, yet none have solved my problem. What I want to be able to do is resize the window of the browser, yet keep the text in the centre of the window.
HTML:
<div id=inside>
Navjeeven Mann
</div>
CSS:
#inside {
position:relative;
width: 100%;
height: 100%;
font-size: 60px;
text-align: center;
color: black;
top:50%;
left:50%;
}
What it looks like now
To keep it centred horizontally, remove the top:50%;left:50%;
#inside {
position:relative;
width: 100%;
height: 100%;
font-size: 60px;
text-align: center;
color: black;
}
<div id="inside">
Navjeeven Mann
</div>
To center it horizontally and vertically, flex really does the job well, unless you have to support older browsers.
html, body {
margin: 0;
}
#inside {
display: flex;
width: 100vw;
height: 100vh;
align-items: center;
justify-content: center;
font-size: 60px;
}
<div id="inside">
Navjeeven Mann
</div>
Looks like you want the text vertically and horizontally centered inside a div. I would first wrap your text inside a p tag. Then changeup the css a bit so your code will look like this:
html, body {
margin: 0;
}
#inside {
position: relative;
width: 100%;
height: 300px; /* change this to your liking */
border: 1px solid #000; /* to show size of div */
}
#inside p {
position: absolute;
left: 0;
right: 0;
top: 50%;
transform: translateY(-50%);
margin: auto;
font-size: 60px;
text-align: center;
color: black;
display: inline-block;
}
<div id="inside">
<p>Navjeeven Mann</p>
</div>
This should work across all devices
I have this html code:
<div id="home-page"> hello from home</div>
<div class="home-page top-div">
some text
</div>
<div class="home-page bottom-div">
other text
</div>
This is the css:
#home-page {
.top-div {
height: 50%;
width: 100%;
background-color: #009900;
margin: auto;
text-align: center;
}
.bottom-div {
height: 50%;
width: 100%;
background-color: #990000;
margin: auto;
text-align: center;
color: #FFFFFF;
}
}
What I want to get is a page split in two parts horizontally, the top part in one colour and the second one in another colour. I tried this but it has no effect on my page.
Does anybody know what I did wrong? Thanks
I think you should define #home2-page also as
#home2-page{
width: 100%;
height: 100%;
margin: 0 auto;
display: block;
}
Using percent in height is dependent on parent div height. If no height is set in the parent div, then height has no meaning.
The same is true for the parent. If you use a percent-height (or no height depending on HTMLElement.style.display) in the parent element, then it's parent needs to have a fixed height. All the way up to the html-element, which you can set to 100% height (and then it should work). html{ height: 100% }
Anyway, that is a silly way to do things, so I suggest you use something slightly more modern instead; The vh vw units (viewport height, viewport width). One vh unit is 1% of the viewport height. Thus, you can replace 50% with 50vh and it'll be something closer to what you wish for.
.top-div {
height: 50vh;
}
Try This:
html,body {
height: 100%;
}
html,body {
height: 100%;
}
.top-div {
height: 50%;
width: 100%;
background-color: #009900;
margin: auto;
text-align: center;
}
.bottom-div {
height: 50%;
width: 100%;
background-color: #990000;
margin: auto;
text-align: center;
color: #FFFFFF;
}
<div id="home2-page"> hello from home</div>
<div class="home2-page top-div">
some text
</div>
<div class="home2-page bottom-div">
other text
</div>
Like #C Travel said, you can't use nested CSS meaning you can't put a class inside a class. You can accomplish your goal by simplifying your code a bit. Checkout my working example below:
CSS:
<style>
.top-div {
height: 50%;
width: 100%;
background-color: #009900;
margin: auto;
text-align: center;
}
.bottom-div {
height: 50%;
width: 100%;
background-color: #990000;
margin: auto;
text-align: center;
color: #FFFFFF;
}
</style>
HTML:
<div class="top-div">
<p>hello from home</p>
<p>some text</p>
</div>
<div class="bottom-div">
<p>other text</p>
</div>
I am trying to horizontally center an image within a div. However, I haven't been able to. I've tried setting the vertical-align to middle and the margin to auto, 0 auto, and every variation I can think of. Nothing works. Here is the code for how it is currently set up:
img {
border: 0;
margin: 0 auto;
}
.intro img {
border-radius: 50%;
vertical-align: middle;
padding: 10px;
}
The image is in the intro div. Any advice you can give would be helpful.
If you want to center your image both horizontally & vertically, this should do the trick :
.intro {
display: table;
width: 500px; /* works with any width */
height: 150px; /* works with any height */
background: #ccc;
}
.imgcontainer {
display: table-cell;
vertical-align: middle;
text-align: center;
}
img {
background: #fff;
padding: 10px;
border-radius: 50%;
}
<div class="intro">
<div class="imgcontainer">
<img src="http://s.gravatar.com/avatar/bf4cc94221382810233575862875e687?r=x&s=50" />
</div>
</div>
Use position:relative in parent .intro and use the code shown below in img, it will work with any width and height
display:block;
margin:auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0
Snippet
.intro {
border: dashed red; /* demo */
display:inline-block; /* demo */
vertical-align:top; /* demo */
position: relative
}
.intro img {
border-radius: 50%;
vertical-align: middle;
display: block;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0
}
.intro:first-of-type {
width: 200px;
height: 200px;
}
.intro:last-of-type {
width: 400px;
height: 300px;
}
<div class="intro">
<img src="//lorempixel.com/100/100" />
</div>
<div class="intro">
<img src="//lorempixel.com/100/100" />
</div>
The css style margin: 0 auto; should do the horizontal part of the trick.
For the vertical part you also need to take care of the parent.
Look at How to vertically align an image inside div for more info.
vertical-align works only in table cells. Try to use Flexbox. The element containing your image should have CSS properties:
display: flex;
align-items: center;