Make header image shorter with centered text - html

I have been trying to get the header image to be shorter, however, I cannot figure out how to. Here is the HTML:
<div class="header">
<img src="images/header_sea(3).jpg" width="99%" class="header_image" alt="sea_sky">
<div class="header_title title"> *.• ʚ welcome to my ocean! ɞ •.* </div>
</div>
Here is the CSS:
html, body {
height: 100%;
width: 99%;
text-align: center;
}
.title{
font-family: 'Poppins', sans-serif;
font-size: 45px;
font-weight: bold;
color: #FB79E1;
text-align: center;
text-shadow: 3px 3px white;
}
.header{
position: relative;
}
.header_image{
opacity: 0.55;
height: 40%;
width: 99%;
}
.header_title{
position:absolute;
top: 0;
left: 0;
height: 100%;
width: 99%;
display: flex;
justify-content: center;
align-items: center;
}
I tried adjusting the height percentage in .header_image, but the image doesn't get shorter when I change the value.

The header needs a size associated with it. Otherwise the image has nothing to be "40%" of since the header is just using auto sizing.
Relevant code
.header {
position: relative;
/* Give the header (containing element) a size, can be %, px, etc.
Also keep in mind to use a percentage as a size the body needs a percentage size as well */
height: 20%;
}
Another good practice is to use semantic elements when possible, so consider using <header> instead of a div with a class of header.
html,
body {
height: 100%;
width: 99%;
text-align: center;
}
.title {
font-family: 'Poppins', sans-serif;
font-size: 45px;
font-weight: bold;
color: #FB79E1;
text-align: center;
text-shadow: 3px 3px white;
}
.header {
position: relative;
/* Give the header (containing element) a size, can be %, px, etc.
Also keep in mind to use a percentage as a size the body needs a percentage size as well */
height: 20%;
}
.header_image {
opacity: 0.55;
height: 100%;
width: 99%;
}
.header_title {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 99%;
display: flex;
justify-content: center;
align-items: center;
}
<header class="header">
<img src="https://98e50e5e-ef9b-4f10-9bb4-65acdcdf4429.id.repl.co/images/header_sea(3).jpg" class="header_image" alt="sea_sky">
<div class="header_title title"> *.• ʚ welcome to my ocean! ɞ •.* </div>
</header>

try removing the width attribute from the image (inline) and change the width in .header_image
I did it for you below
<div class="header">
<img src="https://98e50e5e-ef9b-4f10-9bb4-65acdcdf4429.id.repl.co/images/header_sea(3).jpg"
class="header_image"
alt="sea_sky"
/>
<div class="header_title title"> *.• ʚ welcome to my ocean! ɞ •.* </div>
</div>
.header_image{
opacity: 0.55;
height: 40%;
width: 70%;
}

Related

Change of point location when changing the resolution

I try to understand position and values absolute and relative, but I have one problem.
Why, when I use the "right" attribute, my "A" point changes position when the resolution changes, but when it applies the "left" attribute, everything stays in place well, even when I change the resolution?
Please check on different resolutions:
- Left:
- Right:
Code:
- Left:
html,
body {
background-color: #f4f4f4;
font-family: "Roboto", sans-serif;
}
body {
display: flex;
justify-content: center;
}
main {
background-color: #fefefe;
min-width: 350px;
max-width: 700px;
width: 40vw;
padding: 2rem;
}
.map {
background:url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Simple_world_map.svg/2000px-Simple_world_map.svg.png")
no-repeat;
background-size: cover;
height: 60vh;
position: relative;
}
.absolute {
position: absolute;
left: 100px;
top: 20px;
color: blue;
}
<main>
<form>
<div class="map">
<div class="absolute">
<h1>A</h1>
</div>
</div>
</form>
</main>
Right:
html,
body {
background-color: #f4f4f4;
font-family: "Roboto", sans-serif;
}
body {
display: flex;
justify-content: center;
}
main {
background-color: #fefefe;
min-width: 350px;
max-width: 700px;
width: 40vw;
padding: 2rem;
}
.map {
background:url("https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Simple_world_map.svg/2000px-Simple_world_map.svg.png")
no-repeat;
background-size: cover;
height: 60vh;
position: relative;
}
.absolute {
position: absolute;
right: 447px;
top: 20px;
color: blue;
}
<main>
<form>
<div class="map">
<div class="absolute">
<h1>A</h1>
</div>
</div>
</form>
</main>
My question is why this is happening, why the use of "right" is so unstable?
Because the absolute position is "relative" to .map div that it has no fixed width (
it changes as the browser size changes, from min 350px to max 700px -
inherits this rule from the parent main). So the .map's right border continuously changes its position. The left border, on the other hand, "remains steady" so the h1 never change its position.
I try to explain better with an image:

