The dimensions of images and text in HTML

I'm having trouble adjusting my page to fit the resolution of the screen. The image I'm using is 1920 pixels wide, but it's extending beyond the window. Click this link for the code and images: index1.html and style1.css are not needed.

<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="../css/style.css">
   <style>
    
    </style>
</head>
<body>
<div class="container1">
    <img style="width: 100%" src="../img/Zrzut ekranu 2021-10-18 o 15.21.01.png">
    <p class="text1">
        <b>Manage stress and take control<br>
            in business and personal life!</b>
    </p>
    <p class="text2">
        <b>Learn coherence techniques during meetings<br>
            one-on-one and master your stress.</b>
    </p>
    <button class="button1">
        <b>Sign up for the course</b>
    </button>
    <button class="button2">
        <b>About me</b>
    </button>
</div>
</body>
</html>


.text1 {
    position: relative;
    font-family: Ubuntu;
    font-size: 70px;
    margin: 0;
    color: white;
}
.text2 {
    position: relative;
    font-family: Ubuntu;
    font-size: 25px;
    margin: 0;
    color: white;
}
.button1 {
    position: relative;
    background-color: #D32A34;
    color: white;
    border: none;
    width: 239px;
    height: 71px;
    font-size: 20px;
}
.button2 {
    position: relative;
    border: solid #D32A34;
    color: white;
    background-color: transparent;
    width: 239px;
    height: 71px;
    font-size: 20px;
}

Answer №1

Creating a Responsive Image

To learn how to make an image responsive, check out this online exercise.

.container1 img {
    max-width: 100%;
    height: auto;
}

Using an Image as a Background

For using an image as a background, apply the following styles to your img selector:

.container1 img {
    width: 100vw;
    height: 100vh;
    object-fit: cover;
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
}

The height is set to 100vh, filling 100% of the viewport's height even if the parent element has less height.

object-fit ensures the image looks good on all devices regardless of aspect ratio.

position: fixed makes the image act like a background.

z-index positions the image behind everything else on the page.

.container1 img {
  width: 100vw;
  height: 100vh;
  object-fit: cover;
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
}

.text1 {
  position: relative;
  font-family: Ubuntu;
  font-size: 70px;
  margin: 0;
  color: white;
}

.text2 {
  position: relative;
  font-family: Ubuntu;
  font-size: 25px;
  margin: 0;
  color: white;
}

.button1 {
  position: relative;
  background-color: #D32A34;
  color: white;
  border: none;
  width: 239px;
  height: 71px;
  font-size: 20px;
}

