Can an auto-margin container use an image as its left and right border using :before and :after pseudo-elements?

Hey there! I'm currently working on a project where I need to enclose a Youtube video in a container with automatic margins. The twist is that I want to add two images as a border on the left and right sides using the :before and :after pseudo-elements. Ideally, I want the video to be centered in the browser window, but when the window is resized, the border images should be revealed.

I have a feeling that my positioning is off, and I'm not entirely certain if using :before and :after is the best approach for this. Any suggestions on how to achieve this desired effect would be greatly appreciated.

EDIT: The left image should align its right edge with the left edge of the video, while the right image should align its left edge with the right edge of the video, creating a sort of inline-block effect. Could this potentially be done with a background image behind the entire video?

Below is the HTML code I'm currently working with:

<div class="container">
   <iframe id="ytplayer" type="text/html" width="960" height="540"
    src="http://www.youtube.com/embed/orosuMZsn7I?autoplay=0&origin=http://example.com"
    frameborder="0"/>
   </iframe>
</div>

And here's the corresponding CSS:

<style>
.container {
    width: 960px;
    height: 540px;
    margin-left: auto;
    margin-right: auto;
}
.container:before {
    content: url(img/surround_02.jpg);
    height: 540px;
    width:211px;
    position:absolute;
}
.container:after {
    content: url(img/surround_03.jpg);
    height: 540px;
    width:218px;
    position:absolute;
}
.container iframe {
    position:absolute;
}
</style>

Answer №1

After exploring some options, I came up with a solution for that unique situation:

http://jsfiddle.net/fusuhga6/2/

The structure consists of simple HTML and CSS:

#Container has a width set as a percentage (100%) and a max-width in pixels that matches the combined width of the YouTube iFrame and two images. It is horizontally centered using margin: 0 auto within the browser window or container. The two images are used as background-images of the container, one aligned left and the other aligned right (top). All elements share the same height.

The size of the YouTube DIV is fixed at 250px x 200px in the example. It is also centered horizontally within the #Container using margin: 0 auto.

The benefit of this approach is that in larger windows, the layout remains centered at full width (YouTube + 2 images). As the window narrows, the empty space on the sides decreases. Once the #Container spans the full width of the window and continues to narrow, the YouTube frame stays centered while the images adjust accordingly.

Interesting concept to consider.

Answer №2

Struggling to visualize the end goal, but here are a few tips that might be beneficial.

Firstly, for effective use of absolute positioning in CSS, ensure that a containing element is specified as position: relative; This distinction is important as absolute positioning is relative to the nearest parent element. For more in-depth information, check out this article by Chris Coyier on CSS positioning. By default, an element's position is "static," so if you're not defining another element as relative, absolute positioning may not behave as expected. In this scenario, making .container position:relative should solve the issue.

Secondly, there are some corrections needed in your markup and CSS. You are using a self-closing <iframe> tag initially, which should be replaced with just a > and then conclude with a closing </iframe>. Currently, you have both /> and a separate closing tag for the iframe, which could lead to syntax errors. Rectifying this will tidy up your code.

<div class="container">
   <iframe id="ytplayer" type="text/html" width="960" height="540"
    src="http://www.youtube.com/embed/orosuMZsn7I?autoplay=0&origin=http://example.com"
    frameborder="0">
   </iframe>
</div>

(removed the slash from just after frameborder="0" />)

Lastly, you can simplify your margin declaration by using margin: 0 auto; (or adding top margin if needed). Visualize CSS declarations like margin and padding as a clock, where:

margin: 10px 30px 5px 25px;

translates to

margin-top: 10px;
margin-right: 30px;
margin-bottom: 5px;
margin-left: 25px;

If values are not specified after the first declaration, they inherit the initial value. Therefore, when using:

margin: 5px;

It actually becomes:

margin-top: 5px;
margin-right: 5px;
margin-bottom: 5px;
margin-left: 5px;

By defining the first two values:

margin: 5px auto;

It will be interpreted as:

margin-top: 5px;
margin-right: auto;
margin-bottom: 5px;
margin-left: auto;

Although these tips may not directly address your query, providing a specific link or mockup of your intended design can aid in finding a solution.

Answer №3

After the question was modified and my original answer no longer applied, I devised a new approach: http://jsfiddle.net/fusuhga6/3/

Simply put:
All components are contained within a DIV that houses the Youtube video, with the following styles applied:

`position:relative` 
`margin: 0 auto`

This ensures the video is horizontally centered.

The two images are set as background images in absolute-positioned DIVs nested inside the #youtube DIV, positioned as follows:

#leftPic, #rightPic {  
  position: absolute;
  width: 90px;
  height: 200px;
  }
 #leftPic {
   left: -90px;
  background: url(http://placehold.it/90x200) no-repeat top right;
 }
  #rightPic {
    right: -90px;
  background: url(http://placehold.it/90x200) no-repeat top left;
 }

Their positioning is based on their fixed width. The two images being attached to the #youtube DIV ensures the entire group stays centered.

Even when the window width is reduced to a point where the three blocks overlap, the #youtube DIV remains centered with the other components aligned to it, just with some parts being hidden.

This solution should meet your requirements...

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