Chrome scrollbars resize html

This is only happening in Chrome, not Firefox or IE -
I have three images with text overlayed. The three images are responsive, and take up the full width of the browser window. Here's a screenshot:
When I resize the window to make it narrower, somehow, the html is becoming smaller than the size of the page. For example, here:
This only happens very quickly before the window seems to readjust, and everything is fine. However, I'd still like to fix it.
I've tried using a flexbox footer instead of the vh method but that didn't help.
My HTML and CSS are below. A note about the HTML - I'm sure there is another way to get the three pictures to fit together without any whitespace besides cramming the HTML all together like it is - sorry for being a hack. But that's not the source of the problem, as far as I can tell - it happens when there is only one picture as well.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<link href="styles/index-footer-test-750.css" type="text/css" rel="stylesheet">
<title>Good Boy Dog Care</title>
</head>
<body>
<div class="index-content">
<div id="we-love-dogs-one"><img id="we-love-dogs-one-image" src="images/cute-dog-one-cropped.jpg"><div id="we-love-dogs-one-text"><p>WE</p></div></div><div id="we-love-dogs-two"><img id="we-love-dogs-two-image" src="images/cute-dog-two-cropped.jpg"><div id="we-love-dogs-two-text"><p>LOVE</p></div></div><div id="we-love-dogs-three"><img id="we-love-dogs-three-image" src="images/cute-dog-three-cropped.jpeg"><div id="we-love-dogs-three-text"><p>DOGS</p></div></div>
</div>
<div class="footer">
</div>
</body>
</html>
And CSS:
* {
box-sizing: border-box;
margin: 0;
}
.index-content {
min-height: calc(100vh - 2em);
padding: 0;
margin: 0;
}
.footer {
height: 2em;
background-color: rgba(240, 100, 60, 1);
width: 100%;
cursor: default;
}
#we-love-dogs-one {
width: 34%;
display: inline-block;
margin: 0;
position: relative;
text-align: center;
}
#we-love-dogs-one-image {
width: 100%;
height: auto;
display: block;
float: left;
}
#we-love-dogs-one-text {
left: 0;
position: absolute;
width: 100%;
color: white;
top: calc(50% - 17px);
font-size: 1.5em;
display: inline-block;
font-family: "Montserrat";
}
#we-love-dogs-two {
width: 33%;
display: inline-block;
margin: 0;
position: relative;
text-align: center;
font-family: "Montserrat";
}
#we-love-dogs-two-image {
width: 100%;
height: auto;
display: block;
float: left;
}
#we-love-dogs-two-text {
left: 0;
position: absolute;
width: 100%;
color: white;
top: calc(50% - 24.5px);
font-size: 2em;
display: inline-block;
font-family: "Montserrat";
}
#we-love-dogs-three {
width: 33%;
display: inline-block;
margin: 0;
position: relative;
text-align: center;
}
#we-love-dogs-three-image {
width: 100%;
height: auto;
display: block;
float: left;
}
#we-love-dogs-three-text {
left: 0;
position: absolute;
width: 100%;
color: white;
top: calc(50% - 17px);
font-size: 1.5em;
display: inline-block;
font-family: "Montserrat";
}
Any help would be appreciated. Thanks!
cliffgallagher# , try using divs to auto resize rather than hardly setting the width or height per image, here is an example post: How do I auto-resize an image to fit a div container.
img {
max-width: 100%;
max-height: 100%;
}
.portrait {
height: 80px;
width: 30px;
}
.landscape {
height: 30px;
width: 80px;
}
.square {
height: 75px;
width: 75px;
}
Portrait Div
<div class="portrait">
<img src="http://i.stack.imgur.com/xkF9Q.jpg">
</div>
Landscape Div
<div class="landscape">
<img src="http://i.stack.imgur.com/xkF9Q.jpg">
</div>
Square Div
<div class="square">
<img src="http://i.stack.imgur.com/xkF9Q.jpg">
</div>

