The header image is not spanning the full width of the page

I have been practicing my skills with HTML and CSS, attempting to achieve the desired effect of making an image span the width of the page while centered below the header. Despite trying various options to stretch it, I haven't been able to make it work. It seems like there may be a specific solution that I am missing.

CSS

#header {

height: 80px;
background-color: gray;
margin-bottom: 60px;


}

.container {

}

.MainImage {

background-image:url(computers.jpg);
height: 400px;
background-repeat: no-repeat;
width: 100%;

}

.MainImage img {

position: relative;
background-size: cover;

}

HTML

<body>

<div id = "header">
<div class = "nav">

    <!-- container-fluid gives full width container of whole viewport -->

    <div class = "container-fluid">


    <ul id = "nav" class= "text-left">
        <li><a href = "#"><strong>Home</a></li>
        <li><a href = "#">Technologies</a></li>
        <li><a href = "#">Programs</a></li>
        <li><a href = "#">Blog</strong></a></li>
    </ul>

    <ul id = "nav" class = "text-right">
        <li><a href = "#"><strong>Sign Up</a></li>
        <li><a href = "#">Sign In</a></li>
        <li><a href = "#">Contact</strong></a></li>
    </ul>

    </div><!-- end container-fluid-->
</div><!--end nav-->
</div> <!-- end header --> 



<div id = "Main">


    <div class = "MainImage">


    </div>


</div><!--end Main-->



</body>

http://jsfiddle.net/xehu1tg0/

UPDATE: encountering issues with image display in IE 10

Answer №1

To ensure your .MainImage class displays correctly, you must include the CSS rule background-size: 100% auto;:

