"Stretch an element to the edge of the browser window within a centered container without using JavaScript

My goal is to have a centered wrap with fixed width and dynamic padding, containing p-tags. The challenge is to make the background color of these tags extend to the browser's border on the left while keeping the content inside the centered area.

I was able to achieve this using JavaScript (Example).

Here is the basic structure:

<div>
    <p>asd</p><br>
    <p>asd</p><br>
    <p>asd</p>
</div>

This is the JavaScript code used to style the elements:

$(document).ready(function() {
    $('p').css('padding-left', $('div').css('padding-left')).css('margin-left', "-" + $('div').css('padding-left'));
});

And here is the CSS applied:

div {
    width: 300px;
    background: green;
    padding: 0 calc(50% - 150px)
}

p {
    height: 50px;
    margin-bottom: 10px;
    background: blue;
    display: inline-block;
}

Is there a way to achieve this layout using only CSS without relying on JavaScript? It should be supported on FF, Chrome, iPad iOS5/6 Safari, and IE9+.

/EDIT:

After some feedback and research, I found a SOLUTION to the issue by modifying the structure slightly: http://jsfiddle.net/EFhkH/2/

If you have any recommendations for improving the line breaks after the inline-block elements, feel free to share!

Answer №1

Based on your description, it seems like the desired effect is not being achieved in your fiddle. The blue color does not stay aligned with the left side when the screen is resized. I attempted a solution using CSS and partially succeeded by utilizing the :before element. However, this approach only works for single-line text.

If anyone has ideas on how to improve this, please share!

CSS

#container {
    width: 300px;
    background: silver;
    margin: 0 auto;
}
.text {
    width: 100%;
    background: gray;
}
.text:before {
    content: ".";
    background: gray;
    width: 50%;
    position: absolute;
    left: 0;
    z-index: -1;
    color: gray;
}

HTML

<div id="container">
    <div class="text"><p>line 1</p></div>
    <div class="text"><p>line 2</p></div>
    <div class="text"><p>line 3</p></div>
    <div class="text"><p>line 4a<br />line 4b</p></div>
/div>

Check out the JSFiddle here

EDIT: To make the background appear correctly, I had to include a hidden dot "." in the before element and give it the same color as the background.

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

What are some ways I can customize the appearance of a Bootstrap 5 Navbar specifically when it is in the collapsed

Currently utilizing Bootstrap 5, my goal is to align the Navbar social icons to the left when the navbar is collapsed for small/mobile devices, and to the right when viewed on a larger screen. Can you assist in identifying which selectors need styling for ...

Center an element on the screen by dynamically adjusting its position according to the media query

As I work on creating a responsive website that can adapt to screens of various devices, I am facing a challenge. Each device has a different number of pixels, making it difficult to centrally position a div dynamically. Currently, I am using margin-left: ...

Adjust the text color based on the background image or color

Here on this site, I have designed the first div to display a dark image, while the second one shows a light background. I am aiming to adjust the sidebar text color based on whether it is displayed against the dark or light background. How can I achieve ...

Choose a single SVG file based on its unique ID

Looking for some guidance - I have a vector file with 40 svgs all combined into one image. Each vector is identified by an id inside the <g> tag. How can I select a specific svg from this single file without choosing the entire image in html? PS: E ...

The transition from Vuetify 2 to Vuetify 3 involves replacing deprecated properties like app, clipped-left, and offset-y with the new property called "

Seeking guidance on upgrading from Vuetify/Vue 2 to 3. As a non front-end developer tasked with updating legacy code, I'm facing challenges due to the migration guide assuming CSS knowledge and lacking examples for resolving removed features. The gui ...

Retrieve information from api and showcase it in html

Recently, I came across an interesting API file that caught my attention: My goal is to extract information from this API and present it on a HTML page in a visually appealing format. Despite scouring various forums, I have yet to find a solution that fi ...

Switch image on click (toggle between pause and play buttons)

Having some difficulty setting up a 3D audio player in A-Frame where the pause and play button images need to change depending on whether the audio is playing or not. Interested in seeing my code in action? Check it out here. ...

Nesting functionality in TailwindCSS is currently experiencing issues when used in conjunction with tailwindcss/nesting or postcss

PostCSS Configuration module.exports = { plugins: { "postcss-import": {}, "tailwindcss/nesting": {}, tailwindcss: {}, autoprefixer: {}, }, }; Global CSS Styles .form-control { @apply w-full px-3; & p { @ ...

Choose a paragraph of text that spans multiple lines without exceeding the width

In the image above, the red selection crosses outside of the yellow area when selecting. I would like to only select within the yellow part as shown below: Here is the HTML: <div id="parent"> <div id="child"> This is some content. ...

Bootstrap-4 modal not appearing when activated within a dropdown menu

When I tried to use a dropdown feature in my application, I encountered an issue where one of the dropdown items was supposed to open a modal but it wasn't showing up. However, the modal worked perfectly fine when I accessed it using a normal button i ...

Can a Data Table and a grid be combined within an MVC View?

Is it possible to display data from a database table in a 2x3 grid format, which can be paginated for easy navigation without having to list everything vertically? I currently have the following code that displays all the data one after the other, but I wo ...

What is causing the role="status" attribute to malfunction?

I'm having an issue with the role="status" attribute in my code. When using a screen reader, the paragraph text doesn't get read once it's appended to the body. index.html: <!DOCTYPE html> <html> <head> <title> ...

Issue with Chrome Browser Border Radius Bug when applied to element with Display: Table

I'm facing an issue with border radius in the Chrome browser. When applying a border-radius to an element styled with dashed border-style and display:table, the background-color exceeds the limit of the border. Here's how it appears in Chrome: ...

Replace existing styled-component CSS properties with custom ones

One of my components includes a CheckBox and Label element. I want to adjust the spacing around the label when it's used inside the CheckBox. Can this be achieved with styled()? Debugging Info The issue seems to be that the className prop is not b ...

Using HTML5 to implement a progress bar that updates on click

I currently have an HTML file that contains clickable content. <tr onclick="callfn('Afile.jsp?param1=<%= value1%>');" id="contenttr"> The function callfn is located in a JavaScript file that loads images. Because this function can t ...

Using Bootstrap4 container-fluid on elements other than the "div" tag

For my Bootstrap 4 alpha 5 project: I am experimenting with using the .container-fluid class on HTML elements that are not divs. However, I have noticed a change in output when applying it to non-div elements. My main objective is to create custom CSS for ...

Ensure that the text on the button is aligned to the right and is positioned directly

Looking to adjust the alignment of the menu icon on the left side of the menu text. Unsure about the best approach with my current CSS - should I utilize a grid layout to separate them on one side each, or perhaps use flexbox to align them inline? Below i ...

Utilize HTML to place text above an image within a TextView

While working with a list of Strings that I add to TextViews individually, I encounter an issue with displaying images alongside the text. The HTML format allows me to include images within the text, but currently, the image appears below the text instea ...

Utilizing Nginx with Angular2 for a seamless PathLocationStrategy implementation in html5

Angular2 is a single-page site, so all URL requests need to be redirected to index.html using Nginx. Below is the Nginx server block configuration: server { listen 8901; server_name my_server_ip; root /projects/my_site/dist; location /.+& ...

jQuery now enables the number field to accept not only one period but multiple periods

I need to update a number field in a form to accept multiple periods, specifically for entering software version numbers. Currently, the code I am using only allows for one period to be entered, resulting in an error when two periods are added to the fie ...