Why do my objects have different heights even when I gave them the same?

My HTML:
.title-card {
display: block;
height: 56.25vw;
width: 100%;
}
.title-overlay .title {
color: white;
background-color: rgba(255, 0, 0, 1);
font-weight: 100;
font-size: 2em;
display: table-cell;
vertical-align: middle;
text-align: center;
padding: 1em;
}
.title-overlay {
position: absolute;
left: 15%;
display: table;
width: 70%;
height: 56.25vw;
}
.background {
position: absolute;
width: 100%;
height: 56.25vw;
}
.background-overlay {
font-size: 0;
}
<div class="title-card">
<div class="background-overlay">
<img src="https://i.ytimg.com/vi/3RMr6m1B-qM/maxresdefault.jpg" class="background">
</div>
<div class="title-overlay">
<div class="title">
Hello there, and welcome! This site is still unfinished but feel free to look around.
</div>
</div>
</div>
As you can see, all of my heights are set to 56.25vw. However, in the actual page, the title-card's and background's heights are 698.52px while the title-overlay's height is 698.18px. According to my calculator, 698.52px should be the correct height. So why is my title-overlay a bit shorter?
It is because display:table; calculates height differently from block elements.
I don't know exactly why it does but my guess is that it does not allow half pixels somewhere in the calculation.
For example: set the height of everything to 300px and all elements will have 300px height but if you set all heights to 300.75px the block elements will be 300.75px and the table element will be 300px.
To fix your problem I would recommend just stop using tables.
If you want to center everything the same way you can use a inline-block element with 56.25vw in line-height and then having a inline-block element inside it with line-height: normal;
Example below:
.title-card {
display: block;
height: 56.25vw;
width: 100%;
}
.title-overlay .title {
font-weight: 100;
font-size: 2em;
line-height: normal;
display: inline-block;
vertical-align: middle;
text-align: center;
padding: 1em;
}
.title-overlay .title-container {
color: white;
background-color: rgba(255, 0, 0, 1);
display:inline-block;
line-height:56.25vw;
}
.title-overlay {
position: absolute;
left: 15%;
display: block;
width: 70%;
height: 56.25vw;
}
.background {
position: absolute;
width: 100%;
height: 56.25vw;
}
.background-overlay {
font-size: 0;
}
<div class="title-card">
<div class="background-overlay">
<img src="https://i.ytimg.com/vi/3RMr6m1B-qM/maxresdefault.jpg" class="background">
</div>
<div class="title-overlay">
<div class="title-container">
<div class="title">
Hello there, and welcome! This site is still unfinished but feel free to look around.
</div>
</div>
</div>
</div>
.title-card {
display: block;
height: 56.25vw;
width: 100%;
}
.title-overlay .title {
color: white;
background-color: rgba(255, 0, 0, 1);
font-weight: 100;
font-size: 2em;
display: table-cell;
vertical-align: middle;
text-align: center;
padding: 1em;
}
.title-overlay {
position: relative;
left: 15%;
display: table;
width: 70%;
height: 60vw;
}
.please-work {
position: absolute;
overflow: hidden;
width: 100%;
height: 56.25vw;
}
.background {
position: absolute;
width: 100%;
height: 56.25vw;
}
.background-overlay {
font-size: 0;
}
<div class="title-card">
<div class="background-overlay">
<img src="https://i.ytimg.com/vi/3RMr6m1B-qM/maxresdefault.jpg" class="background">
</div>
<div class="please-work">
<div class="title-overlay">
<div class="title">
Hello there, and welcome! This site is still unfinished but feel free to look around.
</div>
</div>
</div>
</div>
Well, I've found myself a disgusting "fix." I'm not really satisfied with it but if it works, then it works, I guess.

How to center image vertically in div

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>

Keep Text Centered when resizing

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