Tips for creating an adjustable background image size

Is it possible to adjust the height of a section to match the background using CSS?

Currently, the height is determined by the content within the section.

CSS:

.content-block {
  background-size: cover;
  background-position: center center;
}

HTML:

<section id="header-section" class="content-block" style="background: url(./images/bk1.jpg)">
  <div class="container">
    <div class="row">
      <div class="col-xs-6">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore, nam error praesentium dolorum quae provident nulla expedita? Libero, quia, perferendis, illo asperiores blanditiis impedit nemo consectetur commodi architecto cumque cupiditate.</div>
      <div class="col-xs-6">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Possimus, officia, quasi, minus, quas odio explicabo similique blanditiis odit unde dolores assumenda aut sint incidunt eaque maiores recusandae eum quod voluptate.</div>
    </div>
  </div>
</section>

Answer №1

This solution appears to be effective. Check out the JSFiddle example here

html,body {
  height:100%;
}

.content-block {
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  background-image: url(http://placekitten.com/200/300);
  width: 100%;
  height: 100%;
}

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

Is there a more efficient method for replacing all attributes on cloned elements?

While this code does the job, I can't help but feel like it's a bit outdated. I'm not very experienced with JS/JQuery and only use them when necessary. My approach involves cloning an element and then replacing all of its attributes with on ...

What causes the NavBar to show and hide within a specific range?

Recently, I encountered a perplexing issue with my navbar. It functions correctly except for one strange behavior that has left me baffled. Why does the menu appear when I adjust the width to 631px, but disappear at 600px? And vice versa – why does it wo ...

Why Isn't the Element Replicating?

I've been working on a simple comment script that allows users to input their name and message, click submit, and have their comment displayed on the page like YouTube. My plan was to use a prebuilt HTML div and clone it for each new comment, adjustin ...

The Bootstrap Navbar appears hidden beneath other elements on mobile devices

While using bootstrap to style my header contents, I encountered a strange issue. The navbar that appears after clicking on the hamburger menu is displaying behind all the components. Even though I've set the z-index to the maximum value, it still doe ...

Content within a div that is floated left

The scenario at hand is: HTML Structure: <div id="main"> <div id="a"></div> <div id="b">Some Text</div> </div> CSS Styles: #a{ float:left; width:800px; height:150px; background-color:#CCC; } ...

Is it possible in HTML to detect *any* changes made to an input, not just those made by the keyboard?

Let's consider a scenario where we have an input element like this: <input id="myInput" type="text" /> The question now arises, how can we detect when the value of this input is changed programmatically (such as through $("#myInput").val("new ...

Some elements are not visible when inspecting the source code

Hidden fields are essential in my jsp form. Interestingly, when I check the page source in a browser, only the first hidden field is displayed, not the second one. <input type="hidden" name="url" id="url" value="<%=url%>" /> <input type="hi ...

What is the best method for displaying the toggle button for Drawer/Sidebar above the main content and appbar?

After reviewing Vatsal's suggestion, it appears that moving the toggle button above the drawer is the solution to our main issue. By taking advantage of the open state of the drawer, we can adjust the left property of the button in the toggleButton cl ...

Cross-browser compatibility issue: Chrome displays image class in HTML CSS, while Firefox does not

I am building a webpage and have encountered an issue with displaying images in different browsers. Specifically, the image appears correctly in Chrome and Safari but doesn't show up in Firefox. By "doesn't show," I mean that the image is not vi ...

Activate the dropdown menu when ul element is in focus

I am working on creating a dropdown navigation using pure CSS. I want the dropdown menu to appear when clicking on the ul. The issue I am facing is that simple ul:focus > ul does not seem to work, even though there is an anchor within it. However, the :hov ...

Putting an <input/> element inside a Material UI's <TextField/> component in ReactJS - a beginner's guide

I am attempting to style <TextField/> (http://www.material-ui.com/#/components/text-field) to resemble an <input/>. Here is what I have tried: <TextField hintText='Enter username' > <input className="form-control ...

Chrome has some issues with resizing the SVG Pattern element

Utilizing inline svgs, I have a svg circle filled with a pattern that should cover 100% of the container size. However, when the parent element is resized via JavaScript, the pattern no longer reflects the 100% width and height as expected. This issue seem ...

"Customizing the website's font: A step-by-step guide to replacing the default Roboto font with a custom font

Despite my efforts to globally override the font, I am still encountering instances where the Roboto font is not being replaced, particularly on MUI select and autocomplete components. import { createTheme } from '@material-ui/core/styles'; // A ...

Stylesheet failing to respond to CSS Media queries

I have looked at other questions with similar issues and it appears that what's missing in their code is: <meta name="viewport" content="width=device-width, initial-scale=1"> However, I already have this code implemented but still facing probl ...

Restore radio input functionality with a transparent method

I've experimented with various methods, including traditional form reset techniques and jQuery solutions from different sources on the internet without success. Snapshot: The Objective: I am working on a sortable list where users are required to ra ...

PHP - contact form is live for just 2 hours daily

Hello, this is my first time here and I could really use some assistance. Please bear with me as English is not my strong suit compared to most people here, but I'll do my best to explain my query. So, I have a PHP script for a contact form on my web ...

Altering CSS styles through JavaScript or jQuery

Exploring Options After investigating the use of .css() to manipulate CSS properties of specific elements, I came across a website showcasing the following CSS: body, table td, select { font-family: Arial Unicode MS, Arial, sans-serif; font-size: ...

Manipulating DropDownList Attributes in ASP.NET using JavaScript

I am facing an issue with populating a Dropdownlist control on my ASCX page. <asp:DropDownList ID="demoddl" runat="server" onchange="apply(this.options[this.selectedIndex].value,event)" onclick="borderColorChange(this.id, 'Click')" onblur="bo ...

Automatic playback of the next video in a video slider

Looking to upgrade my video slider functionality - switching between videos, playing automatically when one finishes. Struggling to find a solution that combines manual control with automatic playback. My attempt: function videoUrl(hmmmmmm){ ...

Tips for inserting a personalized image or icon into the ANTD Design menu

Can anyone help me figure out how to replace the default icons with a custom image or icon in this example: https://ant.design/components/layout/#components-layout-demo-side I attempted to do it by including the following code: <Menu.Item to="/" key=" ...