2017-05-01 16 views
1

私は問題があり、私は絶望的です。私は解決策を理解していないようです。私はプログラミングの面白いので、本当に悪いコードの場合は、私と一緒に裸!CSS要素の位置を固定する方法は?

私が達成しようとしていることは次のとおりです。私は、新しいWindowsスタイルに似た、タイルレイアウトのウェブサイトを作成したいと考えています。 https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/jquery4u/2013/03/metro-template.png < - 種類のこのような、私の場合はそれを除い:

  1. 各タイルは、同じサイズ(正方形)(■)である
  2. テキストはタイルを中心にする必要があります(.center-ボックスコンテンツ)
  3. 各タイルは、テキスト(.lower-right-box-content)に加えて、右下隅に2つのボタンを伴う必要があります。 ボタンは編集およびごみアイコンであり、タイルのテキスト変更可能。

私はこれを達成しましたが、問題は次のとおりです。ボタンはテキストの長さに依存するようです。

私はそれらをcssファイルの絶対的な位置に割り当てましたが、そのまま置いていません。

タイル内のテキストが長い場合、それらはタイルの境界線の前後に移動します。これらの境界線を越えてはいけません。それは完全な混乱です。私はすべてを試しましたが、私は解決策を見つけることができません。私は皆さんが見て、私が逃しているものを見ることができると期待していますか?ここに私のCSSのコードは次のとおりです。ここで

.box { 
    width: 200px; 
    height: 200px; 
    margin: 2px; 
    color: whitesmoke; 
    float: left; 
    padding: 7px; 
} 


.center-box-content { 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    flex-direction: column; 

    position: relative; 
    top: 50%; 
    left: 50%; 
    transform: translate(-50%, -50%); 
} 


.lower-right-box-content { 
    position: absolute; 
    float: right; 
    padding: 2px; 
    bottom: -60px; 
    right: -5px; 
} 

.red-box { 
    background-color: indianred; 
} 

.blue-box { 
    background-color: deepskyblue; 
} 

.green-box { 
    background-color: mediumseagreen; 

} 

は私のHTMLコードは次のとおりです。(PHPビットは単なるイベント名、イベントの説明、およびイベントの日付であるデータベースからテキストを読み出す)

<!--the entire square tile --> 
<div class="box red-box" height="100%" width="100%"> 
     <!--this centers the text within the tile --> 
     <div class="center-box-content"> 
     <!--echo the name --> 
     <b><?php echo $row["title"]; ?></b> 

     <!--echo the description --> 
     <?php echo $row["description"]; ?> 

     <br> • <!--echo the date --> 
     <?php echo $row["date"]; ?> • 

      <!--edit and delete button in the lower right corner of the tile --> 
      <!--this is the part that keeps moving as the text gets longer --> 
      <div class="lower-right-box-content"> 
       Upcoming Events <span style="display:inline-block; width: 20px;"></span> 
       <a href="#"> 
       <span class="glyphicon glyphicon-pencil glyphicon-pencil" role="button"> </span> 
       </a> 

       <a href="deleteevent.php?id=<?php echo $row["eventid"];?>"> 
       <span class="glyphicon glyphicon-trash glyphicon-trash" role="button"></span> 
       </span> 

       </a> 
      </div> 


     </div> 
</div> 
    <?php }; 
     ?> 

答えて

0

"ボックス"の理想的な管理のために、今日我々はグリッドを使用します。 6列のグリッドの一例のCSS:

body { 
    font-family: 'Ariel', sans-serif; 
    font-weight: 100; 
    font-size: 1em; 
    color: #faf3bc; 
    background-color: #0976B2; 
} 

.grid_1 { width: 15.625%; } /* 125px/800px = 15.625% */ 
.grid_2 { width: 32.5%; } /* 260px/800px = 32.5% */ 
.grid_3 { width: 49.375%; } /* 395px/800px = 49.375% */ 
.grid_4 { width: 65.625%; } /* 525px/800px = 65.625% */ 
.grid_5 { width: 83.125%; } /* 665px/800px = 83.125% */ 
.grid_6 { width: 100%; } /* 800px/800px = 100% */ 

.grid_1, 
.grid_2, 
.grid_3, 
.grid_4, 
.grid_5, 
.grid_6 { 
    /* margin-right: 1.25%; */ 
    float: left; 
    display: block; 
} 
.last {margin-right:0; } 

.container{ 
    clear:both; 
    width: 90%; 
    max-width: 800px; 
    padding: 0% 0; 
    margin: auto; 
} 
img { 
    max-width: 100%; 
} 
h1 { 
    color: #ffffff; 
} 