Preventing Users from Accessing a PHP Page: Best Practices

I'm currently focusing on a problem that involves restricting a user from opening a PHP page. The following is my JavaScript code: <script> $('input[id=f1email1]').on('blur', function(){ var k = $('inp ...

Creating a three-column layout with position fixed in the center

I'm struggling with achieving a maximum width of 400px for the entire set of three columns and centering them. ---------------------------------------------------- | | | |----------------------------- ...

Align three out of seven items in the navbar to the right using Bootstrap 4.1

Bootstrap 4.1 offers a great way to create a Navbar with multiple menu items, but I'm struggling to right justify three specific menu items within the Navbar. I've tried different methods, including using align-self-end, justify-content-end, and ...

Unable to show additional columns beyond 6 while using Bootstrap's row-cols feature

Currently working on developing an auction website where I plan to showcase the listings as cards in columns. I have successfully managed to display a varying number of cards on different screen sizes, but I am facing an issue with showing more than 6 card ...

Differentiating CSS Styles for Odd and Even Table Elements

I am trying to implement a style where every other row in my table (myrow) has a different background color. However, currently it is only showing the color specified for odd rows. .myrow, .myrow:nth-of-type(odd) + .myrow:nth-of-type(even) ~ .myrow: ...

The fixed-top navigation bar in Bootstrap obscures the links within the #content

As a student studying software development, I am currently working on a web development project for my class. Utilizing Bootstrap, I have implemented a navbar-fixed-top and structured the body as a table-striped table with each row containing a <a href= ...

I am attempting to integrate the file path of my images using PHP

Let me start off by showing you the file structure of my website. File Structure Of my website In order to organize my code, I created a header file named header.html.php which contains all the necessary elements for the header, including an image tag fo ...

Submitting numerous data fields using a post AJAX call

I have roughly 143 form fields including text, textarea, and select options that I need to send via an AJAX post request. Is there a way to achieve this efficiently without manually adding each field to the query? Here is how I've set things up: Usi ...

The Flask form within the Bootstrap input-group is breaking onto a new line when viewed on mobile devices

I want the 'Go' button to remain next to the search field. It displays correctly on Desktop view, but on mobile view, the submit button wraps (breaking up the input-group). How can I prevent this? Additionally, Bootstrap is adding gray lines aro ...

Retrieving elements from the DOM based on their class name

Currently utilizing PHP DOM for my project and facing the challenge of retrieving an element within a DOM node with a specific class name. What would be the most effective method to accomplish this? Update: Ultimately decided to switch to using Mechanize ...

Issue with the initial element in CSS and the cause remains a mystery

"first of type" dont like to work: .elements { width:70%; margin:0 auto; display:flex; flex-direction: column; .addElement { width:280px; text-align: center; border:1px solid black; font-family: ...

Issue: The function _lib_getAllUsers__WEBPACK_IMPORTED_MODULE_2___default(...) is not recognized as a valid function

I'm currently following the YouTube series on Next.js by Dave Gray. My goal is to import a function from the file "lib/getAllUsers" https://i.sstatic.net/1SQMg.png However, I'm encountering the following error: Unhandled Runtime Error Error: ...

Can a Chrome App access a user's files on their hard drive with proper permissions?

I'm currently working on a Chrome Packaged App that includes video playback functionality. My main goal is to enable users to stream online media (such as MP4 videos) while also giving them the option to save the video to a location of their choice. ...

Trigger a keypress event following the activation of a button

I've been trying to simulate the press of the backspace key on the keyboard after clicking a button, but all my attempts have been unsuccessful. Here is the code I'm using: function rtf(){ document.getElementById("u0u").focus(); $('#u0u ...

Can parameters be included in the HTML class attribute?

Is it possible to embed specific information into HTML elements, like images, without disrupting valid HTML markup? Would the following example be considered valid HTML? <img src="..." class="isearcher:tags(paris+hilton,gary+suneese)" > The idea is ...

Troubleshooting a Node.js server issue with a two-dimensional array

I am currently facing an issue with submitting a form that contains two-dimensional array fields on a post request in node.js. The problem lies in the fact that the server side is receiving a one-dimensional array with all the values combined. Below is an ...

The v-autocomplete feature in vuetify doesn't allow for editing the text input after an option has been selected

Link to code on CodePen: CodePen Link When you visit the provided page and enter "joe" into the search box, choose one of the two Joes that appear. Try to then select text in the search box or use backspace to delete only the last character. You will no ...

Postback does not show updates made by JQuery

On Page_Load, I have two asp:BulletedLists - one is already filled with data while the other remains empty. Users have the ability to drag and drop <li>'s between these lists using the following function: function Move(element, source, target) ...

Replacing Bootstrap CSS with a custom stylesheet

Within my bootstrap.css file, the following styling is defined: .form-control { display: block; width: 100%; height: 34px; padding: 6px 12px; font-size: 14px; line-height: 1.428571429; color: #555555; ve ...

I am looking to display an image as a value in the dropdown menu of my Contact Form 7 on my WordPress website

I am currently working on a Wordpress website where I have a page showcasing 50 different company logos. Below the logo section, there is a form created using Contact Form 7. The logo section was built using a combination of HTML and CSS. Within the form ...