2016-12-04 5 views
0

以下は私が作成したHTMLとCSSの実装です。私はコンテナ内のいくつかのボタンの幅に問題がありますdiv。ここでネストされたCSS要素の幅をすべて同じにするにはどうすればいいですか?

https://s3.amazonaws.com/f.cl.ly/items/2z3C3Z0L2Q0R282p1V0z/Image%202014-12-19%20at%2010.07.09%20AM.png

私です:どういうわけか、私はいつも、ボタン要素のwidthは、それがであるdiv要素は、これは私が再作成したかった画像であるものの50%が常にあることを確認することができるように、私はそれをしたいです試み:

/* Global resets */ 
 
* {box-sizing: border-box; margin: 0; padding: 0;} 
 
button { 
 
    font-family: "rooney-sans", Avenir-Book, Calibri, sans-serif; 
 
} 
 
body {font-family: "rooney-sans", Avenir-Book, Calibri, sans-serif; color: #424242; line-height: 1.4;} 
 

 
/* Fonts */ 
 
h1 { 
 
    font-family: "rooney-web", 'AmericanTypewriter', Rockwell, serif; 
 
    font-size: 2.5em; 
 
    font-weight: bold; 
 
} 
 

 
.container { 
 
    margin: 2em auto; 
 
    max-width: 630px; 
 
    text-align: center; 
 
} 
 

 
.funding-text { 
 
    border: 1px solid; 
 
} 
 

 
/* Our entire container */ 
 
.funding-box { 
 
    text-align: left; 
 
    max-width: 265px; 
 
    margin: auto; 
 
    font-size: 12px; 
 
} 
 
/* Our input box */ 
 
input.giving-input{ 
 
    width: 100px; 
 
    font-weight: bold; 
 
    padding: 10px; 
 
} 
 

 
.give { 
 
    display: inline-block; 
 
    margin-top: 10px; 
 
} 
 

 
p .days-left{ 
 
    font-weight: bold; 
 
    color: #EF5F3C; 
 
} 
 

 
.padded-text { 
 
    color: #777; 
 
    padding: 15px; 
 
} 
 

 
button, input { 
 
    border-radius: 4px; 
 
} 
 

 
.give-button{ 
 
    background-color: #1CBC2C; 
 
    color: white; 
 
    border-color: #1CBC2C; 
 
    padding: 10px; 
 
    width: 100px; 
 
    margin-left: 10px; 
 
} 
 

 
a { 
 
    margin-top: 10px; 
 
    display: block; 
 
    text-decoration: none; 
 
    visited: false; 
 
} 
 

 
a:visited { 
 
    color: blue; 
 
} 
 

 
.chat-bubble { 
 
    background-color: #424242; 
 
    color: white; 
 
    padding: 15px; 
 
    font-size: 13px; 
 
    text-align: center; 
 
    margin-bottom: 10px; 
 
} 
 

 
.funding-rate { 
 
    background-color: #EF5F3C; 
 
    height: 10px; 
 
    border-bottom: 1px solid; 
 
} 
 

 
.save-button, .share-button { 
 
    background-color: #eaeaea; 
 
    border-color: #eaeaea; 
 
    padding: 10px; 
 
    width: 125px; 
 
} 
 

 
.share-button { 
 
    margin-left: 10px; 
 
}
<div class="container">  
 
    <div class="funding-box"> 
 
    <div class="chat-bubble"><b>$167</b> still needed for this project</div> 
 
    <div class="funding-text"> 
 
     <div class="funding-rate"></div> 
 
     <div class="padded-text"> 
 
     <p><span class="days-left">Only 3 days left</span> to fund this project.<br/><br/> Join the <b>42</b> other doners who have already supported this project. Every dollar helps.</p> 
 
     <span class="give"> 
 
      <input class="giving-input" type="text" value="$50"> 
 
      <button class="give-button">Give now</button> 
 
     </span> 
 
     <a href=""><i>Why give $50?</i></a> 
 
     </div> 
 
    </div> 
 
    <div class="give"> 
 
     <button class="save-button">Save for later</button> 
 
     <button class="share-button">Tell your friends</button> 
 
    </div> 
 
    </div> 
 
</div>

私が午前問題はsave-buttonshare-buttonであるが、また、give-buttonおよびgiving-inputクラス。私は必ずしもハードコーディングして、正しく整列させるようにしたいとは限りません。むしろ、プログラムの方法があれば、親のwidth: 50%に設定することができます。これは、幅広いコードをハードコーディングするのではなく、divです。私はクラス.give {width: 100%}を設定しようとして.giving-input, .give-button, .save-button, .share-button: {width: 50%}を設定しましたが、それは私のためには機能しませんでした。任意のヒントや助けをいただければ幸いです。ありがとう!あなたは49%に幅がすべての入力/ボタンを設定し、margin-leftあなたはすべてが動作するはずがなかったを削除した場合

+0

は、Javaスクリプトがどうなるのか?あなたのボックスがdivの外にあったとしても、送信トレイのdivの幅の半分を設定することができます。 – Obink

答えて

1

は(私もそれは幅の100%を取ることを確認するために戻ってblockからinline-blockからあなたのコンテナのいずれかを変更しました)。ここで

あなたのコードに修正です:

/* Global resets */ 
 
* {box-sizing: border-box; margin: 0; padding: 0;} 
 
button { 
 
    font-family: "rooney-sans", Avenir-Book, Calibri, sans-serif; 
 
} 
 
body {font-family: "rooney-sans", Avenir-Book, Calibri, sans-serif; color: #424242; line-height: 1.4;} 
 

 
/* Fonts */ 
 
h1 { 
 
    font-family: "rooney-web", 'AmericanTypewriter', Rockwell, serif; 
 
    font-size: 2.5em; 
 
    font-weight: bold; 
 
} 
 

 
.container { 
 
    margin: 2em auto; 
 
    max-width: 630px; 
 
    text-align: center; 
 
} 
 

 
.funding-text { 
 
    border: 1px solid; 
 
} 
 

 
/* Our entire container */ 
 
.funding-box { 
 
    text-align: left; 
 
    max-width: 265px; 
 
    margin: auto; 
 
    font-size: 12px; 
 
} 
 
/* Our input box */ 
 
input.giving-input{ 
 
    width: 49%; 
 
    font-weight: bold; 
 
    padding: 10px; 
 
} 
 

 
.give { 
 
    margin-top: 10px; 
 
} 
 

 
p .days-left{ 
 
    font-weight: bold; 
 
    color: #EF5F3C; 
 
} 
 

 
.padded-text { 
 
    color: #777; 
 
    padding: 15px; 
 
} 
 

 
button, input { 
 
    border-radius: 4px; 
 
} 
 

 
.give-button{ 
 
    background-color: #1CBC2C; 
 
    color: white; 
 
    border-color: #1CBC2C; 
 
    padding: 10px; 
 
    width: 49%; 
 
} 
 

 
a { 
 
    margin-top: 10px; 
 
    display: block; 
 
    text-decoration: none; 
 
    visited: false; 
 
} 
 

 
a:visited { 
 
    color: blue; 
 
} 
 

 
.chat-bubble { 
 
    background-color: #424242; 
 
    color: white; 
 
    padding: 15px; 
 
    font-size: 13px; 
 
    text-align: center; 
 
    margin-bottom: 10px; 
 
} 
 

 
.funding-rate { 
 
    background-color: #EF5F3C; 
 
    height: 10px; 
 
    border-bottom: 1px solid; 
 
} 
 

 
.save-button, .share-button { 
 
    background-color: #eaeaea; 
 
    border-color: #eaeaea; 
 
    padding: 10px; 
 
    width: 125px; 
 
} 
 

 
.share-button { 
 
    margin-left: 10px; 
 
}
<div class="container">  
 
    <div class="funding-box"> 
 
    <div class="chat-bubble"><b>$167</b> still needed for this project</div> 
 
    <div class="funding-text"> 
 
     <div class="funding-rate"></div> 
 
     <div class="padded-text"> 
 
     <p><span class="days-left">Only 3 days left</span> to fund this project.<br/><br/> Join the <b>42</b> other doners who have already supported this project. Every dollar helps.</p> 
 
     <span class="give"> 
 
      <input class="giving-input" type="text" value="$50" /> 
 
      <button class="give-button">Give now</button> 
 
     </span> 
 
     <a href=""><i>Why give $50?</i></a> 
 
     </div> 
 
    </div> 
 
    <div class="give"> 
 
     <button class="save-button">Save for later</button> 
 
     <button class="share-button">Tell your friends</button> 
 
    </div> 
 
    </div> 
 
</div>

+0

驚くばかり!ありがとうございました。これは素晴らしい。ちょうどそれが不思議なのは、親div全体の '98% 'を占めるため、' input'と 'button'の両方の' 49%'幅であり、次に2%は'input'と' button'の間にありますか? – user1871869

0

あなたは2枚にして、それぞれが幅の50%を持っている.butここもそう、10pxのパディングを適用することを分割することができます正確な値を取得するには、関数calc(50% - 10px);を使用します。

/* Global resets */ 
 
* {box-sizing: border-box; margin: 0; padding: 0;} 
 
button { 
 
    font-family: "rooney-sans", Avenir-Book, Calibri, sans-serif; 
 
} 
 
body {font-family: "rooney-sans", Avenir-Book, Calibri, sans-serif; color: #424242; line-height: 1.4;} 
 

 
/* Fonts */ 
 
h1 { 
 
    font-family: "rooney-web", 'AmericanTypewriter', Rockwell, serif; 
 
    font-size: 2.5em; 
 
    font-weight: bold; 
 
} 
 

 
.container { 
 
    margin: 2em auto; 
 
    max-width: 630px; 
 
    text-align: center; 
 
} 
 

 
.funding-text { 
 
    border: 1px solid; 
 
} 
 

 
/* Our entire container */ 
 
.funding-box { 
 
    text-align: left; 
 
    max-width: 265px; 
 
    margin: auto; 
 
    font-size: 12px; 
 
} 
 
/* Our input box */ 
 
input.giving-input{ 
 
    width: calc(50% - 10px); 
 
    font-weight: bold; 
 
    padding: 10px; 
 
} 
 

 
.give { 
 
    display: inline-block; 
 
    margin-top: 10px; 
 
} 
 

 
p .days-left{ 
 
    font-weight: bold; 
 
    color: #EF5F3C; 
 
} 
 

 
.padded-text { 
 
    color: #777; 
 
    padding: 15px; 
 
} 
 

 
button, input { 
 
    border-radius: 4px; 
 
} 
 

 
.give-button{ 
 
    background-color: #1CBC2C; 
 
    color: white; 
 
    border-color: #1CBC2C; 
 
    padding: 10px; 
 
    width: calc(50% - 10px); 
 
    margin-left: 10px; 
 
} 
 
a { 
 
    margin-top: 10px; 
 
    display: block; 
 
    text-decoration: none; 
 
    visited: false; 
 
} 
 

 
a:visited { 
 
    color: blue; 
 
} 
 

 
.chat-bubble { 
 
    background-color: #424242; 
 
    color: white; 
 
    padding: 15px; 
 
    font-size: 13px; 
 
    text-align: center; 
 
    margin-bottom: 10px; 
 
} 
 

 
.funding-rate { 
 
    background-color: #EF5F3C; 
 
    height: 10px; 
 
    border-bottom: 1px solid; 
 
} 
 

 
.save-button, .share-button { 
 
    background-color: #eaeaea; 
 
    border-color: #eaeaea; 
 
    padding: 10px; 
 
    width: 125px; 
 
} 
 

 
.share-button { 
 
    margin-left: 10px; 
 
}
<div class="container">  
 
    <div class="funding-box"> 
 
    <div class="chat-bubble"><b>$167</b> still needed for this project</div> 
 
    <div class="funding-text"> 
 
     <div class="funding-rate"></div> 
 
     <div class="padded-text"> 
 
     <p><span class="days-left">Only 3 days left</span> to fund this project.<br/><br/> Join the <b>42</b> other doners who have already supported this project. Every dollar helps.</p> 
 
     <span class="give"> 
 
      <input class="giving-input" type="text" value="$50"> 
 
      <button class="give-button">Give now</button> 
 
     </span> 
 
     <a href=""><i>Why give $50?</i></a> 
 
     </div> 
 
    </div> 
 
    <div class="give"> 
 
     <button class="save-button">Save for later</button> 
 
     <button class="share-button">Tell your friends</button> 
 
    </div> 
 
    </div> 
 
</div>

関連する問題