How to add an image to my header background in html? - html

I would like to add an image to my header background, I tried this, but didn't work.
What am I doing wrong?
div.cabecalho {
padding: 20px;
font-size: 250%;
text-align: center;
background-image: url(https://via.placeholder.com/800/ff0000);
vertical-align: middle;
color: #ffffff;
border-bottom: 30px solid #0f4098;
width: 100%;
align-items: center;
}
div.cabecalho span {
margin-left: 20px;
}
img {
object-fit: cover;
}
<div class="cabecalho">
<img src="https://via.placeholder.com/320x300" width="320" height="300">
<span>Desempenho do <strong>PIB</strong> no terceiro trimestre de 2021</span>
</div>

Judging by text-align: center;, vertical-align: middle, and align-items: center; I'm going to take a wild guess and assume you want the background image centered. You can use background-image: center; in your CSS. background-size: cover; to expand the entire image throughout the header. align-items: center; generally only works with flex or grids
Check here to learn more about background properties.
div.cabecalho {
padding: 20px;
font-size: 250%;
text-align: center;
background-image: url(https://via.placeholder.com/800/ff0000);
background-position: center;
background-size: cover;
color: #ffffff;
border-bottom: 30px solid #0f4098;
width: 100%;
align-items: center;
}
div.cabecalho span {
margin-left: 20px;
}
img {
object-fit: cover;
}
<div class="cabecalho">
<img src="#" width="320" height="300">
<span>Desempenho do <strong>PIB</strong> no terceiro trimestre de 2021</span>
</div>

Your css and html looks like it will work. And in fact, when I tried it myself with copy and paste, but changing out the images, it did work. It may be an issue with the path of your image files. Try navigating directly to them in your browser to see if they display without issue.
Edit: After posting this, the original question was edited to replace the author's image paths and names with placeholders. This would appear to confirm my above statement, as using "run code snippet" with the placeholder images works. Thus the issue still appears to be with the images in use. Perhaps the path is incorrect or perhaps the images are not located there.

Both images are being displayed on my end.
The 800x800 can only be seen in a small screen.
Set your height to 100vh for example and you'll be able to see it.
You may need to adjust the width of your image to fit within your header container.
Also please inspect your css img selector. Even if you remove it, you code will work as img {
object-fit: cover;
} is not working. Your code is applying the width you've set on your html.

Related

html / CSS Align image within div, with main image being background

The issue I'm having is a bit different than that I've found thus far the goal is to make an HTML page in which each background img div will be one page, thus when printing it will be clean.
Here is what the code looks like.
.thirdBackgroundPage {
background-image: url("images/firstBackgroundPage.jpeg");
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.icobp {
height: 75%;
width: auto;
display: block;
margin-left: auto;
margin-right: auto;
bottom: 0;
}
<div class="thirdBackgroundPage">
<img class="icobp" src="images/imageCenterOfBackgroundPage.jpeg" />
</div>
With this said, there are other <, div class, so the first image is on the third page for example, where right now with this, it brings the image in the center top, however, I need it dead center or bottom.
I've tested a few things, but to no avail. Tried setting it up within a table, but this does not work either. Any help is welcome.
You already had other suggestions to better center your div inside the container, anyway that's mine. In the snippet below I just added a css rule that will add some style attribute to the container so that its content will be correctly centered both vertically and horizontally via display:flex;
Since you talk about printing html pages, I want to warn you that html/css are still not suitable for that since there are no tools to address publishing details like page number or page size. You can control the content size but it will never be accurate and I could experience myself that i will become inaccurate starting from page 30 maybe..I can't remember in details.
Anyway I considered that div .thirdBackgroundPage as filling an A4 page so I gave it those size in mm. Let me know if I got it wrong.
.thirdBackgroundPage {
background-image: url("images/firstBackgroundPage.jpeg");
/*height: 100%;*/
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: flex;
}
/*Style attributes I added to the list container*/
.thirdBackgroundPage {
display: flex;
align-items: center;
justify-content: center;
border: solid 1px black;
width: 210mm;
height: 297mm;
}
.icobp {
/*height: 75%;*/
/*Needed to change this to give the pic a size despite the url fake*/
height: 100px;
width: auto;
display: block;
margin: auto;
}
<div class="thirdBackgroundPage">
<img class="icobp" src="images/imageCenterOfBackgroundPage.jpeg" />
</div>
To manipulate the printing you have to use #media print so you can control which one you just want to print. If you needed to center a spcific element you have to use this code #media print as well.And display:none for the other one you dont want to be printed. sometimes you have to use display:block o display:inline-block to get it to work as well. Hope this will help your issue.
#page {
size: A4;
margin: 0;
}
#media print {
html, body {
width: 210mm;
height: 297mm;
}
.thirdBackgroundPage{
vertical-align:middle;
align-items:center;
page-break-before:avoid;
page-break-after:avoid;
page-break-inside:avoid;}
}
<div class="thirdBackgroundPage">
<img class="icobp" src="images/imageCenterOfBackgroundPage.jpeg"/>
</div>

How to hide borders when resizing images?

I put three photos inside a container but since I want there to be space between them, I couldn't leave the original size because they would take up the whole container without leaving the space I want.
To make them smaller I modified the height to 80%.
It worked but since I need to add the shadow to the box, I need it to match the edges of the image.
As you can see from the purple, the box is larger than the actual image. I would like to know how to get a box as big as the actual image, so without the purple section.
I added the background color only to the first pic, but the problem can be extended for all the three pics.
I post the code below.
.background {
height: 100vh;
display: flex;
justify-content: space-between;
}
.background * {
width: 100%;
height: 80%;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
<div class="background">
<div class="firstphoto"></div>
<div class="secondphoto"></div>
<div class="thirdphoto"></div>
</div>
Thanks all! ;)
You can take a look at object-fit property: https://www.w3schools.com/css/css3_object-fit.asp
Also, you should put:
.background > * {
flex: 1/3;
}
So that the boxes are taking the same space.
you should add this to each div that contains an image (if they have the same class)
The div would then be positoinned relatively to the image and you could then edit the box-shadow with the box-shadow property
.col{
position:relative;
top:0px;
left:0px;
width:100%;
height:100%;
margin-right:3.33%;
box-shadow: 10px 10px 10px 10px red;
}
Not sure why you're having the images as background images, but I would just use object-fit. Do note, I replaced the divs with image tags.
.background {
height: 100vh;
display: flex;
justify-content: space-between;
}
.background img {
width: 100%;
height: 80%;
padding: 1rem;
box-sizing: border-box;
object-fit: contain;
}
<div class="background">
<img src="https://picsum.photos/200/300" alt="200x300" title="200x300"/>
<img src="https://picsum.photos/50/150" alt="50x150" title="50x150"/>
<img src="https://picsum.photos/30/150" alt="30x150" title="30x150"/>
</div>

How to make a short horizontal line over an image?

I am trying to draw a short horizontal line over an image. Like a quote followed by horizontal line followed by author name. An example of what I'm trying to do is below.
How can I achieve this in html css?
You can use either an hr or a div with a border. I made a simple example, hope it helps.
html, body{
height: 100%
}
body{
background: lightslategray;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
}
.quote {
background: url('http://lorempixel.com/g/600/200/abstract/?random=true');
color: white;
background-size: cover;
padding: 2rem;
text-align: center;
}
hr {
width: 40%;
}
<div class="quote">
<h1>A dream doesn't become reality through magic<br/> it takes sweat, determination and hard work.</h1>
<hr />
<h2>Colin Powell</h2>
</div>
you can use the horizontal line in html <hr>
You can do it in several way. How you go about it will depend on what you are trying to accomplish. Here are two ways; one using br element and the other using a div (it could be a label too). If you want to use a different color and add more style to it, you want to go with the div
.motto, .author{
display: block;
text-align: center;
}
.motto{
font-size: 1.85em;
}
.br{
width: 50%;
height: 2px;
margin: 25px auto;
border-radius: 2px;
background-color: #0088ee;
}
<i class="motto">It is better to be feared than loved if you cannot be both</i>
<div class="br"></div>
<!-- <hr/> -->
<strong class="author">Niccolo Machiavelli</strong>
Comment out <div class="br"></div> and uncomment <hr/> to see the two results.

HTML/CSS. need help just making my border fill the entire width of the screen

screenshot https://melchillington.github.io/Website1/contact.html
That is my site so far, my issue is in the contact page but also a similar issue with the image on home page (index.html) any helpful tips/advice regarding anything will also be apprecaited I am new to html but wanted to code a website as practice starting from a free template I found online
.headerC {
background: #89cff0;
font: 36pt/40pt courier;
color: white;
line-height: 0.1;
font-variant: small-caps;
border: thick dashed hotpink;
width: fill;
}
<div class="headerC">
<h1 align="center">Contact</h1>
<h5 align="center"> 203-831-9722</h5>
<h5 align="center">234 East Ave 06855</h5>
<h6 align="center">Norwalk, CT</h6>
</div>
Basically I just want that pink dotted border to fill the width I tried a few things but nothing seems to work
Add this to prevent the default margin of the bodyelement:
html, body {
margin: 0;
}
.headerC {
background: #89cff0;
font: 36pt/40pt courier;
color: white;
line-height: 0.1;
font-variant: small-caps;
border: thick dashed hotpink;
}
html,
body {
margin: 0;
}
<div class="headerC">
<h1 align="center">Contact</h1>
<h5 align="center"> 203-831-9722</h5>
<h5 align="center">234 East Ave 06855</h5>
<h6 align="center">Norwalk, CT</h6>
</div>
You have a max-width: 960px set on all divs within the element with the id of 'body'.
You need to override this styling on this particular element.
Either remove that max-width (note: This may have unintended consequences on other elements, without knowledge of your codebase I can't say if this is safe), or alternatively you could set max-width: none !important; on the .headerC element.
As an aside: the use of !important is generally frowned upon, and it's better to have a more organised and well formed CSS structure, but that's out of scope of this question, you can read more here.
EDIT: For the image on index.html you'll need a slightly different approach. Remove the image element from the HTML:
<img src="images/truck.jpg" alt="">
and instead use the image as a background image on the .header element like so with CSS:
background-image: url('images/truck.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: 10%;
So your CSS for the .header element will now look like:
#body.home div.header {
background-image: url('images/truck.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: 10%;
margin: 0;
max-width: none;
overflow: hidden;
padding: 0;
width: 100%;
}
Use the background-position attribute to position the image as you desire.
You will also be able to remove the following from the CSS as it's now redundant:
#body.home div.header img {
display: block;
left: 50%;
margin: 0 auto 0 -563px;
padding: 0;
position: absolute;
width: 1126px;
}

Is there a way to have an outer layer of an image using CSS?

UPDATE:
I don't know why I was downvoted, if someone could tell me what I did wrong it would be cool, If you are sure about your answer I could accept a No or yes as answer and a simple idea of how to do it.
.gallery {
display: inline-flex;
align-items: center;
justify-content: center;
text-align: center;
}
.gallery img {
min-width: 33%;
max-width: 33%;
min-height: 120px;
max-height: 120px;
}
<div class="gallery">
<!--Images users provide examples:-->
<img src="https://unsplash.it/200">
<img src="https://unsplash.it/200/100">
<img src="https://unsplash.it/100/300">
<!--etc...-->
</div>
That's ok but I noticed that some images look ugly because they are horizontal images or vertical and I'm giving them a squared shape (I don't like how they look when I give them width/height auto because they all together look disordered and bad).
What I recently did was In another part of the web was to do something like:
.image-cool {
min-width: 230px;
max-width: 230px;
min-height: 280px;
max-height: 280px;
/*Has a rectangle shape*/
background: #eee;
}
.image-cool img {
max-width: 230px;
max-height: 230px;
}
<div class="image-cool">
<img src="https://unsplash.it/200">
</div>
That last code works in the next way:
If you put multiple divs with images inside all will be aligned in the screen and the images will adjust its width and height limited by their container and images don't look bad anymore.
The question here is if I could achieve the same result of the second code in the first code using CSS and without adding more HTML like a div or container.
Why would I not want another div?
Because the current HTML of the first part is essential in that way for some long scripts, Yeah I could modify it all but It will take time and I'm just asking to see if I can save some time.
Thanks a lot for your time! :)
Instead of using img tag, you can use background-image and background-size: cover on a div therefore any image size can fit into the element.
.gallery {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
text-align: center;
}
.gallery-image {
width: 33%;
height: 120px;
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
<div class="gallery">
<div class="gallery-image" style="background-image:url(https://unsplash.it/100/300)"></div>
<div class="gallery-image" style="background-image:url(https://unsplash.it/200/100)"></div>
<div class="gallery-image" style="background-image:url(https://unsplash.it/200)"></div>
</div>