a:link {color:#b4c34f; text-decoration:none;}  /* unvisited link */ 
a:visited {color:#b4c34f; text-decoration:none;} /* visited link */ 
a:hover {color:#faf3bc; text-decoration:underline;} /* mouse over link */ 
a:active {color:#b4c34f; text-decoration:none;} /* selected link */ 

a.selected {color:#ffffff;} /* selected link */ 

ul { 
    list-style-type:none; 
} 

.form-input { 
    height: 30px; 
} 
    @media screen and (max-width : 705px) { 
.grid_1, 
.grid_2, 
.grid_3, 
.grid_4, 
.grid_5, 
.grid_6 { 
    width: 100%; 
}} 

HTML生で3 "箱" のために:

<div class='grid_2'> 
</div> 
<div class='grid_2'> 
</div> 
<div class='grid_2 last'> 
</div> 

HTML生で2 "箱" のために:

<div class='grid_3'> 
</div> 
<div class='grid_3 last'> 
</div> 

たぶん、あなたは、より多くのグリッド列を必要とし、先に進んで、応答性の高いWebデザインを学びます。

0

正方形は、これらの絶対配置された要素の親のの直接のですか?絶対位置が正しく動作するようにする必要があります。position: relativeが必要です。

これらの要素からfloatを消去します。絶対配置された要素には意味がありません。

0

Here is the jsfiddle link

あなたは.lower-right-box-contentposition:relativeに設定して、ボタンが絶対的な位置付けにする必要があります。

<div class="lower-right-box-content"> 
    <div style="display:inline-block;margin-right:40px;text-align:justify;"> Upcoming Events longggg text does not matter </div> 
     <a href="#"> 
      <span class="glyphicon glyphicon-pencil glyphicon-pencil" role="button" style='position:absolute;bottom:10px;right:5px;'> </span> 
     </a> 

      <a href="deleteevent.php?id=<?php echo $row["eventid"];?>"> 
      <span class="glyphicon glyphicon-trash glyphicon-trash" role="button" style="position:absolute;bottom:10px;right:20px;"> 
      </span> 
      </a 
     </div> 
</div> 

わかりやすくするためにインラインスタイルを使用しました。スタイルシートのスタイルを適切なセレクタで更新してください。

0
  1. ボタンを独自のdivに配置します。
  2. position: relativeをボックスに追加します。
  3. position: absoluteを使用して、テキストとボタンを下部に配置します。
  4. 長いテキストがボタンと重ならないようにするには、テキストにパディングを追加します。

.box { 
 
    width: 200px; 
 
    height: 200px; 
 
    margin: 2px; 
 
    color: whitesmoke; 
 
    float: left; 
 
    display: flex; 
 
    align-items: center; 
 
    justify-content: center; 
 
    flex-direction: column; 
 
    position: relative; 
 
} 
 

 
.center-box-content { 
 
    text-align: center; 
 
} 
 

 
.upcoming { 
 
    position: absolute; 
 
    bottom: 0; 
 
    text-align: center; 
 
    left: 0; 
 
    right: 0; 
 
    padding: 0 30px; /* ensure the text clears the buttons */ 
 
} 
 

 
.buttons { 
 
    position: absolute; 
 
    bottom: 0; 
 
    right: 0; 
 
} 
 

 
.red-box { 
 
    background-color: indianred; 
 
} 
 

 
.blue-box { 
 
    background-color: deepskyblue; 
 
} 
 

 
.green-box { 
 
    background-color: mediumseagreen; 
 
}
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/> 
 
<div class="box red-box"> 
 
    <div class="center-box-content"> 
 
    <b>Name</b> 
 
    <br>Description 
 
    <br> • Date • 
 
    </div> 
 
    <div class="lower-right-box-content"> 
 
    <span class="upcoming">Upcoming events</span> 
 
    <div class="buttons"> 
 
     <a href="#"> 
 
     <span class="glyphicon glyphicon-pencil glyphicon-pencil" role="button"></span> 
 
     </a> 
 
     <a href="#"> 
 
     <span class="glyphicon glyphicon-trash glyphicon-trash" role="button"></span> 
 
     </a> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<div class="box red-box"> 
 
    <div class="center-box-content"> 
 
    <b>Name</b> 
 
    <br>Description 
 
    <br> • Date • 
 
    </div> 
 
    <div class="lower-right-box-content"> 
 
    <span class="upcoming">Upcoming events events events</span> 
 
    <div class="buttons"> 
 
     <a href="#"> 
 
     <span class="glyphicon glyphicon-pencil glyphicon-pencil" role="button"></span> 
 
     </a> 
 
     <a href="#"> 
 
     <span class="glyphicon glyphicon-trash glyphicon-trash" role="button"></span> 
 
     </a> 
 
    </div> 
 
    </div> 
 
</div>

関連する問題