亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

CSS:<div>元素內的中心元素

CSS:<div>元素內的中心元素

九州編程 2019-12-16 09:49:47
CSS:<div>元素內的中心元素要居中HTML元素,我可以使用CSS left: 50%;。但是,這使元素相對于整個窗口居中。我有一個元素,它是元素的子<div>元素,并且我想相對于此父元素而<div>不是整個窗口居中顯示該子元素。我不想容器<div>有所有的內容為中心,只是一個特定的孩子。我怎樣才能做到這一點?
查看完整描述

3 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

設置text-align:center;為父div和margin:auto;子div。


#parent {

    text-align:center;

    background-color:blue;

    height:400px;

    width:600px;

}

.block {

    height:100px;

    width:200px;

    text-align:left;

}

.center {

    margin:auto;

    background-color:green;

}

.left {

    margin:auto auto auto 0;

    background-color:red;

}

.right {

    margin:auto 0 auto auto;

    background-color:yellow;

}

<div id="parent">

    <div id="child1" class="block center">

        a block to align center and with text aligned left

    </div>

    <div id="child2" class="block left">

        a block to align left and with text aligned left

    </div>

    <div id="child3" class="block right">

        a block to align right and with text aligned left

    </div>

</div>

這是一個幾乎可以集中所有內容的好資源。

http://howtocenterincss.com/


查看完整回答
反對 回復 2019-12-16
  • 3 回答
  • 0 關注
  • 468 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號