steps to create a flexible width div with a set height

Is it possible to create a responsive div that is fixed from both sides? It would look something like this:

                               Screen (big)
+----------------------------------------------------------------+
|                                                                |
|....20px.....______________________________________....20px.....|
|            |                                      |            |
|            |                 div                  |            |
|            |______________________________________|            |
|                                                                |
|                                                                |
+----------------------------------------------------------------+




                Screen (small)
+--------------------------------------------+
|                                            |
|....20px.....__________________....20px.....|
|            |                  |            |
|            |       div        |            |
|            |__________________|            |
|                                            |
|                                            |
+--------------------------------------------+

It should be noted that my focus is just on left and right side.

Now, is the implementation of this possible?

Answer №1

This is a very straightforward solution.

CodePen

.container {
  padding: 10px;
  background: pink;
}
.item {
  height: 80px;
  background: #FFA500;
}
<div class="container">
  <div class="item"></div>
</div>

Answer №2

No need to wrap it:

<style>
div
{
border: 1px solid blue;
padding: 5px;
}
</style>

<div>Hello!</div>

Answer №3

Are you looking for this solution? http://codepen.io/panchroma/pen/xGJPyX
Add a parent div with 20px padding on the left and right sides.

HTML

<div class="container">
  <div class="content-div"></div>
</div>  

CSS

.container {
  padding-left: 20px;
  padding-right: 20px;
}
.content-div {
  background-color: pink;
  min-height: 100px;
}

Answer №4

It's a simple task to accomplish, do you perhaps want to create a more intricate layout?

All you have to do is specify the margin with your preferred values and utilize a standard div (display: block). You may also need to eliminate padding and margin from the body and html elements. For example:

HTML:

<html>
    <head>
        ...
    </head>
    <body>
       <div class="contents">...</div>
    </body>
</html>

CSS:

body, html {
  padding: 0px;
  margin: 0px;
}

.contents {
  margin: 0px 20px;
}

Check out this interactive demo for reference.

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 way to shift text upward while it is contained within a span element inside a list item?

In my code, I have the following setup: <li> <a class="dw"><span>Design Goals</span></a> </li> .dw { background-image: url(/Content/images/icons/fugue/document-globe.png); } However, when I check this in my browser ...

What is the best way to make a scrollable div with top and bottom padding without adding extra elements inside?

I have created a fiddle to demonstrate my question. Essentially, I am looking to create a scrollable container with padding at the top and bottom that remains visible during scrolling, ensuring there is always a consistent distance from the edge to the con ...

jquery allows you to toggle the visibility of content

There are two divs with a button positioned above them. Initially, only one div, the catalogusOrderBox, is visible. When the button named manualButton is clicked, it should display the manualOrderBox div while hiding the catalogusOrderBox div. The text on ...

Activate the "double" class within the image carousel

I have successfully implemented an image carousel using a combination of HTML and PHP. The functionality of the image carousel is as follows: This image carousel consists of two main components: the main image area and a thumbnail area with the id of "b ...

Arranging a list with both Left and Right alignment on a single line in proper sequence

After trying to work with a list containing 5 items, I'm having trouble getting it to display as shown in the desired image. I am new to CSS so any guidance is appreciated. #navlist li { display: inline; } #navlist #right { float: right; margin-righ ...

Adjust the page height when switching between two divs

Currently, I have two divs that are being displayed one at a time using the toggle method. Please refer to the screenshot below: In the image provided, when the user clicks on "show more", the div will toggle based on the first item in the list. If the u ...

Ensure that all Woocommerce products have consistent height while allowing for variations in width

I am struggling with achieving consistent height for all the products on my website, while allowing for variable widths. I attempted to resolve this using UI, but unfortunately it was unsuccessful. Does anyone have suggestions on how to accomplish this u ...

Retrieve information from a JavaScript file to incorporate into an HTML file

Seeking a solution to store the base URL in a separate JavaScript file, I've implemented a JavaScript file named config.js containing: export const baseUrl = "https://example.com/"; There are multiple HTML files utilizing this base URL for ...

Prevent carousel from rotating while video is playing in Bootstrap 4

Currently, I am in the process of constructing a Bootstrap 4 carousel featuring various videos and other content. My goal is to have the carousel pause when a video is playing (with auto-play enabled) and resume once the video reaches its conclusion. I&ap ...

What is the best way to determine the length of an array using input from an HTML form?

Currently, I am in the process of developing a PHP file and one feature that I would like to implement is creating an array as illustrated in Example 1. As far as my understanding goes, an Array functions like a list where items can be added as shown in Ex ...

Determining the Nearest Form to an Element using jQuery

I developed a JavaScript/jQuery script that allows me to check all checkboxes within a form. The function works effectively, but the issue is that it checks all checkboxes on the page regardless of their form wrapper. Here is the function: function toggl ...

Calculate the sum of two numbers in a Django application and display the result on the same page

I am a beginner in Django and I'm looking to calculate the sum of two numbers, x and y. The values for x and y will be input by the user. Check out my views.py code: from django.shortcuts import render from django.http import HttpResponse def home ...

Is there a way for me to identify a click on a specific element along with all of its child elements, excluding one

I'm attempting to implement a feature where clicking on a specific div triggers a function in JavaScript/jQuery, but I don't want the function to be triggered if I click on a child element of that div. Here's the structure I'm working ...

Exploring the Beauty of Trees through Twitter Bootstrap

I've been exploring the creation of a tree structure (similar to a directory tree) using mainly CSS and minimal JS for things like states. I'm wondering if there are any reliable tree plugins available that work well with bootstrap or jquery-ui b ...

What could be the reason for my button not activating the form while inside a bootstrap modal?

I have developed a Bootstrap modal with two buttons. Both buttons are submit type and are supposed to trigger the form wrapped around them. However, this is not happening, and I am unable to identify the issue. Here is my code: <?php if ($_SERVER[" ...

Streamlining form submission processes with VBA automation of check box selections

Currently, I am facing a challenge with automating the process of filling out a web form using VBA. Specifically, I have hit a roadblock when it comes to selecting a checkbox in the form before proceeding. Here is the HTML code pertaining to the checkbox: ...

Center the two consecutive <div> elements within the container

There is a situation where <div id="container"> <div id="div2">Title</div> <div id="div3">text text</div> </div> and in this scenario, #container { height:500px; width:100%; text- ...

Implementing a line break within a JavaScript function specifically designed for formatting images

As I am not a javascript developer, the code I have been given for debugging is somewhat challenging. The issue at hand involves an iOS module where a .js CSS file has been set up and images are loading improperly, resulting in the need for horizontal scro ...

I'm experiencing some unwanted white spaces between my div elements, likely caused by varying heights. Is there a way to remove these spaces without having to resort to setting a fixed

I came across a similar thread discussing my issue, where the suggested solution was to set a fixed height. However, this approach is causing some problems with the responsiveness of the theme I am using. Let me provide more details. The theme I am workin ...

The MVC5 model is unable to retrieve the text value entered in the textbox

I'm having trouble understanding what I'm doing wrong. Controller: public class PictureController : Controller { // GET: Picture public ActionResult Index() { return View(); } public ActionResult AddGroup() { ...