动态调整嵌入式YouTube视频的大小不会居中?

时间:2016-11-30 16:15:20

标签: javascript html css youtube embed

所以我设法使用我在这个主题上找到的一些教程,让这个youtube视频在页面上动态调整大小。但是它不会在页面上正确居中。

我该怎么做呢?是否有一种更容易的方法来动态调整我忽略的嵌入式视频的大小?

谢谢你们和gals。

* {
    font-family: "Trebuchet MS";
}

/* BODY */

body {
    background-color: #F7F3FB;
}

body hr {
    width: 55%;
}

/*LOGO*/

#logo {
    font-size: 4em;
    text-align: center;
}

#logo p {
    margin: 0px;
}

/* NAVBAR */

#navbar a {
    text-decoration: none;
    color: black;
}

#navbar {
    text-align: center;
    font-size: 1.4em;
}

/* CONTENT */

#content {
    width: 30%;
    text-align: center;
    margin: auto;
    font-size: 1.05em;
}

/* GALLERY */
a:-webkit-any-link {
    text-decoration: none;
    /* ignoring default link settings */
}

  -moz-transition: opacity .25s ease-in-out;
    -webkit-transition: opacity .25s ease-in-out;
}

.fade:hover {
    opacity: 1;
    /* sets default view to a 100% opacity when on hover state */
}

.img-wrapper {
    width: 300px;
    height: 200px;
    border: 0.1em solid #ccc;
    border-radius: 0.4em;
    background-color: #f3f3f3;
    box-shadow: 0.1em 0.1em 0.5em -0.2em #777;
    margin: 1em 1em;
}

img {
    border-radius: 0.4em 0.4em 0em 0em;
    /* radius should be the same as the img-wrapper */
}

.gallery-wrapper {
    max-width: 1200px;
    margin: auto;
}

.gallery-wrapper ul li {
    display: inline-block;
    /* sit wrappers in rows, not column block */
}

h1 {
    padding-left: 14em;
}

h4 {
    /* style the photos titles */
    text-align: center;
    font-size: 1em;
    margin: 0;
    padding: 0.5em 2em;
    text-transform: uppercase;
    font-weight: bold;
    color: black;
}

.logo {
    margin-left: 22em;
    margin-bottom: 4em;
}

/* VIDEO PAGE */

.video {
    text-align: center;
    padding: 0;
    width: 50%;
}

video {
    width: 50%;
}

.description {
    font-size: 1.25em;
}

.job {
    font-weight: bold;
}

.video-container {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 35px;
    height: 0;
    overflow: hidden;
}

.video-container iframe {
    position: absolute;
    top:0;
    left: 0;
    width: 100%;
    height: 100%;
}
<!DOCTYPE html>
<html>
    <head>
        <title>GingeSwag Productions</title>
        <link href="main.css" rel="stylesheet">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <script type="text/javascript" src="main.js"></script>
    </head>
    
    <body>
        
        <div id="logo">
            <p>GingeSwagProductions</p>
        </div>
        
        <div id="navbar">
            <p>
                <a href="index.html">Home</a> -
                <a href="film.html">Film</a> -
                <a href="photography.html">Photography</a> -
                <a href="about.html">About</a> -
                <a href="contact.html">Contact</a>
            </p>
        </div>
        
        <hr>
        
        
        <div class="video">
            <h2>Silver</h2>
            <div class="video-container">
                <iframe width="560" height="315" src="https://www.youtube.com/embed/hW3_awq0-lk" frameborder="0" allowfullscreen></iframe>
            </div>
            <p>Silver is a short film following the first meeting of a young adopted boy with his neighbour.</p>
            <p>Jobrole: Production Designer.</p>
        </div>
    </body>
    
    <footer></footer>
</html>

2 个答案:

答案 0 :(得分:1)

margin: 0 auto上使用.video。像:

.video {
  margin: 0 auto;
}

请看下面的代码段:

.video {
    margin: 0 auto;
}

* {
    font-family: "Trebuchet MS";
}

/* BODY */

body {
    background-color: #F7F3FB;
}

body hr {
    width: 55%;
}

/*LOGO*/

#logo {
    font-size: 4em;
    text-align: center;
}

#logo p {
    margin: 0px;
}

/* NAVBAR */

#navbar a {
    text-decoration: none;
    color: black;
}

#navbar {
    text-align: center;
    font-size: 1.4em;
}

/* CONTENT */

#content {
    width: 30%;
    text-align: center;
    margin: auto;
    font-size: 1.05em;
}

/* GALLERY */
a:-webkit-any-link {
    text-decoration: none;
    /* ignoring default link settings */
}

  -moz-transition: opacity .25s ease-in-out;
    -webkit-transition: opacity .25s ease-in-out;
}

.fade:hover {
    opacity: 1;
    /* sets default view to a 100% opacity when on hover state */
}

.img-wrapper {
    width: 300px;
    height: 200px;
    border: 0.1em solid #ccc;
    border-radius: 0.4em;
    background-color: #f3f3f3;
    box-shadow: 0.1em 0.1em 0.5em -0.2em #777;
    margin: 1em 1em;
}

img {
    border-radius: 0.4em 0.4em 0em 0em;
    /* radius should be the same as the img-wrapper */
}

.gallery-wrapper {
    max-width: 1200px;
    margin: auto;
}

.gallery-wrapper ul li {
    display: inline-block;
    /* sit wrappers in rows, not column block */
}

h1 {
    padding-left: 14em;
}

h4 {
    /* style the photos titles */
    text-align: center;
    font-size: 1em;
    margin: 0;
    padding: 0.5em 2em;
    text-transform: uppercase;
    font-weight: bold;
    color: black;
}

.logo {
    margin-left: 22em;
    margin-bottom: 4em;
}

/* VIDEO PAGE */

.video {
    text-align: center;
    padding: 0;
    width: 50%;
}

video {
    width: 50%;
}

.description {
    font-size: 1.25em;
}

.job {
    font-weight: bold;
}

.video-container {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 35px;
    height: 0;
    overflow: hidden;
}

.video-container iframe {
    position: absolute;
    top:0;
    left: 0;
    width: 100%;
    height: 100%;
}
<!DOCTYPE html>
<html>
    <head>
        <title>GingeSwag Productions</title>
        <link href="main.css" rel="stylesheet">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <script type="text/javascript" src="main.js"></script>
    </head>
    
    <body>
        
        <div id="logo">
            <p>GingeSwagProductions</p>
        </div>
        
        <div id="navbar">
            <p>
                <a href="index.html">Home</a> -
                <a href="film.html">Film</a> -
                <a href="photography.html">Photography</a> -
                <a href="about.html">About</a> -
                <a href="contact.html">Contact</a>
            </p>
        </div>
        
        <hr>
        
        
        <div class="video">
            <h2>Silver</h2>
            <div class="video-container">
                <iframe width="560" height="315" src="https://www.youtube.com/embed/hW3_awq0-lk" frameborder="0" allowfullscreen></iframe>
            </div>
            <p>Silver is a short film following the first meeting of a young adopted boy with his neighbour.</p>
            <p>Jobrole: Production Designer.</p>
        </div>
    </body>
    
    <footer></footer>
</html>

希望这有帮助!

答案 1 :(得分:0)

.video {    
width: 50%;
margin: 0 auto;
}

用此

替换您的视频css