.MainImage {
    background-image:url(http://www.onlineuniversities.com/wp-content/uploads/improving-tech-literacy.jpg);
    height: 500px;
    background-repeat: no-repeat;
    background-size: 100% auto;
    width: 100%;
}

Note that the height and width properties only impact the container element, not the background image itself.

Using 100% auto specifies that the image should be 100% of the container's width and automatically adjust its height proportionally (in this case, the first value represents the x-axis and the second value relates to the y-axis).

I hope this explanation clarifies things for you!

For a visual example, see: http://jsfiddle.net/w1020vu1/

Answer №2

Initially, by utilizing CSS's background-image attribute instead of an img element with a src attribute, your .MainImage img selector becomes redundant.

Regarding the styles for .MainImage, the height and width attributes only impact the container element, not the background image itself. To adjust the size of the background image, apply this style:

.MainImage {
    background-size: 100% auto;
}

Answer №3

After reviewing the responses given, it became clear to me that potential compatibility issues with my Internet Explorer could be a factor.

Within the < head > section of my code, I inserted the following snippet:

<meta http-equiv="X-UA-Compatible" content="IE=80" />

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 a text container in PHP for displaying information

As someone who is new to PHP and HTML, I'm curious about the CSS statements needed to create a text box that will display a value from one of my PHP functions. Right now, the function retrieves data from an SQL server and returns it, but I would like ...

Adding a side border to a v-card in Vuetify.js

Is there a way to include a colored vertical border on just one side of a v-card component? Here's an example: https://i.sstatic.net/2jULy.png ...

What is the process for adding a counter variable to a field within a Twig file?

I'm working on a Twig file where I need to create a table and populate it with data using a loop. The issue is that all my field names are the same, so I want to add a counter variable to differentiate them. How can I achieve this? Below is the code ...

What is your rationale for using a navigation that has the flex-column class?

Is there a way to align a navigation menu to the right/end when using the flex-column class in Bootstrap 4? I attempted <div class="nav justify-content-end flex-column"> however, the navigation items are still aligned to the left ...

Is the checkbox automatically selected if a property is specified?

I am completely frustrated by this problem. I am struggling to find a way to toggle a checkbox using JavaScript. Here is the HTML code snippet in question: <div class="form-group"> <label class="col-sm-3 control-label"></label> < ...

Utilizing CSS Scroll-snap for Seamless Navigation

Attempting to create a basic carousel using scroll-snap, but encountering an issue where the carousel is randomly scrolling on load instead of starting with the first picture. See code snippet below. Any ideas on resolving this? .carousel { ...

Guide to executing the "brew" command on a Windows operating system

Encountering the error message "'brew' is not recognized as an internal or external command" in the Windows command prompt. https://i.sstatic.net/suFT2.png Attempting to set up the codeigniter-reactjs-example project from github, where the init ...

Apply a transformation effect to the current active menu item in the Wordpress CSS main navigation

I am currently working on a local WordPress installation with a theme. One specific feature I am trying to achieve is to have a dynamic colored line with a triangle shape indicating the 'current page' on the menu. My goal is to duplicate this tri ...

The search query highlighted in pagination takes us to page 2

I am currently facing an issue with a search function that provides paginated results. The result I receive is correct, but the problem arises when the highlighted page defaults to page 2 instead of page 1 upon clicking the search button. Despite trying ...

Exploring elements using dynamic xpaths in selenium

When working with Selenium WebDriver, I am facing difficulties selecting the <input class="cs-autocomplete-input" tabindex=""> element due to its fluid ID. This is because there are multiple items with the same class name on the web page, making it c ...

The conversation refusing to end

When attempting to add an entry to a database using a popup window, I encountered errors in the console upon clicking in the Name field. The error message displayed was: VM247 1:1 Uncaught TypeError: Cannot set property 'result' of undefined If ...

The issue with CSS classes and IDs not being applied to unordered lists has been encountered

Need help with modifying specific lists using class or id, but running into issues. Here's a code snippet to illustrate the problem: <!DOCTYPE html> <html> <head> <style> ul#square { list-style: square;} ...

What is the best way to ensure that text highlighting appears consistent on all browsers?

I am facing an issue with the appearance of text highlighting between Chrome and Firefox browsers. Is there a way to ensure consistency in the appearance across all browsers? a { text-decoration: none; font-weight: 900; font-size: 4vw !impo ...

Setting up the foundation for a Bootstrap footer: A step-by-step guide

I've been struggling to implement the HTML5 structure within Bootstrap 4 grid system to match the design shown in the attached images. The layout should resemble the top image when viewed on a phone, phablet, or tablet in portrait mode, and the bottom ...

Hello, I'm experiencing an issue with the dynamic append tag in jQuery

This is the code I'm working on: I am trying to resize either .modal-left or .modal-right, but the function doesn't seem to be working. Interestingly, it worked when I executed it in the browser console. I suspect that this issue may be caused ...

What could be the reason my divs are not aligning to the top?

I'm having an issue with displaying posts one under the other. They are showing up one by one as intended, but not one under one. Here is a visual representation: In the image provided, you can see that there is a gap between the 1st and 4th post. I ...

picture protrudes from the frame [WordPress template]

Check out my website: If you scroll to the bottom of the site, you'll see an image of a bride sitting on a couch. I recently added this code to the stylesheet: .novia img {min-width:1000px; float:none;} This code was meant to maintain a fixed heigh ...

Image source not functioning properly

I am attempting to dynamically load an image and HTML using Angular but it doesn't seem to be working. Here is what I have tried: <img ng-src="img/{{product.Category}}/{{product.id}}.jpg"> and also this: <img src="img/{{product.Category}}/ ...

How can I use JavaScript or JQuery to retrieve the HTML content as a string from a specific URL?

How can I extract the URL of a link from a webpage and add it to my code without an ID for that specific link? Is there a way to search based on the content within the <a> tag? ...

I want to customize my drop-down menu so that it only displays categories from my MySQL table when the parent_id is not equal to 0

Greetings! As indicated by the title, I am on a quest to create categories with subcategories. Despite my efforts scouring through Stack Overflow for assistance, none of the examples provided have proven helpful. Allow me to provide a visual representatio ...