.button2 {
  position: relative;
  border: solid #D32A34;
  color: white;
  background-color: transparent;
  width: 239px;
  height: 71px;
  font-size: 20px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="container1">
    <img src="https://laaouatni.github.io/w11-clone/images/1dark.jpg">
    <!-- text 1 -->
    <p class="text1">
      <b>Master stress and take control in business and personal life!<br></b>
    </p>
    <!-- text 2 -->
    <p class="text2">
      <b>Learn coherence techniques in one-on-one meetings and conquer your stress.<br></b>
    </p>
    <!-- button 1 -->
    <button class="button1"><b>Sign up for the course</b></button>
    <!-- button 2 -->
    <button class="button2"><b>About Me</b></button>
  </div>
</body>

</html>

Answer №2

If you adjust 'width: 100%' to 'width: 100vw', it will be based on the viewport width

Answer №3

Start by customizing your .box1 or webpage layout and setting its width to 100% or 100vw. Using pixels may not produce the desired result as some screen space is allocated for scrolling. Cheers!

Answer №4

When optimizing your images, consider using 100vw (view width) instead of 100% and ensure all margins and padding are set to 0 with the following CSS:

* {
    margin: 0;
    padding: 0;
}

Answer №5

To eliminate the gap between the image and box-sizing, integrate this style in your stylesheet:

"body {padding:0;margin:0;} "

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating an XML response to generate an HTML dropdown menu in PHP

My onChange function in javascript calls a PHP file to fetch UPS rates and update an HTML dropdown list. Everything was working fine until I needed to add an item to the options list based on a comparison. Javascript: function fetch_UPS(el){ var zip = ...

What is the purpose of using "recaptcha_get_html" function to echo?

Hello everyone. I've been struggling to incorporate Google's reCAPTCHA into my project and I keep encountering an error when trying to use the recaptcha_get_html function. It keeps showing up as undefined and despite searching high and low, I can ...

Using an aria-label attribute on an <option> tag within a dropdown menu may result in a DAP violation

Currently, I am conducting accessibility testing for an Angular project at my workplace. Our team relies on the JAWS screen reader and a helpful plugin that detects UI issues and highlights them as violations. Unfortunately, I've come across an issue ...

When the window is resized, the div shifts position

I recently added a div to my website with the following code: <div id="div"></div> #div { height: 400px; width: 300px; position: absolute; right: 59%; text-align: left; } Howe ...

Retrieve an HTML element that is a select option with jQuery

I have a select input containing different options as shown below: <select id="myArea"> <option class="myClass_1" style="color:red;" value="1">Area 1</option> <option class="myClass_2" style="color:green;" value="2">Area 2& ...

Tips for incorporating a JavaScript script into local HTML code

I am facing an issue with my code on jsfiddle. It works perfectly there, but when I try to run it locally, it doesn't seem to work. I have checked the code multiple times and even downloaded the jQuery file to link it, but still no luck. I feel like i ...

What is the best way to erase information displayed when hovering over an element using mouseout?

Whenever the user hovers over an image, an information box appears regarding that specific image. The information inside the box changes as I move over another image, but when not hovering over any images, the information box remains visible. Unfortunately ...

Achieving success by correctly reaching the window's edge during an event (onscroll)

This happens to be my 1st inquiry. Specifically, I have a navigation menu with a transparent background and I am attempting to alter the background once it reaches the top edge of the window. window.addEventListener("scroll", navSticky); function navSt ...

What is the best way to create a JavaScript "input" field that only accepts vowel letters?

Is there a way to create an "input" field that only accepts vowel letters? Additionally, I want to ensure that copying and dragging text into the input field will also be filtered out. <div class="field"> <input class="fie ...

Obtaining the field name from a SQL Server database by utilizing the field ID

When viewing my selections, I have several drop-down lists that display the ID and I would like the name to be shown to the user for clarity. However, when I list everything that has been added, it displays the chosen ID instead of the name. I attempted t ...

Improving Java Performance by frequently replacing one String with another

Currently, I am developing an HttpServlet extension (plugin) for a CMS that is responsible for filtering the HTML response. Within my method filterResponse, I retrieve the requested text/html as a String, which is one of three parameters. The main task a ...

Ways to alter the appearance of individual characters in the text input

My latest project involves dynamically changing the CSS styles of each individual character in a given text input. For example, if the user types "Stack" into the input field, I want the 'S' to appear in red, 't' in blue, 'a' ...

"An issue with IE9 causing small dots on rounded corners during rendering

Help Needed: Strange IE9 Rendering Issue I'm experiencing an odd rendering problem in Internet Explorer 9. You see those little white dots on the left? What could be causing them? Any suggestions on how to remove them? Microsoft, why always so myst ...

Having an issue with the Show/Hide Javascript toggle on the same page. Multiple hidden texts are appearing simultaneously. Seeking a solution without the use of JQuery

This code is functioning efficiently. I am looking for a way to display and conceal various texts using different expand/hide links within multiple tables on the same page. I prefer to achieve this without using JQuery, just like in this straightforward sc ...

Is it possible to ensure uniformity in the appearance of all images with MVC Image Handling?

I'm currently developing an MVC 4 application that allows users to upload images. These uploaded images are then displayed in a different view. Users can upload images up to 10MB in size, which are then resized to 800 x ? pixels using the WebImage (Sy ...

Why won't applying a binding style affect the appearance of my Vue component?

const EventLevelBoard = { name: "EventLevel", data: { levelStyle: { display: "block" }, levelStyleinner: [ { display: "block" }, { display: "block" }, { display: "block&qu ...

Altering an element's visibility from 'none' will trigger its animation to play once again

Take a look at the snippet below and you'll see that the sliding animation plays twice - once when the .addClass statement runs, and again when #test's display is switched to 'initial' (timeouts are in place to help visualize the issue) ...

What is the best way to create an animation for a dynamic menu background image

While hovering over a list item, I want to create an animation where the background image appears to zoom out. $(function () { var image = $('.menu').find('img').attr('src'); $('.menu ul li').mouseover(fun ...

Is there a way I can replicate this expandable div?

I have successfully implemented a script that expands and collapses. However, I am facing an issue when trying to use two of these scripts on the same page. I attempted to simply add "_two" to all instances of "accordion," but it doesn't seem to be f ...

Styling Collapse Text in Bootstrap Framework

I have a question about the "Accordion example" in the Bootstrap Collapse Component. My query is regarding removing the underline from the text "Collapsible Group Item" 1-3 when they are activated. Normally, this text is not underlined, but upon hovering, ...