The parent div has margin and is set to auto, but the child does not fill the entire width at 100%

My goal is to design a menu that spans the entire width of the page with some margin on the left and right sides. Here is the layout:

<div class="parent">
    <div class="content">
        <div class="element">
            <a><p>Text1</p></a>
        </div>
        <div class="element">
            <a><p>Text2</p></a>
        </div>
        <div class="element"> 
            <a><p>Text3</p></a>
        </div>
        <div class="element">
            <a><p>Text4</p></a>
        </div>
    </div>
</div>

Here is the CSS code for this design:

.parent
{
    width:100%;
    float:left;
}
.content
{
    width:auto;
    margin:0 20px;
    float:left;
    border-top:1px solid;
    border-left:1px solid;
    border-right:1px solid;
}
.element
{
    width:100%;
    float:left
}
.element a
{
    width:auto;
    padding:10px;
    border-bottom:1px solid;
    text-align:center;
    display:block;
}

The intention is to have a full-width div with margins on the sides, while the bordered content inside fills the remaining space.

You can view an example here on jsfiddle.

Answer №1

Let's start by eliminating the float:left; property from the .parent, .content, and .element elements. This will ensure everything stretches to 100% width. In fact, there is no need for any styling on the .element element based on your request.

The existing margin on both sides of the .content container means you won't have to make any adjustments here.

.content {
  margin: 0 20px;
  border-top: 1px solid;
  border-left: 1px solid;
  border-right: 1px solid;
}
.element a {
  padding: 10px;
  border-bottom: 1px solid;
  text-align: center;
  display: block;
}
<div class="parent">
  <div class="content">
    <div class="element">
      <a>
        <p>Text1</p>
      </a>
    </div>
    <div class="element">
      <a>
        <p>Text2</p>
      </a>
    </div>
    <div class="element">
      <a>
        <p>Text3</p>
      </a>
    </div>
    <div class="element">
      <a>
        <p>Text4</p>
      </a>
    </div>
  </div>
</div>

(I also removed the unnecessary width: auto; from the .content element)

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

How to submit a textarea with the enter key without needing to refresh the page

I'm attempting to handle the submission of a textarea when the user hits the enter key, storing the text in a variable, then swapping out the form with the text and adding a new form at the end, all without refreshing. I had success doing this with an ...

Tips for aligning the dialog box in the center of the screen based on the current scroll position

Is there a way to center the dialog box vertically at the current scroll position of the window when any of the "show dialog" buttons are clicked? For instance, if I click on location 3, I want the dialog box to be centered vertically within the current v ...

Elements with inline-block display not aligning horizontally next to each other

I am puzzled as to why the two sections, section.structure and section.specificity, are not aligning properly. It seems that when I reduce the width, they line up correctly at a certain point which I haven't identified yet. My goal is to have the wid ...

Sending the value from a for loop through AJAX and populating it into a form field

Currently, I have implemented a piece of JavaScript code that captures user input, sends a request to an endpoint using AJAX, and appends a specific field from the returned results as an option within a datalist. This functionality is working perfectly fin ...

Can someone guide me on the process of assigning multiple classes in my situation?

Can someone assist me with combining multiple classes in an ng-class statement? I am attempting to utilize multiple classes for my element using ng-class. Here is what I have: <div id='test' ng-class="{blue: test, scale: scale, 'non-sca ...

What is the best way to position an element in the center of the screen

What is the best way to vertically center a button within a div? Click here for an image of the issue I recently started learning HTML & CSS and I am struggling to align a button in the vertical center of a div. Text-align:center doesn't seem to be ...

Creating a default option of "please select" on an Angular select element with a null value for validation

Within my Angular application, I am using a select element with an ngFor loop: <select formControlName="type" required> <option *ngFor="let type of typeList" [ngValue]="type.value">{{ type.caption }}</option> </select> When view ...

closing the space between rows at the top

Having trouble adjusting the top margin to match the left and right margins between items. How can I narrow the top gap so it aligns with the spacing of each bootstrap button? https://i.stack.imgur.com/1dZEC.jpg html <div ng-controller="RecipeCo ...

Tips for expanding the boundary of a Font Awesome icon

The image is in a h1 editing margin of h1 only edits margin-left increases the margin on the left of the image. trying to increase margin of i element does nothing either on i element does nothing either. <h1 class="display-4"><i class="fas fa- ...

Repeating nested items

I'm experiencing difficulty with repeaters. I'm attempting to display a list of appliances under unique categories from my database. The category names repeat correctly, but the appliance names are repeating for all categories (meaning all catego ...

Exploring the world of jQuery waypoints and the art of modifying

This is only the second question I'm asking here, so please be gentle! I've been experimenting with jQuery waypoints to dynamically show and hide a border under my navigation menu based on scroll position. For instance, when the sticky nav is ov ...

Bootstrap version 5 has a feature where the dropdown menu extends beyond the right side of the screen

I'm facing an issue with the default bootstrap 5 navbar. When I attempt to add a dropdown on the right side, the list of dropdown items extends beyond the screen on the right. You can see the problem here. I've tried the solutions suggested in s ...

Expanding and Shrinking Text Areas with Jquery

I am looking to create a textarea that comes preloaded with content. The height of the textarea should adjust to auto when there is content present, but switch to a height of 4.2rem when it is empty. Additionally, I want the textarea to dynamically increas ...

Limit the display of an element to a single occurrence by utilizing jQuery cookies

I have a unique piece of HTML and JS code that showcases an eye-catching modal box. This remarkable feature is present on every page of my website. However, to enhance the user experience, I would like to ensure that this div element is displayed only once ...

Unexpected rendering occurs when using a CSS double transition

In my React application, I am working on implementing smooth page transitions for a more visually appealing user experience. While using simple fade-in/fade-out transitions, I have noticed that transitions between pages with similar content are not as smoo ...

Step-by-step guide on showcasing an image within the sidebar-wrapper CSS class

I am trying to include an image in the #sidebar-wrapper class using the code below: CSS: #sidebar-wrapper { background-image:url('/images/nav.jpg'); margin-left: -250px; left: 250px; width: 250px; position: fixed; height: 100%; ov ...

The issue with the ng-click property not triggering arises when it is assigned to a button that is generated dynamically

I need to construct a table dynamically based on the results returned from SQL. In this scenario, populating the table with the start time, end time, and user is straightforward: var StartTime = document.createElement('td'); var EndTime = docume ...

Creating an interactive webpage with Javascript and HTML

I'm facing a challenge with my component setup, which is structured as follows: import { Component, VERSION } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ ...

I appear to be having issues with the pseudo-element. Is there something I am doing incorrectly? This relates to the first child

I initially tried to make the opening paragraph stand out by applying a red color, but unexpectedly, all elements on the page turned red. view image here <!DOCTYPE html> <html lang="en"> <head> <link rel="styleshee ...

Eliminate any HTML content using Jsoup while retaining the text lines

I am working with a String that contains e-mail content, and my goal is to eliminate all HTML coding from this String. This is the current code I have: public static String html2text(String html) { Document document = Jsoup.parse(html); document = ...